public void testScreenshot() throws Exception {
  Log.d(LOG_TAG,"starting testScreenshot");
  ScreenshotStubActivity activity=getActivity();
  assertNotNull(activity);
  File screenshotDir=getScreenshotDir();
  NewScreenshotObserver observer=new NewScreenshotObserver(screenshotDir.getAbsolutePath());
  observer.startWatching();
  takeScreenshot();
  if (observer.getCreatedPath() == null) {
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 {
    screenshotFile.delete();
  }
}

public void testScreenshot ( ) throws Exception { Log . d ( LOG_TAG , "starting testScreenshot" ) ; ScreenshotStubActivity activity = getActivity ( ) ; assertNotNull ( activity ) ; File screenshotDir = getScreenshotDir ( ) ; NewScreenshotObserver observer = new NewScreenshotObserver ( screenshotDir . getAbsolutePath ( ) ) ; observer . startWatching ( ) ; takeScreenshot ( ) ; if ( observer . getCreatedPath ( ) == null ) { 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 { screenshotFile . delete ( ) ; } }
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 .
