Search in sources :

Example 6 with ShadowLooper

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

the class LooperModeConfigurerClassTest method shouldUseRealisticShadows.

@Test
@LooperMode(Mode.PAUSED)
public void shouldUseRealisticShadows() {
    assertThat(ConfigurationRegistry.get(LooperMode.Mode.class)).isSameInstanceAs(Mode.PAUSED);
    ShadowLooper looper = Shadow.extract(Looper.getMainLooper());
    assertThat(looper).isInstanceOf(ShadowPausedLooper.class);
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) LooperMode(org.robolectric.annotation.LooperMode) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Example 7 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.

the class AndroidCallableTest method shouldCallOnExceptionForExceptionInDoInBackground.

@Test
public void shouldCallOnExceptionForExceptionInDoInBackground() {
    final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
    final boolean[] callRecord = new boolean[] { false, false, false, false };
    final boolean[] correctAnswer = new boolean[] { true, false, true, true };
    Executors.newSingleThreadExecutor().submit(new AndroidCallable<String>() {

        @Override
        public void onPreCall() {
            callRecord[0] = true;
        }

        @Override
        public String doInBackground() throws Exception {
            throw new Exception();
        }

        @Override
        public void onSuccess(String result) {
            callRecord[1] = true;
        }

        @Override
        public void onException(Exception e) {
            callRecord[2] = true;
        }

        @Override
        public void onFinally() {
            callRecord[3] = true;
        }
    });
    // Run all the pending tasks on the ui thread
    while (!callRecord[callRecord.length - 1]) looper.runToEndOfTasks();
    assertThat(callRecord, equalTo(correctAnswer));
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) Test(org.junit.Test)

Example 8 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.

the class AndroidCallableTest method shouldHaveCorrectStackTrace.

@Test
public void shouldHaveCorrectStackTrace() {
    final Exception[] exception = { null };
    final StackTraceElement[] here;
    final StackTraceElement[][] there = new StackTraceElement[][] { null };
    final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
    Ln.setLoggingLevel(Log.DEBUG);
    try {
        throw new UnsupportedOperationException();
    } catch (UnsupportedOperationException e) {
        here = e.getStackTrace();
    }
    Executors.newSingleThreadExecutor(new MyThreadFactory(new Thread[] { null })).submit(new AndroidCallable<String>() {

        @Override
        public String doInBackground() throws Exception {
            try {
                throw new NullPointerException();
            } catch (NullPointerException e) {
                there[0] = e.getStackTrace();
                throw e;
            }
        }

        @Override
        public void onException(Exception e) {
            exception[0] = e;
        }

        @Override
        public void onSuccess(String result) {
        }
    }, false);
    // Run all the pending tasks on the ui thread
    while (exception[0] == null) looper.runToEndOfTasks();
    assertThat(exception[0].getStackTrace().length, equalTo(here.length + there[0].length + 3));
    int i = 0;
    while (i < there[0].length) {
        assertThat(exception[0].getStackTrace()[i], equalTo(there[0][i]));
        ++i;
    }
    for (// skip 3 frames due to differences in where we got our stacktrace from
    int j = i + 3, k = 0; // skip 3 frames due to differences in where we got our stacktrace from
    k < here.length; // skip 3 frames due to differences in where we got our stacktrace from
    ++k, // skip 3 frames due to differences in where we got our stacktrace from
    ++j) // line numbers may be off
    assertThat(exception[0].getStackTrace()[j].getFileName(), equalTo(here[k].getFileName()));
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) Test(org.junit.Test)

Example 9 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.

the class AndroidCallableTest method shouldCrashAppForExceptionInOnFinally.

@Test(expected = RuntimeException.class)
public void shouldCrashAppForExceptionInOnFinally() {
    final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
    Executors.newSingleThreadExecutor().submit(new AndroidCallable<String>() {

        @Override
        public String doInBackground() throws Exception {
            return "";
        }

        @Override
        public void onSuccess(String result) {
            throw new RuntimeException();
        }

        @Override
        public void onException(Exception e) {
        }

        @Override
        public void onFinally() {
            throw new RuntimeException();
        }
    });
    // noinspection InfiniteLoopStatement
    while (true) looper.runToEndOfTasks();
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) Test(org.junit.Test)

Example 10 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.

the class AndroidCallableTest method shouldCallMethodsUsingProperThreadsWithException.

@Test
public void shouldCallMethodsUsingProperThreadsWithException() throws Exception {
    final Thread fgThread = Thread.currentThread();
    final Thread[] bgThread = { null };
    final Thread[] answers = new Thread[5];
    final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
    Executors.newSingleThreadExecutor(new MyThreadFactory(bgThread)).submit(new StringAndroidCallable(answers, true));
    // Run all the pending tasks on the ui thread
    while (answers[answers.length - 1] == null) looper.runToEndOfTasks();
    final Thread[] correctAnswer = new Thread[] { fgThread, bgThread[0], fgThread, null, fgThread };
    assertThat(answers, equalTo(correctAnswer));
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) Test(org.junit.Test)

Aggregations

ShadowLooper (org.robolectric.shadows.ShadowLooper)66 Test (org.junit.Test)56 BackgroundThreadExecutor (org.prebid.mobile.tasksmanager.BackgroundThreadExecutor)42 HttpUrl (okhttp3.HttpUrl)35 MockResponse (okhttp3.mockwebserver.MockResponse)35 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)23 HashSet (java.util.HashSet)10 AdManagerAdRequest (com.google.android.gms.ads.admanager.AdManagerAdRequest)8 MoPubView (com.mopub.mobileads.MoPubView)7 Bundle (android.os.Bundle)5 Handler (android.os.Handler)4 MediaCodec (android.media.MediaCodec)3 Looper (android.os.Looper)3 Message (android.os.Message)3 NativeCustomTemplateAd (com.google.android.gms.ads.formats.NativeCustomTemplateAd)3 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)3 Scheduler (io.reactivex.rxjava3.core.Scheduler)3 Field (java.lang.reflect.Field)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3