Android Testing 2

Post on 23-Feb-2016

34 views 0 download

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

ANDROID TESTING 2

The objective 0

The objective1

The objective2

The objective3

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

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.

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

UI Test1. sends key events to the UI with key

events. via instrumentation classes

2. confirms that the selection matches the result you expect

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);

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

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.

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

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

Test running result

What if the test fails??

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

What will happen???

Thank you!