gpt4 book ai didi

android - Espresso : How can I test that the activity finished with result RESULT_OK?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:12 24 4
gpt4 key购买 nike

在我的应用程序中,当用户单击“注册”按钮时,将启动 RegisterActivity。用户填写表单后,详细信息将发布到 Web 服务,如果注册成功,RegisterActivity 将以 RESULT_OK 结束。下面的代码示例总结了这一点:

public void submitRegistration() {

showProgressDialog(R.string.registration, R.string.please_wait);

getWebApi().register(buildRegistrationFromUI(), new Callback<ApiResponse>() {

@Override
public void success(ApiResponse apiResponse, Response response) {
hideProgressDialog();
setResult(RESULT_OK);
finish();
}

@Override
public void failure(RetrofitError error) {
hideProgressDialog();
showErrorDialog(ApiError.parse(error));
}
});
}

使用 Espresso,我如何检查 Activity 是否已通过 setResult(RESULT_OK) 完成?

请注意:我想要创建模拟 Intent 。我想检查意向结果状态。

最佳答案

setResult(...) 方法所做的只是更改 Activity 类中字段的值

 public final void setResult(int resultCode, Intent data) {
synchronized (this) {
mResultCode = resultCode;
mResultData = data;
}
}

因此我们可以使用 Java 反射访问 mResultCode 字段来测试该值是否确实已设置为 RESULT_OK。

@Rule
public ActivityTestRule<ContactsActivity> mActivityRule = new ActivityTestRule<>(
ContactsActivity.class);


@Test
public void testResultOk() throws NoSuchFieldException, IllegalAccessException {
Field f = Activity.class.getDeclaredField("mResultCode"); //NoSuchFieldException
f.setAccessible(true);
int mResultCode = f.getInt(mActivityRule.getActivity());

assertTrue("The result code is not ok. ", mResultCode == Activity.RESULT_OK);
}

关于android - Espresso : How can I test that the activity finished with result RESULT_OK?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33778708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com