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 /** 24 * This is a simple framework for a test of an Application. See 25 * {@link android.test.ApplicationTestCase ApplicationTestCase} for more information on 26 * how to write and extend Application tests. 27 * 28 * <p>To run this test, you can type: 29 * adb shell am instrument -w \ 30 * -e class com.example.android.activityinstrumentation.MainActivityTest \ 31 * com.example.android.activityinstrumentation.tests/android.test.InstrumentationTestRunner 32 * 33 * <p>Individual tests are defined as any method beginning with 'test'. 34 * 35 * <p>ActivityInstrumentationTestCase2 allows these tests to run alongside a running 36 * copy of the application under inspection. Calling getActivity() will return a 37 * handle to this activity (launching it if needed). 38 */ 39 public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> { 40 41 public MainActivityTest() { 42 super("com.example.android.activityinstrumentation", MainActivity.class); 43 } 44 45 /** 46 * Test to make sure that spinner values are persisted across activity restarts. 47 * 48 * <p>Launches the main activity, sets a spinner value, closes the activity, then relaunches 49 * that activity. Checks to make sure that the spinner values match what we set them to. 50 */ 51 // BEGIN_INCLUDE (test_name) 52 public void testSpinnerValuePersistedBetweenLaunches() { 53 // END_INCLUDE (test_name) 54 final int TEST_SPINNER_POSITION_1 = MainActivity.WEATHER_PARTLY_CLOUDY; 55 56 // BEGIN_INCLUDE (launch_activity) 57 // Launch the activity 58 Activity activity = getActivity(); 59 // END_INCLUDE (launch_activity) 60 61 // BEGIN_INCLUDE (write_to_ui) 62 // Set spinner to test position 1 63 final Spinner spinner1 = (Spinner) activity.findViewById(R.id.spinner); 64 activity.runOnUiThread(new Runnable() { 65 @Override 66 public void run() { 67 // Attempts to manipulate the UI must be performed on a UI thread. 68 // Calling this outside runOnUiThread() will cause an exception. 69 // 70 // You could also use @UiThreadTest, but activity lifecycle methods 71 // cannot be called if this annotation is used. 72 spinner1.requestFocus(); 73 spinner1.setSelection(TEST_SPINNER_POSITION_1); 74 } 75 }); 76 // END_INCLUDE (write_to_ui) 77 78 // BEGIN_INCLUDE (relaunch_activity) 79 // Close the activity 80 activity.finish(); 81 setActivity(null); // Required to force creation of a new activity 82 83 // Relaunch the activity 84 activity = this.getActivity(); 85 // END_INCLUDE (relaunch_activity) 86 87 // BEGIN_INCLUDE (check_results) 88 // Verify that the spinner was saved at position 1 89 final Spinner spinner2 = (Spinner) activity.findViewById(R.id.spinner); 90 int currentPosition = spinner2.getSelectedItemPosition(); 91 assertEquals(TEST_SPINNER_POSITION_1, currentPosition); 92 // END_INCLUDE (check_results) 93 94 // Since this is a stateful test, we need to make sure that the activity isn't simply 95 // echoing a previously-stored value that (coincidently) matches position 1 96 final int TEST_SPINNER_POSITION_2 = MainActivity.WEATHER_SNOW; 97 98 // Set spinner to test position 2 99 activity.runOnUiThread(new Runnable() { 100 @Override 101 public void run() { 102 spinner2.requestFocus(); 103 spinner2.setSelection(TEST_SPINNER_POSITION_2); 104 } 105 }); 106 107 // Close the activity 108 activity.finish(); 109 setActivity(null); 110 111 // Relaunch the activity 112 activity = this.getActivity(); 113 114 // Verify that the spinner was saved at position 2 115 final Spinner spinner3 = (Spinner) activity.findViewById(R.id.spinner); 116 currentPosition = spinner3.getSelectedItemPosition(); 117 assertEquals(TEST_SPINNER_POSITION_2, currentPosition); 118 } 119 } 120