android testing 2
DESCRIPTION
Android Testing 2. The objective 0. The objective1. The objective2. The objective3. What to Test. 1. Initial conditions 2. UI 3. State management. initial conditions test. Verifies that the application under test is initialized correctly: 1. The item select listener is initialized. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/1.jpg)
ANDROID TESTING 2
![Page 2: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/2.jpg)
The objective 0
![Page 3: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/3.jpg)
The objective1
![Page 4: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/4.jpg)
The objective2
![Page 5: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/5.jpg)
The objective3
![Page 6: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/6.jpg)
What to Test1. Initial conditions 2. UI3. State management
![Page 7: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/7.jpg)
initial conditions testVerifies 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.
![Page 8: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/8.jpg)
Code for initial testpublic 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.*/
![Page 9: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/9.jpg)
UI Test1. sends key events to the UI with key
events. via instrumentation classes
2. confirms that the selection matches the result you expect
![Page 10: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/10.jpg)
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 loopthis.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
![Page 11: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/11.jpg)
Code for UI Test(2)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
![Page 12: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/12.jpg)
State Management TestTwo 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.
![Page 13: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/13.jpg)
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); //2mActivity.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
![Page 14: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/14.jpg)
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); //4mActivity.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
![Page 15: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/15.jpg)
Test running result
![Page 16: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/16.jpg)
What if the test fails??
At the end of onCreate in SpinnerActivity.java:spinner.setOnItemSelectedListener(null);
What will happen???
![Page 17: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/17.jpg)
![Page 18: Android Testing 2](https://reader035.vdocuments.site/reader035/viewer/2022081520/56816420550346895dd5dfdf/html5/thumbnails/18.jpg)
Thank you!