ScreenshotTestpublic class ScreenshotTest extends android.test.ActivityInstrumentationTestCase2 Functional tests for the global screenshot feature. |
Fields Summary |
---|
private static final String | LOG_TAG | private static final int | SCREEN_WAIT_TIME_SEC |
Constructors Summary |
---|
public ScreenshotTest()
super(ScreenshotStubActivity.class);
|
Methods Summary |
---|
private java.io.File | getScreenshotDir()Get the directory where screenshot images are stored.
// TODO: get this dir location from a constant
return new File(Environment.getExternalStorageDirectory(), "Pictures" + File.separator +
"Screenshots");
| private boolean | isValidImage(java.io.File screenshotFile)Return true if file is valid image file
Bitmap b = BitmapFactory.decodeFile(screenshotFile.getAbsolutePath());
// TODO: do more checks on image
return b != null;
| private void | takeScreenshot()Inject the key sequence to take a screenshot.
getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_POWER));
getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_VOLUME_DOWN));
// the volume down key event will cause the 'volume adjustment' UI to appear in the
// foreground, and steal UI focus
// unfortunately this means the next key event will get directed to the
// 'volume adjustment' UI, instead of this test's activity
// for this reason this test must be signed with platform certificate, to grant this test
// permission to inject key events to another process
getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_VOLUME_DOWN));
getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_POWER));
| public void | testScreenshot()A simple test for screenshots that launches an Activity, injects the key event combo
to trigger the screenshot, and verifies the screenshot was taken successfully.
if (true) {
// Disable until this works again.
return;
}
Log.d(LOG_TAG, "starting testScreenshot");
// launch the activity.
ScreenshotStubActivity activity = getActivity();
assertNotNull(activity);
File screenshotDir = getScreenshotDir();
NewScreenshotObserver observer = new NewScreenshotObserver(
screenshotDir.getAbsolutePath());
observer.startWatching();
takeScreenshot();
// unlikely, but check if a new screenshot file was already created
if (observer.getCreatedPath() == null) {
// wait for screenshot to be created
synchronized(observer) {
observer.wait(SCREEN_WAIT_TIME_SEC*1000);
}
}
assertNotNull(String.format("Could not find screenshot after %d seconds",
SCREEN_WAIT_TIME_SEC), observer.getCreatedPath());
File screenshotFile = new File(screenshotDir, observer.getCreatedPath());
try {
assertTrue(String.format("Detected new screenshot %s but its not a file",
screenshotFile.getName()), screenshotFile.isFile());
assertTrue(String.format("Detected new screenshot %s but its not an image",
screenshotFile.getName()), isValidImage(screenshotFile));
} finally {
// delete the file to prevent external storage from filing up
screenshotFile.delete();
}
|
|