Android的ES preSSO和帐户选择器 [英] Android espresso and account picker

查看:226
本文介绍了Android的ES preSSO和帐户选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让使用es preSSO仪器测试。
我有一个活动,其中帐户选择器是当应用程序启动时弹出-ED(主要活动)。
如果客户点击(在对话框中)取消,选择器是弹出再升;如果用户点击添加,结果拿起活动的结果。

I am having trouble making the instrumentation test using the Espresso. I have an activity where account picker is popup-ed when app is started (main activity). If customer clicks on cancel (in dialog), picker is popup up again; If user clicks on add, the result is picked up on activity result.

我不知道如何创建ES preSSO一个简单的测试,这将包括选择器。
当我创建在MainActivity仪器测试,我得到这个消息:
在第一阶段没有活动RESUMED ...

I dont know how to create a simple test with espresso which will include that picker. When I create the Instrumentation test with the MainActivity, I got this message: No activities in stage RESUMED...

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity>{
    MainActivity myActivity;
    public MainActivityTest(){
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    public void testAccountPicker(){
        onView(withText("Choose an account")).check(matches(isDisplayed()));
    }
}

有谁有类似的问题?

Does anybody had similar problem?

为感谢名单提前你的答案。

Thanx for your answers in advance.

推荐答案

这是一个艰难的:)。这里的问题是,一旦流离开你的应用程序(谷歌帐户选择器是一个外部应用程序),ES preSSO结束测试。帐户选择器是从包 com.google.android.gms ,这样外部的活动。一旦它开始,测试完成后,你就永远无法匹配对话框中任何事情。

That's a tough one :). The problem here is that once the flow leaves your application (Google Account Picker is an external application), Espresso ends the test. Account Picker is an activity from the package com.google.android.gms, thus external. Once it's started, your test is finished and you'll never be able to match anything in the dialog.

您有三种可能的解决方案,让您的测试可行的:

You have three possible solutions to make your tests feasible:


  • 在您的应用程序,以伪造意图使用类路径替代;或

  • 修复您的应用程序可测性;或

  • 使用依赖注入,如匕首

  • Using classpath substitution on your app to fake the intents; or
  • Fixing your app "testability"; or
  • Using dependency injection, like Dagger

我将展示如何使用类路径替换。该技术是非常简单的:你应该在测试过程中隔离你的意图创造一个单独的类,比如 IntentsFactory 和,覆盖类

I'll show how to use classpath substitution. The technique is really simple: you should isolate your Intent creation in a separate class, say IntentsFactory and, during tests, override that class.

说你的工厂在 com.yourapp.factories.IntentsFactory ,它是这样的:

Say your factory is in com.yourapp.factories.IntentsFactory and it is something like this:

public class IntentsFactory {
    public static Intent getAccountPickerIntent (Context context) {
        return AccountPicker.newChooseAccountIntent(null, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null);
    }
}

你应该在你的测试应用程序创建(说它是 com.yourapp.tests )的软件包,具有相同的名称和方法,但返回不同的意图,一嘲笑/哑之一:

You should create in your test app (say it is com.yourapp.tests) a package with the same name and methods, but that returns a different Intent, a mocked/dummy one:

public class IntentsFactory {
    public static Intent getAccountPickerIntent (Context context) {
        return new Intent(context, MyDummyAccountPickerActivity.class);
    }
}

当你的测试执行,他们会用最近类在classpath中,也就是从你的测试IntentsFactory。而不是返回该派流向另一个应用程序的意图,流量会去到一个类项目和Es preSSO不会结束测试。

Whenever your tests execute, they will use the "nearest" class in the classpath, that is, the IntentsFactory from your tests. Instead of returning an intent that send the flow to another app, the flow will go to an class of your project and Espresso won't end the tests.

这里唯一需要注意的是,你必须创建 MyDummyAccountPickerActivity 将返回结果和捆绑类似于框架类返回的。活动应该在你的应用程序的清单存在,你就会有指示模拟器的Dalvik运行时允许类路径(看看这个的这个并的这个链接)替换使用以下命令行:

The only caveat here is that you'll have to create the MyDummyAccountPickerActivity which will return a result and a Bundle similar to the one returned by the framework class. The activity should exists in your app's manifest and you'll have to instruct your emulator Dalvik runtime to allow classpath (check it out this this and this links) substitution with the following command line:

adb shell setprop dalvik.vm.dexopt-flags v=n,o=v
adb shell stop installd
adb shell start installd

和执行你的测试。

我也有类似的问题,以测试相机,它的深入讨论<一个href=\"https://groups.google.com/forum/#!searchin/android-test-kit-discuss/classpath/android-test-kit-discuss/QSm-JqHYAs0/xK0ALdFpMK0J\"相对=nofollow>中居preSSO论坛

I had a similar problem to test the Camera and it's thoroughly discussed in Espresso forum

这篇关于Android的ES preSSO和帐户选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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