Search in sources :

Example 1 with AdUnit

use of org.prebid.mobile.AdUnit in project prebid-mobile-android by prebid.

the class ExtraTests method testMultipleDemand.

@Test
public void testMultipleDemand() throws Exception {
    final ArrayList<AdUnit> adUnits = new ArrayList<AdUnit>();
    final ArrayList<OnCompleteListener> spies = new ArrayList<>();
    PrebidMobile.setApplicationContext(m.getActivity().getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888df0");
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    PrebidMobile.setShareGeoLocation(true);
    mHandler.post(new Runnable() {

        @Override
        public void run() {
            final MoPubView adObject = new MoPubView(m.getActivity());
            adObject.setAdUnitId(Constants.MOPUB_BANNER_ADUNIT_ID_300x250_APPNEXUS);
            adObject.setMinimumWidth(300);
            adObject.setMinimumHeight(250);
            BannerAdUnit adUnit = new BannerAdUnit("1cfdfe39-18f2-45b9-964f-63d64cdc0399", 300, 250);
            adUnits.add(adUnit);
            OnCompleteListener l = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    String keywords = adObject.getKeywords();
                    String[] keywordsArray = keywords.split(",");
                    assertEquals(15, keywordsArray.length);
                    assertTrue(keywords.contains("hb_pb:1.20"));
                    assertTrue(keywords.contains("hb_pb_rubicon:1.20"));
                    assertTrue(keywords.contains("hb_cache_id:"));
                    assertTrue(keywords.contains("hb_pb_appnexus:0.50"));
                    assertTrue(keywords.contains("hb_cache_id_appnexus:"));
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(adObject);
                    adObject.loadAd();
                }
            };
            l = spy(l);
            spies.add(l);
            adUnit.fetchDemand(adObject, l);
        }
    });
    Thread.sleep(10000);
    verify(spies.get(0), times(1)).onComplete(ResultCode.SUCCESS);
    onWebView().check(webContent(containingTextInBody("ucTag.renderAd")));
}
Also used : OnCompleteListener(org.prebid.mobile.OnCompleteListener) BannerAdUnit(org.prebid.mobile.BannerAdUnit) AdUnit(org.prebid.mobile.AdUnit) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit) FrameLayout(android.widget.FrameLayout) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ResultCode(org.prebid.mobile.ResultCode) MoPubView(com.mopub.mobileads.MoPubView) BannerAdUnit(org.prebid.mobile.BannerAdUnit) Test(org.junit.Test)

Example 2 with AdUnit

use of org.prebid.mobile.AdUnit in project prebid-mobile-android by prebid.

the class ExtraTests method testMultipleAdUnitsAllDemandFetched.

@Test
public void testMultipleAdUnitsAllDemandFetched() throws Exception {
    final ArrayList<AdUnit> adUnits = new ArrayList<AdUnit>();
    final ArrayList<OnCompleteListener> spies = new ArrayList<>();
    final ArrayList<MoPubInterstitial> moPubInterstitials = new ArrayList<>();
    PrebidMobile.setApplicationContext(m.getActivity().getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888df0");
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    PrebidMobile.setShareGeoLocation(true);
    mHandler.post(new Runnable() {

        @Override
        public void run() {
            // test 0 set up Banner
            final MoPubView mopubTest1 = new MoPubView(m.getActivity());
            mopubTest1.setAdUnitId("9a8c2ccd3dae405bb925397d35eed8f9");
            mopubTest1.setMinimumHeight(50);
            mopubTest1.setMinimumWidth(320);
            BannerAdUnit adUnit1 = new BannerAdUnit("7cd2c7c8-cebe-4206-b5a4-97b9e840729e", 320, 50);
            adUnits.add(adUnit1);
            OnCompleteListener listener1 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(mopubTest1);
                    mopubTest1.setId(1);
                    mopubTest1.loadAd();
                }
            };
            listener1 = spy(listener1);
            spies.add(listener1);
            adUnit1.fetchDemand(mopubTest1, listener1);
            // test 1 set up Banner
            final MoPubView mopubTest2 = new MoPubView(m.getActivity());
            mopubTest2.setAdUnitId("50564379db734ebbb347849221a1081e");
            mopubTest2.setMinimumHeight(50);
            mopubTest2.setMinimumWidth(320);
            BannerAdUnit adUnit2 = new BannerAdUnit("525a5fee-ffbb-4f16-935d-3717c56e7aeb", 320, 50);
            adUnits.add(adUnit2);
            OnCompleteListener listener2 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(mopubTest2);
                    mopubTest2.setId(2);
                    mopubTest2.loadAd();
                }
            };
            listener2 = spy(listener2);
            spies.add(listener2);
            adUnit2.fetchDemand(mopubTest2, listener2);
            // test 2 set up Banner
            final MoPubView mopubTest3 = new MoPubView(m.getActivity());
            mopubTest3.setAdUnitId("5ff9556b05964e65b684ec54013df59d");
            mopubTest2.setMinimumHeight(250);
            mopubTest2.setMinimumWidth(300);
            BannerAdUnit adUnit3 = new BannerAdUnit("511c39f2-b527-41af-811a-adac6911bdfc", 300, 250);
            adUnits.add(adUnit3);
            OnCompleteListener listener3 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(mopubTest3);
                    mopubTest3.setId(3);
                    mopubTest3.loadAd();
                }
            };
            listener3 = spy(listener3);
            spies.add(listener3);
            adUnit3.fetchDemand(mopubTest3, listener3);
            // test 3 set up Banner
            final MoPubView mopubTest4 = new MoPubView(m.getActivity());
            mopubTest4.setAdUnitId("c5c9267bcf6247cb91a116d1ef6c7487");
            mopubTest4.setMinimumHeight(250);
            mopubTest4.setMinimumWidth(300);
            BannerAdUnit adUnit4 = new BannerAdUnit("42ad4418-9b36-4e39-ae54-2f7a13ad8616", 300, 250);
            adUnits.add(adUnit4);
            OnCompleteListener listener4 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(mopubTest4);
                    mopubTest4.setId(4);
                    mopubTest4.loadAd();
                }
            };
            listener4 = spy(listener4);
            spies.add(listener4);
            adUnit4.fetchDemand(mopubTest4, listener4);
            // test 4 set up Banner
            final MoPubView mopubTest5 = new MoPubView(m.getActivity());
            mopubTest5.setAdUnitId(Constants.MOPUB_BANNER_ADUNIT_ID_300x250_APPNEXUS);
            mopubTest5.setMinimumHeight(250);
            mopubTest5.setMinimumWidth(300);
            BannerAdUnit adUnit5 = new BannerAdUnit(Constants.PBS_CONFIG_ID_300x250_APPNEXUS, 300, 250);
            adUnits.add(adUnit5);
            OnCompleteListener listener5 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(mopubTest5);
                    mopubTest5.setId(5);
                    mopubTest5.loadAd();
                }
            };
            listener5 = spy(listener5);
            spies.add(listener5);
            adUnit4.fetchDemand(mopubTest5, listener5);
            // test 5 set up Interstitial
            final MoPubInterstitial mopubInstl1 = new MoPubInterstitial(m.getActivity(), Constants.MOPUB_INTERSTITIAL_ADUNIT_ID_APPNEXUS);
            moPubInterstitials.add(mopubInstl1);
            InterstitialAdUnit adUnit6 = new InterstitialAdUnit(Constants.PBS_CONFIG_ID_INTERSTITIAL_APPNEXUS);
            adUnits.add(adUnit6);
            OnCompleteListener listener6 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    mopubInstl1.load();
                }
            };
            listener6 = spy(listener6);
            spies.add(listener6);
            adUnit6.fetchDemand(mopubInstl1, listener6);
            // test 6 set up Interstitial
            final MoPubInterstitial mopubInstl2 = new MoPubInterstitial(m.getActivity(), "c3fca03154a540bfa7f0971fb984e3e8");
            moPubInterstitials.add(mopubInstl2);
            InterstitialAdUnit adUnit7 = new InterstitialAdUnit("bde00f49-0a1b-483a-9716-e2dd427b794c");
            adUnits.add(adUnit7);
            OnCompleteListener listener7 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    mopubInstl2.load();
                }
            };
            listener7 = spy(listener7);
            spies.add(listener7);
            adUnit7.fetchDemand(mopubInstl2, listener7);
            // test 7 set up Interstitial
            final MoPubInterstitial mopubInstl3 = new MoPubInterstitial(m.getActivity(), "12ecf78eb8314f8bb36192a6286adc56");
            moPubInterstitials.add(mopubInstl3);
            InterstitialAdUnit adUnit8 = new InterstitialAdUnit("6ceca3d4-f5b8-4717-b4d9-178843f873f8");
            adUnits.add(adUnit8);
            OnCompleteListener listener8 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    mopubInstl3.load();
                }
            };
            listener8 = spy(listener8);
            spies.add(listener8);
            adUnit8.fetchDemand(mopubInstl3, listener8);
        }
    });
    Thread.sleep(10000);
    // verify line by line for easier debug
    ArgumentCaptor<ResultCode> resultCode = ArgumentCaptor.forClass(ResultCode.class);
    verify(spies.get(0), times(1)).onComplete(resultCode.capture());
    if (ResultCode.SUCCESS.equals(resultCode.getValue())) {
        onWebView(withParent(withId(1))).check(webContent(containingTextInBody("143135824")));
    } else {
        LogUtil.e("MultipleAdUnits", "For test 0, actual result code is " + resultCode.getValue());
    }
    ArgumentCaptor<ResultCode> resultCode1 = ArgumentCaptor.forClass(ResultCode.class);
    verify(spies.get(1), times(1)).onComplete(resultCode1.capture());
    if (ResultCode.SUCCESS.equals(resultCode1.getValue())) {
        onWebView(withParent(withId(2))).check(webContent(containingTextInBody("143208584")));
    } else {
        LogUtil.e("MultipleAdUnits", "For test 1, actual result code is " + resultCode1.getValue());
    }
    ArgumentCaptor<ResultCode> resultCode2 = ArgumentCaptor.forClass(ResultCode.class);
    verify(spies.get(2), times(1)).onComplete(resultCode2.capture());
    if (ResultCode.SUCCESS.equals(resultCode2.getValue())) {
        onWebView(withParent(withId(3))).check(webContent(containingTextInBody("143208598")));
    } else {
        LogUtil.e("MultipleAdUnits", "For test 2, actual result code is " + resultCode2.getValue());
    }
    ArgumentCaptor<ResultCode> resultCode3 = ArgumentCaptor.forClass(ResultCode.class);
    verify(spies.get(3), times(1)).onComplete(resultCode3.capture());
    if (ResultCode.SUCCESS.equals(resultCode3.getValue())) {
        onWebView(withParent(withId(4))).check(webContent(containingTextInBody("143208640")));
    } else {
        LogUtil.e("MultipleAdUnits", "For test 3, actual result code is " + resultCode3.getValue());
    }
    ArgumentCaptor<ResultCode> resultCode4 = ArgumentCaptor.forClass(ResultCode.class);
    verify(spies.get(4), times(1)).onComplete(resultCode4.capture());
    if (ResultCode.SUCCESS.equals(resultCode4.getValue())) {
        onWebView(withParent(withId(5))).check(webContent(containingTextInBody("ucTag.renderAd")));
    } else {
        LogUtil.e("MultipleAdUnits", "For test 4, actual result code is " + resultCode4.getValue());
    }
    ArgumentCaptor<ResultCode> resultCode5 = ArgumentCaptor.forClass(ResultCode.class);
    verify(spies.get(5), times(1)).onComplete(resultCode5.capture());
    if (ResultCode.SUCCESS.equals(resultCode5.getValue())) {
        moPubInterstitials.get(0).show();
        Thread.sleep(2000);
        assertEquals("com.mopub.mobileads.MoPubActivity", TestUtil.getCurrentActivity().getClass().getName());
        onWebView().check(webContent(containingTextInBody("ucTag.renderAd")));
        Espresso.pressBack();
    } else {
        LogUtil.e("MultipleAdUnits", "For test 5, actual result code is " + resultCode5.getValue());
    }
    ArgumentCaptor<ResultCode> resultCode6 = ArgumentCaptor.forClass(ResultCode.class);
    verify(spies.get(6), times(1)).onComplete(resultCode6.capture());
    if (ResultCode.SUCCESS.equals(resultCode6.getValue())) {
        moPubInterstitials.get(1).show();
        Thread.sleep(2000);
        assertEquals("com.mopub.mobileads.MoPubActivity", TestUtil.getCurrentActivity().getClass().getName());
        onWebView().check(webContent(containingTextInBody("143393807")));
        Espresso.pressBack();
    } else {
        LogUtil.e("MultipleAdUnits", "For test 6, actual result code is " + resultCode6.getValue());
    }
    ArgumentCaptor<ResultCode> resultCode7 = ArgumentCaptor.forClass(ResultCode.class);
    verify(spies.get(7), times(1)).onComplete(resultCode7.capture());
    if (ResultCode.SUCCESS.equals(resultCode7.getValue())) {
        moPubInterstitials.get(2).show();
        Thread.sleep(5000);
        assertEquals("com.mopub.mobileads.MoPubActivity", TestUtil.getCurrentActivity().getClass().getName());
        onWebView().check(webContent(containingTextInBody("143393864")));
        Espresso.pressBack();
    } else {
        LogUtil.e("MultipleAdUnits", "For test 7, actual result code is " + resultCode7.getValue());
    }
}
Also used : MoPubInterstitial(com.mopub.mobileads.MoPubInterstitial) ArrayList(java.util.ArrayList) BannerAdUnit(org.prebid.mobile.BannerAdUnit) OnCompleteListener(org.prebid.mobile.OnCompleteListener) BannerAdUnit(org.prebid.mobile.BannerAdUnit) AdUnit(org.prebid.mobile.AdUnit) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit) FrameLayout(android.widget.FrameLayout) ResultCode(org.prebid.mobile.ResultCode) MoPubView(com.mopub.mobileads.MoPubView) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit) Test(org.junit.Test)

Example 3 with AdUnit

use of org.prebid.mobile.AdUnit in project prebid-mobile-android by prebid.

the class AdManagerComplexTest method testFetchDemandBidsAutoRefresh.

@Test
public void testFetchDemandBidsAutoRefresh() throws Exception {
    final IntegerWrapper requestCountWrapper = new IntegerWrapper();
    fetchDemandHelper(31_000, new FetchDemandCompleteListener() {

        @Override
        public void preFetchDemand() {
            mockServer.enqueue(new MockResponse().setResponseCode(200).setBody("{\n" + "  \"seatbid\": [\n" + "    {\n" + "      \"bid\": [\n" + "        {\n" + "          \"ext\": {\n" + "            \"prebid\": {\n" + "              \"targeting\": {\n" + "                \"hb_cache_id\": \"top_bid_1\",\n" + "                \"key1\": \"value1\"\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      ]\n" + "    }\n" + "  ]\n" + "}"));
        }

        @Override
        public void executeFetchDemand(AdUnit adUnit, final List<Map<String, String>> resultList) {
            adUnit.fetchDemand(new OnCompleteListener2() {

                @Override
                public void onComplete(ResultCode resultCode, Map<String, String> unmodifiableMap) {
                    requestCountWrapper.value++;
                    resultList.add(new HashMap(unmodifiableMap));
                }
            });
        }

        @Override
        public void postFetchDemand(List<Map<String, String>> resultList) {
            Map<String, String> result = resultList.get(1);
            assertEquals("top_bid_1", result.get("hb_cache_id"));
            assertEquals("value1", result.get("key1"));
        }
    });
    assertEquals(2, requestCountWrapper.value);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) BannerAdUnit(org.prebid.mobile.BannerAdUnit) AdUnit(org.prebid.mobile.AdUnit) HashMap(java.util.HashMap) OnCompleteListener2(org.prebid.mobile.OnCompleteListener2) HashMap(java.util.HashMap) Map(java.util.Map) ResultCode(org.prebid.mobile.ResultCode) Test(org.junit.Test)

Example 4 with AdUnit

use of org.prebid.mobile.AdUnit in project prebid-mobile-android by prebid.

the class DemandRequestBuilder method getOpenRtbImpressions.

private JSONArray getOpenRtbImpressions(List<AdUnit> adUnits, boolean secureParams) throws JSONException {
    JSONArray imps = new JSONArray();
    for (AdUnit adUnit : adUnits) {
        JSONObject imp = new JSONObject();
        imp.put("id", UUID.randomUUID().toString());
        if (secureParams) {
            imp.put("secure", 1);
        }
        JSONArray sizes = new JSONArray();
        for (AdSize size : adSizes) {
            JSONObject sizeObject = new JSONObject();
            sizeObject.put("w", size.getWidth());
            sizeObject.put("h", size.getHeight());
            sizes.put(sizeObject);
        }
        if (adUnit instanceof InterstitialAdUnit) {
            imp.put("instl", 1);
            Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            JSONObject sizeObject = new JSONObject();
            sizeObject.put("w", size.x);
            sizeObject.put("h", size.y);
            sizes.put(sizeObject);
        }
        JSONObject formats = new JSONObject();
        formats.put("format", sizes);
        imp.put("banner", formats);
        JSONObject ext = new JSONObject();
        JSONObject prebid = new JSONObject();
        JSONObject storedRequest = new JSONObject();
        storedRequest.put("id", configId);
        prebid.put("storedrequest", storedRequest);
        ext.put("prebid", prebid);
        imp.put("ext", ext);
        imps.put(imp);
    }
    return imps;
}
Also used : AdUnit(org.prebid.mobile.AdUnit) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit) JSONObject(org.json.JSONObject) AdSize(org.prebid.mobile.drprebid.model.AdSize) JSONArray(org.json.JSONArray) Point(android.graphics.Point) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit) Display(android.view.Display)

Example 5 with AdUnit

use of org.prebid.mobile.AdUnit in project prebid-mobile-android by prebid.

the class RealTimeDemandTest method startTest.

public void startTest() {
    GeneralSettings generalSettings = SettingsManager.getInstance(mContext).getGeneralSettings();
    PrebidServerSettings prebidServerSettings = SettingsManager.getInstance(mContext).getPrebidServerSettings();
    AdUnit adUnit = null;
    if (generalSettings.getAdFormat() == AdFormat.BANNER) {
        AdSize adSize = generalSettings.getAdSize();
        adUnit = new BannerAdUnit(prebidServerSettings.getConfigId(), adSize.getWidth(), adSize.getHeight());
    } else if (generalSettings.getAdFormat() == AdFormat.INTERSTITIAL) {
        adUnit = new InterstitialAdUnit(prebidServerSettings.getConfigId());
    }
    List<AdUnit> adUnits = Collections.singletonList(adUnit);
    String hostUrl;
    switch(prebidServerSettings.getPrebidServer()) {
        case APPNEXUS:
            PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
            hostUrl = Constants.EndpointUrls.APPNEXUS_PREBID_SERVER;
            break;
        case RUBICON:
            PrebidMobile.setPrebidServerHost(Host.RUBICON);
            hostUrl = Constants.EndpointUrls.RUBICON_PREBID_SERVER;
            break;
        case CUSTOM:
            PrebidMobile.setPrebidServerHost(Host.CUSTOM);
            hostUrl = buildCustomServerEndpoint(prebidServerSettings.getCustomPrebidServerUrl());
            Host.CUSTOM.setHostUrl(hostUrl);
            break;
        default:
            PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
            hostUrl = Constants.EndpointUrls.APPNEXUS_PREBID_SERVER;
    }
    DemandRequestBuilder builder = new DemandRequestBuilder(mContext, prebidServerSettings.getConfigId(), generalSettings.getAdSize());
    String request = builder.buildRequest(adUnits, prebidServerSettings.getAccountId(), true);
    testResponseCount = 0;
    testResults = new DemandTestResults(request);
    for (int i = 0; i < REQUEST_MAX; i++) {
        runTest(hostUrl, request, requestCompletionListener);
    }
}
Also used : PrebidServerSettings(org.prebid.mobile.drprebid.model.PrebidServerSettings) BannerAdUnit(org.prebid.mobile.BannerAdUnit) AdUnit(org.prebid.mobile.AdUnit) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit) AdSize(org.prebid.mobile.drprebid.model.AdSize) DemandTestResults(org.prebid.mobile.drprebid.model.DemandTestResults) GeneralSettings(org.prebid.mobile.drprebid.model.GeneralSettings) BannerAdUnit(org.prebid.mobile.BannerAdUnit) InterstitialAdUnit(org.prebid.mobile.InterstitialAdUnit)

Aggregations

AdUnit (org.prebid.mobile.AdUnit)10 BannerAdUnit (org.prebid.mobile.BannerAdUnit)9 Test (org.junit.Test)7 ResultCode (org.prebid.mobile.ResultCode)7 OnCompleteListener (org.prebid.mobile.OnCompleteListener)6 InterstitialAdUnit (org.prebid.mobile.InterstitialAdUnit)5 AdManagerAdRequest (com.google.android.gms.ads.admanager.AdManagerAdRequest)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 MockResponse (okhttp3.mockwebserver.MockResponse)4 Bundle (android.os.Bundle)3 FrameLayout (android.widget.FrameLayout)3 MoPubView (com.mopub.mobileads.MoPubView)3 HttpUrl (okhttp3.HttpUrl)2 AdSize (org.prebid.mobile.drprebid.model.AdSize)2 Point (android.graphics.Point)1 Display (android.view.Display)1 AdManagerAdView (com.google.android.gms.ads.admanager.AdManagerAdView)1 MoPubInterstitial (com.mopub.mobileads.MoPubInterstitial)1