我有一个应用程序,如果使用具有自定义方案的特定URL,则启动特定活动.例如,如果在webview中使用“myscheme://www.myapp.com/mypath”,我的应用程序就会启动.为此,我在清单中配置了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,它应该返回指定我的应用程序和活动的组件.由于大部分工作都是由Android系统完成的,而不是我的应用程序,假设Robolectric不能模仿这一点是否公平,因此不能用于测试此功能?我是否正确地假设我可以/应该单位测试天气我的清单设置正确吗?
谢谢.
解决方法
我不会以这种方式测试它.你基本上是测试Android的一部分.
我会有一个测试,你AndroidManifest.xml声明是正确的.我会进行一到两次测试,检查您的活动是否正确处理数据意图
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。