diff options
author | Tsu Chiang Chuang <tsu@google.com> | 2012-04-23 16:06:58 -0700 |
---|---|---|
committer | Tsu Chiang Chuang <tsu@google.com> | 2012-05-01 17:30:09 -0700 |
commit | fea39c3755075a2f3484f3197d93f7d42c818d67 (patch) | |
tree | 89ee581954efdbaf15881a11b56ddd2b2fd3f401 /tests/Compatibility/src | |
parent | 122b2c32dea573a3efbd0d608a70b7cd169d99dd (diff) | |
download | frameworks_base-fea39c3755075a2f3484f3197d93f7d42c818d67.zip frameworks_base-fea39c3755075a2f3484f3197d93f7d42c818d67.tar.gz frameworks_base-fea39c3755075a2f3484f3197d93f7d42c818d67.tar.bz2 |
App Compatibility test. Launches a given app and detects crashes or ANRs.
Change-Id: I504868ca2b7785849960be287b8d92ca3055b9ff
Diffstat (limited to 'tests/Compatibility/src')
-rw-r--r-- | tests/Compatibility/src/com/android/compatibilitytest/AppCompatibility.java | 165 | ||||
-rw-r--r-- | tests/Compatibility/src/com/android/compatibilitytest/AppCompatibilityRunner.java | 35 |
2 files changed, 200 insertions, 0 deletions
diff --git a/tests/Compatibility/src/com/android/compatibilitytest/AppCompatibility.java b/tests/Compatibility/src/com/android/compatibilitytest/AppCompatibility.java new file mode 100644 index 0000000..4d60c83 --- /dev/null +++ b/tests/Compatibility/src/com/android/compatibilitytest/AppCompatibility.java @@ -0,0 +1,165 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.compatibilitytest; + +import android.app.ActivityManager; +import android.app.ActivityManager.ProcessErrorStateInfo; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.test.InstrumentationTestCase; +import android.util.Log; + +import junit.framework.Assert; + +import java.util.Collection; + +/** + * Application Compatibility Test that launches an application and detects crashes. + */ +public class AppCompatibility extends InstrumentationTestCase { + + private static final String TAG = "AppCompability"; + private static final String PACKAGE_TO_LAUNCH = "package_to_launch"; + private static final String APP_LAUNCH_TIMEOUT_MSECS = "app_launch_timeout_ms"; + private static final String WORKSPACE_LAUNCH_TIMEOUT_MSECS = "workspace_launch_timeout_ms"; + + private int mAppLaunchTimeout = 7000; + private int mWorkspaceLaunchTimeout = 2000; + + private Context mContext; + private ActivityManager mActivityManager; + private PackageManager mPackageManager; + private AppCompatibilityRunner mRunner; + private Bundle mArgs; + + @Override + public void setUp() throws Exception{ + super.setUp(); + mRunner = (AppCompatibilityRunner) getInstrumentation(); + assertNotNull("Could not fetch InstrumentationTestRunner.",mRunner); + + mContext = mRunner.getTargetContext(); + Assert.assertNotNull("Could not get the Context", mContext); + + mActivityManager = (ActivityManager) + mContext.getSystemService(Context.ACTIVITY_SERVICE); + Assert.assertNotNull("Could not get Activity Manager", mActivityManager); + + mPackageManager = mContext.getPackageManager(); + Assert.assertNotNull("Missing Package Manager", mPackageManager); + + mArgs = mRunner.getBundle(); + + // Parse optional inputs. + String appLaunchTimeoutMsecs = mArgs.getString(APP_LAUNCH_TIMEOUT_MSECS); + if (appLaunchTimeoutMsecs != null) { + mAppLaunchTimeout = Integer.parseInt(appLaunchTimeoutMsecs); + } + String workspaceLaunchTimeoutMsecs = mArgs.getString(WORKSPACE_LAUNCH_TIMEOUT_MSECS); + if (workspaceLaunchTimeoutMsecs != null) { + mWorkspaceLaunchTimeout = Integer.parseInt(workspaceLaunchTimeoutMsecs); + } + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + /** + * Actual test case that launches the package and throws an exception on the first error. + * @throws Exception + */ + public void testAppStability() throws Exception { + String packageName = mArgs.getString(PACKAGE_TO_LAUNCH); + if (packageName != null) { + Log.d(TAG, "Launching app " + packageName); + Collection<ProcessErrorStateInfo> err = launchActivity(packageName); + // Make sure there are no errors when launching the application, otherwise raise an + // exception with the first error encountered. + assertNull(getFirstError(err), err); + } else { + Log.d(TAG, "Missing argument, use " + PACKAGE_TO_LAUNCH + + " to specify the package to launch"); + } + } + + /** + * Gets the first error in collection and return the long message for it. + * @param in {@link Collection} of {@link ProcessErrorStateInfo} to parse. + * @return {@link String} the long message of the error. + */ + private String getFirstError(Collection<ProcessErrorStateInfo> in) { + if (in == null) { + return null; + } + ProcessErrorStateInfo err = in.iterator().next(); + if (err != null) { + return err.stackTrace; + } + return null; + } + + /** + * Launches and activity and queries for errors. + * @param packageName {@link String} the package name of the application to launch. + * @return {@link Collection} of {@link ProcessErrorStateInfo} detected during the app launch. + */ + private Collection<ProcessErrorStateInfo> launchActivity(String packageName) { + Intent homeIntent = new Intent(Intent.ACTION_MAIN); + homeIntent.addCategory(Intent.CATEGORY_HOME); + homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + Intent intent = mPackageManager.getLaunchIntentForPackage(packageName); + + // We check for any Crash or ANR dialogs that are already up, and we ignore them. This is + // so that we don't report crashes that were caused by prior apps (which those particular + // tests should have caught and reported already). Otherwise, test failures would cascade + // from the initial broken app to many/all of the tests following that app's launch. + final Collection<ProcessErrorStateInfo> preErr = mActivityManager.getProcessesInErrorState(); + + // Launch Activity + mContext.startActivity(intent); + + try { + Thread.sleep(mAppLaunchTimeout); + } catch (InterruptedException e) { + // ignore + } + + // Send the "home" intent and wait 2 seconds for us to get there + mContext.startActivity(homeIntent); + try { + Thread.sleep(mWorkspaceLaunchTimeout); + } catch (InterruptedException e) { + // ignore + } + + // See if there are any errors. We wait until down here to give ANRs as much time as + // possible to occur. + final Collection<ProcessErrorStateInfo> postErr = + mActivityManager.getProcessesInErrorState(); + // Take the difference between the error processes we see now, and the ones that were + // present when we started + if (preErr != null && postErr != null) { + postErr.removeAll(preErr); + } + return postErr; + } +} diff --git a/tests/Compatibility/src/com/android/compatibilitytest/AppCompatibilityRunner.java b/tests/Compatibility/src/com/android/compatibilitytest/AppCompatibilityRunner.java new file mode 100644 index 0000000..258937f --- /dev/null +++ b/tests/Compatibility/src/com/android/compatibilitytest/AppCompatibilityRunner.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2012, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.compatibilitytest; + +import android.os.Bundle; +import android.test.InstrumentationTestRunner; + +public class AppCompatibilityRunner extends InstrumentationTestRunner { + + private Bundle mArgs; + + @Override + public void onCreate(Bundle args) { + super.onCreate(args); + mArgs = args; + } + + public Bundle getBundle() { + return mArgs; + } +} |