Download presentation
Presentation is loading. Please wait.
1
Android Testing 2
2
The objective 0
3
The objective1
4
The objective2
5
The objective3
6
What to Test 1. Initial conditions 2. UI 3. State management
7
initial conditions test
Verifies that the application under test is initialized correctly: 1. The item select listener is initialized. 2. The adapter that provides values to the spinner is initialized. 3. The adapter contains the right number of entries.
8
Code for initial test public void testPreConditions() {
assertTrue(mSpinner.getOnItemSelectedListener() != null); assertTrue(mPlanetData != null); assertEquals(mPlanetData.getCount(), ADAPTER_COUNT); //ADAPTER_COUNT等于9 } // end of testPreConditions() method definition /* mActivity = getActivity(); mSpinner = (Spinner) mActivity.findViewById(com.android.example.spinner.R.id.Spinner01); mPlanetData = mSpinner.getAdapter(); //mPlanetData is the Adapter. */ 什么是Adapter
9
UI Test 1. sends key events to the UI with key events. via instrumentation classes 2. confirms that the selection matches the result you expect
10
Code for UI Test(1) public void testSpinnerUI() {
mActivity.runOnUiThread( new Runnable() { public void run() { mSpinner.requestFocus(); mSpinner.setSelection(INITIAL_POSITION); } // end of run() method definition } // end of anonymous Runnable object instantiation ); // end of invocation of runOnUiThread this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER); for (int i = 1; i <= TEST_POSITION; i++) { this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); } // end of for loop Class Runnable represents a command that can be executed, contains only one method run();
11
Code for UI Test(2) } // end of testSpinnerUI() method definition
mPos = mSpinner.getSelectedItemPosition(); mSelection = (String) mSpinner.getItemAtPosition(mPos); TextView resultView = (TextView) mActivity.findViewById( com.android.example.spinner.R.id.SpinnerResult); String resultText = (String) resultView.getText(); assertEquals(resultText, mSelection); } // end of testSpinnerUI() method definition
12
State Management Test Two tests verify that SpinnerActivity maintains its state when it is paused or terminated. 1. When an activity is hidden(not quit), it is paused--- onPause() 2. When it re-appears, it resumes---onResume() 3. SpinnerActivity uses them for saving and restoring state.
13
Code for State Management Test1
// verifies that the spinner selection is maintained after the entire application //is shut down and then restarted. public void testStateDestroy() { mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION); //2 mActivity.setSpinnerSelection(TEST_STATE_DESTROY_SELECTION); // "earth" mActivity.finish(); //terminates the app mActivity = this.getActivity(); //start the app int currentPosition = mActivity.getSpinnerPosition(); String currentSelection = mActivity.getSpinnerSelection(); assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition); assertEquals(TEST_STATE_DESTROY_SELECTION, currentSelection); } // end of testStateDestroy() method definition Difference between Position and Selection
14
Code for State Management Test2
//verifies that the spinner selection is maintained after the activity is paused and then //resumed. public void testStatePause() { Instrumentation mInstr = this.getInstrumentation(); mActivity.setSpinnerPosition(TEST_STATE_PAUSE_POSITION); //4 mActivity.setSpinnerSelection(TEST_STATE_PAUSE_SELECTION);// "Jupiter" mInstr.callActivityOnPause(mActivity); //Force the spinner to a different selection: mActivity.setSpinnerPosition(0); mActivity.setSpinnerSelection(""); //Use instrumentation to call the Activity's onResume(): mInstr.callActivityOnResume(mActivity); //Get the current state of the spinner: int currentPosition = mActivity.getSpinnerPosition(); String currentSelection = mActivity.getSpinnerSelection(); assertEquals(TEST_STATE_PAUSE_POSITION,currentPosition); assertEquals(TEST_STATE_PAUSE_SELECTION,currentSelection); } // end of testStatePause() method definition
15
Test running result
16
What if the test fails?? At the end of onCreate in SpinnerActivity.java: spinner.setOnItemSelectedListener(null); What will happen???
18
Thank you!
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.