1 /* 2 * Copyright 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.activityinstrumentation; 18 19 import android.app.Activity; 20 import android.test.ActivityInstrumentationTestCase2; 21 import android.widget.Spinner; 22 23 import com.example.android.activityinstrumentation.MainActivity; 24 import com.example.android.activityinstrumentation.R; 25 26 /** 27 * This is a simple framework for a test of an Application. See 28 * {@link android.test.ApplicationTestCase ApplicationTestCase} for more information on 29 * how to write and extend Application tests. 30 * 31 * <p>To run this test, you can type: 32 * adb shell am instrument -w \ 33 * -e class com.example.android.activityinstrumentation.MainActivityTest \ 34 * quux.tests/android.test.InstrumentationTestRunner 35 * 36 * <p>Individual tests are defined as any method beginning with 'test'. 37 * 38 * <p>ActivityInstrumentationTestCase2 allows these tests to run alongside a running 39 * copy of the application under inspection. Calling getActivity() will return a 40 * handle to this activity (launching it if needed). 41 */ 42 public class SampleTests extends ActivityInstrumentationTestCase2<MainActivity> { 43 44 public SampleTests() { 45 super("com.example.android.activityinstrumentation", MainActivity.class); 46 } 47 48 /** 49 * Test to make sure that spinner values are persisted across activity restarts. 50 * 51 * <p>Launches the main activity, sets a spinner value, closes the activity, then relaunches 52 * that activity. Checks to make sure that the spinner values match what we set them to. 53 */ 54 // BEGIN_INCLUDE (test_name) 55 public void testSpinnerValuePersistedBetweenLaunches() { 56 // END_INCLUDE (test_name) 57 final int TEST_SPINNER_POSITION_1 = MainActivity.WEATHER_PARTLY_CLOUDY; 58 59 // BEGIN_INCLUDE (launch_activity) 60 // Launch the activity 61 Activity activity = getActivity(); 62 // END_INCLUDE (launch_activity) 63 64 // BEGIN_INCLUDE (write_to_ui) 65 // Set spinner to test position 1 66 final Spinner spinner1 = (Spinner) activity.findViewById(R.id.spinner); 67 activity.runOnUiThread(new Runnable() { 68 @Override 69 public void run() { 70 // Attempts to manipulate the UI must be performed on a UI thread. 71 // Calling this outside runOnUiThread() will cause an exception. 72 // 73 // You could also use @UiThreadTest, but activity lifecycle methods 74 // cannot be called if this annotation is used. 75 spinner1.requestFocus(); 76 spinner1.setSelection(TEST_SPINNER_POSITION_1); 77 } 78 }); 79 // END_INCLUDE (write_to_ui) 80 81 // BEGIN_INCLUDE (relaunch_activity) 82 // Close the activity 83 activity.finish(); 84 setActivity(null); // Required to force creation of a new activity 85 86 // Relaunch the activity 87 activity = this.getActivity(); 88 // END_INCLUDE (relaunch_activity) 89 90 // BEGIN_INCLUDE (check_results) 91 // Verify that the spinner was saved at position 1 92 final Spinner spinner2 = (Spinner) activity.findViewById(R.id.spinner); 93 int currentPosition = spinner2.getSelectedItemPosition(); 94 assertEquals(TEST_SPINNER_POSITION_1, currentPosition); 95 // END_INCLUDE (check_results) 96 97 // Since this is a stateful test, we need to make sure that the activity isn't simply 98 // echoing a previously-stored value that (coincidently) matches position 1 99 final int TEST_SPINNER_POSITION_2 = MainActivity.WEATHER_SNOW; 100 101 // Set spinner to test position 2 102 activity.runOnUiThread(new Runnable() { 103 @Override 104 public void run() { 105 spinner2.requestFocus(); 106 spinner2.setSelection(TEST_SPINNER_POSITION_2); 107 } 108 }); 109 110 // Close the activity 111 activity.finish(); 112 setActivity(null); 113 114 // Relaunch the activity 115 activity = this.getActivity(); 116 117 // Verify that the spinner was saved at position 2 118 final Spinner spinner3 = (Spinner) activity.findViewById(R.id.spinner); 119 currentPosition = spinner3.getSelectedItemPosition(); 120 assertEquals(TEST_SPINNER_POSITION_2, currentPosition); 121 } 122 } 123