Search in sources :

Example 6 with ShadowPackageManager

use of org.robolectric.shadows.ShadowPackageManager in project android_packages_apps_Settings by omnirom.

the class LiveCaptionPreferenceControllerTest method getAvailabilityStatus_canResolveIntent_shouldReturnAvailable.

@Test
public void getAvailabilityStatus_canResolveIntent_shouldReturnAvailable() {
    final ShadowPackageManager pm = Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager());
    pm.addResolveInfoForIntent(LiveCaptionPreferenceController.LIVE_CAPTION_INTENT, new ResolveInfo());
    assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) Test(org.junit.Test)

Example 7 with ShadowPackageManager

use of org.robolectric.shadows.ShadowPackageManager in project robolectric by robolectric.

the class ActivityController method attach.

private ActivityController<T> attach(@Nullable Bundle activityOptions, @Nullable @WithType("android.app.Activity$NonConfigurationInstances") Object lastNonConfigurationInstances) {
    if (attached) {
        return this;
    }
    // make sure the component is enabled
    Context context = RuntimeEnvironment.getApplication().getBaseContext();
    PackageManager packageManager = context.getPackageManager();
    ComponentName componentName = new ComponentName(context.getPackageName(), this.component.getClass().getName());
    ((ShadowPackageManager) extract(packageManager)).addActivityIfNotPresent(componentName);
    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
    ShadowActivity shadowActivity = Shadow.extract(component);
    shadowActivity.callAttach(getIntent(), activityOptions, lastNonConfigurationInstances);
    shadowActivity.attachController(this);
    attached = true;
    return this;
}
Also used : Context(android.content.Context) PackageManager(android.content.pm.PackageManager) ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) ShadowActivity(org.robolectric.shadows.ShadowActivity) ComponentName(android.content.ComponentName)

Example 8 with ShadowPackageManager

use of org.robolectric.shadows.ShadowPackageManager 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_)

Example 9 with ShadowPackageManager

use of org.robolectric.shadows.ShadowPackageManager in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class AvatarViewMixinTest method queryProviderAuthority_useNewShadowPackagteManager_returnAuthority.

@Test
public void queryProviderAuthority_useNewShadowPackagteManager_returnAuthority() {
    final AvatarViewMixin avatarViewMixin = new AvatarViewMixin(mActivity, mImageView);
    ShadowPackageManager shadowPackageManager = Shadow.extract(mContext.getPackageManager());
    final PackageInfo accountProvider = new PackageInfo();
    accountProvider.packageName = "test.pkg";
    accountProvider.applicationInfo = new ApplicationInfo();
    accountProvider.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
    accountProvider.applicationInfo.packageName = accountProvider.packageName;
    accountProvider.providers = new ProviderInfo[1];
    accountProvider.providers[0] = new ProviderInfo();
    accountProvider.providers[0].authority = DUMMY_AUTHORITY;
    accountProvider.providers[0].packageName = accountProvider.packageName;
    accountProvider.providers[0].name = "test.class";
    accountProvider.providers[0].applicationInfo = accountProvider.applicationInfo;
    final ResolveInfo resolveInfo = new ResolveInfo();
    resolveInfo.providerInfo = accountProvider.providers[0];
    shadowPackageManager.addResolveInfoForIntent(AvatarViewMixin.INTENT_GET_ACCOUNT_DATA, resolveInfo);
    assertThat(avatarViewMixin.queryProviderAuthority()).isEqualTo(DUMMY_AUTHORITY);
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ProviderInfo(android.content.pm.ProviderInfo) ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) PackageInfo(android.content.pm.PackageInfo) ApplicationInfo(android.content.pm.ApplicationInfo) BatteryFixSliceTest(com.android.settings.homepage.contextualcards.slices.BatteryFixSliceTest) Test(org.junit.Test)

Example 10 with ShadowPackageManager

use of org.robolectric.shadows.ShadowPackageManager in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class LiveCaptionPreferenceControllerTest method getAvailabilityStatus_noResolveIntent_shouldReturnUnavailable.

@Test
public void getAvailabilityStatus_noResolveIntent_shouldReturnUnavailable() {
    final ShadowPackageManager pm = Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager());
    pm.setResolveInfosForIntent(LiveCaptionPreferenceController.LIVE_CAPTION_INTENT, Collections.emptyList());
    assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
Also used : ShadowPackageManager(org.robolectric.shadows.ShadowPackageManager) Test(org.junit.Test)

Aggregations

ShadowPackageManager (org.robolectric.shadows.ShadowPackageManager)17 Test (org.junit.Test)9 ResolveInfo (android.content.pm.ResolveInfo)7 ApplicationInfo (android.content.pm.ApplicationInfo)5 ComponentName (android.content.ComponentName)4 Before (org.junit.Before)4 Intent (android.content.Intent)3 PackageInfo (android.content.pm.PackageInfo)3 PackageManager (android.content.pm.PackageManager)3 ServiceInfo (android.content.pm.ServiceInfo)3 Context (android.content.Context)2 ProviderInfo (android.content.pm.ProviderInfo)2 BatteryFixSliceTest (com.android.settings.homepage.contextualcards.slices.BatteryFixSliceTest)2 ActivityThread (android.app.ActivityThread)1 Application (android.app.Application)1 LoadedApk (android.app.LoadedApk)1 Package (android.content.pm.PackageParser.Package)1 Resources (android.content.res.Resources)1 Bundle (android.os.Bundle)1 Lifecycle (com.android.settingslib.core.lifecycle.Lifecycle)1