Instrumentation使用例子
- /*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package come.example.android.notepad.test;
- import android.test.ActivityInstrumentationTestCase2;
- import com.example.android.notepad.NotesList;
- import com.example.android.notepad.NoteEditor;
- import com.example.android.notepad.NotesList;
- import com.example.android.notepad.R;
- import android.app.Activity;
- import android.app.Instrumentation;
- import android.app.Instrumentation.ActivityMonitor;
- import android.content.Intent;
- import android.os.SystemClock;
- import android.test.InstrumentationTestCase;
- import android.view.KeyEvent;
- import android.widget.TextView;
- /**
- * Make sure that the main launcher activity opens up properly, which will be
- * verified by {@link #testActivityTestCaseSetUpProperly}.
- */
- public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList> {
- NotesList mActivity = null;
- /**
- * Creates an {@link ActivityInstrumentationTestCase2} for the {@link NotesList} activity.
- */
- public NotePadTest() {
- super(NotesList.class);
- }
- //private static Instrumentation instrumentation = new Instrumentation();
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- //Start the NotesList activity by instrument
- Intent intent = new Intent();
- intent.setClassName("com.example.android.notepad", NotesList.class.getName());
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- Instrumentation inst = getInstrumentation();
- mActivity = (NotesList) inst.startActivitySync(intent);
- }
- @Override
- protected void tearDown() {
- mActivity.finish();
- try {
- super.tearDown();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Verifies that the activity under test can be launched.
- */
- /*
- public void testActivityTestCaseSetUpProperly() {
- assertNotNull("activity should be launched successfully", getActivity());
- }
- */
- public void testActivity() throws Exception {
- //Add activity monitor to check whether the NoteEditor activity's ready
- ActivityMonitor am = getInstrumentation().addMonitor(NoteEditor.class.getName(), null, false);
- //Evoke the system menu and press on the menu entry "Add note";
- getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
- getInstrumentation().invokeMenuActionSync(mActivity, R.id.menu_add, 0);
- //Direct to the NoteEditor activity
- Activity noteEditorActivity = getInstrumentation().waitForMonitorWithTimeout(am, 60000);
- assertEquals(NoteEditor.class,noteEditorActivity.getClass());
- SystemClock.sleep(3000);
- //assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));
- TextView noteEditor = (TextView) noteEditorActivity.findViewById(R.id.note);
- //Get the text directly, DON'T need to runOnMainSync at all!!!
- String text = noteEditor.getText().toString();
- assertEquals(text,"");
- //runOnMainSync to change the text
- getInstrumentation().runOnMainSync(new PerformSetText(noteEditor,"Note1"));
- //inject events to change the text
- getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_1);
- getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_2);
- getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_P);
- getInstrumentation().sendStringSync("gotohell");
- //getInstrumentation().callActivityOnPause(noteEditorActivity);
- Thread.sleep(5000);
- //getInstrumentation().callActivityOnResume(noteEditorActivity);
- //Save the new created note
- getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
- getInstrumentation().invokeMenuActionSync(noteEditorActivity, R.id.menu_save, 0);
- }
- private class PerformSetText implements Runnable {
- TextView tv;
- String txt;
- public PerformSetText(TextView t,String text) {
- tv = t;
- txt = text;
- }
- public void run() {
- tv.setText(txt);
- }
- }
- }