Search in sources :

Example 46 with ShadowLooper

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

the class AndroidCallableTest method shouldCrashAppForExceptionInOnSuccess.

@Test(expected = RuntimeException.class)
public void shouldCrashAppForExceptionInOnSuccess() {
    final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
    final boolean[] callRecord = new boolean[] { false };
    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() {
            callRecord[0] = true;
        }
    });
    // Run all the pending tasks on the ui thread
    while (!callRecord[callRecord.length - 1]) looper.runToEndOfTasks();
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) Test(org.junit.Test)

Example 47 with ShadowLooper

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

the class AndroidCallableTest method shouldCrashAppForExceptionInOnException.

@Test(expected = RuntimeException.class)
public void shouldCrashAppForExceptionInOnException() {
    final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
    final boolean[] callRecord = new boolean[] { false };
    Executors.newSingleThreadExecutor().submit(new AndroidCallable<String>() {

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

        @Override
        public void onSuccess(String result) {
        }

        @Override
        public void onException(Exception e) {
            throw new RuntimeException();
        }

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

Example 48 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.

the class DemandFetcherTest method testBaseConditions.

@Test
public void testBaseConditions() throws Exception {
    AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
    AdManagerAdRequest request = builder.build();
    DemandFetcher demandFetcher = new DemandFetcher(request);
    demandFetcher.setPeriodMillis(0);
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(300, 250));
    RequestParams requestParams = new RequestParams("12345", AdType.BANNER, sizes);
    demandFetcher.setRequestParams(requestParams);
    assertEquals(DemandFetcher.STATE.STOPPED, FieldUtils.readField(demandFetcher, "state", true));
    demandFetcher.start();
    assertEquals(DemandFetcher.STATE.RUNNING, FieldUtils.readField(demandFetcher, "state", true));
    ShadowLooper fetcherLooper = Shadows.shadowOf(demandFetcher.getHandler().getLooper());
    fetcherLooper.runOneTask();
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    demandFetcher.destroy();
    assertEquals(DemandFetcher.STATE.DESTROYED, FieldUtils.readField(demandFetcher, "state", true));
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 49 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.

the class PrebidServerAdapterTest method testConnectionHasSetCustomHeaders.

@Test
public void testConnectionHasSetCustomHeaders() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.oneBidFromAppNexus()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId("12345");
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    HashMap<String, String> customHeaders = new HashMap<>();
    customHeaders.put("test-key", "test-value");
    PrebidMobile.setCustomHeaders(customHeaders);
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(300, 250));
    RequestParams requestParams = new RequestParams("67890", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    RecordedRequest recordedRequest = server.takeRequest();
    Headers headers = recordedRequest.getHeaders();
    assertEquals("test-value", headers.get("test-key"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Headers(okhttp3.Headers) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 50 with ShadowLooper

use of org.robolectric.shadows.ShadowLooper in project prebid-mobile-android by prebid.

the class PrebidServerAdapterTest method testUpdateTimeoutMillis2.

@Test
public void testUpdateTimeoutMillis2() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBidResponseNoTmax()));
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.noBidResponseTmaxTooLarge()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId("12345");
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(300, 250));
    RequestParams requestParams = new RequestParams("67890", AdType.BANNER, sizes);
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertEquals("Actual Prebid Mobile timeout is " + PrebidMobile.getTimeoutMillis(), 2000, PrebidMobile.getTimeoutMillis());
    assertTrue(!PrebidMobile.timeoutMillisUpdated);
    adapter.requestDemand(requestParams, mockListener, uuid);
    bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runToEndOfTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertEquals("Actual Prebid Mobile timeout is " + PrebidMobile.getTimeoutMillis(), 2000, PrebidMobile.getTimeoutMillis());
    assertTrue(PrebidMobile.timeoutMillisUpdated);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HttpUrl(okhttp3.HttpUrl) 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