From 01213f9baa77b34c414bb42e76cfa38d622be067 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Tue, 14 Jul 2015 20:18:44 -0400 Subject: [PATCH] Add simple UI Automator tests. --- .travis.yml | 13 ++++ build.gradle | 4 + process-phoenix/build.gradle | 9 +++ .../src/androidTest/AndroidManifest.xml | 12 +++ .../processphoenix/ProcessPhoenixTest.java | 75 +++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 process-phoenix/src/androidTest/AndroidManifest.xml create mode 100644 process-phoenix/src/androidTest/java/com/jakewharton/processphoenix/ProcessPhoenixTest.java diff --git a/.travis.yml b/.travis.yml index a2c9cc4..82e57b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,23 @@ android: components: - build-tools-22.0.1 - android-22 + - extra-android-m2repository + - sys-img-armeabi-v7a-android-18 jdk: - oraclejdk8 +install: ./gradlew assemble + +before_script: + - echo no | android create avd --force -n test -t android-18 --abi armeabi-v7a + - emulator -avd test -no-skin -no-audio -no-window & + - android-wait-for-emulator + - adb shell input keyevent 82 + +script: + - ./gradlew installDebug connectedCheck + after_success: - .buildscript/deploy_snapshot.sh diff --git a/build.gradle b/build.gradle index 03379cc..c037982 100644 --- a/build.gradle +++ b/build.gradle @@ -20,5 +20,9 @@ ext { ci = 'true'.equals(System.getenv('CI')) + supportTestRunner = 'com.android.support.test:runner:0.3' + supportTestRules = 'com.android.support.test:rules:0.3' + supportTestUiAutomator = 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' + truth = 'com.google.truth:truth:0.27' butterKnife = 'com.jakewharton:butterknife:7.0.0' } diff --git a/process-phoenix/build.gradle b/process-phoenix/build.gradle index a68e79e..fb600f2 100644 --- a/process-phoenix/build.gradle +++ b/process-phoenix/build.gradle @@ -1,11 +1,20 @@ apply plugin: 'com.android.library' +dependencies { + androidTestCompile rootProject.ext.supportTestRunner + androidTestCompile rootProject.ext.supportTestRules + androidTestCompile rootProject.ext.supportTestUiAutomator + androidTestCompile rootProject.ext.butterKnife + androidTestCompile rootProject.ext.truth +} + android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { minSdkVersion rootProject.ext.minSdkVersion + testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' } compileOptions { diff --git a/process-phoenix/src/androidTest/AndroidManifest.xml b/process-phoenix/src/androidTest/AndroidManifest.xml new file mode 100644 index 0000000..31ee89b --- /dev/null +++ b/process-phoenix/src/androidTest/AndroidManifest.xml @@ -0,0 +1,12 @@ + + + + + diff --git a/process-phoenix/src/androidTest/java/com/jakewharton/processphoenix/ProcessPhoenixTest.java b/process-phoenix/src/androidTest/java/com/jakewharton/processphoenix/ProcessPhoenixTest.java new file mode 100644 index 0000000..e7c2308 --- /dev/null +++ b/process-phoenix/src/androidTest/java/com/jakewharton/processphoenix/ProcessPhoenixTest.java @@ -0,0 +1,75 @@ +package com.jakewharton.processphoenix; + +import android.app.Instrumentation; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; +import android.support.test.uiautomator.UiDevice; +import android.support.test.uiautomator.UiObject; +import android.support.test.uiautomator.UiObjectNotFoundException; +import android.support.test.uiautomator.UiSelector; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static android.content.Intent.CATEGORY_LAUNCHER; +import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; +import static com.google.common.truth.Truth.assertThat; +import static java.util.concurrent.TimeUnit.SECONDS; + +@RunWith(AndroidJUnit4.class) +public final class ProcessPhoenixTest { + private static final String PROCESS_ID = "Process ID: "; + private static final String EXTRA_TEXT = "Extra Text: "; + private static final String TARGET_PACKAGE = "com.jakewharton.processphoenix.sample"; + + private final Context context = InstrumentationRegistry.getTargetContext(); + private final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + private UiDevice device = UiDevice.getInstance(instrumentation); + + @Before public void launchSample() { + PackageManager pm = context.getPackageManager(); + Intent intent = pm.getLaunchIntentForPackage(TARGET_PACKAGE); + if (intent == null) { + throw new AssertionError("ProcessPhoenix Sample not installed."); + } + intent.addCategory(CATEGORY_LAUNCHER); + intent.addFlags(FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + device.waitForWindowUpdate(TARGET_PACKAGE, SECONDS.toMillis(2)); + } + + @Test public void triggerRebirthCreatesNewProcess() throws UiObjectNotFoundException { + UiObject originalObject = device.findObject(new UiSelector().textStartsWith(PROCESS_ID)); + int originalId = Integer.parseInt(originalObject.getText().substring(PROCESS_ID.length())); + + device.findObject(new UiSelector().text("Restart Process")).click(); + device.waitForWindowUpdate(TARGET_PACKAGE, SECONDS.toMillis(2)); + + UiObject newObject = device.findObject(new UiSelector().textStartsWith(PROCESS_ID)); + int newId = Integer.parseInt(newObject.getText().substring(PROCESS_ID.length())); + + assertThat(originalId).isNotEqualTo(newId); + } + + @Test public void triggerRebirthWithIntentCreatesNewProcessUsingIntent() + throws UiObjectNotFoundException { + UiObject originalObject = device.findObject(new UiSelector().textStartsWith(PROCESS_ID)); + int originalId = Integer.parseInt(originalObject.getText().substring(PROCESS_ID.length())); + + device.findObject(new UiSelector().text("Restart Process with Intent")).click(); + device.waitForWindowUpdate(TARGET_PACKAGE, SECONDS.toMillis(2)); + + UiObject newObject = device.findObject(new UiSelector().textStartsWith(PROCESS_ID)); + int newId = Integer.parseInt(newObject.getText().substring(PROCESS_ID.length())); + + assertThat(originalId).isNotEqualTo(newId); + + UiObject extraObject = device.findObject(new UiSelector().textStartsWith(EXTRA_TEXT)); + String extraText = extraObject.getText().substring(EXTRA_TEXT.length()); + + assertThat(extraText).isEqualTo("Hello!"); + } +}