@Before public void setUp() throws Exception { Glide.tearDown(); RobolectricPackageManager pm = RuntimeEnvironment.getRobolectricPackageManager(); ApplicationInfo info = pm.getApplicationInfo(RuntimeEnvironment.application.getPackageName(), 0); info.metaData = new Bundle(); info.metaData.putString(SetupModule.class.getName(), "GlideModule"); // Ensure that target's size ready callback will be called synchronously. target = mock(Target.class); imageView = new ImageView(RuntimeEnvironment.application); imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100)); imageView.layout(0, 0, 100, 100); doAnswer(new CallSizeReady()).when(target).getSize(isA(SizeReadyCallback.class)); Handler bgHandler = mock(Handler.class); when(bgHandler.post(isA(Runnable.class))).thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return true; } }); Lifecycle lifecycle = mock(Lifecycle.class); RequestManagerTreeNode treeNode = mock(RequestManagerTreeNode.class); requestManager = new RequestManager(Glide.get(getContext()), lifecycle, treeNode); requestManager.resumeRequests(); }
/** * Register a handler in the package manager for a image capture intent */ private void registerMediaStoreIntentHandler() { // Add something that will 'handle' the media capture intent RobolectricPackageManager packageManager = 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); packageManager.addResolveInfoForIntent(intent, info); }
@Test public void should_start_url_intent() { // Given String url = "geo:22.5430883,114.1043205"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), ShadowResolveInfo.newResolveInfo("", "", "")); // When boolean result = Intents.startUri(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(result).isTrue(); assertThat(startedIntent.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedIntent.getData().toString()).isEqualTo(url); }
@Test public void should_return_false_when_nothing_on_the_device_can_open_the_uri() { // Given String url = "http://www.google.com"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), (ResolveInfo) null); // When boolean result = Intents.startUri(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(result).isFalse(); assertThat(startedIntent).isNull(); }
@Test public void should_start_external_uri() { // Given String url = "http://www.google.com"; RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), ShadowResolveInfo.newResolveInfo("", "", "")); // When Intents.startExternalUrl(activity, url); // Then ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(startedIntent.getAction()).isEqualTo(Intent.ACTION_VIEW); assertThat(startedIntent.getData().toString()).isEqualTo(url); }
private void registerIntentHandler(String handler) { // Add something that will 'handle' the given intent type RobolectricPackageManager packageManager = 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("*/*"); } packageManager.addResolveInfoForIntent(intent, info); }
/** * Register a handler in the package manager for a image capture intent */ private void registerMediaStoreIntentHandler() { // Add something that will 'handle' the media capture intent RobolectricPackageManager packageManager = 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(Intents.Scan.ACTION); packageManager.addResolveInfoForIntent(intent, info); }
@Test public void testDownloadContent() { ResolveInfo resolverInfo = new ResolveInfo(); resolverInfo.activityInfo = new ActivityInfo(); resolverInfo.activityInfo.applicationInfo = new ApplicationInfo(); resolverInfo.activityInfo.applicationInfo.packageName = ListActivity.class.getPackage().getName(); resolverInfo.activityInfo.name = ListActivity.class.getName(); RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); final String url = "http://example.com/file.doc"; rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)), resolverInfo); WebView webView = (WebView) activity.findViewById(R.id.web_view); ShadowWebView shadowWebView = (ShadowWebView) ShadowExtractor.extract(webView); when(item.getUrl()).thenReturn(url); shadowWebView.getDownloadListener().onDownloadStart(url, "", "", "", 0l); assertThat((View) activity.findViewById(R.id.empty)).isVisible(); activity.findViewById(R.id.download_button).performClick(); assertNotNull(shadowOf(activity).getNextStartedActivity()); }
@Test public void testDownloadPdf() throws InterruptedException { ResolveInfo resolverInfo = new ResolveInfo(); resolverInfo.activityInfo = new ActivityInfo(); resolverInfo.activityInfo.applicationInfo = new ApplicationInfo(); resolverInfo.activityInfo.applicationInfo.packageName = ListActivity.class.getPackage().getName(); resolverInfo.activityInfo.name = ListActivity.class.getName(); RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); when(item.getUrl()).thenReturn("http://example.com/file.pdf"); rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(item.getUrl())), resolverInfo); WebView webView = activity.findViewById(R.id.web_view); ShadowWebView shadowWebView = (ShadowWebView) ShadowExtractor.extract(webView); WebFragment fragment = (WebFragment) activity.getSupportFragmentManager() .findFragmentByTag(WebFragment.class.getName()); shadowWebView.getDownloadListener().onDownloadStart(item.getUrl(), "", "", "application/pdf", 0l); shadowWebView.getWebViewClient().onPageFinished(webView, PDF_LOADER_URL); verify(fragment.mFileDownloader).downloadFile( eq(item.getUrl()), eq("application/pdf"), any(FileDownloader.FileDownloaderCallback.class)); }
@Test public void testSelectItemOpenExternal() { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putBoolean(activity.getString(R.string.pref_external), true) .commit(); controller.pause().resume(); activity.onItemSelected(new TestWebItem() { @Override public String getUrl() { return "http://example.com"; } }); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW); }
@Test public void testSelectItemStartActionView() { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.browser", "DefaultActivity")); PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putBoolean(activity.getString(R.string.pref_external), true) .commit(); controller.pause().resume(); activity.onItemSelected(new TestWebItem() { @Override public String getUrl() { return "http://example.com"; } }); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW); }
@Test public void testSelectItemOpenChooser() { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://news.ycombinator.com/item?id=1")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://news.ycombinator.com/item?id=1")), ShadowResolveInfo.newResolveInfo("label", "com.android.browser", "DefaultActivity")); PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putBoolean(activity.getString(R.string.pref_external), true) .commit(); controller.pause().resume(); activity.onItemSelected(new TestWebItem() { @Override public String getUrl() { return "http://news.ycombinator.com/item?id=1"; } }); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_CHOOSER); }
@Test public void execute_whenUberAppInsalled_shouldPointToUberApp() throws IOException { String expectedUri = readUriResourceWithUserAgentParam("src/test/resources/deeplinkuris/just_client_provided", USER_AGENT_DEEPLINK); Activity activity = Robolectric.setupActivity(Activity.class); ShadowActivity shadowActivity = shadowOf(activity); RobolectricPackageManager packageManager = RuntimeEnvironment.getRobolectricPackageManager(); PackageInfo uberPackage = new PackageInfo(); uberPackage.packageName = UBER_PACKAGE_NAME; packageManager.addPackage(uberPackage); RideParameters rideParameters = new RideParameters.Builder().build(); RequestDeeplink requestDeeplink = new RequestDeeplink.Builder(activity) .setRideParameters(rideParameters) .setSessionConfiguration(new SessionConfiguration.Builder().setClientId("clientId").build()) .build(); requestDeeplink.execute(); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertEquals(expectedUri, startedIntent.getData().toString()); }
@Test public void testAfterUpdateEvent() throws PackageManager.NameNotFoundException { Context context = RuntimeEnvironment.application; AfterUpdateEvent event = timeToAct.watchEvent(new AfterUpdateEvent(context, KEY)); assertFalse(event.isHappened()); RobolectricPackageManager packageManager = RuntimeEnvironment.getRobolectricPackageManager(); packageManager.getPackageInfo(context.getPackageName(), 0).versionCode++; timeToAct.forceWatchEvent(event); // reinitialize assertTrue(event.isHappened()); }
static void simulateAppUpdate() { RobolectricPackageManager rpm = RuntimeEnvironment.getRobolectricPackageManager(); PackageInfo packageInfo = new PackageInfo(); packageInfo.packageName = RuntimeEnvironment.application.getPackageName(); packageInfo.lastUpdateTime = new Date().getTime(); rpm.addPackage(packageInfo); Once.initialise(RuntimeEnvironment.application); }
private void addManifestActivitiesToPackageManager(AndroidManifest appManifest, Application application) { if (appManifest != null) { Map<String,ActivityData> activityDatas = appManifest.getActivityDatas(); RobolectricPackageManager packageManager = (RobolectricPackageManager) application.getPackageManager(); for (ActivityData data : activityDatas.values()) { String name = data.getName(); String activityName = name.startsWith(".") ? appManifest.getPackageName() + name : name; packageManager.addResolveInfoForIntent(new Intent(activityName), new ResolveInfo()); } } }
/** * Seems Robolectric didn't map the implicit intent, so mock them by myself. */ private void addImplicitIntentToPM() { RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); AndroidManifest appManifest = ShadowApplication.getInstance().getAppManifest(); for (Map.Entry<String, ActivityData> activity : appManifest.getActivityDatas().entrySet()) { String activityName = activity.getKey(); ActivityData activityData = activity.getValue(); if (activityData.getTargetActivity() != null) { activityName = activityData.getTargetActivityName(); } ApplicationInfo applicationInfo = new ApplicationInfo(); applicationInfo.packageName = RuntimeEnvironment.application.getPackageName(); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.resolvePackageName = RuntimeEnvironment.application.getPackageName(); resolveInfo.activityInfo = new ActivityInfo(); resolveInfo.activityInfo.applicationInfo = applicationInfo; resolveInfo.activityInfo.name = activityName; for (IntentFilterData intentFilterData : activityData.getIntentFilters()) { for (String intentFilterAction : intentFilterData.getActions()) { Intent i = new Intent(intentFilterAction); i.setPackage(RuntimeEnvironment.application.getPackageName()); rpm.addResolveInfoForIntent(i, resolveInfo); } } } }
@Override public Application createApplication(Method method, AndroidManifest appManifest, Config config) { // FIXME If a future version of Robolectric implements "setInstallerPackageName", remove this. RobolectricPackageManager oldManager = Robolectric.packageManager; RobolectricPackageManager newManager = new FullEnvPackageManager(); for (PackageInfo pkg : oldManager.getInstalledPackages(0)) newManager.addPackage(pkg); Robolectric.packageManager = newManager; return new PiwikTestApplication(); }
@Before public void setUp() throws PackageManager.NameNotFoundException { Context context = RuntimeEnvironment.application.getApplicationContext(); // Assume app just installed RobolectricPackageManager roboPackMan = RuntimeEnvironment.getRobolectricPackageManager(); PackageInfo pkgInfo = roboPackMan.getPackageInfo(context.getPackageName(), 0); pkgInfo.firstInstallTime = System.currentTimeMillis(); }
public static void addResolver(Intent intent) { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( intent, ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); }
@SuppressLint("NewApi") @Test public void testOptionExternal() { RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager(); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); packageManager.addResolveInfoForIntent( new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(HackerNewsClient.WEB_ITEM_PATH, "1"))), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity")); Intent intent = new Intent(); intent.putExtra(ItemActivity.EXTRA_ITEM, new TestItem() { @NonNull @Override public String getType() { return STORY_TYPE; } @Override public String getUrl() { return "http://example.com"; } @Override public boolean isStoryType() { return true; } @Override public String getId() { return "1"; } }); controller.withIntent(intent).create().start().resume(); // inflate menu, see https://github.com/robolectric/robolectric/issues/1326 ShadowLooper.pauseMainLooper(); controller.visible(); ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable(); // open article shadowOf(activity).clickMenuItem(R.id.menu_external); shadowOf(ShadowPopupMenu.getLatestPopupMenu()) .getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_article)); ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable(); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW); // open item shadowOf(activity).clickMenuItem(R.id.menu_external); shadowOf(ShadowPopupMenu.getLatestPopupMenu()) .getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_comments)); ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable(); assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW); }
@Before public void setUp() throws Exception { rpm = (RobolectricPackageManager) Robolectric.application.getPackageManager(); }