Search in sources :

Example 1 with Scheduler

use of org.robolectric.util.Scheduler in project OneSignal-Android-SDK by OneSignal.

the class OneSignalPackagePrivateHelper method runFocusRunnables.

public static boolean runFocusRunnables() {
    Looper looper = ActivityLifecycleHandler.focusHandlerThread.getHandlerLooper();
    if (looper == null)
        return false;
    Scheduler scheduler = shadowOf(looper).getScheduler();
    if (scheduler == null)
        return false;
    return scheduler.advanceToLastPostedRunnable();
}
Also used : Looper(android.os.Looper) Scheduler(org.robolectric.util.Scheduler)

Example 2 with Scheduler

use of org.robolectric.util.Scheduler in project robolectric by robolectric.

the class ParallelUniverse method setUpApplicationState.

@Override
public void setUpApplicationState(Method method, TestLifecycle testLifecycle, AndroidManifest appManifest, Config config, ResourceTable compileTimeResourceTable, ResourceTable appResourceTable, ResourceTable systemResourceTable) {
    ReflectionHelpers.setStaticField(RuntimeEnvironment.class, "apiLevel", sdkConfig.getApiLevel());
    RuntimeEnvironment.application = null;
    RuntimeEnvironment.setMasterScheduler(new Scheduler());
    RuntimeEnvironment.setMainThread(Thread.currentThread());
    DefaultPackageManager packageManager = new DefaultPackageManager();
    RuntimeEnvironment.setRobolectricPackageManager(packageManager);
    RuntimeEnvironment.setCompileTimeResourceTable(compileTimeResourceTable);
    RuntimeEnvironment.setAppResourceTable(appResourceTable);
    RuntimeEnvironment.setSystemResourceTable(systemResourceTable);
    initializeAppManifest(appManifest, appResourceTable, packageManager);
    packageManager.setDependencies(appManifest, appResourceTable);
    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        Security.insertProviderAt(new BouncyCastleProvider(), 1);
    }
    String qualifiers = Qualifiers.addPlatformVersion(config.qualifiers(), sdkConfig.getApiLevel());
    qualifiers = Qualifiers.addSmallestScreenWidth(qualifiers, 320);
    qualifiers = Qualifiers.addScreenWidth(qualifiers, 320);
    Resources systemResources = Resources.getSystem();
    Configuration configuration = systemResources.getConfiguration();
    configuration.smallestScreenWidthDp = Qualifiers.getSmallestScreenWidth(qualifiers);
    configuration.screenWidthDp = Qualifiers.getScreenWidth(qualifiers);
    systemResources.updateConfiguration(configuration, systemResources.getDisplayMetrics());
    RuntimeEnvironment.setQualifiers(qualifiers);
    Class<?> contextImplClass = ReflectionHelpers.loadClass(getClass().getClassLoader(), shadowsAdapter.getShadowContextImplClassName());
    Class<?> activityThreadClass = ReflectionHelpers.loadClass(getClass().getClassLoader(), shadowsAdapter.getShadowActivityThreadClassName());
    // Looper needs to be prepared before the activity thread is created
    if (Looper.myLooper() == null) {
        Looper.prepareMainLooper();
    }
    ShadowLooper.getShadowMainLooper().resetScheduler();
    Object activityThread = ReflectionHelpers.newInstance(activityThreadClass);
    RuntimeEnvironment.setActivityThread(activityThread);
    ReflectionHelpers.setField(activityThread, "mInstrumentation", new RoboInstrumentation());
    ReflectionHelpers.setField(activityThread, "mCompatConfiguration", configuration);
    Context systemContextImpl = ReflectionHelpers.callStaticMethod(contextImplClass, "createSystemContext", ClassParameter.from(activityThreadClass, activityThread));
    final Application application = (Application) testLifecycle.createApplication(method, appManifest, config);
    RuntimeEnvironment.application = application;
    if (application != null) {
        shadowsAdapter.bind(application, appManifest);
        ApplicationInfo applicationInfo;
        try {
            applicationInfo = packageManager.getApplicationInfo(appManifest.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        Class<?> compatibilityInfoClass = ReflectionHelpers.loadClass(getClass().getClassLoader(), "android.content.res.CompatibilityInfo");
        LoadedApk loadedApk = ReflectionHelpers.callInstanceMethod(activityThread, "getPackageInfo", ClassParameter.from(ApplicationInfo.class, applicationInfo), ClassParameter.from(compatibilityInfoClass, null), ClassParameter.from(int.class, Context.CONTEXT_INCLUDE_CODE));
        try {
            Context contextImpl = systemContextImpl.createPackageContext(applicationInfo.packageName, Context.CONTEXT_INCLUDE_CODE);
            ReflectionHelpers.setField(activityThreadClass, activityThread, "mInitialApplication", application);
            ApplicationTestUtil.attach(application, contextImpl);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        Resources appResources = application.getResources();
        ReflectionHelpers.setField(loadedApk, "mResources", appResources);
        ReflectionHelpers.setField(loadedApk, "mApplication", application);
        appResources.updateConfiguration(configuration, appResources.getDisplayMetrics());
        application.onCreate();
    }
}
Also used : Context(android.content.Context) Configuration(android.content.res.Configuration) LoadedApk(android.app.LoadedApk) Scheduler(org.robolectric.util.Scheduler) RoboInstrumentation(org.robolectric.android.fakes.RoboInstrumentation) DefaultPackageManager(org.robolectric.res.builder.DefaultPackageManager) ApplicationInfo(android.content.pm.ApplicationInfo) PackageManager(android.content.pm.PackageManager) DefaultPackageManager(org.robolectric.res.builder.DefaultPackageManager) Resources(android.content.res.Resources) Application(android.app.Application) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 3 with Scheduler

use of org.robolectric.util.Scheduler in project robolectric by robolectric.

the class ShadowLooperTest method shouldSetNewScheduler_whenLooperIsReset.

@Test
public void shouldSetNewScheduler_whenLooperIsReset() {
    HandlerThread ht = getHandlerThread();
    Looper looper = ht.getLooper();
    ShadowLooper sLooper = shadowOf(looper);
    Scheduler old = sLooper.getScheduler();
    sLooper.reset();
    assertThat(old).isNotSameAs(sLooper.getScheduler());
}
Also used : Looper(android.os.Looper) HandlerThread(android.os.HandlerThread) Scheduler(org.robolectric.util.Scheduler) Test(org.junit.Test)

Example 4 with Scheduler

use of org.robolectric.util.Scheduler in project robolectric by robolectric.

the class ShadowLooperTest method reset_setsGlobalScheduler_forMainLooper_byDefault.

@Test
public void reset_setsGlobalScheduler_forMainLooper_byDefault() {
    ShadowLooper sMainLooper = ShadowLooper.getShadowMainLooper();
    Scheduler s = new Scheduler();
    RuntimeEnvironment.setMasterScheduler(s);
    sMainLooper.reset();
    assertThat(sMainLooper.getScheduler()).isSameAs(s);
}
Also used : Scheduler(org.robolectric.util.Scheduler) Test(org.junit.Test)

Example 5 with Scheduler

use of org.robolectric.util.Scheduler in project robolectric by robolectric.

the class ShadowMessageTest method recycle_shouldRemoveMessageFromScheduler.

private void recycle_shouldRemoveMessageFromScheduler() {
    ShadowLooper.pauseMainLooper();
    Handler h = new Handler();
    Message msg = Message.obtain(h, 234);
    msg.sendToTarget();
    Scheduler scheduler = Robolectric.getForegroundThreadScheduler();
    assertThat(scheduler.size()).as("before recycle").isEqualTo(1);
    shadowOf(msg).recycleUnchecked();
    assertThat(scheduler.size()).as("after recycle").isEqualTo(0);
}
Also used : Message(android.os.Message) Scheduler(org.robolectric.util.Scheduler) Handler(android.os.Handler)

Aggregations

Scheduler (org.robolectric.util.Scheduler)30 Test (org.junit.Test)22 HandlerThread (android.os.HandlerThread)8 Handler (android.os.Handler)6 Looper (android.os.Looper)6 LooperMode (org.robolectric.annotation.LooperMode)5 Application (android.app.Application)4 Context (android.content.Context)3 ApplicationInfo (android.content.pm.ApplicationInfo)2 Message (android.os.Message)2 ArrayList (java.util.ArrayList)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 ShadowLooper.shadowMainLooper (org.robolectric.shadows.ShadowLooper.shadowMainLooper)2 ActivityThread (android.app.ActivityThread)1 Instrumentation (android.app.Instrumentation)1 LoadedApk (android.app.LoadedApk)1 JobScheduler (android.app.job.JobScheduler)1 PackageManager (android.content.pm.PackageManager)1 Configuration (android.content.res.Configuration)1 Resources (android.content.res.Resources)1