1 / 18

Android Testing 2

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.

oriana
Download Presentation

Android Testing 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. */

  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 (inti = 1; i <= TEST_POSITION; i++) { this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); } // end of for loop this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);

  11. Code for UI Test(2) mPos = mSpinner.getSelectedItemPosition(); mSelection = (String) mSpinner.getItemAtPosition(mPos); TextViewresultView = (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 intcurrentPosition = mActivity.getSpinnerPosition(); String currentSelection = mActivity.getSpinnerSelection(); assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition); assertEquals(TEST_STATE_DESTROY_SELECTION, currentSelection); } // end of testStateDestroy() method definition

  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: intcurrentPosition = 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???

  17. Thank you!

More Related