Refactor Dialog Tests

Bug 3184944

The dialog tests start an activity that had a list of buttons
where each button launched a different type of dialog and then
hit the down button a number of times and then the center button
to launch them. Replace this approach with an intent based
approach where the tests launch an intent specifying what dialog
to create. This should make the tests less fragile to UI changes.

Change-Id: I3d36e2f9e6c1b4a065ffda59289a85278e04c8cf
diff --git a/tests/res/layout/dialog_stub_layout.xml b/tests/res/layout/dialog_stub_layout.xml
index 11b502f..b0a8861 100644
--- a/tests/res/layout/dialog_stub_layout.xml
+++ b/tests/res/layout/dialog_stub_layout.xml
@@ -14,65 +14,8 @@
      limitations under the License.
 -->
 
-<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
-    android:layout_width="match_parent" android:layout_height="match_parent"
-    android:orientation="vertical">
-    <LinearLayout
-        android:layout_width="match_parent" android:layout_height="match_parent"
-        android:orientation="vertical">
-        <Button android:id="@+id/dialog_test_button_1"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button1"/>
-        <Button android:id="@+id/dialog_test_button_2"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button2"/>
-        <Button android:id="@+id/dialog_test_button_3"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button3"/>
-        <Button android:id="@+id/dialog_test_button_4"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button4"/>
-        <Button android:id="@+id/dialog_test_button_5"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button5"/>
-        <Button android:id="@+id/dialog_test_button_6"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button6"/>
-        <Button android:id="@+id/dialog_test_button_7"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button7"/>
-        <Button android:id="@+id/dialog_test_button_8"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button8"/>
-        <Button android:id="@+id/dialog_test_button_9"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button9"/>
-        <Button android:id="@+id/dialog_test_button_10"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button10"/>
-        <Button android:id="@+id/dialog_test_button_11"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button11"/>
-        <Button android:id="@+id/dialog_test_button_12"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button12"/>
-        <Button android:id="@+id/dialog_test_button_13"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button13"/>
-         <Button android:id="@+id/dialog_test_button_14"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button14"/>
-        <Button android:id="@+id/dialog_test_button_15"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button15"/>
-        <Button android:id="@+id/dialog_test_button_16"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button16"/>
-        <Button android:id="@+id/dialog_test_button_17"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button17"/>
-        <Button android:id="@+id/dialog_test_button_18"
-            android:layout_width="match_parent" android:layout_height="wrap_content"
-            android:text="@string/dialog_stub_dialog_test_button18"/>
-    </LinearLayout>
-</ScrollView>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
+        android:layout_width="match_parent" 
+        android:layout_height="match_parent"
+        >
+</LinearLayout>
diff --git a/tests/res/values/strings.xml b/tests/res/values/strings.xml
index a779b47..8fcd75a 100644
--- a/tests/res/values/strings.xml
+++ b/tests/res/values/strings.xml
@@ -40,24 +40,6 @@
     <string name="text">DialogTest</string>
     <string name="text_country">Country</string>
     <string name="text_name">Name</string>
-    <string name="dialog_stub_dialog_test_button1">Test Dialog without theme</string>
-    <string name="dialog_stub_dialog_test_button2">Test Dialog with theme</string>
-    <string name="dialog_stub_dialog_test_button3">Test AlertDialog</string>
-    <string name="dialog_stub_dialog_test_button4">Test AlertDialog with theme</string>
-    <string name="dialog_stub_dialog_test_button5">Test DatePickerDialog</string>
-    <string name="dialog_stub_dialog_test_button6">Test DatePickerDialog with theme</string>
-    <string name="dialog_stub_dialog_test_button7">Test TimePickerDialog</string>
-    <string name="dialog_stub_dialog_test_button8">Test TimePickerDialog with theme</string>
-    <string name="dialog_stub_dialog_test_button9">Test onStart() and onStop()</string>
-    <string name="dialog_stub_dialog_test_button10">Test AlertDialog deprecated</string>
-    <string name="dialog_stub_dialog_test_button11">Test AlertDialog callback</string>
-    <string name="dialog_stub_dialog_test_button12">Test AlertDialog setview </string>
-    <string name="dialog_stub_dialog_test_button13">Test AlertDialog deprecated with Message</string>
-    <string name="dialog_stub_dialog_test_button14">Test AlertDialog with theme</string>
-    <string name="dialog_stub_dialog_test_button15">Test AlertDialog cancelable</string>
-    <string name="dialog_stub_dialog_test_button16">Test AlertDialog can\'n cancelable</string>
-    <string name="dialog_stub_dialog_test_button17">Test Dialog cancelable</string>
-    <string name="dialog_stub_dialog_test_button18">Test Dialog not cancelable</string>
     <string name="hello_world">Hello, World!</string>
     <string name="hello_android">Hello, Android!</string>
     <string name="alert_dialog_username">Name:</string>
diff --git a/tests/src/android/app/cts/DialogStubActivity.java b/tests/src/android/app/cts/DialogStubActivity.java
index f715b51..817e716 100644
--- a/tests/src/android/app/cts/DialogStubActivity.java
+++ b/tests/src/android/app/cts/DialogStubActivity.java
@@ -16,6 +16,8 @@
 
 package android.app.cts;
 
+import com.android.cts.stub.R;
+
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.DatePickerDialog;
@@ -25,21 +27,19 @@
 import android.app.TimePickerDialog.OnTimeSetListener;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.Intent;
 import android.content.DialogInterface.OnCancelListener;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
+import android.test.ActivityInstrumentationTestCase2;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.View;
-import android.view.View.OnClickListener;
-import android.widget.Button;
 import android.widget.DatePicker;
 import android.widget.TimePicker;
 
-import com.android.cts.stub.R;
-
 /*
  * Stub class for  Dialog, AlertDialog, DatePickerDialog, TimePickerDialog etc.
  */
@@ -289,49 +289,27 @@
         return (String) mDialog.getWindow().getAttributes().getTitle();
     }
 
+    private static final String TEST_DIALOG_NUMBER_EXTRA = "testDialogNumber";
+
+    public static <T extends Activity> T startDialogActivity(
+            ActivityInstrumentationTestCase2<T> testCase, int dialogNumber) {
+        Intent intent = new Intent(Intent.ACTION_MAIN);
+        intent.putExtra(TEST_DIALOG_NUMBER_EXTRA, dialogNumber);
+        testCase.setActivityIntent(intent);
+        return testCase.getActivity();
+    }
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
         setContentView(R.layout.dialog_stub_layout);
 
-        findViewById(R.id.dialog_test_button_1).setOnClickListener(
-                new MockClickListener(TEST_DIALOG_WITHOUT_THEME));
-        findViewById(R.id.dialog_test_button_2).setOnClickListener(
-                new MockClickListener(TEST_DIALOG_WITH_THEME));
-        findViewById(R.id.dialog_test_button_3).setOnClickListener(
-                new MockClickListener(TEST_ALERTDIALOG));
-        findViewById(R.id.dialog_test_button_4).setOnClickListener(
-                new MockClickListener(TEST_CUSTOM_ALERTDIALOG));
-        final Button dialogTestButton5 = (Button) findViewById(R.id.dialog_test_button_5);
-        dialogTestButton5.setOnClickListener(new MockClickListener(TEST_DATEPICKERDIALOG));
-        findViewById(R.id.dialog_test_button_6).setOnClickListener(
-                new MockClickListener(TEST_DATEPICKERDIALOG_WITH_THEME));
-        findViewById(R.id.dialog_test_button_7).setOnClickListener(
-                new MockClickListener(TEST_TIMEPICKERDIALOG));
-        findViewById(R.id.dialog_test_button_8).setOnClickListener(
-                new MockClickListener(TEST_TIMEPICKERDIALOG_WITH_THEME));
-        findViewById(R.id.dialog_test_button_9).setOnClickListener(
-                new MockClickListener(TEST_ONSTART_AND_ONSTOP));
-        findViewById(R.id.dialog_test_button_10).setOnClickListener(
-                new MockClickListener(TEST_ALERTDIALOG_DEPRECATED));
-        findViewById(R.id.dialog_test_button_11).setOnClickListener(
-                new MockClickListener(TEST_ALERTDIALOG_CALLBACK));
-        findViewById(R.id.dialog_test_button_12).setOnClickListener(
-                new MockClickListener(TEST_CUSTOM_ALERTDIALOG_VIEW));
-        findViewById(R.id.dialog_test_button_13).setOnClickListener(
-                new MockClickListener(TEST_ALERTDIALOG_DEPRECATED_WITH_MESSAGE));
-
-        findViewById(R.id.dialog_test_button_14).setOnClickListener(
-                new MockClickListener(TEST_ALERTDIALOG_THEME));
-        findViewById(R.id.dialog_test_button_15).setOnClickListener(
-                new MockClickListener(TEST_ALERTDIALOG_CANCELABLE));
-        findViewById(R.id.dialog_test_button_16).setOnClickListener(
-                new MockClickListener(TEST_ALERTDIALOG_NOT_CANCELABLE));
-        findViewById(R.id.dialog_test_button_17).setOnClickListener(
-                new MockClickListener(TEST_PROTECTED_CANCELABLE));
-        findViewById(R.id.dialog_test_button_18).setOnClickListener(
-                new MockClickListener(TEST_PROTECTED_NOT_CANCELABLE));
+        Intent intent = getIntent();
+        int dialogNum = intent.getIntExtra(TEST_DIALOG_NUMBER_EXTRA, -1);
+        if (dialogNum != -1) {
+            showDialog(dialogNum);
+        }
     }
 
     public void setUpTitle(final String title) {
@@ -383,18 +361,6 @@
 
     }
 
-    private class MockClickListener implements OnClickListener {
-        private final int mId;
-
-        public MockClickListener(final int id) {
-            mId = id;
-        }
-
-        public void onClick(View v) {
-            showDialog(mId);
-        }
-    }
-
     class MockOnClickListener implements DialogInterface.OnClickListener {
         private final int mId;
 
diff --git a/tests/tests/app/src/android/app/cts/AlertDialogTest.java b/tests/tests/app/src/android/app/cts/AlertDialogTest.java
index 4ab57da..b5711ed 100644
--- a/tests/tests/app/src/android/app/cts/AlertDialogTest.java
+++ b/tests/tests/app/src/android/app/cts/AlertDialogTest.java
@@ -16,6 +16,11 @@
 
 package android.app.cts;
 
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+import dalvik.annotation.TestTargets;
+
 import android.app.AlertDialog;
 import android.app.Instrumentation;
 import android.content.Context;
@@ -29,10 +34,6 @@
 import android.view.KeyEvent;
 import android.view.View;
 import android.widget.Button;
-import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetClass;
-import dalvik.annotation.TestTargetNew;
-import dalvik.annotation.TestTargets;
 
 /*
  * Test AlertDialog
@@ -55,15 +56,10 @@
     protected void setUp() throws Exception {
         super.setUp();
         mInstrumentation = getInstrumentation();
-        mActivity = getActivity();
     }
 
-    protected void popDialog(int index) {
-        while (index != 0) {
-            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
-            index--;
-        }
-        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
+    protected void startDialogActivity(int dialogNumber) {
+        mActivity = DialogStubActivity.startDialogActivity(this, dialogNumber);
     }
 
     @TestTargets({
@@ -121,7 +117,7 @@
     }
 
     private void doTestAlertDialog(int index) throws Throwable {
-        popDialog(index);
+        startDialogActivity(index);
         assertTrue(mActivity.getDialog().isShowing());
 
         mPositiveButton = ((AlertDialog) (mActivity.getDialog())).getButton(
@@ -294,7 +290,7 @@
         )
     })
     public void testAlertDialogDeprecatedAPIWithMessage() throws Throwable {
-        popDialog(DialogStubActivity.TEST_ALERTDIALOG_DEPRECATED_WITH_MESSAGE);
+        startDialogActivity(DialogStubActivity.TEST_ALERTDIALOG_DEPRECATED_WITH_MESSAGE);
         assertTrue(mActivity.getDialog().isShowing());
 
         mPositiveButton = ((AlertDialog) (mActivity.getDialog())).getButton(
@@ -368,7 +364,7 @@
         )
     })
     public void testCustomAlertDialog() {
-        popDialog(DialogStubActivity.TEST_CUSTOM_ALERTDIALOG);
+        startDialogActivity(DialogStubActivity.TEST_CUSTOM_ALERTDIALOG);
         assertTrue(mActivity.getDialog().isShowing());
     }
 
@@ -405,7 +401,7 @@
         )
     })
     public void testCustomAlertDialogView() {
-        popDialog(DialogStubActivity.TEST_CUSTOM_ALERTDIALOG_VIEW);
+        startDialogActivity(DialogStubActivity.TEST_CUSTOM_ALERTDIALOG_VIEW);
         assertTrue(mActivity.getDialog().isShowing());
     }
 
@@ -437,7 +433,7 @@
         )
     })
     public void testCallback() {
-        popDialog(DialogStubActivity.TEST_ALERTDIALOG_CALLBACK);
+        startDialogActivity(DialogStubActivity.TEST_ALERTDIALOG_CALLBACK);
         assertTrue(mActivity.onCreateCalled);
 
         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
@@ -453,7 +449,7 @@
         args = {Context.class, int.class}
     )
     public void testAlertDialogTheme() throws Exception {
-        popDialog(DialogStubActivity.TEST_ALERTDIALOG_THEME);
+        startDialogActivity(DialogStubActivity.TEST_ALERTDIALOG_THEME);
         assertTrue(mActivity.getDialog().isShowing());
     }
 
@@ -464,7 +460,7 @@
         args = {Context.class, boolean.class, OnCancelListener.class}
     )
     public void testAlertDialogCancelable() throws Exception {
-        popDialog(DialogStubActivity.TEST_ALERTDIALOG_CANCELABLE);
+        startDialogActivity(DialogStubActivity.TEST_ALERTDIALOG_CANCELABLE);
         assertTrue(mActivity.getDialog().isShowing());
         assertFalse(mActivity.onCancelCalled);
         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
@@ -479,7 +475,7 @@
         args = {Context.class, boolean.class, OnCancelListener.class}
     )
     public void testAlertDialogNotCancelable() throws Exception {
-        popDialog(DialogStubActivity.TEST_ALERTDIALOG_NOT_CANCELABLE);
+        startDialogActivity(DialogStubActivity.TEST_ALERTDIALOG_NOT_CANCELABLE);
         assertTrue(mActivity.getDialog().isShowing());
         assertFalse(mActivity.onCancelCalled);
         mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
diff --git a/tests/tests/app/src/android/app/cts/DatePickerDialogTest.java b/tests/tests/app/src/android/app/cts/DatePickerDialogTest.java
index 140ccaa..4ca5a4e 100644
--- a/tests/tests/app/src/android/app/cts/DatePickerDialogTest.java
+++ b/tests/tests/app/src/android/app/cts/DatePickerDialogTest.java
@@ -47,15 +47,6 @@
     protected void setUp() throws Exception {
         super.setUp();
         mInstrumentation = getInstrumentation();
-        mActivity = getActivity();
-    }
-
-    @Override
-    protected void tearDown() throws Exception {
-        if (mActivity != null) {
-            mActivity.finish();
-        }
-        super.tearDown();
     }
 
     @TestTargets({
@@ -144,7 +135,7 @@
     }
 
     private void doTestDatePickerDialog(int index) throws Exception {
-        popDialog(index);
+        startDialogActivity(index);
         final DatePickerDialog datePickerDialog = (DatePickerDialog) mActivity.getDialog();
         assertTrue(datePickerDialog.isShowing());
         final TextView title = (TextView) datePickerDialog.findViewById(
@@ -178,15 +169,7 @@
         assertTrue(DialogStubActivity.onRestoreInstanceStateCalled);
     }
 
-    private void popDialog(int index) {
-        assertTrue(index > 0);
-
-        while (index != 0) {
-            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
-            index--;
-        }
-
-        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
+    private void startDialogActivity(int index) {
+        mActivity = DialogStubActivity.startDialogActivity(this, index);
     }
-
 }
diff --git a/tests/tests/app/src/android/app/cts/DialogTest.java b/tests/tests/app/src/android/app/cts/DialogTest.java
index 6a48358..3500d27 100644
--- a/tests/tests/app/src/android/app/cts/DialogTest.java
+++ b/tests/tests/app/src/android/app/cts/DialogTest.java
@@ -15,8 +15,14 @@
  */
 package android.app.cts;
 
-import java.lang.ref.WeakReference;
 import com.android.cts.stub.R;
+
+import dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+import dalvik.annotation.TestTargets;
+import dalvik.annotation.ToBeFixed;
+
 import android.app.Dialog;
 import android.app.Instrumentation;
 import android.content.Context;
@@ -24,7 +30,6 @@
 import android.content.DialogInterface.OnCancelListener;
 import android.content.DialogInterface.OnDismissListener;
 import android.content.DialogInterface.OnKeyListener;
-import android.content.pm.ActivityInfo;
 import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.Canvas;
@@ -45,11 +50,8 @@
 import android.view.Window;
 import android.view.WindowManager;
 import android.widget.LinearLayout;
-import dalvik.annotation.TestLevel;
-import dalvik.annotation.TestTargetClass;
-import dalvik.annotation.TestTargetNew;
-import dalvik.annotation.TestTargets;
-import dalvik.annotation.ToBeFixed;
+
+import java.lang.ref.WeakReference;
 
 @TestTargetClass(Dialog.class)
 public class DialogTest extends ActivityInstrumentationTestCase2<DialogStubActivity> {
@@ -84,30 +86,12 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-
         mInstrumentation = getInstrumentation();
         mContext = mInstrumentation.getContext();
-        mActivity = getActivity();
     }
 
-    @Override
-    protected void tearDown() throws Exception {
-        super.tearDown();
-
-        if (mActivity != null) {
-            mActivity.finish();
-        }
-    }
-
-    protected void popDialog(int index) {
-        assertTrue(index >= 0);
-
-        while (index != 0) {
-            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
-            index--;
-        }
-
-        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
+    private void startDialogActivity(int dialogNumber) {
+        mActivity = DialogStubActivity.startDialogActivity(this, dialogNumber);
     }
 
     @TestTargets({
@@ -139,7 +123,7 @@
             args = {}
         )
     })
-    public void testDialog(){
+    public void testConstructor(){
         new Dialog(mContext);
         Dialog d = new Dialog(mContext, 0);
         // According to javadoc of constructors, it will set theme to system default theme,
@@ -154,16 +138,17 @@
         final Window w = d.getWindow();
         ta = w.getContext().getTheme().obtainStyledAttributes(R.styleable.TextAppearance);
         assertTextAppearanceStyle(ta);
+    }
 
-        // test protected constructor
-        // Dialog(Context context, boolean cancelable, OnCancelListener cancelListener)
+    public void testConstructor_protectedCancellable() {
+        startDialogActivity(DialogStubActivity.TEST_PROTECTED_CANCELABLE);
         mActivity.onCancelListenerCalled = false;
-        popDialog(DialogStubActivity.TEST_PROTECTED_CANCELABLE);
         sendKeys(KeyEvent.KEYCODE_BACK);
         assertTrue(mActivity.onCancelListenerCalled);
+    }
 
-        // open DialogStubActivity.TEST_PROTECTED_NOT_CANCELABLE
-        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_DPAD_CENTER);
+    public void testConstructor_protectedNotCancellable() {
+        startDialogActivity(DialogStubActivity.TEST_PROTECTED_NOT_CANCELABLE);
         mActivity.onCancelListenerCalled = false;
         sendKeys(KeyEvent.KEYCODE_BACK);
         assertFalse(mActivity.onCancelListenerCalled);
@@ -209,7 +194,7 @@
         )
     })
     public void testOnStartCreateStop(){
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
 
         assertTrue(d.isOnStartCalled);
@@ -243,7 +228,7 @@
         )
     })
     public void testAccessOwnerActivity() {
-        popDialog(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
         Dialog d = mActivity.getDialog();
         assertNotNull(d);
         assertSame(mActivity, d.getOwnerActivity());
@@ -284,7 +269,7 @@
         )
     })
     public void testShow() throws Throwable {
-        popDialog(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
         final Dialog d = mActivity.getDialog();
         final View decor = d.getWindow().getDecorView();
 
@@ -324,7 +309,7 @@
         )
     })
     public void testOnSaveInstanceState() {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
 
         assertFalse(d.isOnSaveInstanceStateCalled);
@@ -342,7 +327,7 @@
         args = {}
     )
     public void testGetCurrentFocus() throws Throwable {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
         assertNull(d.getCurrentFocus());
         runTestOnUiThread(new Runnable() {
@@ -386,7 +371,7 @@
         )
     })
     public void testSetContentView() throws Throwable {
-        popDialog(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
         final Dialog d = mActivity.getDialog();
         assertNotNull(d);
 
@@ -472,7 +457,7 @@
     })
     public void testSetTitle() {
         final String expectedTitle = "Test Dialog Without theme";
-        popDialog(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
 
         assertNotNull(mActivity.getDialog());
         mActivity.setUpTitle(expectedTitle);
@@ -500,7 +485,7 @@
         )
     })
     public void testOnKeyDownKeyUp() {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
         assertFalse(d.isOnKeyDownCalled);
         assertFalse(d.isOnKeyUpCalled);
@@ -524,7 +509,7 @@
          args = {int.class, int.class, android.view.KeyEvent.class}
      )
      public void testOnKeyMultiple() {
-         popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+         startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
          final TestDialog d = (TestDialog) mActivity.getDialog();
 
          assertNull(d.keyMultipleEvent);
@@ -553,7 +538,7 @@
         )
     })
     public void testTouchEvent() {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
 
         assertNull(d.onTouchEvent);
@@ -603,7 +588,7 @@
         )
     })
     public void testTrackballEvent() {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
         final MotionEvent trackBallEvent = MotionEvent.obtain(MOTION_DOWN_TIME, MOTION_EVENT_TIME,
                 MotionEvent.ACTION_DOWN, MOTION_X, MOTION_Y, 0);
@@ -636,7 +621,7 @@
         args = {android.view.WindowManager.LayoutParams.class}
     )
     public void testOnWindowAttributesChanged() throws Throwable {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
 
         assertTrue(d.isOnWindowAttributesChangedCalled);
@@ -661,7 +646,7 @@
         args = {}
     )
     public void testOnContentChanged() throws Throwable {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
         assertNotNull(d);
 
@@ -683,7 +668,7 @@
         args = {boolean.class}
     )
     public void testOnWindowFocusChanged() throws Throwable {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
         assertTrue(d.isOnWindowFocusChangedCalled);
         d.isOnWindowFocusChangedCalled = false;
@@ -712,7 +697,7 @@
         )
     })
     public void testDispatchKeyEvent() {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
 
         sendKeys(KeyEvent.KEYCODE_0);
@@ -829,7 +814,7 @@
             + "onContextMenuClosed() should be called in onPanelClosed() source code, "
             + "but now onMenuItemSelected() and onPanelClosed() method are empty, is this a bug?")
     public void testOptionMenu() throws Throwable {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
         assertFalse(d.isOnMenuOpenedCalled);
         assertFalse(d.isOnCreatePanelViewCalled);
@@ -943,7 +928,7 @@
             + "onContextMenuClosed() should be called in onPanelClosed() source code, "
             + "but now onMenuItemSelected() and onPanelClosed() method are empty, is this a bug?")
     public void testContextMenu() throws Throwable {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
         final LinearLayout parent = new LinearLayout(mContext);
         final MockView v = new MockView(mContext);
@@ -1035,7 +1020,7 @@
     @ToBeFixed(bug = "1695243",
             explanation = "It still get KeyEvent while set takeKeyEvents to false")
     public void testTakeKeyEvents() throws Throwable {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
         final View v = d.getWindow().getDecorView();
         assertNull(d.getCurrentFocus());
@@ -1067,7 +1052,7 @@
         args = {int.class}
     )
     public void testRequestWindowFeature() {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         // called requestWindowFeature at TestDialog onCreate method
         assertTrue(((TestDialog) mActivity.getDialog()).isRequestWindowFeature);
     }
@@ -1080,7 +1065,7 @@
         args = {int.class, int.class}
     )
     public void testSetFeatureDrawableResource() throws Throwable {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         runTestOnUiThread(new Runnable() {
             public void run() {
                 mActivity.getDialog().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
@@ -1098,7 +1083,7 @@
         args = {int.class, android.net.Uri.class}
     )
     public void testSetFeatureDrawableUri() {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         mActivity.getDialog().setFeatureDrawableUri(0, Uri.parse("http://www.google.com"));
     }
 
@@ -1110,7 +1095,7 @@
         args = {int.class, android.graphics.drawable.Drawable.class}
     )
     public void testSetFeatureDrawable() {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         mActivity.getDialog().setFeatureDrawable(0, new MockDrawable());
     }
 
@@ -1122,7 +1107,7 @@
         args = {int.class, int.class}
     )
     public void testSetFeatureDrawableAlpha() {
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         mActivity.getDialog().setFeatureDrawableAlpha(0, 0);
     }
 
@@ -1132,7 +1117,7 @@
         args = {}
     )
     public void testGetLayoutInflater() {
-        popDialog(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
         final Dialog d = mActivity.getDialog();
         assertEquals(d.getWindow().getLayoutInflater(), d.getLayoutInflater());
     }
@@ -1142,17 +1127,21 @@
         method = "setCancelable",
         args = {boolean.class}
     )
-    public void testSetCancelable() {
-        popDialog(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+    public void testSetCancelable_true() {
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
         final Dialog d = mActivity.getDialog();
 
         d.setCancelable(true);
         assertTrue(d.isShowing());
         sendKeys(KeyEvent.KEYCODE_BACK);
         assertFalse(d.isShowing());
+    }
+
+    public void testSetCancellable_false() {
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        final Dialog d = mActivity.getDialog();
 
         d.setCancelable(false);
-        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
         assertTrue(d.isShowing());
         sendKeys(KeyEvent.KEYCODE_BACK);
         assertTrue(d.isShowing());
@@ -1175,8 +1164,8 @@
             args = {android.content.DialogInterface.OnCancelListener.class}
         )
     })
-    public void testCancel() throws Throwable {
-        popDialog(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+    public void testCancel_listener() throws Throwable {
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
         final Dialog d = mActivity.getDialog();
 
         assertTrue(d.isShowing());
@@ -1190,8 +1179,12 @@
 
         assertFalse(d.isShowing());
         assertTrue(mOnCancelListenerCalled);
+    }
 
-        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
+    public void testCancel_noListener() throws Throwable {
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        final Dialog d = mActivity.getDialog();
+
         assertTrue(d.isShowing());
         mOnCancelListenerCalled = false;
         d.setOnCancelListener(null);
@@ -1209,7 +1202,7 @@
     )
     public void testSetCancelMessage() throws Exception {
         mCalledCallback = false;
-        popDialog(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
+        startDialogActivity(DialogStubActivity.TEST_ONSTART_AND_ONSTOP);
         final TestDialog d = (TestDialog) mActivity.getDialog();
         final HandlerThread ht = new HandlerThread("DialogTest");
         ht.start();
@@ -1239,9 +1232,9 @@
         method = "setOnDismissListener",
         args = {android.content.DialogInterface.OnDismissListener.class}
     )
-    public void testSetOnDismissListener() throws Throwable {
+    public void testSetOnDismissListener_listener() throws Throwable {
         mCalledCallback = false;
-        popDialog(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
         final Dialog d = mActivity.getDialog();
 
         d.setOnDismissListener(new OnDismissListener() {
@@ -1255,9 +1248,11 @@
         dialogDismiss(d);
         assertTrue(mCalledCallback);
         assertFalse(d.isShowing());
+    }
 
-        // show the dialog again
-        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
+    public void testSetOnDismissListener_noListener() throws Throwable {
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        final Dialog d = mActivity.getDialog();
         assertTrue(d.isShowing());
         mCalledCallback = false;
         d.setOnDismissListener(null);
@@ -1273,7 +1268,7 @@
     )
     public void testSetDismissMessage() throws Throwable {
         mCalledCallback = false;
-        popDialog(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
+        startDialogActivity(DialogStubActivity.TEST_DIALOG_WITHOUT_THEME);
         final Dialog d = mActivity.getDialog();
 
         final HandlerThread ht = new HandlerThread("DialogTest");
diff --git a/tests/tests/app/src/android/app/cts/TimePickerDialogTest.java b/tests/tests/app/src/android/app/cts/TimePickerDialogTest.java
index 01f1ec9..417d046 100644
--- a/tests/tests/app/src/android/app/cts/TimePickerDialogTest.java
+++ b/tests/tests/app/src/android/app/cts/TimePickerDialogTest.java
@@ -26,7 +26,6 @@
 import android.content.Context;
 import android.os.Bundle;
 import android.test.ActivityInstrumentationTestCase2;
-import android.view.KeyEvent;
 import android.widget.TimePicker;
 
 /**
@@ -59,7 +58,6 @@
         super.setUp();
 
         mContext = getInstrumentation().getContext();
-        mActivity = getActivity();
         mOnTimeSetListener = new OnTimeSetListener(){
             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                 mCallbackHour = hourOfDay;
@@ -131,7 +129,7 @@
     )
     public void testOnTimeChanged() throws Throwable {
         final int minute = 34;
-        popDialog(DialogStubActivity.TEST_TIMEPICKERDIALOG);
+        startDialogActivity(DialogStubActivity.TEST_TIMEPICKERDIALOG);
         final TimePickerDialog d = (TimePickerDialog) mActivity.getDialog();
 
         runTestOnUiThread(new Runnable() {
@@ -181,14 +179,7 @@
         assertFalse(b2.getBoolean(IS_24_HOUR));
     }
 
-    private void popDialog(int index) {
-        assertTrue(index > 0);
-
-        while (index != 0) {
-            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
-            index--;
-        }
-
-        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
+    private void startDialogActivity(int dialogNumber) {
+        mActivity = DialogStubActivity.startDialogActivity(this, dialogNumber);
     }
 }