Instrumentation.ActivityMonitor及例子

来源:互联网 发布:怎么提高淘宝信誉评级 编辑:程序博客网 时间:2024/06/06 13:03
public static class

Instrumentation.ActivityMonitor

extends Object
java.lang.Object   ↳android.app.Instrumentation.ActivityMonitor

类概述

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.

摘要

公有构造函数Instrumentation.ActivityMonitor(IntentFilter which, Instrumentation.ActivityResult result, boolean block)
Create a new ActivityMonitor that looks for a particular kind of intent to be started.
Instrumentation.ActivityMonitor(String cls, Instrumentation.ActivityResult result, boolean block)
Create a new ActivityMonitor that looks for a specific activity class to be started.
公有方法final IntentFiltergetFilter()
Retrieve the filter associated with this ActivityMonitor.
final intgetHits()
Retrieve the number of times the monitor has been hit so far.
final ActivitygetLastActivity()
Retrieve the most recent activity class that was seen by this monitor.
final Instrumentation.ActivityResultgetResult()
Retrieve the result associated with this ActivityMonitor, or null if none.
final booleanisBlocking()
Check whether this monitor blocks activity starts (not allowing the actual activity to run) or allows them to execute normally.
final ActivitywaitForActivity()
Block until an Activity is created that matches this monitor, returning the resulting activity.
final ActivitywaitForActivityWithTimeout(long timeOut)
Block until an Activity is created that matches this monitor, returning the resulting activity or till the timeOut period expires.
[展开]
继承的方法
 来自 class java.lang.Object

公有构造函数

public Instrumentation.ActivityMonitor (IntentFilter which, Instrumentation.ActivityResult result, boolean block)

引入自:API 级别1

Create a new ActivityMonitor that looks for a particular kind of intent to be started.

参数
whichThe set of intents this monitor is responsible for.resultA canned result to return if the monitor is hit; can be null.blockControls whether the monitor should block the activity start (returning its canned result) or let the call proceed.
参见
  • addMonitor(Instrumentation.ActivityMonitor)

public Instrumentation.ActivityMonitor (String cls, Instrumentation.ActivityResult result, boolean block)

引入自:API 级别1

Create a new ActivityMonitor that looks for a specific activity class to be started.

参数
clsThe activity class this monitor is responsible for.resultA canned result to return if the monitor is hit; can be null.blockControls whether the monitor should block the activity start (returning its canned result) or let the call proceed.
参见
  • addMonitor(Instrumentation.ActivityMonitor)

公有方法

public final IntentFilter getFilter ()

引入自:API 级别1

Retrieve the filter associated with this ActivityMonitor.

public final int getHits ()

引入自:API 级别1

Retrieve the number of times the monitor has been hit so far.

public final Activity getLastActivity ()

引入自:API 级别1

Retrieve the most recent activity class that was seen by this monitor.

public final Instrumentation.ActivityResult getResult ()

引入自:API 级别1

Retrieve the result associated with this ActivityMonitor, or null if none.

public final boolean isBlocking ()

引入自:API 级别1

Check whether this monitor blocks activity starts (not allowing the actual activity to run) or allows them to execute normally.

public final Activity waitForActivity ()

引入自:API 级别1

Block until an Activity is created that matches this monitor, returning the resulting activity.

返回值
  • Activity

public final Activity waitForActivityWithTimeout (long timeOut)

引入自:API 级别1

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.

参数
timeOutTime to wait before the activity is created.
返回值

  • 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. 1. public void test单击链接() {  
  2. 2.     final Instrumentation inst = getInstrumentation();  
  3. 3.     IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW);  
  4. 4.     intentFilter.addDataScheme("http");  
  5. 5.     intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);  
  6. 6.     View link = this.getActivity().findViewById(R.id.link);  
  7. 7.     ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false);  
  8. 8.     try {  
  9. 9.            assertEquals(0, monitor.getHits());  
  10. 10.           TouchUtils.clickView(this, link);  
  11. 11.           monitor.waitForActivityWithTimeout(5000);  
  12. 12.           assertEquals(1, monitor.getHits());  
  13. 13.    } finally {  
  14. 14.          inst.removeMonitor(monitor);  
  15. 15.    }  
  16. 16. } 

在代码清单3-4里,测试用例的第2行首先获取当前待测应用的仪表盘对象。接着在第3~5行创建了一个意图过滤器(Intent Filter),指明测试用例中感兴趣的意图,即要监听的意图。并且在第7行将我们的监听器添加到仪表盘对象的监听列表中。由于没有人单击待测应用上的超链接,因此在测试代码第9行验证监听器不应该监听到任何对打开浏览器的请求。但是在第10行,测试代码显式单击了超链接,打开浏览器访问谷歌的首页,第12行的验证代码断言监听程序应该监听到一次网页浏览的请求。由于启动浏览器打开网页是一个相对较长的过程,因此在第11行告诉监听程序等待满足要求的活动创建成功,最多等待5秒钟。第14行执行清理操作,注意这里的清理操作是放在finally块中执行的,而代码清单3-4的测试步骤都被包含在try... finally块中,这样做的目的是即使测试过程当中有任何错误,也可以执行清理操作,避免影响后续的测试用例。


0 0