1212
1313public class SpinnerActivityTest extends
1414 ActivityInstrumentationTestCase2 <SpinnerActivity > {
15- public static final int TEST_STATE_DESTROY_POSITION = 2 ;
16- public static final String TEST_STATE_DESTROY_SELECTION = "Earth" ;
17-
18- public static final int TEST_STATE_PAUSE_POSITION = 4 ;
19- public static final String TEST_STATE_PAUSE_SELECTION = "Jupiter" ;
2015
2116 private SpinnerActivity mActivity ;
2217 private Spinner mSpinner ;
@@ -43,41 +38,18 @@ protected void setUp() throws Exception {
4338
4439 public void testPreConditions () {
4540 assertTrue (mSpinner .getOnItemSelectedListener () != null );
46- assertTrue (mPlanetData != null );
41+ assertNotNull (mPlanetData );
4742 assertTrue (mPlanetData .getCount () > 0 );
4843 } // end of testPreConditions() method definition
4944
50- public void testSpinnerSelection () {
51- mActivity .runOnUiThread (new Runnable () {
52- public void run () {
53- mSpinner .requestFocus ();
54- mSpinner .setSelection (0 );
55- }
56- });
57- int count = mPlanetData .getCount ();
58- int testposition = count % 2 ;
59- this .sendKeys (KeyEvent .KEYCODE_DPAD_CENTER );
60- for (int i = 1 ; i <= testposition ; i ++) {
61- this .sendKeys (KeyEvent .KEYCODE_DPAD_DOWN );
62- }
63-
64- this .sendKeys (KeyEvent .KEYCODE_DPAD_CENTER );
65-
66- // Ensure that the text is not empty
67- int mPos = mSpinner .getSelectedItemPosition ();
68- String mSelection = (String ) mSpinner .getItemAtPosition (mPos );
69-
70- // Compare selection in the Spinner with the text view
71- TextView resultView = (TextView ) mActivity
72- .findViewById (com .android .example .spinner .R .id .SpinnerResult );
73-
74- String resultText = (String ) resultView .getText ();
75-
76- assertEquals (resultText , mSelection );
77- }
45+ public static final int TEST_STATE_DESTROY_POSITION = 2 ;
46+ public static final String TEST_STATE_DESTROY_SELECTION = "Earth" ;
7847
48+ public static final int TEST_STATE_PAUSE_POSITION = 4 ;
49+ public static final String TEST_STATE_PAUSE_SELECTION = "Jupiter" ;
50+
7951 @ UiThreadTest
80- public void terminationShouldPreserveInstanceState () {
52+ public void testTerminationShouldPreserveInstanceState () {
8153 mActivity .setSpinnerPosition (TEST_STATE_DESTROY_POSITION );
8254 mActivity .setSpinnerSelection (TEST_STATE_DESTROY_SELECTION );
8355
@@ -93,7 +65,7 @@ public void terminationShouldPreserveInstanceState() {
9365 }
9466
9567 @ UiThreadTest
96- public void pauseShouldPreserveInstanceState () {
68+ public void testPauseShouldPreserveInstanceState () {
9769 Instrumentation mInstr = this .getInstrumentation ();
9870 mActivity .setSpinnerPosition (TEST_STATE_PAUSE_POSITION );
9971 mActivity .setSpinnerSelection (TEST_STATE_PAUSE_SELECTION );
@@ -111,4 +83,32 @@ public void pauseShouldPreserveInstanceState() {
11183 assertEquals (TEST_STATE_PAUSE_SELECTION , currentSelection );
11284 }
11385
86+ public void testSpinnerSelection () {
87+ mActivity .runOnUiThread (new Runnable () {
88+ public void run () {
89+ mSpinner .requestFocus ();
90+ mSpinner .setSelection (0 );
91+ }
92+ });
93+ int count = mPlanetData .getCount ();
94+ int testposition = count % 2 ;
95+ this .sendKeys (KeyEvent .KEYCODE_DPAD_CENTER );
96+ for (int i = 1 ; i <= testposition ; i ++) {
97+ this .sendKeys (KeyEvent .KEYCODE_DPAD_DOWN );
98+ }
99+
100+ this .sendKeys (KeyEvent .KEYCODE_DPAD_CENTER );
101+
102+ // Ensure that the text is not empty
103+ int mPos = mSpinner .getSelectedItemPosition ();
104+ String mSelection = (String ) mSpinner .getItemAtPosition (mPos );
105+
106+ // Compare selection in the Spinner with the text view
107+ TextView resultView = (TextView ) mActivity
108+ .findViewById (com .android .example .spinner .R .id .SpinnerResult );
109+
110+ String resultText = (String ) resultView .getText ();
111+
112+ assertEquals (resultText , mSelection );
113+ }
114114}
0 commit comments