测试一个活动返回预期结果 [英] Testing that an Activity returns the expected result

查看:127
本文介绍了测试一个活动返回预期结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下活动:

package codeguru.startactivityforresult;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ChildActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.child);

        this.resultButton = (Button) this.findViewById(R.id.result_button);
        this.resultButton.setOnClickListener(onResult);
    }

    private View.OnClickListener onResult = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent result = new Intent();
            result.putExtra(ChildActivity.this.getString(R.string.result), ChildActivity.this.getResources().getInteger(R.integer.result));
            ChildActivity.this.setResult(RESULT_OK, result);
            ChildActivity.this.finish();
        }
    };
    private Button resultButton = null;
}

和下面的JUnit测试:

And the following JUnit test:

package codeguru.startactivityforresult;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.Button;
import junit.framework.Assert;

public class ChildActivityTest extends ActivityInstrumentationTestCase2<ChildActivity> {

    public ChildActivityTest() {
        super(ChildActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();

        this.setActivityInitialTouchMode(false);

        this.activity = this.getActivity();
        this.resultButton = (Button) this.activity.findViewById(R.id.result_button);
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @UiThreadTest
    public void testResultButtonOnClick() {
        Assert.assertTrue(this.resultButton.performClick());
        Assert.fail("How do I check the returned result?");
    }
    private Activity activity;
    private Button resultButton;
}

我如何确保点击按钮设置正确的结果(与调用的setResult())将返回到启动此活性的研究有任何活动 startActivityForResult()

How do I make sure that clicking the button sets the correct result (with the call to setResult()) that will be returned to any activity which starts this acitivity with startActivityForResult()?

推荐答案

随着问题的当前活动的实现,即通过点击ChildActivity按钮设置结果,那么立即销毁活动,不会有太多我们可以在ChildActivityTest做测试结果相关的东西。

With current Activity implementation in the question, i.e. by clicking button in ChildActivity set the result then destroy the activity immediately, there are not much we can do in the ChildActivityTest for testing result related stuff.

相关问题测试onActivityResult()显示答案如何单元测试startActivityForResult()和/或onActivityResult()独立于MainActivityTest。通过独立意味着MainActivityTest不依赖于ChildActivity的互动,仪表将捕捉ChildActivity创建和杀死它,然后立即返回准备烤模拟ActivityResult,因此单元测试 MainActivity。

The answer in related question Testing onActivityResult() shows how to unit-test startActivityForResult() and/or onActivityResult() standalone in MainActivityTest. By standalone means MainActivityTest does not depend on ChildActivity's interaction, instrumentation will capture ChildActivity creation and kill it immediately then return a ready baked mock ActivityResult, hence unit test MainActivity.

如果你不想仪器中断,并返回模拟ActivityResult,可以让ChildActivity继续前进,然后模拟在ChildActivity的相互作用因此,回到现实ActivityResult回MainActivity。说,如果你MainActivity开始ChildActivity的结果,那么更新一个TextView,来测试整个终端到终端的互动/合作,请参见下面示例code:

If you don't want instrumentation interrupt and return the mock ActivityResult, you can let ChildActivity keep going then simulating the interaction in ChildActivity consequently and return the real ActivityResult back to MainActivity. Says if you MainActivity start ChildActivity for result then update a TextView, to test the the whole end-to-end interaction/cooperation, see sample code below:

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
  ... ...

  public void testStartActivityForResult() {
    MainActivity mainActivity = getActivity();
    assertNotNull(activity);

    // Check initial value in TextView:
    TextView text = (TextView) mainActivity.findViewById(com.example.R.id.textview1);
    assertEquals(text.getText(), "default vaule");

    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    // Create an ActivityMonitor that monitor ChildActivity, do not interrupt, do not return mock result:
    Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(ChildActivity.class.getName(), null , false);

    // Simulate a button click in MainActivity that start ChildActivity for result:
    final Button button = (Button) mainActivity.findViewById(com.example.R.id.button1);
    mainActivity.runOnUiThread(new Runnable() {
      public void run() {
        button.performClick();
      }
    });

    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    getInstrumentation().waitForIdleSync();
    ChildActivity childActivity = (ChildActivity) getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5);
    // ChildActivity is created and gain focus on screen:
    assertNotNull(childActivity);

    // Simulate a button click in ChildActivity that set result and finish ChildActivity:
    final Button button2 = (Button) childActivity.findViewById(com.example.R.id.button1);
    childActivity.runOnUiThread(new Runnable() {
      public void run() {
        button2.performClick();
      }
    });

    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    getInstrumentation().waitForIdleSync();
    // TextView in MainActivity should be changed:
    assertEquals(text.getText(), "default value changed");
  }

  ... ...
}

我添加三个Thread.sleep()方法调用在这里,让你可以运行JUnit测试时,有机会看到该按钮点击模拟。正如你可以在这里看到,一个独立的ChildActivityTest不足以测试整个合作中,我们实际上是通过MainActivityTest测试ChildActivity.setResult()间接的,因为我们需要模拟从一开始整个互动。

I add three Thread.sleep() calls here so that you can get a chance see the button clicking simulation when running JUnit Test. As you can see here, a standalone ChildActivityTest is not sufficient to test the whole cooperation, we are actually testing ChildActivity.setResult() indirectly via MainActivityTest, as we need simulate the whole interaction from the very beginning.

这篇关于测试一个活动返回预期结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆