首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Instrumentation发布活动

Android Instrumentation发布活动
EN

Stack Overflow用户
提问于 2013-03-17 20:55:30
回答 1查看 7.4K关注 0票数 5

我有一个包含按钮的简单活动。当我按下按钮时,第二个活动就会运行。现在我是Android Instrumentation测试的新手。到目前为止,这就是我所写的

代码语言:javascript
复制
public class TestSplashActivity extends
    ActivityInstrumentationTestCase2<ActivitySplashScreen> {

private Button mLeftButton;
private ActivitySplashScreen activitySplashScreen;
private ActivityMonitor childMonitor = null;
public TestSplashActivity() {
    super(ActivitySplashScreen.class);
}

@Override
protected void setUp() throws Exception {
    super.setUp();
    final ActivitySplashScreen a = getActivity();
    assertNotNull(a);
    activitySplashScreen=a;
    mLeftButton=(Button) a.findViewById(R.id.btn1);

}

@SmallTest
public void testNameOfButton(){
    assertEquals("Press Me", mLeftButton.getText().toString());
    this.childMonitor = new ActivityMonitor(SecondActivity.class.getName(), null, true);
    this.getInstrumentation().addMonitor(childMonitor);
    activitySplashScreen.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            mLeftButton.performClick();
    }});

    Activity childActivity=this.getInstrumentation().waitForMonitorWithTimeout(childMonitor, 5000);
    assertEquals(childActivity, SecondActivity.class);

}

}

现在,我获得按钮文本的第一个断言起作用了。但是当我调用执行单击时,我得到了一个异常

代码语言:javascript
复制
  Only the original thread that created a view hierarchy can touch its views. 

现在,我在Android应用程序的上下文中理解了这一例外,但现在是在工具测试方面。如何在按钮上执行单击事件,以及如何检查第二个活动是否已加载。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-17 21:24:33

假设您有一个扩展InstrumentationTestCase的测试类,并且您在一个测试方法中,它应该遵循以下逻辑:

  1. 注册您对要检查的活动的兴趣。
  2. 启动它
  3. 执行您想要执行的操作。检查组件是否正确,执行用户操作,以及您对"sequence"
  4. Perform中下一个活动感兴趣的类型。
  5. 重复,遵循此逻辑...

在代码方面,这将导致类似如下的结果:

代码语言:javascript
复制
Instrumentation mInstrumentation = getInstrumentation();
// We register our interest in the activity
Instrumentation.ActivityMonitor monitor = mInstrumentation.addMonitor(YourClass.class.getName(), null, false);
// We launch it
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(mInstrumentation.getTargetContext(), YourClass.class.getName());
mInstrumentation.startActivitySync(intent);

Activity currentActivity = getInstrumentation().waitForMonitor(monitor);
assertNotNull(currentActivity);
// We register our interest in the next activity from the sequence in this use case
mInstrumentation.removeMonitor(monitor);
monitor = mInstrumentation.addMonitor(YourNextClass.class.getName(), null, false);

要发送点击,请执行类似以下操作:

代码语言:javascript
复制
View v = currentActivity.findViewById(....R.id...);
assertNotNull(v);
TouchUtils.clickView(this, v);
mInstrumentation.sendStringSync("Some text to send into that view, if it would be a text view for example. If it would be a button it would already have been clicked by now.");
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15460949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档