Instrumentation.ActivityMonitor及例子
来源:互联网 发布:怎么提高淘宝信誉评级 编辑:程序博客网 时间:2024/06/06 13:03
Instrumentation.ActivityMonitor
extends Object类概述
Information about a particular kind of Intent that is being monitored. An instance of this class is added to the current instrumentation throughaddMonitor(Instrumentation.ActivityMonitor)
; after being added, when a new activity is being started the monitor will be checked and, if matching, its hit count updated and (optionally) the call stopped and a canned result returned.
An ActivityMonitor can also be used to look for the creation of an activity, through the waitForActivity()
method. This will return after a matching activity has been created with that activity object.
摘要
公有构造函数
public Instrumentation.ActivityMonitor (IntentFilter which, Instrumentation.ActivityResult result, boolean block)
Create a new ActivityMonitor that looks for a particular kind of intent to be started.
参数
参见
addMonitor(Instrumentation.ActivityMonitor)
public Instrumentation.ActivityMonitor (String cls, Instrumentation.ActivityResult result, boolean block)
Create a new ActivityMonitor that looks for a specific activity class to be started.
参数
参见
addMonitor(Instrumentation.ActivityMonitor)
公有方法
public final IntentFilter getFilter ()
Retrieve the filter associated with this ActivityMonitor.
public final int getHits ()
Retrieve the number of times the monitor has been hit so far.
public final Activity getLastActivity ()
Retrieve the most recent activity class that was seen by this monitor.
public final Instrumentation.ActivityResult getResult ()
Retrieve the result associated with this ActivityMonitor, or null if none.
public final boolean isBlocking ()
Check whether this monitor blocks activity starts (not allowing the actual activity to run) or allows them to execute normally.
public final Activity waitForActivity ()
Block until an Activity is created that matches this monitor, returning the resulting activity.
返回值
- Activity
public final Activity waitForActivityWithTimeout (long timeOut)
Block until an Activity is created that matches this monitor, returning the resulting activity or till the timeOut period expires. If the timeOut expires before the activity is started, return null.
参数
返回值
- Activity
Instrumentation.ActivityMonitor嵌套类
仪表盘对象是用来监视整个应用或者所有待测活动(Activities)与Android系统交互的所有过程,而ActivityMonitor嵌套类则是用来监视应用中单个活动的,它可以用来监视一些指定的意图。创建好ActivityMonitor的实例后,通过调用Instrumentation.addMonitor函数来添加这个实例。当活动启动后,系统会匹配Instrumentation中的ActivityMonitory实例列表,如果匹配,就会累加计数器。
本书的示例工程“chapter3/cn.hzbook.android.test.chapter3.activitymonitor”和它的测试工程“chapter3/cn.hzbook.android.test.chapter3.activitymonitory.test”就演示了ActivityMonitor的用法。应用“activitymonitor”中就只有一个超链接,单击它会打开谷歌的首页,如代码清单3-4所示。
代码清单3-4 Instrumentation.ActivityMonitor使用示例
- 1. public void test单击链接() {
- 2. final Instrumentation inst = getInstrumentation();
- 3. IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW);
- 4. intentFilter.addDataScheme("http");
- 5. intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);
- 6. View link = this.getActivity().findViewById(R.id.link);
- 7. ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false);
- 8. try {
- 9. assertEquals(0, monitor.getHits());
- 10. TouchUtils.clickView(this, link);
- 11. monitor.waitForActivityWithTimeout(5000);
- 12. assertEquals(1, monitor.getHits());
- 13. } finally {
- 14. inst.removeMonitor(monitor);
- 15. }
- 16. }
在代码清单3-4里,测试用例的第2行首先获取当前待测应用的仪表盘对象。接着在第3~5行创建了一个意图过滤器(Intent Filter),指明测试用例中感兴趣的意图,即要监听的意图。并且在第7行将我们的监听器添加到仪表盘对象的监听列表中。由于没有人单击待测应用上的超链接,因此在测试代码第9行验证监听器不应该监听到任何对打开浏览器的请求。但是在第10行,测试代码显式单击了超链接,打开浏览器访问谷歌的首页,第12行的验证代码断言监听程序应该监听到一次网页浏览的请求。由于启动浏览器打开网页是一个相对较长的过程,因此在第11行告诉监听程序等待满足要求的活动创建成功,最多等待5秒钟。第14行执行清理操作,注意这里的清理操作是放在finally块中执行的,而代码清单3-4的测试步骤都被包含在try... finally块中,这样做的目的是即使测试过程当中有任何错误,也可以执行清理操作,避免影响后续的测试用例。
- Instrumentation.ActivityMonitor及例子
- Instrumentation使用例子
- <instrumentation>
- Instrumentation
- Instrumentation
- instrumentation
- Instrumentation
- instrumentation
- Instrumentation
- ActivityMonitor 类的功能
- android基础知识12:android自动化测试06—Instrumentation 01 例子
- Instrumentation研究
- Instrumentation Testing
- Android Instrumentation
- Instrumentation Testing
- Instrumentation Testing
- android Instrumentation
- Instrumentation.java
- 在c中,指针数组和数组指针的表示和理解
- Android_BUG_ViewPager+Fragment(Fragment中嵌套Viewpager,Viewpager中有嵌套Fragement的场景)爆出的BUG
- memcopy()函数c语言实现和汇编实现比较
- SharePoint JS Link 之( 三)如何设置JSLink的引用
- NGUI中利用UIGrid实现下拉列表
- Instrumentation.ActivityMonitor及例子
- QGroupBox应用,extension widget create!
- Android_图片处理_高效Bitmap
- 特别困的学生(Extraordinarily Tired Students)
- 用Meta标签代码让360双核浏览器默认极速模式打开网站不是兼容模式(顺带解决很多兼容性问题)
- select跳转的三种方式
- (笔记)委托的几种用法
- DisplayImageOptions对象的使用和".9"图片的冲突问题
- gcc -D选项的作用