1 package com.android.dialer.dialpad;
2 
3 import android.test.ActivityInstrumentationTestCase2;
4 import android.view.View;
5 
6 import com.android.dialer.DialtactsActivity;
7 import com.android.dialer.R;
8 
9 /**
10  * Tests that rely on instrumenting an actual instance of a {@link DialpadFragment}.
11  */
12 public class DialpadFragmentInstrumentationTest extends
13         ActivityInstrumentationTestCase2<DialtactsActivity> {
14     private DialtactsActivity mActivity;
15 
DialpadFragmentInstrumentationTest()16     public DialpadFragmentInstrumentationTest() {
17         super(DialtactsActivity.class);
18     }
19 
20     @Override
setUp()21     protected void setUp() throws Exception {
22         super.setUp();
23         mActivity = getActivity();
24     }
25 
26     /**
27      * Simulates a long click event on the zero key with a prior onPressed callback.
28      *
29      */
testManualLongClickZero_DeletesPreviousCharacter()30     public void testManualLongClickZero_DeletesPreviousCharacter() {
31         final DialpadFragment fragment = showDialpad();
32         pressAndReleaseKey(9, fragment);
33 
34         getInstrumentation().runOnMainSync(new Runnable() {
35             @Override
36             public void run() {
37                 final View zeroKey = findViewByDigit(0, fragment);
38                 fragment.onPressed(zeroKey, true);
39                 fragment.onLongClick(zeroKey);
40             }
41         });
42 
43         assertEquals("9+", fragment.getDigitsWidget().getText().toString());
44     }
45 
46     /**
47      * Simulates a long click event on the zero key without a prior onPressed
48      * callback.
49      */
testSystemLongClickZero_PreservesPreviousCharacter()50     public void testSystemLongClickZero_PreservesPreviousCharacter() {
51         final DialpadFragment fragment = showDialpad();
52         pressAndReleaseKey(9, fragment);
53 
54         getInstrumentation().runOnMainSync(new Runnable() {
55             @Override
56             public void run() {
57                 final View zeroKey = findViewByDigit(0, fragment);
58                 fragment.onLongClick(zeroKey);
59             }
60         });
61 
62         assertEquals("9+", fragment.getDigitsWidget().getText().toString());
63     }
64 
showDialpad()65     private DialpadFragment showDialpad() {
66         getInstrumentation().runOnMainSync(new Runnable() {
67             @Override
68             public void run() {
69                 mActivity.showDialpad();
70             }
71         });
72         getInstrumentation().waitForIdleSync();
73         return (DialpadFragment) mActivity.getFragmentManager().findFragmentByTag(
74                 DialtactsActivity.TAG_DIALPAD_FRAGMENT);
75     }
76 
pressAndReleaseKey(int digit, final DialpadFragment fragment)77     private void pressAndReleaseKey(int digit, final DialpadFragment fragment) {
78         final View dialpadKey = findViewByDigit(digit, fragment);
79         final String digitsBefore = fragment.getDigitsWidget().getText().toString();
80         getInstrumentation().runOnMainSync(new Runnable() {
81             @Override
82             public void run() {
83                 fragment.onPressed(dialpadKey, true);
84                 fragment.onPressed(dialpadKey, false);
85             }
86         });
87         assertEquals(digitsBefore + String.valueOf(digit),
88                 fragment.getDigitsWidget().getText().toString());
89     }
90 
findViewByDigit(int digit, DialpadFragment fragment)91     private View findViewByDigit(int digit, DialpadFragment fragment) {
92         return fragment.getView().findViewById(getViewIdByDigit(digit));
93     }
94 
getViewIdByDigit(int digit)95     private int getViewIdByDigit(int digit) {
96         switch (digit) {
97             case 0:
98                 return R.id.zero;
99             case 1:
100                 return R.id.one;
101             case 2:
102                 return R.id.two;
103             case 3:
104                 return R.id.three;
105             case 4:
106                 return R.id.four;
107             case 5:
108                 return R.id.five;
109             case 6:
110                 return R.id.six;
111             case 7:
112                 return R.id.seven;
113             case 8:
114                 return R.id.eight;
115             case 9:
116                 return R.id.nine;
117             default:
118                 return 0;
119         }
120     }
121 }
122