android - 可以(应该)使用 robolectric 来测试 Intent Filter

标签 android unit-testing robolectric

如果使用具有自定义方案的特定 URL,我有一个启动特定 Activity 的应用程序。例如,如果在 webview 中使用“myscheme://www.myapp.com/mypath”,我的应用程序就会启动。为此,我在 list 中配置了 Intent 过滤器:

<intent-filter>
    <action android:name="android.intent.action.View" />
    <data android:scheme="myscheme" android:host="www.myapp.com" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

我想通过编写单元测试来验证它是否有效并继续有效。

@Test   
public void testIntentHandling()
{
    Activity launcherActivity = new Activity();
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myscheme://www.myapp.com/mypath"));
    launcherActivity.startActivity(intent);

    ShadowActivity shadowActivity = Robolectric.shadowOf(launcherActivity);
    Intent startedIntent = shadowActivity.getNextStartedActivity();
    ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent);
    assertNotNull(shadowIntent);
    System.out.println(shadowIntent.getAction());
    System.out.println(shadowIntent.getData().toString());
    System.out.println(shadowIntent.getComponent().toShortString());

    assertEquals("com.mycompany", shadowIntent.getComponent().getPackageName());
}

但是,这不起作用。我得到的是“shadowIntent.getComponent()”返回 null,而它应该返回指定我的应用程序和 Activity 的组件。由于大部分工作是由 Android 系统完成的,而不是我的应用程序,是否可以假设 Robolectric 不模仿这一点,因此不能用于测试此功能是否公平?我是否可以/应该对我的 list 设置是否正确进行单元测试?

谢谢。

最佳答案

我不会以这种方式测试它。您基本上是在测试 Android 的一部分。

我会进行一项测试,即您的 AndroidManifest.xml 声明是否正确。我会进行一项/两项测试,以检查您的 Activity 是否正确处理数据 Intent

关于android - 可以(应该)使用 robolectric 来测试 Intent Filter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18193132/

相关文章:

android - 从 Android 应用程序打开文件管理器

python - 如何获取单元测试的两个 pickle.dump 的内容?

android - 带有 robolectric 的 sqlcipher 的 UnsatisfiedLinkError

android - 如何在 Android Studio 1.1.0 和 gradle 2.2.1 中使用 Robolectric 2.4

android - 如何在 Android 中模拟测试权限?

Android 4.0 ImageView setImageBitmap 不工作

android - android SDK 24 的 buildToolsVersion 是什么?

c# - 使用固定装置测试 Entity Framework

java - 在 android 的棉花糖设备中从 RSA key 生成私钥的异常

java - 为什么我们不能模拟最后一个类?