Search in sources :

Example 6 with Scheduler

use of org.robolectric.util.Scheduler in project edx-app-android by edx.

the class BaseFragmentActivityTest method assertAnimateLayouts.

/**
 * Generic method for asserting view animation method functionality
 *
 * @param view    The animated view
 * @param trigger A {@link Runnable} that triggers the animation
 */
protected void assertAnimateLayouts(View view, Runnable trigger) {
    // The foreground scheduler needs to be paused so that the
    // temporary visibility of the animated View can be verified.
    Scheduler foregroundScheduler = ShadowApplication.getInstance().getForegroundThreadScheduler();
    boolean wasPaused = foregroundScheduler.isPaused();
    if (!wasPaused) {
        foregroundScheduler.pause();
    }
    assertThat(view).isGone();
    trigger.run();
    assertThat(view).isVisible();
    Animation animation = view.getAnimation();
    assertNotNull(animation);
    assertThat(animation.getStartTime()).isLessThanOrEqualTo(AnimationUtils.currentAnimationTimeMillis());
    assertThat(animation).hasStartOffset(0);
    foregroundScheduler.unPause();
    assertThat(view).isGone();
    if (wasPaused) {
        foregroundScheduler.pause();
    }
}
Also used : Scheduler(org.robolectric.util.Scheduler) Animation(android.view.animation.Animation)

Example 7 with Scheduler

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

the class OneSignalPackagePrivateHelper method runAllNetworkRunnables.

public static boolean runAllNetworkRunnables() throws Exception {
    startedRunnable = false;
    RunnableArg runnable = new RunnableArg<UserStateSynchronizer.NetworkHandlerThread>() {

        @Override
        void run(UserStateSynchronizer.NetworkHandlerThread handlerThread) throws Exception {
            synchronized (handlerThread.mHandler) {
                Scheduler scheduler = shadowOf(handlerThread.getLooper()).getScheduler();
                while (scheduler.runOneTask()) startedRunnable = true;
            }
        }
    };
    processNetworkHandles(runnable);
    return startedRunnable;
}
Also used : Scheduler(org.robolectric.util.Scheduler)

Example 8 with Scheduler

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

the class TestHelpers method flushBufferedSharedPrefs.

static void flushBufferedSharedPrefs() {
    TestOneSignalPrefs.WritePrefHandlerThread handlerThread = TestOneSignalPrefs.prefsHandler;
    if (handlerThread.getLooper() == null)
        return;
    Scheduler scheduler = shadowOf(handlerThread.getLooper()).getScheduler();
    while (scheduler.runOneTask()) ;
}
Also used : Scheduler(org.robolectric.util.Scheduler) JobScheduler(android.app.job.JobScheduler) TestOneSignalPrefs(com.onesignal.OneSignalPackagePrivateHelper.TestOneSignalPrefs)

Example 9 with Scheduler

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

the class AndroidTestEnvironment method setUpApplicationState.

@Override
public void setUpApplicationState(Method method, Configuration configuration, AndroidManifest appManifest) {
    for (TestEnvironmentLifecyclePlugin e : testEnvironmentLifecyclePlugins) {
        e.onSetupApplicationState();
    }
    Config config = configuration.get(Config.class);
    ConfigurationRegistry.instance = new ConfigurationRegistry(configuration.map());
    clearEnvironment();
    RuntimeEnvironment.setTempDirectory(new TempDirectory(createTestDataDirRootPath(method)));
    if (ShadowLooper.looperMode() == LooperMode.Mode.LEGACY) {
        RuntimeEnvironment.setMasterScheduler(new Scheduler());
        RuntimeEnvironment.setMainThread(Thread.currentThread());
        ShadowLegacyLooper.internalInitializeBackgroundThreadScheduler();
    }
    if (!loggingInitialized) {
        ShadowLog.setupLogging();
        loggingInitialized = true;
    }
    if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
        Security.addProvider(new BouncyCastleProvider());
    }
    android.content.res.Configuration androidConfiguration = new android.content.res.Configuration();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    Bootstrap.applyQualifiers(config.qualifiers(), apiLevel, androidConfiguration, displayMetrics);
    Locale locale = apiLevel >= VERSION_CODES.N ? androidConfiguration.getLocales().get(0) : androidConfiguration.locale;
    Locale.setDefault(locale);
    // Looper needs to be prepared before the activity thread is created
    if (Looper.myLooper() == null) {
        Looper.prepareMainLooper();
    }
    if (ShadowLooper.looperMode() == LooperMode.Mode.LEGACY) {
        ShadowLooper.getShadowMainLooper().resetScheduler();
    } else {
        RuntimeEnvironment.setMasterScheduler(new LooperDelegatingScheduler(Looper.getMainLooper()));
    }
    preloadClasses(apiLevel);
    RuntimeEnvironment.setAndroidFrameworkJarPath(sdkJarPath);
    Bootstrap.setDisplayConfiguration(androidConfiguration, displayMetrics);
    RuntimeEnvironment.setActivityThread(ReflectionHelpers.newInstance(ActivityThread.class));
    ReflectionHelpers.setStaticField(ActivityThread.class, "sMainThreadHandler", new Handler(Looper.myLooper()));
    Instrumentation instrumentation = createInstrumentation();
    InstrumentationRegistry.registerInstance(instrumentation, new Bundle());
    Supplier<Application> applicationSupplier = createApplicationSupplier(appManifest, config, androidConfiguration, displayMetrics);
    RuntimeEnvironment.setApplicationSupplier(applicationSupplier);
    if (configuration.get(LazyLoad.class) == LazyLoad.ON) {
        RuntimeEnvironment.setConfiguredApplicationClass(getApplicationClass(appManifest, config, new ApplicationInfo()));
    } else {
        // force eager load of the application
        RuntimeEnvironment.getApplication();
    }
}
Also used : Locale(java.util.Locale) Configuration(org.robolectric.pluginapi.config.ConfigurationStrategy.Configuration) Config(org.robolectric.annotation.Config) Scheduler(org.robolectric.util.Scheduler) Bundle(android.os.Bundle) LazyLoad(org.robolectric.annotation.experimental.LazyApplication.LazyLoad) ApplicationInfo(android.content.pm.ApplicationInfo) ConfigurationRegistry(org.robolectric.config.ConfigurationRegistry) Handler(android.os.Handler) Instrumentation(android.app.Instrumentation) ShadowInstrumentation(org.robolectric.shadows.ShadowInstrumentation) ShadowActivityThread(org.robolectric.shadows.ShadowActivityThread) ActivityThread(android.app.ActivityThread) DisplayMetrics(android.util.DisplayMetrics) TestEnvironmentLifecyclePlugin(org.robolectric.pluginapi.TestEnvironmentLifecyclePlugin) TempDirectory(org.robolectric.util.TempDirectory) ShadowApplication(org.robolectric.shadows.ShadowApplication) Application(android.app.Application) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 10 with Scheduler

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

the class ShadowLegacyLooperTest method soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler.

@Test
public void soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler() {
    Looper mainLooper = Looper.getMainLooper();
    Scheduler scheduler = shadowOf(mainLooper).getScheduler();
    ShadowLegacyLooper shadowLooper = Shadow.extract(mainLooper);
    shadowLooper.quit = true;
    assertThat(ApplicationProvider.getApplicationContext().getMainLooper()).isSameInstanceAs(mainLooper);
    Scheduler s = new Scheduler();
    RuntimeEnvironment.setMasterScheduler(s);
    ShadowLooper.resetThreadLoopers();
    Application application = new Application();
    ReflectionHelpers.callInstanceMethod(application, "attach", ReflectionHelpers.ClassParameter.from(Context.class, ((Application) ApplicationProvider.getApplicationContext()).getBaseContext()));
    assertWithMessage("Looper.getMainLooper()").that(Looper.getMainLooper()).isSameInstanceAs(mainLooper);
    assertWithMessage("app.getMainLooper()").that(application.getMainLooper()).isSameInstanceAs(mainLooper);
    assertWithMessage("scheduler").that(shadowOf(mainLooper).getScheduler()).isNotSameInstanceAs(scheduler);
    assertWithMessage("scheduler").that(shadowOf(mainLooper).getScheduler()).isSameInstanceAs(s);
    assertWithMessage("quit").that(shadowOf(mainLooper).hasQuit()).isFalse();
}
Also used : Context(android.content.Context) ShadowLooper.shadowMainLooper(org.robolectric.shadows.ShadowLooper.shadowMainLooper) Looper(android.os.Looper) Scheduler(org.robolectric.util.Scheduler) Application(android.app.Application) Test(org.junit.Test)

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