-
Notifications
You must be signed in to change notification settings - Fork 931
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instrumentation test infrastructure.
A custom test runner subclass flips a boolean on a debug module before the graph is created. When the graph does get created, we use that boolean to force mock mode and skip wrapping the debug controls (both because it would never be used but also to ensure view-based matchers of Espresso work). We also kill the keyguard and hold a wake lock so that the screen is on and visible.
- Loading branch information
1 parent
ea724b1
commit 696c76d
Showing
7 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/androidTestInternal/java/com/jakewharton/u2020/U2020TestRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.jakewharton.u2020; | ||
|
||
import android.app.KeyguardManager; | ||
import android.content.Context; | ||
import android.os.PowerManager; | ||
import android.support.test.runner.AndroidJUnitRunner; | ||
|
||
import static android.content.Context.KEYGUARD_SERVICE; | ||
import static android.content.Context.POWER_SERVICE; | ||
import static android.os.PowerManager.ACQUIRE_CAUSES_WAKEUP; | ||
import static android.os.PowerManager.FULL_WAKE_LOCK; | ||
import static android.os.PowerManager.ON_AFTER_RELEASE; | ||
|
||
public final class U2020TestRunner extends AndroidJUnitRunner { | ||
@Override public void onStart() { | ||
// Inform the app we are an instrumentation test before the object graph is initialized. | ||
DebugU2020Module.instrumentationTest = true; | ||
|
||
runOnMainSync(new Runnable() { | ||
@SuppressWarnings("deprecation") // We don't care about deprecation here. | ||
@Override public void run() { | ||
Context app = getTargetContext().getApplicationContext(); | ||
|
||
String name = U2020TestRunner.class.getSimpleName(); | ||
// Unlock the device so that the tests can input keystrokes. | ||
KeyguardManager keyguard = (KeyguardManager) app.getSystemService(KEYGUARD_SERVICE); | ||
keyguard.newKeyguardLock(name).disableKeyguard(); | ||
// Wake up the screen. | ||
PowerManager power = (PowerManager) app.getSystemService(POWER_SERVICE); | ||
power.newWakeLock(FULL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE, name) | ||
.acquire(); | ||
} | ||
}); | ||
|
||
super.onStart(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/androidTestInternal/java/com/jakewharton/u2020/ui/DummyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.jakewharton.u2020.ui; | ||
|
||
import android.support.test.rule.ActivityTestRule; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static android.os.SystemClock.sleep; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public final class DummyTest { | ||
@Rule public final ActivityTestRule<MainActivity> main = | ||
new ActivityTestRule<>(MainActivity.class); | ||
|
||
@Test public void noneOfTheThings() { | ||
sleep(SECONDS.toMillis(5)); // Long enough to see some data from mock mode. | ||
assertTrue(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/internalDebug/java/com/jakewharton/u2020/IsInstrumentationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.jakewharton.u2020; | ||
|
||
import java.lang.annotation.Retention; | ||
import javax.inject.Qualifier; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Qualifier @Retention(RUNTIME) | ||
public @interface IsInstrumentationTest { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters