Search in sources :

Example 1 with ShadowContextImpl._ContextImpl_

use of org.robolectric.shadows.ShadowContextImpl._ContextImpl_ in project robolectric by robolectric.

the class ShadowActivity method callAttach.

public void callAttach(Intent intent, @Nullable Bundle activityOptions, @Nullable @WithType("android.app.Activity$NonConfigurationInstances") Object lastNonConfigurationInstances) {
    Application application = RuntimeEnvironment.getApplication();
    Context baseContext = application.getBaseContext();
    ComponentName componentName = new ComponentName(application.getPackageName(), realActivity.getClass().getName());
    ActivityInfo activityInfo;
    PackageManager packageManager = application.getPackageManager();
    shadowOf(packageManager).addActivityIfNotPresent(componentName);
    try {
        activityInfo = packageManager.getActivityInfo(componentName, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException e) {
        throw new RuntimeException("Activity is not resolved even if we made sure it exists", e);
    }
    CharSequence activityTitle = activityInfo.loadLabel(baseContext.getPackageManager());
    ActivityThread activityThread = (ActivityThread) RuntimeEnvironment.getActivityThread();
    Instrumentation instrumentation = activityThread.getInstrumentation();
    // on the application, meaning non-static shadow system service state will not be shared.
    if (activityOptions != null && VERSION.SDK_INT >= VERSION_CODES.O) {
        int displayId = ActivityOptions.fromBundle(activityOptions).getLaunchDisplayId();
        if (displayId != Display.DEFAULT_DISPLAY && // INVALID_DISPLAY indicates no display was set in the options bundle
        displayId != Display.INVALID_DISPLAY) {
            LoadedApk loadedApk = activityThread.getPackageInfo(ShadowActivityThread.getApplicationInfo(), null, Context.CONTEXT_INCLUDE_CODE);
            _LoadedApk_ loadedApkReflector = reflector(_LoadedApk_.class, loadedApk);
            loadedApkReflector.setResources(application.getResources());
            loadedApkReflector.setApplication(application);
            baseContext = reflector(_ContextImpl_.class).createActivityContext(activityThread, loadedApk, activityInfo, reflector(_ContextImpl_.class, baseContext).getActivityToken(), displayId, /* overrideConfiguration= */
            application.getResources().getConfiguration());
            reflector(_ContextImpl_.class, baseContext).setOuterContext(realActivity);
        }
    }
    reflector(_Activity_.class, realActivity).callAttach(realActivity, baseContext, activityThread, instrumentation, application, intent, activityInfo, activityTitle, lastNonConfigurationInstances);
    int theme = activityInfo.getThemeResource();
    if (theme != 0) {
        realActivity.setTheme(theme);
    }
}
Also used : Context(android.content.Context) ActivityInfo(android.content.pm.ActivityInfo) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) LoadedApk(android.app.LoadedApk) Instrumentation(android.app.Instrumentation) ActivityThread(android.app.ActivityThread) ShadowContextImpl._ContextImpl_(org.robolectric.shadows.ShadowContextImpl._ContextImpl_) PackageManager(android.content.pm.PackageManager) ShadowLoadedApk._LoadedApk_(org.robolectric.shadows.ShadowLoadedApk._LoadedApk_) ComponentName(android.content.ComponentName) Application(android.app.Application)

Example 2 with ShadowContextImpl._ContextImpl_

use of org.robolectric.shadows.ShadowContextImpl._ContextImpl_ 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

ActivityThread (android.app.ActivityThread)2 Application (android.app.Application)2 LoadedApk (android.app.LoadedApk)2 ComponentName (android.content.ComponentName)2 Context (android.content.Context)2 PackageManager (android.content.pm.PackageManager)2 ShadowContextImpl._ContextImpl_ (org.robolectric.shadows.ShadowContextImpl._ContextImpl_)2 ShadowLoadedApk._LoadedApk_ (org.robolectric.shadows.ShadowLoadedApk._LoadedApk_)2 Instrumentation (android.app.Instrumentation)1 ActivityInfo (android.content.pm.ActivityInfo)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Package (android.content.pm.PackageParser.Package)1 Resources (android.content.res.Resources)1 ShadowActivityThread (org.robolectric.shadows.ShadowActivityThread)1 ShadowActivityThread._AppBindData_ (org.robolectric.shadows.ShadowActivityThread._AppBindData_)1 ShadowApplication (org.robolectric.shadows.ShadowApplication)1 ShadowPackageManager (org.robolectric.shadows.ShadowPackageManager)1