Search in sources :

Example 36 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class ExtraTests method testSameConfigIdOnDifferentAdObjects.

@Test
public void testSameConfigIdOnDifferentAdObjects() 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 adObject1 = new MoPubView(m.getActivity());
            adObject1.setAdUnitId(Constants.MOPUB_BANNER_ADUNIT_ID_320x50_APPNEXUS);
            adObject1.setMinimumWidth(320);
            adObject1.setMinimumHeight(50);
            BannerAdUnit adUnit1 = new BannerAdUnit(Constants.PBS_CONFIG_ID_320x50_APPNEXUS, 320, 50);
            adUnits.add(adUnit1);
            OnCompleteListener l1 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    assertTrue(adObject1.getKeywords().contains("hb_pb:0.50"));
                    assertTrue(adObject1.getKeywords().contains("hb_cache_id:"));
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(adObject1);
                    adObject1.loadAd();
                }
            };
            l1 = spy(l1);
            spies.add(l1);
            adUnit1.fetchDemand(adObject1, l1);
            AdManagerAdRequest.Builder builder = new AdManagerAdRequest.Builder();
            final AdManagerAdRequest adObject2 = builder.build();
            BannerAdUnit adUnit2 = new BannerAdUnit(Constants.PBS_CONFIG_ID_320x50_APPNEXUS, 320, 50);
            adUnits.add(adUnit2);
            OnCompleteListener l2 = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    assertEquals(10, adObject2.getCustomTargeting().size());
                    assertTrue(adObject2.getCustomTargeting().keySet().contains("hb_pb"));
                    assertTrue(adObject2.getCustomTargeting().keySet().contains("hb_cache_id"));
                    assertEquals("0.50", adObject2.getCustomTargeting().getString("hb_pb"));
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    AdManagerAdView adView = new AdManagerAdView(m.getActivity());
                    adView.setAdSizes(AdSize.BANNER);
                    adView.setAdUnitId(Constants.DFP_BANNER_ADUNIT_ID_ALL_SIZES_APPNEXUS);
                    adFrame.addView(adView);
                    adView.setId(1);
                    adView.loadAd(adObject2);
                }
            };
            l2 = spy(l2);
            spies.add(l2);
            adUnit1.fetchDemand(adObject2, l2);
        }
    });
    Thread.sleep(10000);
    verify(spies.get(0), times(1)).onComplete(ResultCode.SUCCESS);
    verify(spies.get(1), times(1)).onComplete(ResultCode.SUCCESS);
}
Also used : 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) AdManagerAdView(com.google.android.gms.ads.admanager.AdManagerAdView) FrameLayout(android.widget.FrameLayout) AdManagerAdRequest(com.google.android.gms.ads.admanager.AdManagerAdRequest) ResultCode(org.prebid.mobile.ResultCode) MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Example 37 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class ExtraTests method testEmptyInvalidPrebidServerConfigId.

@Test
public void testEmptyInvalidPrebidServerConfigId() throws Exception {
    PrebidMobile.setApplicationContext(m.getActivity().getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888df0");
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    PrebidMobile.setShareGeoLocation(true);
    final OnCompleteListener[] listener = new OnCompleteListener[1];
    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.setMinimumHeight(250);
            adObject.setMinimumWidth(300);
            BannerAdUnit adUnit = new BannerAdUnit("", 300, 250);
            OnCompleteListener l = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(adObject);
                    adObject.loadAd();
                }
            };
            listener[0] = spy(l);
            adUnit.fetchDemand(adObject, listener[0]);
        }
    });
    Thread.sleep(1000);
    verify(listener[0], times(1)).onComplete(ResultCode.INVALID_CONFIG_ID);
}
Also used : OnCompleteListener(org.prebid.mobile.OnCompleteListener) FrameLayout(android.widget.FrameLayout) ResultCode(org.prebid.mobile.ResultCode) MoPubView(com.mopub.mobileads.MoPubView) BannerAdUnit(org.prebid.mobile.BannerAdUnit) Test(org.junit.Test)

Example 38 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class ExtraTests method testAppNexusAgeTargeting2.

// Passing year -1 is invalid yob, should not send yob and get back a no bid
@Test
public void testAppNexusAgeTargeting2() throws Exception {
    boolean errorThrown = false;
    try {
        TargetingParams.setYearOfBirth(-1);
    } catch (Exception e) {
        errorThrown = true;
        assertEquals(0, TargetingParams.getYearOfBirth());
    }
    assertTrue(errorThrown);
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            if (request.getPath().equals("/")) {
                String postData = request.getBody().readUtf8();
                assertTrue("Post data should not contain yob: " + postData, !postData.contains("yob"));
                return getAppNexusDemand(postData);
            }
            return new MockResponse().setResponseCode(404);
        }
    });
    PrebidMobile.setApplicationContext(m.getActivity().getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888df0");
    Host.CUSTOM.setHostUrl(server.url("/").toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setShareGeoLocation(true);
    final OnCompleteListener[] listener = new OnCompleteListener[1];
    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.setMinimumHeight(250);
            adObject.setMinimumWidth(300);
            BannerAdUnit adUnit = new BannerAdUnit("47706260-ee91-4cd7-b656-2185aca89f59", 300, 250);
            OnCompleteListener l = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(adObject);
                    adObject.loadAd();
                }
            };
            listener[0] = spy(l);
            adUnit.fetchDemand(adObject, listener[0]);
        }
    });
    Thread.sleep(10000);
    verify(listener[0], times(1)).onComplete(ResultCode.NO_BIDS);
    onView(withId(R.id.adFrame)).check(matches(isDisplayed()));
    onWebView().check(webMatches(getCurrentUrl(), containsString("ads.mopub.com")));
    onWebView().check(webContent(containingTextInBody("Hello, I'm not a Prebid ad.")));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Dispatcher(okhttp3.mockwebserver.Dispatcher) JSONException(org.json.JSONException) IOException(java.io.IOException) BannerAdUnit(org.prebid.mobile.BannerAdUnit) OnCompleteListener(org.prebid.mobile.OnCompleteListener) FrameLayout(android.widget.FrameLayout) ResultCode(org.prebid.mobile.ResultCode) MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Example 39 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class ExtraTests method testAppNexusAgeTargeting3.

// Passing year 2018 is valid yob, should send yob and but get back a no bid, since campaign targeting 25-34
@Test
public void testAppNexusAgeTargeting3() throws Exception {
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            if (request.getPath().equals("/")) {
                String postData = request.getBody().readUtf8();
                assertTrue("Post data does not contain yob 2018: " + postData, postData.contains("2018"));
                return getAppNexusDemand(postData);
            }
            return new MockResponse().setResponseCode(404);
        }
    });
    PrebidMobile.setApplicationContext(m.getActivity().getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("bfa84af2-bd16-4d35-96ad-31c6bb888df0");
    Host.CUSTOM.setHostUrl(server.url("/").toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setShareGeoLocation(true);
    TargetingParams.setYearOfBirth(2018);
    final OnCompleteListener[] listener = new OnCompleteListener[1];
    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.setMinimumHeight(250);
            adObject.setMinimumWidth(300);
            BannerAdUnit adUnit = new BannerAdUnit("47706260-ee91-4cd7-b656-2185aca89f59", 300, 250);
            OnCompleteListener l = new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                    FrameLayout adFrame = m.getActivity().findViewById(R.id.adFrame);
                    adFrame.addView(adObject);
                    adObject.loadAd();
                }
            };
            listener[0] = spy(l);
            adUnit.fetchDemand(adObject, listener[0]);
        }
    });
    Thread.sleep(10000);
    verify(listener[0], times(1)).onComplete(ResultCode.NO_BIDS);
    onView(withId(R.id.adFrame)).check(matches(isDisplayed()));
    onWebView().check(webMatches(getCurrentUrl(), containsString("ads.mopub.com")));
    onWebView().check(webContent(containingTextInBody("Hello, I'm not a Prebid ad.")));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Dispatcher(okhttp3.mockwebserver.Dispatcher) BannerAdUnit(org.prebid.mobile.BannerAdUnit) OnCompleteListener(org.prebid.mobile.OnCompleteListener) FrameLayout(android.widget.FrameLayout) ResultCode(org.prebid.mobile.ResultCode) MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Example 40 with MoPubView

use of com.mopub.mobileads.MoPubView in project prebid-mobile-android by prebid.

the class ExtraTests method testPostDataWithContext.

@Test
public void testPostDataWithContext() throws Exception {
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            if (request.getPath().equals("/testPostData")) {
                String postDataString = request.getBody().readUtf8();
                try {
                    JSONObject postData = new JSONObject(postDataString);
                    JSONObject device = postData.getJSONObject("device");
                    String ifa = device.getString("ifa");
                    assertTrue(ifa != null && !ifa.isEmpty());
                    String ua = device.getString("ua");
                    assertTrue(ua != null && !ua.isEmpty());
                } catch (JSONException e) {
                    fail("error:" + e);
                }
                return getAppNexusDemand(postDataString);
            }
            return new MockResponse().setResponseCode(404);
        }
    });
    PrebidMobile.setApplicationContext(m.getActivity().getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("1001");
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    Host.CUSTOM.setHostUrl(server.url("testPostData").toString());
    mHandler.post(new Runnable() {

        @Override
        public void run() {
            BannerAdUnit adUnit = new BannerAdUnit("1001-1", 300, 250);
            final MoPubView adObject = new MoPubView(m.getActivity());
            adUnit.fetchDemand(adObject, new OnCompleteListener() {

                @Override
                public void onComplete(ResultCode resultCode) {
                }
            });
        }
    });
    Thread.sleep(5_000);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OnCompleteListener(org.prebid.mobile.OnCompleteListener) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) Matchers.containsString(org.hamcrest.Matchers.containsString) Dispatcher(okhttp3.mockwebserver.Dispatcher) ResultCode(org.prebid.mobile.ResultCode) BannerAdUnit(org.prebid.mobile.BannerAdUnit) MoPubView(com.mopub.mobileads.MoPubView) Test(org.junit.Test)

Aggregations

MoPubView (com.mopub.mobileads.MoPubView)48 Test (org.junit.Test)38 FrameLayout (android.widget.FrameLayout)26 BannerAdUnit (org.prebid.mobile.BannerAdUnit)23 OnCompleteListener (org.prebid.mobile.OnCompleteListener)23 ResultCode (org.prebid.mobile.ResultCode)23 MockResponse (okhttp3.mockwebserver.MockResponse)15 HttpUrl (okhttp3.HttpUrl)8 Dispatcher (okhttp3.mockwebserver.Dispatcher)8 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)8 Matchers.containsString (org.hamcrest.Matchers.containsString)7 BackgroundThreadExecutor (org.prebid.mobile.tasksmanager.BackgroundThreadExecutor)7 ShadowLooper (org.robolectric.shadows.ShadowLooper)7 MoPubInterstitial (com.mopub.mobileads.MoPubInterstitial)5 JSONException (org.json.JSONException)5 ArrayList (java.util.ArrayList)4 JSONObject (org.json.JSONObject)4 AdUnit (org.prebid.mobile.AdUnit)4 InterstitialAdUnit (org.prebid.mobile.InterstitialAdUnit)4 MoPubErrorCode (com.mopub.mobileads.MoPubErrorCode)3