OSDN Git Service

Cleaned up unit tests.
authorFelipe Leme <felipeal@google.com>
Fri, 11 Dec 2015 21:06:45 +0000 (13:06 -0800)
committerFelipe Leme <felipeal@google.com>
Sat, 12 Dec 2015 01:15:56 +0000 (17:15 -0800)
This refactoring will make it easier to add new test cases for the
upcoming bugreport details notification.

BUG: 25794470
Change-Id: I0bf6ab54a7b4aa1dfa51ad0354682e5764b8a209

packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java

index 6b6b0da..c7ce311 100644 (file)
@@ -78,7 +78,6 @@ import com.android.shell.ActionSendMultipleConsumerActivity.CustomActionSendMult
  */
 @LargeTest
 public class BugreportReceiverTest extends InstrumentationTestCase {
-
     private static final String TAG = "BugreportReceiverTest";
 
     // Timeout for UI operations, in milliseconds.
@@ -92,6 +91,11 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
     private static final String BUGREPORT_CONTENT = "Dump, might as well dump!\n";
     private static final String SCREENSHOT_CONTENT = "A picture is worth a thousand words!\n";
 
+    private static final int PID = 42;
+    private static final String PROGRESS_PROPERTY = "dumpstate.42.progress";
+    private static final String MAX_PROPERTY = "dumpstate.42.max";
+    private static final String NAME = "BUG, Y U NO REPORT?";
+
     private String mPlainTextPath;
     private String mZipPath;
     private String mScreenshotPath;
@@ -106,43 +110,39 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
         mContext = instrumentation.getTargetContext();
         mUiBot = new UiBot(UiDevice.getInstance(instrumentation), TIMEOUT);
         mListener = ActionSendMultipleConsumerActivity.getListener(mContext);
+
+        cancelExistingNotifications();
+
         mPlainTextPath = getPath(BUGREPORT_FILE);
         mZipPath = getPath(ZIP_FILE);
         mScreenshotPath = getPath(SCREENSHOT_FILE);
-        cancelExistingNotifications();
+        createTextFile(mPlainTextPath, BUGREPORT_CONTENT);
+        createTextFile(mScreenshotPath, SCREENSHOT_CONTENT);
+        createZipFile(mZipPath, BUGREPORT_FILE, BUGREPORT_CONTENT);
+
         BugreportPrefs.setWarningState(mContext, BugreportPrefs.STATE_HIDE);
     }
 
     public void testFullWorkflow() throws Exception {
-        final String name = "BUG, Y U NO REPORT?";
-        // TODO: call method to remove property instead
-        SystemProperties.set("dumpstate.42.progress", "0");
-        SystemProperties.set("dumpstate.42.max", "0");
-
-        Intent intent = new Intent(INTENT_BUGREPORT_STARTED);
-        intent.putExtra(EXTRA_PID, 42);
-        intent.putExtra(EXTRA_NAME, name);
-        intent.putExtra(EXTRA_MAX, 1000);
-        mContext.sendBroadcast(intent);
+        resetProperties();
+        sendBugreportStarted(1000);
 
-        assertProgressNotification(name, "0.00%");
+        assertProgressNotification(NAME, "0.00%");
 
-        SystemProperties.set("dumpstate.42.progress", "108");
-        assertProgressNotification(name, "10.80%");
+        SystemProperties.set(PROGRESS_PROPERTY, "108");
+        assertProgressNotification(NAME, "10.80%");
 
-        SystemProperties.set("dumpstate.42.progress", "500");
-        assertProgressNotification(name, "50.00%");
+        SystemProperties.set(PROGRESS_PROPERTY, "500");
+        assertProgressNotification(NAME, "50.00%");
 
-        SystemProperties.set("dumpstate.42.max", "2000");
-        assertProgressNotification(name, "25.00%");
+        SystemProperties.set(MAX_PROPERTY, "2000");
+        assertProgressNotification(NAME, "25.00%");
 
-        createTextFile(mPlainTextPath, BUGREPORT_CONTENT);
-        createTextFile(mScreenshotPath, SCREENSHOT_CONTENT);
-        Bundle extras = sendBugreportFinishedIntent(42, mPlainTextPath, mScreenshotPath);
+        Bundle extras =
+                sendBugreportFinishedAndGetSharedIntent(PID, mPlainTextPath, mScreenshotPath);
         assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT);
 
-        String service = BugreportProgressService.class.getName();
-        assertFalse("Service '" + service + "' is still running", isServiceRunning(service));
+        assertServiceNotRunning();
     }
 
     public void testBugreportFinished_withWarning() throws Exception {
@@ -150,11 +150,8 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
         BugreportPrefs.setWarningState(mContext, BugreportPrefs.STATE_SHOW);
 
         // Send notification and click on share.
-        createTextFile(mPlainTextPath, BUGREPORT_CONTENT);
-        Intent intent = new Intent(INTENT_BUGREPORT_FINISHED);
-        intent.putExtra(EXTRA_BUGREPORT, mPlainTextPath);
-        mContext.sendBroadcast(intent);
-        mUiBot.clickOnNotification(mContext.getString(R.string.bugreport_finished_title));
+        sendBugreportFinished(null, mPlainTextPath, null);
+        acceptBugreport();
 
         // Handle the warning
         mUiBot.getVisibleObject(mContext.getString(R.string.bugreport_confirm));
@@ -176,28 +173,22 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
     }
 
     public void testBugreportFinished_plainBugreportAndScreenshot() throws Exception {
-        createTextFile(mPlainTextPath, BUGREPORT_CONTENT);
-        createTextFile(mScreenshotPath, SCREENSHOT_CONTENT);
-        Bundle extras = sendBugreportFinishedIntent(mPlainTextPath, mScreenshotPath);
+        Bundle extras = sendBugreportFinishedAndGetSharedIntent(mPlainTextPath, mScreenshotPath);
         assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT);
     }
 
     public void testBugreportFinished_zippedBugreportAndScreenshot() throws Exception {
-        createZipFile(mZipPath, BUGREPORT_FILE, BUGREPORT_CONTENT);
-        createTextFile(mScreenshotPath, SCREENSHOT_CONTENT);
-        Bundle extras = sendBugreportFinishedIntent(mZipPath, mScreenshotPath);
+        Bundle extras = sendBugreportFinishedAndGetSharedIntent(mZipPath, mScreenshotPath);
         assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT);
     }
 
     public void testBugreportFinished_plainBugreportAndNoScreenshot() throws Exception {
-        createTextFile(mPlainTextPath, BUGREPORT_CONTENT);
-        Bundle extras = sendBugreportFinishedIntent(mPlainTextPath, null);
+        Bundle extras = sendBugreportFinishedAndGetSharedIntent(mPlainTextPath, null);
         assertActionSendMultiple(extras, BUGREPORT_CONTENT, null);
     }
 
     public void testBugreportFinished_zippedBugreportAndNoScreenshot() throws Exception {
-        createZipFile(mZipPath, BUGREPORT_FILE, BUGREPORT_CONTENT);
-        Bundle extras = sendBugreportFinishedIntent(mZipPath, null);
+        Bundle extras = sendBugreportFinishedAndGetSharedIntent(mZipPath, null);
         assertActionSendMultiple(extras, BUGREPORT_CONTENT, null);
     }
 
@@ -221,17 +212,67 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
         mUiBot.getObject(percent);
     }
 
+    void resetProperties() {
+        // TODO: call method to remove property instead
+        SystemProperties.set(PROGRESS_PROPERTY, "0");
+        SystemProperties.set(MAX_PROPERTY, "0");
+    }
+
+    /**
+     * Sends a "bugreport started" intent with the default values.
+     */
+    private void sendBugreportStarted(int max) {
+        Intent intent = new Intent(INTENT_BUGREPORT_STARTED);
+        intent.putExtra(EXTRA_PID, PID);
+        intent.putExtra(EXTRA_NAME, NAME);
+        intent.putExtra(EXTRA_MAX, max);
+        mContext.sendBroadcast(intent);
+    }
+
     /**
      * Sends a "bugreport finished" intent and waits for the result.
      *
-     * @return extras sent to the bugreport finished consumer.
+     * @return extras sent in the shared intent.
      */
-    private Bundle sendBugreportFinishedIntent(String bugreportPath, String screenshotPath) {
-        return sendBugreportFinishedIntent(null, bugreportPath, screenshotPath);
+    private Bundle sendBugreportFinishedAndGetSharedIntent(String bugreportPath,
+            String screenshotPath) {
+        return sendBugreportFinishedAndGetSharedIntent(null, bugreportPath, screenshotPath);
     }
 
-    private Bundle sendBugreportFinishedIntent(Integer pid, String bugreportPath,
+    /**
+     * Sends a "bugreport finished" intent and waits for the result.
+     *
+     * @return extras sent in the shared intent.
+     */
+    private Bundle sendBugreportFinishedAndGetSharedIntent(Integer pid, String bugreportPath,
             String screenshotPath) {
+        sendBugreportFinished(pid, bugreportPath, screenshotPath);
+        return acceptBugreportAndGetSharedIntent();
+    }
+
+    /**
+     * Accepts the notification to share the finished bugreport and waits for the result.
+     *
+     * @return extras sent in the shared intent.
+     */
+    private Bundle acceptBugreportAndGetSharedIntent() {
+        acceptBugreport();
+        mUiBot.chooseActivity(UI_NAME);
+        return mListener.getExtras();
+    }
+
+    /**
+     * Accepts the notification to share the finished bugreport.
+     */
+    private void acceptBugreport() {
+        mUiBot.clickOnNotification(mContext.getString(R.string.bugreport_finished_title));
+    }
+
+    /**
+     * Sends a "bugreport finished" intent.
+     *
+     */
+    private void sendBugreportFinished(Integer pid, String bugreportPath, String screenshotPath) {
         Intent intent = new Intent(INTENT_BUGREPORT_FINISHED);
         if (pid != null) {
             intent.putExtra(EXTRA_PID, pid);
@@ -244,10 +285,6 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
         }
 
         mContext.sendBroadcast(intent);
-
-        mUiBot.clickOnNotification(mContext.getString(R.string.bugreport_finished_title));
-        mUiBot.chooseActivity(UI_NAME);
-        return mListener.getExtras();
     }
 
     /**
@@ -318,6 +355,11 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
         fail("Did not find entry '" + entryName + "' on file '" + uri + "'");
     }
 
+    private void assertServiceNotRunning() {
+        String service = BugreportProgressService.class.getName();
+        assertFalse("Service '" + service + "' is still running", isServiceRunning(service));
+    }
+
     private boolean isServiceRunning(String name) {
         ActivityManager manager = (ActivityManager) mContext
                 .getSystemService(Context.ACTIVITY_SERVICE);