Search in sources :

Example 61 with ShadowLooper

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

the class PrebidNativeNativeTest method testSuccessfulPrebidNativeAdExpiry.

@Test
public void testSuccessfulPrebidNativeAdExpiry() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.validResponsePrebidNativeNative()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId(PBS_ACCOUNT_ID_APPNEXUS);
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    RequestParams requestParams = new RequestParams(PBS_CONFIG_ID_NATIVE_APPNEXUS, AdType.NATIVE, null, new HashMap<String, Set<String>>(), new HashSet<String>(), null, null, null, null, null, new ArrayList<DataObject>());
    requestParams.setNativeRequestParams(new NativeRequestParams());
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runOneTask();
    bgLooper.runOneTask();
    runAllMainThreadExecutorTasks();
    Robolectric.getBackgroundThreadScheduler().runOneTask();
    Robolectric.getBackgroundThreadScheduler().runOneTask();
    String cacheId = "";
    try {
        Class clazz = Class.forName(CacheManager.class.getName());
        Field field = clazz.getDeclaredField("savedValues");
        field.setAccessible(true);
        HashMap<String, String> map = (HashMap<String, String>) field.get(clazz);
        for (String key : map.keySet()) {
            cacheId = key;
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    HashMap<String, String> bids = new HashMap<String, String>();
    bids.put("hb_bidder", "appnexus");
    bids.put("hb_bidder_appnexus", "appnexus");
    bids.put("hb_cache_id", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
    bids.put("hb_cache_id_appnexus", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
    bids.put("hb_env", "mobile-app");
    bids.put("hb_env_appnexus", "mobile-app");
    bids.put("hb_pb", "0.00");
    bids.put("hb_pb_appnexus", "0.00");
    bids.put("hb_cache_host_appnex", "prebid.sin3.adnxs.com");
    bids.put("hb_cache_path_appnex", "/pbc/v1/cache");
    bids.put("hb_cache_path", "/pbc/v1/cache");
    bids.put("hb_cache_host", "prebid.sin3.adnxs.com");
    bids.put("hb_cache_id_local", cacheId);
    verify(mockListener).onDemandReady(bids, uuid);
    // Test findNative on responded cacheId local
    NativeCustomTemplateAd nativeCustomTemplateAd = Mockito.mock(NativeCustomTemplateAd.class);
    Mockito.when(nativeCustomTemplateAd.getText("isPrebid")).thenReturn("1");
    Mockito.when(nativeCustomTemplateAd.getText("hb_cache_id_local")).thenReturn(cacheId);
    AdViewUtils.findNative(nativeCustomTemplateAd, new PrebidNativeAdListener() {

        @Override
        public void onPrebidNativeLoaded(PrebidNativeAd ad) {
            assertTrue(ad != null);
            View view = new View(activity);
            view.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
            ad.registerPrebidNativeAdEventListener(new PrebidNativeAdEventListener() {

                @Override
                public void onAdClicked() {
                    fail();
                }

                @Override
                public void onAdImpression() {
                    fail();
                }

                @Override
                public void onAdExpired() {
                    adExpired = true;
                }
            });
            assertFalse(adExpired);
            Robolectric.getBackgroundThreadScheduler().advanceBy(302, TimeUnit.SECONDS);
            Robolectric.getForegroundThreadScheduler().advanceBy(302, TimeUnit.SECONDS);
            assertTrue(adExpired);
        }

        @Override
        public void onPrebidNativeNotFound() {
            fail();
        }

        @Override
        public void onPrebidNativeNotValid() {
            fail();
        }
    });
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) Field(java.lang.reflect.Field) MockResponse(okhttp3.mockwebserver.MockResponse) View(android.view.View) HttpUrl(okhttp3.HttpUrl) NativeCustomTemplateAd(com.google.android.gms.ads.formats.NativeCustomTemplateAd) Test(org.junit.Test)

Example 62 with ShadowLooper

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

the class PrebidNativeNativeTest method testSuccessfulPrebidNativeAdClick.

@Test
public void testSuccessfulPrebidNativeAdClick() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.validResponsePrebidNativeNative()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId(PBS_ACCOUNT_ID_APPNEXUS);
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    RequestParams requestParams = new RequestParams(PBS_CONFIG_ID_NATIVE_APPNEXUS, AdType.NATIVE, null, new HashMap<String, Set<String>>(), new HashSet<String>(), null, null, null, null, null, new ArrayList<DataObject>());
    requestParams.setNativeRequestParams(new NativeRequestParams());
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runOneTask();
    bgLooper.runOneTask();
    runAllMainThreadExecutorTasks();
    Robolectric.getBackgroundThreadScheduler().runOneTask();
    Robolectric.getBackgroundThreadScheduler().runOneTask();
    String cacheId = "";
    try {
        Class clazz = Class.forName(CacheManager.class.getName());
        Field field = clazz.getDeclaredField("savedValues");
        field.setAccessible(true);
        HashMap<String, String> map = (HashMap<String, String>) field.get(clazz);
        for (String key : map.keySet()) {
            cacheId = key;
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    HashMap<String, String> bids = new HashMap<String, String>();
    bids.put("hb_bidder", "appnexus");
    bids.put("hb_bidder_appnexus", "appnexus");
    bids.put("hb_cache_id", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
    bids.put("hb_cache_id_appnexus", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
    bids.put("hb_env", "mobile-app");
    bids.put("hb_env_appnexus", "mobile-app");
    bids.put("hb_pb", "0.00");
    bids.put("hb_pb_appnexus", "0.00");
    bids.put("hb_cache_host_appnex", "prebid.sin3.adnxs.com");
    bids.put("hb_cache_path_appnex", "/pbc/v1/cache");
    bids.put("hb_cache_path", "/pbc/v1/cache");
    bids.put("hb_cache_host", "prebid.sin3.adnxs.com");
    bids.put("hb_cache_id_local", cacheId);
    verify(mockListener).onDemandReady(bids, uuid);
    // Test findNative on responded cacheId local
    NativeCustomTemplateAd nativeCustomTemplateAd = Mockito.mock(NativeCustomTemplateAd.class);
    Mockito.when(nativeCustomTemplateAd.getText("isPrebid")).thenReturn("1");
    Mockito.when(nativeCustomTemplateAd.getText("hb_cache_id_local")).thenReturn(cacheId);
    AdViewUtils.findNative(nativeCustomTemplateAd, new PrebidNativeAdListener() {

        @Override
        public void onPrebidNativeLoaded(PrebidNativeAd ad) {
            assertTrue(ad != null);
            View view = new View(activity);
            view.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
            ad.registerView(view, new PrebidNativeAdEventListener() {

                @Override
                public void onAdClicked() {
                    adClicked = true;
                }

                @Override
                public void onAdImpression() {
                    fail();
                }

                @Override
                public void onAdExpired() {
                    fail();
                }
            });
            activity.addContentView(view, new ViewGroup.LayoutParams(200, 200));
            view.setVisibility(View.VISIBLE);
            assertFalse(adClicked);
            view.performClick();
            assertTrue(adClicked);
        }

        @Override
        public void onPrebidNativeNotFound() {
            fail();
        }

        @Override
        public void onPrebidNativeNotValid() {
            fail();
        }
    });
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) Field(java.lang.reflect.Field) MockResponse(okhttp3.mockwebserver.MockResponse) View(android.view.View) HttpUrl(okhttp3.HttpUrl) NativeCustomTemplateAd(com.google.android.gms.ads.formats.NativeCustomTemplateAd) Test(org.junit.Test)

Example 63 with ShadowLooper

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

the class PrebidNativeNativeTest method runAllMainThreadExecutorTasks.

private void runAllMainThreadExecutorTasks() {
    ShadowLooper mainLooper = Shadows.shadowOf(((MainThreadExecutor) TasksManager.getInstance().mainThreadExecutor).getMainExecutor().getLooper());
    mainLooper.runToEndOfTasks();
}
Also used : MainThreadExecutor(org.prebid.mobile.tasksmanager.MainThreadExecutor) ShadowLooper(org.robolectric.shadows.ShadowLooper)

Example 64 with ShadowLooper

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

the class PrebidNativeNativeTest method testSuccessfulPrebidNativeResponse.

@Test
public void testSuccessfulPrebidNativeResponse() {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(MockPrebidServerResponses.validResponsePrebidNativeNative()));
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId(PBS_ACCOUNT_ID_APPNEXUS);
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    PrebidServerAdapter adapter = new PrebidServerAdapter();
    RequestParams requestParams = new RequestParams(PBS_CONFIG_ID_NATIVE_APPNEXUS, AdType.NATIVE, null, new HashMap<String, Set<String>>(), new HashSet<String>(), null, null, null, null, null, new ArrayList<DataObject>());
    requestParams.setNativeRequestParams(new NativeRequestParams());
    String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    ShadowLooper bgLooper = Shadows.shadowOf(((BackgroundThreadExecutor) TasksManager.getInstance().backgroundThreadExecutor).getBackgroundHandler().getLooper());
    bgLooper.runOneTask();
    bgLooper.runOneTask();
    runAllMainThreadExecutorTasks();
    Robolectric.getBackgroundThreadScheduler().runOneTask();
    Robolectric.getBackgroundThreadScheduler().runOneTask();
    String cacheId = "";
    try {
        Class clazz = Class.forName(CacheManager.class.getName());
        Field field = clazz.getDeclaredField("savedValues");
        field.setAccessible(true);
        HashMap<String, String> map = (HashMap<String, String>) field.get(clazz);
        for (String key : map.keySet()) {
            cacheId = key;
        }
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    HashMap<String, String> bids = new HashMap<String, String>();
    bids.put("hb_bidder", "appnexus");
    bids.put("hb_bidder_appnexus", "appnexus");
    bids.put("hb_cache_id", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
    bids.put("hb_cache_id_appnexus", "dfad62f2-8ed5-4cf8-a597-f702147b7c98");
    bids.put("hb_env", "mobile-app");
    bids.put("hb_env_appnexus", "mobile-app");
    bids.put("hb_pb", "0.00");
    bids.put("hb_pb_appnexus", "0.00");
    bids.put("hb_cache_host_appnex", "prebid.sin3.adnxs.com");
    bids.put("hb_cache_path_appnex", "/pbc/v1/cache");
    bids.put("hb_cache_path", "/pbc/v1/cache");
    bids.put("hb_cache_host", "prebid.sin3.adnxs.com");
    bids.put("hb_cache_id_local", cacheId);
    verify(mockListener).onDemandReady(bids, uuid);
    // Test findNative on responded cacheId local
    NativeCustomTemplateAd nativeCustomTemplateAd = Mockito.mock(NativeCustomTemplateAd.class);
    Mockito.when(nativeCustomTemplateAd.getText("isPrebid")).thenReturn("1");
    Mockito.when(nativeCustomTemplateAd.getText("hb_cache_id_local")).thenReturn(cacheId);
    AdViewUtils.findNative(nativeCustomTemplateAd, new PrebidNativeAdListener() {

        @Override
        public void onPrebidNativeLoaded(PrebidNativeAd ad) {
            assertTrue(ad != null);
        }

        @Override
        public void onPrebidNativeNotFound() {
            fail();
        }

        @Override
        public void onPrebidNativeNotValid() {
            fail();
        }
    });
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) Field(java.lang.reflect.Field) MockResponse(okhttp3.mockwebserver.MockResponse) HttpUrl(okhttp3.HttpUrl) NativeCustomTemplateAd(com.google.android.gms.ads.formats.NativeCustomTemplateAd) Test(org.junit.Test)

Example 65 with ShadowLooper

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

the class PrebidServerAdapterTest method testInvalidPrebidServerIdSyntaxForAppNexusHostedPrebidServer.

// @Test
// TODO: Activate this test after setting up test server
public void testInvalidPrebidServerIdSyntaxForAppNexusHostedPrebidServer() {
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    // invalid account id
    PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888d");
    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(320, 50));
    RequestParams requestParams = new RequestParams("6ace8c7d-88c0-4623-8117-75bc3f0a2e45", 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();
    verify(mockListener).onDemandFailed(ResultCode.INVALID_ACCOUNT_ID, uuid);
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) BackgroundThreadExecutor(org.prebid.mobile.tasksmanager.BackgroundThreadExecutor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

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