/** * Returns whether a permission should be treated as granted to the package for backward * compatibility reasons. * * Before Robolectric 4.0 the ShadowPackageManager treated every requested permission as * automatically granted. 4.0 changes this behavior, and only treats a permission as granted if * PackageInfo.requestedPermissionFlags[permissionIndex] & REQUESTED_PERMISSION_GRANTED == * REQUESTED_PERMISSION_GRANTED which matches the real PackageManager's behavior. * * Since many existing tests didn't set the requestedPermissionFlags on their {@code * PackageInfo} objects, but assumed that all permissions are granted, we auto-grant all * permissions if the requestedPermissionFlags is not set. If the requestedPermissionFlags is set, * we assume that the test is configuring the permission grant state, and we don't override this * setting. */ private boolean isGrantedForBackwardsCompatibility(String pkgName, PackageInfo permissionsInfo) { // Note: it might be cleaner to auto-grant these permissions when the package is added to the // PackageManager. But many existing tests modify the requested permissions _after_ adding the // package to the PackageManager, without updating the requestedPermissionsFlags. return permissionsInfo.requestedPermissionsFlags == null // Robolectric uses the PackageParser to create the current test package's PackageInfo from // the manifest XML. The parser populates the requestedPermissionsFlags, but doesn't grant // the permissions. Several tests rely on the test package being granted all permissions, so // we treat this as a special case. || pkgName.equals(RuntimeEnvironment.application.getPackageName()); }
public ShadowActivityManager() { ActivityManager.RunningAppProcessInfo processInfo = new ActivityManager.RunningAppProcessInfo(); fillInProcessInfo(processInfo); processInfo.processName = RuntimeEnvironment.application.getPackageName(); processInfo.pkgList = new String[] {RuntimeEnvironment.application.getPackageName()}; processes.add(processInfo); }
@Implementation(minSdk = JELLY_BEAN_MR1) protected String getCreatorPackage() { return (creatorPackage == null) ? RuntimeEnvironment.application.getPackageName() : creatorPackage; }
private ActivityInfo getActivityInfo(Application application) { try { return application.getPackageManager().getActivityInfo(new ComponentName(application.getPackageName(), component.getClass().getName()), PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA); } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException(e); } }
private ResName qualifyFromNonAssetFileName(String fileName) { // Resources from a jar belong to the "android" namespace, except when they come from "resource_files.zip" // when they are application resources produced by Bazel. if (fileName.startsWith("jar:") && !fileName.contains("resource_files.zip")) { // Must remove "jar:" prefix, or else qualifyFromFilePath fails on Windows return ResName.qualifyFromFilePath("android", fileName.replaceFirst("jar:", "")); } else { return ResName.qualifyFromFilePath(RuntimeEnvironment.application.getPackageName(), fileName); } }
/** Removes a USB device from available USB devices map. */ public void removeUsbDevice(UsbDevice usbDevice) { Preconditions.checkNotNull(usbDevice); usbDevices.remove(usbDevice.getDeviceName()); revokePermission(usbDevice, RuntimeEnvironment.application.getPackageName()); }
@Override public void run() { //noinspection ResultOfMethodCallIgnored heapDumpFile.setReadable(true, false); final Uri heapDumpUri = getUriForFile(getBaseContext(), "com.squareup.leakcanary.fileprovider." + getApplication().getPackageName(), heapDumpFile); runOnUiThread(new Runnable() { @Override public void run() { startShareIntentChooser(heapDumpUri); } }); } });
@Override public String getPackageName() { return sEnablePackageNameFake ? FAKE_PACKAGE_NAME : super.getPackageName(); }
private static void doValidateProcessNames(final Application app, final String[] process_names) { try { final PackageInfo info = app.getPackageManager().getPackageInfo(app.getPackageName(), GET_ACTIVITIES | GET_SERVICES | GET_RECEIVERS | GET_PROVIDERS); final Set<String> defined_process_names = new HashSet<>(); if (info.activities != null) for (final ActivityInfo activity : info.activities) defined_process_names.add(activity.processName); if (info.services != null) for (final ServiceInfo service : info.services) defined_process_names.add(service.processName); if (info.receivers != null) for (final ActivityInfo receiver : info.receivers) defined_process_names.add(receiver.processName); if (info.providers != null) for (final ProviderInfo provider : info.providers) defined_process_names.add(provider.processName); for (final String process_name : process_names) if (! defined_process_names.contains(getFullProcessName(app, process_name))) throw new IllegalArgumentException("Process name \"" + process_name + "\" is not used by any component in AndroidManifest.xml"); } catch (final PackageManager.NameNotFoundException ignored) {} // Should never happen }
/** Returns true if the caller has permission to access the device. */ @Implementation protected boolean hasPermission(UsbDevice device) { return hasPermissionForPackage(device, RuntimeEnvironment.application.getPackageName()); }
/** * Adds a USB device into available USB devices map with permission value. If the USB device * already exists, updates the USB device with new permission value. */ public void addOrUpdateUsbDevice(UsbDevice usbDevice, boolean hasPermission) { Preconditions.checkNotNull(usbDevice); Preconditions.checkNotNull(usbDevice.getDeviceName()); usbDevices.put(usbDevice.getDeviceName(), usbDevice); if (hasPermission) { grantPermission(usbDevice); } else { revokePermission(usbDevice, RuntimeEnvironment.application.getPackageName()); } }
@Implementation(minSdk = M) public StatusBarNotification[] getActiveNotifications() { StatusBarNotification[] statusBarNotifications = new StatusBarNotification[notifications.size()]; int i = 0; for (Map.Entry<Key, Notification> entry : notifications.entrySet()) { statusBarNotifications[i++] = new StatusBarNotification( RuntimeEnvironment.application.getPackageName(), null /* opPkg */, entry.getKey().id, entry.getKey().tag, android.os.Process.myUid() /* uid */, android.os.Process.myPid() /* initialPid */, 0 /* score */, entry.getValue(), android.os.Process.myUserHandle(), 0 /* postTime */); } return statusBarNotifications; }
@Implementation(minSdk = N) @HiddenApi protected void grantPermission(UsbDevice device) { grantPermission(device, RuntimeEnvironment.application.getPackageName()); }
/** * {@inheritDoc} */ @Override public void setEnabled(boolean enabled) { if (supportedAndroidVersion) { ACRA.log.i(LOG_TAG, "ACRA is " + (enabled ? "enabled" : "disabled") + " for " + context.getPackageName()); reportExecutor.setEnabled(enabled); } else { ACRA.log.w(LOG_TAG, "ACRA requires ICS or greater. ACRA is disabled and will NOT catch crashes or send messages."); } }
@Test public void createPackageContextRemoteViews() throws Exception { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remote_views); remoteViews.apply(context, new FrameLayout(context)); }
@Test public void createPackageContext() throws Exception { Context packageContext = context.createPackageContext(context.getPackageName(), 0); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.cloneInContext(packageContext); inflater.inflate(R.layout.remote_views, new FrameLayout(context), false); }
@Test public void shouldAssignThePackageNameFromTheManifest() throws Exception { Application application = ApplicationProvider.getApplicationContext(); assertThat(application.getPackageName()).isEqualTo("org.robolectric"); assertThat(application).isInstanceOf(TestApplication.class); }
@Test public void create_withResourceId_shouldSetDataSource() { Application context = ApplicationProvider.getApplicationContext(); ShadowMediaPlayer.addMediaInfo( DataSource.toDataSource("android.resource://" + context.getPackageName() + "/123"), new ShadowMediaPlayer.MediaInfo(100, 10)); MediaPlayer mp = MediaPlayer.create(context, 123); ShadowMediaPlayer shadow = shadowOf(mp); assertThat(shadow.getDataSource()) .isEqualTo( DataSource.toDataSource("android.resource://" + context.getPackageName() + "/123")); }
@Before public void setUp() throws Exception { ACRA.log = new RobolectricLog(); ACRA.DEV_LOGGING = true; Robolectric.setupContentProvider(AcraContentProvider.class, RuntimeEnvironment.application.getPackageName() + ".acra"); resolver = RuntimeEnvironment.application.getContentResolver(); file = File.createTempFile("test", "." + JSON_EXTENSION); Shadows.shadowOf(MimeTypeMap.getSingleton()).addExtensionMimeTypMapping(JSON_EXTENSION, JSON_MIMETYPE); }
@Test @Config(minSdk = LOLLIPOP) public void enforcePermissionChecks() throws Exception { shadowOf(userManager).enforcePermissionChecks(true); try { userManager.isManagedProfile(); fail("Expected exception"); } catch (SecurityException expected) {} Application context = ApplicationProvider.getApplicationContext(); PackageInfo packageInfo = shadowOf(context.getPackageManager()) .getInternalMutablePackageInfo(context.getPackageName()); packageInfo.requestedPermissions = new String[] {permission.MANAGE_USERS}; shadowOf(userManager).setManagedProfile(true); assertThat(userManager.isManagedProfile()).isTrue(); }