Android Testing 2

18
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 Presentation

Transcript of Android Testing 2

Page 1: Android Testing 2

ANDROID TESTING 2

Page 2: Android Testing 2

The objective 0

Page 3: Android Testing 2

The objective1

Page 4: Android Testing 2

The objective2

Page 5: Android Testing 2

The objective3

Page 6: Android Testing 2

What to Test1. Initial conditions 2. UI3. State management

Page 7: Android Testing 2

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

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

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

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

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

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

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

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

Test running result

Page 16: Android Testing 2

What if the test fails??

At the end of onCreate in SpinnerActivity.java:spinner.setOnItemSelectedListener(null);

What will happen???

Page 17: Android Testing 2
Page 18: Android Testing 2

Thank you!