Instrumentation使用例子

Instrumentation使用例子

[java]  view plain copy
  1. /* 
  2.  * Copyright (C) 2008 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package come.example.android.notepad.test;  
  18.   
  19. import android.test.ActivityInstrumentationTestCase2;  
  20.   
  21. import com.example.android.notepad.NotesList;  
  22. import com.example.android.notepad.NoteEditor;  
  23. import com.example.android.notepad.NotesList;  
  24. import com.example.android.notepad.R;  
  25.   
  26. import android.app.Activity;  
  27. import android.app.Instrumentation;  
  28. import android.app.Instrumentation.ActivityMonitor;  
  29. import android.content.Intent;  
  30. import android.os.SystemClock;  
  31. import android.test.InstrumentationTestCase;  
  32. import android.view.KeyEvent;  
  33. import android.widget.TextView;  
  34.   
  35.   
  36. /** 
  37.  * Make sure that the main launcher activity opens up properly, which will be 
  38.  * verified by {@link #testActivityTestCaseSetUpProperly}. 
  39.  */  
  40. public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList> {  
  41.   
  42.     NotesList mActivity = null;  
  43.       
  44.     /** 
  45.      * Creates an {@link ActivityInstrumentationTestCase2} for the {@link NotesList} activity. 
  46.      */  
  47.     public NotePadTest() {  
  48.         super(NotesList.class);  
  49.     }  
  50.     //private static Instrumentation instrumentation = new Instrumentation();  
  51.       
  52.     @Override   
  53.     protected void setUp() throws Exception {  
  54.         super.setUp();  
  55.           
  56.         //Start the NotesList activity by instrument  
  57.         Intent intent = new Intent();  
  58.         intent.setClassName("com.example.android.notepad", NotesList.class.getName());  
  59.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  60.         Instrumentation inst = getInstrumentation();  
  61.         mActivity = (NotesList) inst.startActivitySync(intent);  
  62.   
  63.     }  
  64.       
  65.      @Override  
  66.     protected void tearDown()  {  
  67.         mActivity.finish();  
  68.         try {  
  69.             super.tearDown();  
  70.         } catch (Exception e) {  
  71.             e.printStackTrace();  
  72.         }  
  73.      }  
  74.        
  75.        
  76.   
  77.     /** 
  78.      * Verifies that the activity under test can be launched. 
  79.      */  
  80.      /* 
  81.     public void testActivityTestCaseSetUpProperly() { 
  82.         assertNotNull("activity should be launched successfully", getActivity()); 
  83.     } 
  84.     */  
  85.   
  86.      public void testActivity() throws Exception {  
  87.               
  88.         //Add activity monitor to check whether the NoteEditor activity's ready  
  89.         ActivityMonitor am = getInstrumentation().addMonitor(NoteEditor.class.getName(), nullfalse);  
  90.           
  91.         //Evoke the system menu and press on the menu entry "Add note";  
  92.         getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);  
  93.         getInstrumentation().invokeMenuActionSync(mActivity, R.id.menu_add, 0);  
  94.           
  95.         //Direct to the NoteEditor activity  
  96.         Activity noteEditorActivity = getInstrumentation().waitForMonitorWithTimeout(am, 60000);  
  97.         assertEquals(NoteEditor.class,noteEditorActivity.getClass());  
  98.         SystemClock.sleep(3000);  
  99.         //assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));  
  100.   
  101.         TextView noteEditor = (TextView) noteEditorActivity.findViewById(R.id.note);  
  102.     
  103.         //Get the text directly, DON'T need to runOnMainSync at all!!!  
  104.         String text = noteEditor.getText().toString();  
  105.         assertEquals(text,"");  
  106.           
  107.         //runOnMainSync to change the text  
  108.         getInstrumentation().runOnMainSync(new PerformSetText(noteEditor,"Note1"));  
  109.           
  110.         //inject events to change the text  
  111.         getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_1);  
  112.         getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_2);  
  113.         getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_P);  
  114.         getInstrumentation().sendStringSync("gotohell");  
  115.         //getInstrumentation().callActivityOnPause(noteEditorActivity);  
  116.         Thread.sleep(5000);  
  117.         //getInstrumentation().callActivityOnResume(noteEditorActivity);  
  118.           
  119.         //Save the new created note  
  120.         getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);  
  121.         getInstrumentation().invokeMenuActionSync(noteEditorActivity, R.id.menu_save, 0);  
  122.   
  123.      }  
  124.        
  125.      private class PerformSetText implements Runnable {  
  126.         TextView tv;  
  127.         String txt;  
  128.         public PerformSetText(TextView t,String text) {  
  129.             tv = t;  
  130.             txt = text;  
  131.         }  
  132.     
  133.         public void run() {  
  134.             tv.setText(txt);  
  135.         }  
  136.     }  
  137. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值