在安卓中测试Intent

4

我有一个安卓应用。当用户点击按钮A时,会触发以下安卓伪代码:


//inside FirstActivity
@Override
public void onClick(View view) {
    startActivity(new Intent(this, AnotherActivity.class));
}

如果我没记错的话,在AnotherActivity中的onResume方法应该被调用了,对吗?

我使用ActivityInstrumentationTestCase2<FirstActivity>来测试我的活动,但我无法实例化AnotherActivity

所以问题是,如何测试这个:“当按下一个按钮时,正确的活动会被恢复,并且正确的额外信息会传递给意图。”

4个回答

9
你可以使用工具来创建一个ActivityMonitor,这将监控是否启动了新的活动。
ActivityMonitor am = getInstrumentation().addMonitor(Activity3.class.getName(), null, true;

然后您需要使用button.performClick()来“按下按钮”。最后,您检查活动监视器是否被触发。
am.waitForActivitywithTimeout(timeout);
assertEquals(1, am.getHits());

我已经有一段时间没有使用ActivityInstrumentationTestCase2了,所以我不能保证这些步骤完全正确。无论如何,我建议您查看Robolectric:这是一个非常棒的 Android 单元测试框架,可以改变您的生活。它将帮助您克服许多难以或不可能通过默认的 instrumentation 测试进行测试的情况。


2

经过一段时间后,我想发布我几乎总是使用的解决方案。最初我喜欢@aleph_null的解决方案,但事实证明它使测试变得难以忍受,所以现在我使用的是:

首先,我有这个接口

/**
 * Simple interface to launch other activities.
 */
public interface ActivityLauncher {

    /**
     * Starts an activity with the Intent provided.
     * @param intent an intent
     */
    public void start(Context context, Intent intent);

    /**
     *
     * Returns the intent as set by {@link #start(android.content.Context,     android.content.Intent) start} or null if not yet
     * called.
     *
     * @return an intent or null
     */
    public Intent getIntent();
}

而我有两种实现方式:

/**
 * Default implementation of ActivityLauncher
 */
public class DefaultActivityLauncher implements ActivityLauncher{

    private Intent intent;

    public DefaultActivityLauncher(){}

    @Override
    public void start(Context context, Intent intent) {
        this.intent = intent;
        context.startActivity(intent);
    }

    @Override
    public Intent getIntent() {
        return intent;
    }
}

并且

/**
 * Mock implementation of ActivityLauncher that simply sets the intent but does not actually starts
 * an activity.
 */
public class MockActivityLauncher implements ActivityLauncher {

    private Intent intent;

    @Override
    public void start(Context context, Intent intent) {
        this.intent = intent;
    }

    @Override
    public Intent getIntent() {
        return intent;
    }
}

然后我使用类似于Dagger的依赖注入框架,就像这样:
 public class MyActivity {

    @Inject ActivityLauncher launcher;

    public void onCreate(Bundle bundle){

        // some code omitted for brevity
        findViewById(R.id.goToOtherActivityButton).setOnClick(new OnClickListener(){
           Intent intent = new Intent(getContext(), MyOtherActivity.class);
           launcher.start(getContext(), intent);
        });
    }

    public ActivityLauncher getLauncher(){
        return launcher;
    }
}

测试就像这样简单:checkIntentIsValid(activity.geLauncher().getIntent())


-1
这段代码可能会给你一个好的想法。
如果你正在调用 Activity1 ---> Activity2
你可以通过这种方法发送 UserName
Intent intent = new Intent(getBaseContext(), Activity2.class);
intent.putExtra("UserName ", UserName );
startActivity(intent);

要在Activity2中检索Extra(),您需要使用以下代码:

String UserName = (String) getIntent().getSerializableExtra("UserName ");

希望这可以帮助您。

以下编辑以获得更好的理解

public class Activity2 extends Activity {
    private String UserName;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_2);
        UserName= (String) getIntent().getSerializableExtra("UserName");
        Log.i(Tag, "UserName: "+ UserName);
     }

    // you can call this method from click or where ever you want
    private void AnyMethod()
    {
           Intent intent = new Intent(getBaseContext(), Activity3.class);
           intent.putExtra("UserName ", UserName );
           startActivity(intent);
    }

 }

-1
@Override
public void onClick(View view) {
    startActivity(new Intent(this, AnotherActivity.class));
}

你的代码并不总是正确的。

例如:

btn.setOnClickListener(new OnClickListener(
    @Override
    public void onClick(View view) {
        startActivity(new Intent(this, AnotherActivity.class));
    }
));

请将此代码替换为:CurrentActivity.this
它看起来像:
@Override
public void onClick(View view) {
    startActivity(new Intent(CurrentActivity.this, AnotherActivity.class));
}

请确保您的清单文件中包含以下代码:

<Activity name=".AnotherActivity">
</Activity>

3
我认为OP所说的“testing”不是指那个意思。 - Steve Pomeroy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接