Skip to content

Commit

Permalink
Add simple UI Automator tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Jul 28, 2015
1 parent 83b0e2e commit 0e73580
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@ 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

script:
- ./gradlew installDebug connectedCheck

after_success:
- .buildscript/deploy_snapshot.sh

Expand Down
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
8 changes: 8 additions & 0 deletions process-phoenix/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
apply plugin: 'com.android.library'

dependencies {
androidTestCompile rootProject.ext.supportTestRunner
androidTestCompile rootProject.ext.supportTestRules
androidTestCompile rootProject.ext.supportTestUiAutomator
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 {
Expand Down
15 changes: 15 additions & 0 deletions process-phoenix/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.jakewharton.processphoenix"
>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<!-- TODO http://b.android.com/179651 -->
<uses-sdk
android:minSdkVersion="18"
tools:overrideLibrary="android.support.test, android.app, android.support.test.rule, android.support.test.uiautomator.v18"
/>
<application/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.jakewharton.processphoenix;

import android.app.Instrumentation;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.PowerManager;
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.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.content.Context.KEYGUARD_SERVICE;
import static android.content.Context.POWER_SERVICE;
import static android.content.Intent.CATEGORY_LAUNCHER;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static android.os.PowerManager.ACQUIRE_CAUSES_WAKEUP;
import static android.os.PowerManager.FULL_WAKE_LOCK;
import static android.os.PowerManager.ON_AFTER_RELEASE;
import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertTrue;

@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 final UiDevice device = UiDevice.getInstance(instrumentation);

private PowerManager.WakeLock wakeLock;

@Before public void launchSample() {
PackageManager pm = context.getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(TARGET_PACKAGE);
if (intent == null) {
throw new AssertionError("ProcessPhoenix Sample not installed.");
}

// Unlock the device so that the tests can input keystrokes.
KeyguardManager keyguard = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
keyguard.newKeyguardLock("test").disableKeyguard();
// Wake up the screen.
PowerManager power = (PowerManager) context.getSystemService(POWER_SERVICE);
wakeLock = power.newWakeLock(FULL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE, "test");
wakeLock.acquire();

intent.addCategory(CATEGORY_LAUNCHER);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
waitForWindow();
}

private void waitForWindow() {
assertTrue(device.waitForWindowUpdate(TARGET_PACKAGE, SECONDS.toMillis(15)));
}

@After public void tearDown() {
wakeLock.release();
}

@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();
waitForWindow();

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();
waitForWindow();

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!");
}
}

0 comments on commit 0e73580

Please sign in to comment.