Search in sources :

Example 6 with Package

use of android.content.pm.PackageParser.Package in project robolectric by robolectric.

the class LegacyManifestParserTest method createPackage_protectionLevelNotDeclated_shouldParseToNormal.

@Test
public void createPackage_protectionLevelNotDeclated_shouldParseToNormal() {
    Package parsedPackage = LegacyManifestParser.createPackage(androidManifest);
    int protectionLevel = getPermissionInfo(parsedPackage.permissions, "permission_with_minimal_fields").protectionLevel;
    assertThat(protectionLevel).isEqualTo(PermissionInfo.PROTECTION_NORMAL);
}
Also used : Package(android.content.pm.PackageParser.Package) Test(org.junit.Test)

Example 7 with Package

use of android.content.pm.PackageParser.Package in project robolectric by robolectric.

the class LegacyManifestParserTest method createPackage_protectionLevelVendorOrOem_shouldParseCorrectFlags.

@Test
public void createPackage_protectionLevelVendorOrOem_shouldParseCorrectFlags() {
    Package parsedPackage = LegacyManifestParser.createPackage(androidManifest);
    int protectionLevel = getPermissionInfo(parsedPackage.permissions, "vendor_privileged_or_oem_permission").protectionLevel;
    assertThat(protectionLevel).isEqualTo(PermissionInfo.PROTECTION_FLAG_VENDOR_PRIVILEGED | PermissionInfo.PROTECTION_FLAG_OEM);
}
Also used : Package(android.content.pm.PackageParser.Package) Test(org.junit.Test)

Example 8 with Package

use of android.content.pm.PackageParser.Package in project robolectric by robolectric.

the class LegacyManifestParserTest method createPackage_signatureOrPrivileged_shouldParseCorrectFlags.

@Test
public void createPackage_signatureOrPrivileged_shouldParseCorrectFlags() {
    Package parsedPackage = LegacyManifestParser.createPackage(androidManifest);
    int protectionLevel = getPermissionInfo(parsedPackage.permissions, "signature_or_privileged_permission").protectionLevel;
    assertThat(protectionLevel).isEqualTo(PermissionInfo.PROTECTION_SIGNATURE | PermissionInfo.PROTECTION_FLAG_PRIVILEGED);
}
Also used : Package(android.content.pm.PackageParser.Package) Test(org.junit.Test)

Example 9 with Package

use of android.content.pm.PackageParser.Package in project robolectric by robolectric.

the class AndroidTestEnvironment method installAndCreateApplication.

private Application installAndCreateApplication(AndroidManifest appManifest, Config config, android.content.res.Configuration androidConfiguration, DisplayMetrics displayMetrics, ShadowActivityThread shadowActivityThread, _ActivityThread_ activityThreadReflector, Instrumentation androidInstrumentation) {
    ActivityThread activityThread = (ActivityThread) RuntimeEnvironment.getActivityThread();
    Context systemContextImpl = reflector(_ContextImpl_.class).createSystemContext(activityThread);
    RuntimeEnvironment.systemContext = systemContextImpl;
    Application dummyInitialApplication = new Application();
    activityThreadReflector.setInitialApplication(dummyInitialApplication);
    ShadowApplication shadowInitialApplication = Shadow.extract(dummyInitialApplication);
    shadowInitialApplication.callAttach(systemContextImpl);
    Package parsedPackage = loadAppPackage(config, appManifest);
    ApplicationInfo applicationInfo = parsedPackage.applicationInfo;
    ComponentName actualComponentName = new ComponentName(applicationInfo.packageName, androidInstrumentation.getClass().getSimpleName());
    ReflectionHelpers.setField(androidInstrumentation, "mComponent", actualComponentName);
    // unclear why, but prior to P the processName wasn't set
    if (apiLevel < P && applicationInfo.processName == null) {
        applicationInfo.processName = parsedPackage.packageName;
    }
    setUpPackageStorage(applicationInfo, parsedPackage);
    // Bit of a hack... Context.createPackageContext() is called before the application is created.
    // It calls through
    // to ActivityThread for the package which in turn calls the PackageManagerService directly.
    // This works for now
    // but it might be nicer to have ShadowPackageManager implementation move into the service as
    // there is also lots of
    // code in there that can be reusable, e.g: the XxxxIntentResolver code.
    ShadowActivityThread.setApplicationInfo(applicationInfo);
    shadowActivityThread.setCompatConfiguration(androidConfiguration);
    Bootstrap.setUpDisplay();
    activityThread.applyConfigurationToResources(androidConfiguration);
    Application application = createApplication(appManifest, config, applicationInfo);
    RuntimeEnvironment.setConfiguredApplicationClass(application.getClass());
    RuntimeEnvironment.application = application;
    if (application != null) {
        final Class<?> appBindDataClass;
        try {
            appBindDataClass = Class.forName("android.app.ActivityThread$AppBindData");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        final Object appBindData = ReflectionHelpers.newInstance(appBindDataClass);
        final _AppBindData_ _appBindData_ = reflector(_AppBindData_.class, appBindData);
        _appBindData_.setProcessName(parsedPackage.packageName);
        _appBindData_.setAppInfo(applicationInfo);
        activityThreadReflector.setBoundApplication(appBindData);
        final LoadedApk loadedApk = activityThread.getPackageInfo(applicationInfo, null, Context.CONTEXT_INCLUDE_CODE);
        final _LoadedApk_ _loadedApk_ = reflector(_LoadedApk_.class, loadedApk);
        Context contextImpl;
        if (apiLevel >= VERSION_CODES.LOLLIPOP) {
            contextImpl = reflector(_ContextImpl_.class).createAppContext(activityThread, loadedApk);
        } else {
            try {
                contextImpl = systemContextImpl.createPackageContext(applicationInfo.packageName, Context.CONTEXT_INCLUDE_CODE);
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        ShadowPackageManager shadowPackageManager = Shadow.extract(contextImpl.getPackageManager());
        shadowPackageManager.addPackageInternal(parsedPackage);
        activityThreadReflector.setInitialApplication(application);
        ShadowApplication shadowApplication = Shadow.extract(application);
        shadowApplication.callAttach(contextImpl);
        reflector(_ContextImpl_.class, contextImpl).setOuterContext(application);
        if (apiLevel >= VERSION_CODES.O) {
            reflector(_ContextImpl_.class, contextImpl).setClassLoader(this.getClass().getClassLoader());
        }
        Resources appResources = application.getResources();
        _loadedApk_.setResources(appResources);
        _loadedApk_.setApplication(application);
        if (RuntimeEnvironment.getApiLevel() >= VERSION_CODES.O) {
            // Preload fonts resources
            FontsContract.setApplicationContextForResources(application);
        }
        registerBroadcastReceivers(application, appManifest);
        appResources.updateConfiguration(androidConfiguration, displayMetrics);
        // propagate any updates to configuration via RuntimeEnvironment.setQualifiers
        Bootstrap.updateConfiguration(appResources);
        if (ShadowAssetManager.useLegacy()) {
            populateAssetPaths(appResources.getAssets(), appManifest);
        }
        PerfStatsCollector.getInstance().measure("application onCreate()", () -> application.onCreate());
    }
    return application;
}
Also used : Context(android.content.Context) LoadedApk(android.app.LoadedApk) ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) ShadowActivityThread(org.robolectric.shadows.ShadowActivityThread) ActivityThread(android.app.ActivityThread) ShadowContextImpl._ContextImpl_(org.robolectric.shadows.ShadowContextImpl._ContextImpl_) ShadowLoadedApk._LoadedApk_(org.robolectric.shadows.ShadowLoadedApk._LoadedApk_) PackageManager(android.content.pm.PackageManager) ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) ComponentName(android.content.ComponentName) Package(android.content.pm.PackageParser.Package) Resources(android.content.res.Resources) ShadowApplication(org.robolectric.shadows.ShadowApplication) ShadowApplication(org.robolectric.shadows.ShadowApplication) Application(android.app.Application) ShadowActivityThread._AppBindData_(org.robolectric.shadows.ShadowActivityThread._AppBindData_)

Aggregations

Package (android.content.pm.PackageParser.Package)9 Test (org.junit.Test)5 ApplicationInfo (android.content.pm.ApplicationInfo)2 PermissionGroup (android.content.pm.PackageParser.PermissionGroup)2 ActivityThread (android.app.ActivityThread)1 Application (android.app.Application)1 LoadedApk (android.app.LoadedApk)1 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 ActivityInfo (android.content.pm.ActivityInfo)1 PackageManager (android.content.pm.PackageManager)1 PackageParser (android.content.pm.PackageParser)1 Activity (android.content.pm.PackageParser.Activity)1 ActivityIntentInfo (android.content.pm.PackageParser.ActivityIntentInfo)1 Permission (android.content.pm.PackageParser.Permission)1 Service (android.content.pm.PackageParser.Service)1 ServiceIntentInfo (android.content.pm.PackageParser.ServiceIntentInfo)1 PathPermission (android.content.pm.PathPermission)1 PermissionGroupInfo (android.content.pm.PermissionGroupInfo)1 ProviderInfo (android.content.pm.ProviderInfo)1