/** * Register a handler in the package manager for a image capture intent */ private void registerMediaStoreIntentHandler() { // Add something that will 'handle' the media capture intent ShadowPackageManager shadowPackageManager = shadowOf(RuntimeEnvironment.application.getPackageManager()); ResolveInfo info = new ResolveInfo(); info.isDefault = true; ApplicationInfo applicationInfo = new ApplicationInfo(); applicationInfo.packageName = "does.not.matter"; info.activityInfo = new ActivityInfo(); info.activityInfo.applicationInfo = applicationInfo; info.activityInfo.name = "DoesNotMatter"; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); shadowPackageManager.addResolveInfoForIntent(intent, info); }
private void registerIntentHandler(String handler) { // Add something that will 'handle' the given intent type ShadowPackageManager shadowPackageManager = shadowOf(RuntimeEnvironment.application.getPackageManager()); ResolveInfo info = new ResolveInfo(); info.isDefault = true; ApplicationInfo applicationInfo = new ApplicationInfo(); applicationInfo.packageName = "does.not.matter"; info.activityInfo = new ActivityInfo(); info.activityInfo.applicationInfo = applicationInfo; info.activityInfo.name = "DoesNotMatter"; info.activityInfo.exported = true; Intent intent = new Intent(handler); if(handler.equals(Intent.ACTION_GET_CONTENT)) { intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); } shadowPackageManager.addResolveInfoForIntent(intent, info); }
@Test public void sensors() throws Exception { Activity activity = Robolectric.setupActivity(MainActivity.class); Button button = activity.findViewById(R.id.buttonSensors); button.performClick(); Intent intent = shadowOf(activity).getNextStartedActivity(); assertNull(intent); ShadowPackageManager pm = shadowOf(RuntimeEnvironment.application.getPackageManager()); pm.setSystemFeature(PackageManager.FEATURE_USB_HOST, true); button.performClick(); intent = shadowOf(activity).getNextStartedActivity(); if (intent.getComponent() != null) { assertEquals(TestListActivity.class.getCanonicalName(), intent.getComponent().getClassName()); } }
@Test public void startActivityToRatePlayStorePresent() { String packageName = "com.test"; String storeUrl = "market://details?id=" + packageName; Activity activity = spy(Robolectric.buildActivity(Activity.class).get()); UHActivityLifecycle lifecycle = mock(UHActivityLifecycle.class); ShadowPackageManager packageManager = shadowOf(RuntimeEnvironment.application.getPackageManager()); ResolveInfo info = new ResolveInfo(); info.isDefault = true; ApplicationInfo appInfo = new ApplicationInfo(); appInfo.packageName = "com.play"; info.activityInfo = new ActivityInfo(); info.activityInfo.applicationInfo = appInfo; info.activityInfo.name = "Test"; Intent storeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(storeUrl)); packageManager.addResolveInfoForIntent(storeIntent, info); UHInternal.getInstance().setActivityLifecycle(lifecycle); when(lifecycle.getCurrentActivity()).thenReturn(activity); when(activity.getPackageName()).thenReturn(packageName); ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class); doNothing().when(activity).startActivity(intent.capture()); // update session data UHOperation operation = mock(UHOperation.class); UHInternal.getInstance().setOperationFactory(new UHOperationTestFactory(operation)); Map<String,Object> data = new HashMap<>(); data.put("rated",true); UHInternal.getInstance().rateThisApp(); verify(operation).updateSessionData(data, null); assertEquals(intent.getValue().getAction(), Intent.ACTION_VIEW); assertEquals(intent.getValue().getData(), Uri.parse(storeUrl)); }
@Test public void clickTest() { String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}; Intent intent = new Intent(); intent.putExtra(ConstantKey.TYPE, TestType.CHAMBER_TEST); ActivityController controller = Robolectric.buildActivity(TestListActivity.class, intent).create(); controller.start().visible(); Activity activity = (Activity) controller.get(); RecyclerView recyclerView = activity.findViewById(R.id.list_types); recyclerView.getChildAt(1).performClick(); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); Intent nextIntent = shadowOf(activity).getNextStartedActivity(); assertNull(nextIntent); ShadowApplication application = shadowOf(activity.getApplication()); application.grantPermissions(permissions); controller.resume(); ShadowPackageManager pm = shadowOf(RuntimeEnvironment.application.getPackageManager()); pm.setSystemFeature(PackageManager.FEATURE_CAMERA, true); pm.setSystemFeature(PackageManager.FEATURE_CAMERA_FLASH, true); recyclerView.getChildAt(1).performClick(); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); Intent nextIntent2 = shadowOf(activity).getNextStartedActivity(); if (nextIntent2.getComponent() != null) { assertEquals(ChamberTestActivity.class.getCanonicalName(), nextIntent2.getComponent().getClassName()); } }