From 1c62f2426b4b96d13f9921bc6b4dc6afba9a87e2 Mon Sep 17 00:00:00 2001 From: Kowshickkarthick Subramanian Date: Fri, 28 Feb 2020 21:30:51 +0000 Subject: [PATCH] Merge pull request #367 in MOBILE-SDK/app_mobile-sdk-android from ANAdResponseInfo_Implementation to develop Squashed commit of the following: commit 90524f530ac20305abfc087b393633528909228d Author: Abhas Vohra Date: Sat Feb 29 01:18:45 2020 +0530 Handled NO BID case for VideoAd commit ce17bbe7090ce9417d5d0b77852c2314aca3272c Author: Abhas Vohra Date: Sat Feb 29 01:17:53 2020 +0530 Handled NO BID case commit f11f486598888388007a06f37d43323e7f9afd74 Author: Abhas Vohra Date: Sat Feb 29 01:03:14 2020 +0530 Removed AdResponseInfo APIs from BaseAdDispatcher and consequent uses commit 13636e5250b5682185ddb7e665f79f94d9b440e8 Author: Abhas Vohra Date: Fri Feb 28 21:58:25 2020 +0530 Updated Sample Apps commit d58805f9ea279db889dd06b8a5a14fedac52da39 Author: Abhas Vohra Date: Fri Feb 28 20:56:14 2020 +0530 Moved the common implementation to the BaseNativeAdResponse commit 099318021b00a572fdd65f05485a1f46ecaf53f1 Author: Abhas Vohra Date: Fri Feb 28 20:45:41 2020 +0530 Overridden AdResponseInfo methods in the BaseNativeAdResponse commit a071eb84cd32a9502cc1f64477abd899f5c6e1ea Author: Abhas Vohra Date: Fri Feb 28 20:17:39 2020 +0530 Fixed Unit Tests commit 4ee0d449de4918c54e6beb7a4e5ba2e8a5d38d9c Author: Abhas Vohra Date: Fri Feb 28 19:24:19 2020 +0530 Fixed broken CSR Unit Tests commit 3fdae39579e05fbfd8627f8a849b27c7034836f5 Merge: f1d92a2 7f7c349 Author: Abhas Vohra Date: Fri Feb 28 14:16:04 2020 +0530 Merge with develop commit f1d92a29c682af12af3ddae5fade972dfaa5e950 Author: Abhas Vohra Date: Fri Feb 28 14:09:26 2020 +0530 Removed ANAdResponseInfo form AdListener's and VideoAdListener's onAdRequestFailed, set the AdResponse to the AdUnit (except Native) commit afca02e0320be2f2a13cc730d95c64db3ad8a8ed Merge: 19bb156 1e51ae1 Author: Abhas Vohra Date: Thu Feb 27 15:27:08 2020 +0530 Merge: latest develop commit 19bb156ff444cbf87847a8904bda9f003f1711b1 Author: Abhas Vohra Date: Thu Feb 27 00:08:26 2020 +0530 PR comments commit 8a9b7084d1ac0de656d21e36b7256fba46d1c3d9 Author: Abhas Vohra Date: Mon Feb 24 20:56:27 2020 +0530 BannerAdView returns null for Banner Native AdResponseInfo, fixed Unit tests, Added Unit tests commit ee6823011f172932c9e967639cf2dfd197c65c19 Author: Abhas Vohra Date: Thu Feb 20 20:44:24 2020 +0530 Refined Unit Test commit 2e5043d3f378aecf47b5f399b8d4031ef722b977 Author: Abhas Vohra Date: Thu Feb 20 18:09:51 2020 +0530 Changed the Native Ad implementation as per iOS commit d2d05469fbf2adb0a15d2e0b5eca7cba9d6ff46a Author: Abhas Vohra Date: Thu Feb 20 03:18:04 2020 +0530 Updated as per the comments commit 820bb8796c75e8d6bb0fc734e6686dc96a634f09 Author: Abhas Vohra Date: Wed Feb 19 22:27:43 2020 +0530 Added public facing APIs for getting the Member Id and Publisher Id commit 132698450bbef7a0958291288a13efa21dc3c4d4 Author: Abhas Vohra Date: Wed Feb 19 21:29:27 2020 +0530 Code Refactor commit 7eef54b6b5068cdc6619cccfb5ce2d9a837e977f Merge: bd33061 eaacc30 Author: Abhas Vohra Date: Wed Feb 19 21:18:56 2020 +0530 Merged Publisher ID changes commit eaacc30798d06213396cc10df85683641784da63 Author: Abhas Vohra Date: Wed Feb 19 19:55:00 2020 +0530 Added Publisher ID to the UTRequest Parameters, Added Unit Tests ... and 5 more commits --- .../com/example/simplebanner/MyActivity.java | 1 + .../com/example/simplesrm/MyActivity.java | 13 +- .../example/simplevideo/MainActivity.java | 1 + .../example/kotlinsample/NativeActivity.kt | 2 +- .../example/kotlinsample/VideoActivity.kt | 1 + .../instreamvideo/InstreamVideoView.java | 9 - .../opensdk/instreamvideo/VideoAd.java | 46 ++++- .../instreamvideo/VideoAdDispatcher.java | 2 - .../instreamvideo/VideoAdLoadListener.java | 3 +- .../opensdk/instreamvideo/VideoWebView.java | 7 - .../ANAdResponseInfoVideoTests.java | 119 ++++++++++++ .../opensdk/instreamvideo/BaseRoboTest.java | 1 + .../TestANClickThroughAction.java | 1 - .../instreamvideo/TestUTResponses.java | 3 +- .../opensdk/instreamvideo/VideoAdTest.java | 2 - .../appnexus/opensdk/ANAdResponseInfo.java | 107 +++++++++++ .../appnexus/opensdk/ANMultiAdRequest.java | 37 +++- sdk/src/com/appnexus/opensdk/Ad.java | 1 + sdk/src/com/appnexus/opensdk/AdFetcher.java | 2 +- sdk/src/com/appnexus/opensdk/AdListener.java | 3 +- sdk/src/com/appnexus/opensdk/AdView.java | 56 ++++-- .../opensdk/AdViewRequestManager.java | 10 +- .../appnexus/opensdk/BaseAdDispatcher.java | 2 +- .../opensdk/BaseNativeAdResponse.java | 12 ++ .../com/appnexus/opensdk/NativeAdRequest.java | 28 ++- .../opensdk/NativeAdRequestListener.java | 3 +- .../appnexus/opensdk/NativeAdResponse.java | 18 +- .../com/appnexus/opensdk/RequestManager.java | 2 +- .../com/appnexus/opensdk/ut/UTAdRequest.java | 15 +- .../appnexus/opensdk/ut/UTAdRequester.java | 3 +- .../com/appnexus/opensdk/ut/UTAdResponse.java | 89 ++++++--- .../opensdk/ut/UTRequestParameters.java | 20 ++ .../opensdk/ut/adresponse/BaseAdResponse.java | 23 +-- .../ut/adresponse/CSMSDKAdResponse.java | 6 +- .../ut/adresponse/CSMVASTAdResponse.java | 6 +- .../opensdk/ut/adresponse/CSRAdResponse.java | 6 +- .../ut/adresponse/RTBHTMLAdResponse.java | 6 +- .../ut/adresponse/RTBNativeAdResponse.java | 5 +- .../ut/adresponse/RTBVASTAdResponse.java | 6 +- .../ut/adresponse/SSMHTMLAdResponse.java | 6 +- .../opensdk/ANAdResponseInfoBannerTests.java | 178 ++++++++++++++++++ .../ANAdResponseInfoBannerVideoTests.java | 101 ++++++++++ .../ANAdResponseInfoInterstitialTests.java | 129 +++++++++++++ .../opensdk/ANAdResponseInfoNativeTest.java | 118 ++++++++++++ .../opensdk/ANMultiAdRequestApiTest.java | 12 +- .../opensdk/ANMultiAdRequestLoadTests.java | 6 +- ...MultiAdRequestToRequestParametersTest.java | 54 +++++- .../opensdk/ANOmidViewabiltyTests.java | 1 - .../opensdk/AdRequestToAdRequesterTest.java | 4 +- .../opensdk/AdViewRequestManagerTest.java | 5 + .../opensdk/BannerAdViewVideoLoadAdTest.java | 10 + .../com/appnexus/opensdk/BaseNativeTest.java | 7 +- .../com/appnexus/opensdk/BaseViewAdTest.java | 1 + .../ExecutorForBackgroundTasksTests.java | 8 +- .../opensdk/InterstitialAdViewLoadAdTest.java | 10 +- .../opensdk/MRAIDImplementationTest.java | 4 +- .../MediatedNativeAdViewControllerTest.java | 4 +- .../MediatedSSMAdViewControllerTest.java | 3 - .../appnexus/opensdk/NativeRequestTest.java | 6 +- .../com/appnexus/opensdk/TestResponsesUT.java | 11 ++ .../com/appnexus/opensdk/UTAdRequestTest.java | 40 +++- .../appnexus/opensdk/UTAdResponseTest.java | 12 +- .../mocks/MockFBNativeBannerAdResponse.java | 12 ++ .../testviews/MediatedNativeSuccessful.java | 12 ++ .../testviews/MediatedNativeSuccessful2.java | 12 ++ .../com/appnexussdktestapp/NativeActivity.kt | 2 +- 66 files changed, 1264 insertions(+), 181 deletions(-) create mode 100644 instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/ANAdResponseInfoVideoTests.java create mode 100644 sdk/src/com/appnexus/opensdk/ANAdResponseInfo.java create mode 100644 sdk/test/com/appnexus/opensdk/ANAdResponseInfoBannerTests.java create mode 100644 sdk/test/com/appnexus/opensdk/ANAdResponseInfoBannerVideoTests.java create mode 100644 sdk/test/com/appnexus/opensdk/ANAdResponseInfoInterstitialTests.java create mode 100644 sdk/test/com/appnexus/opensdk/ANAdResponseInfoNativeTest.java diff --git a/examples/java/SimpleBanner/app/src/main/java/com/example/simplebanner/MyActivity.java b/examples/java/SimpleBanner/app/src/main/java/com/example/simplebanner/MyActivity.java index 2b7928285..ba9ae4e8c 100644 --- a/examples/java/SimpleBanner/app/src/main/java/com/example/simplebanner/MyActivity.java +++ b/examples/java/SimpleBanner/app/src/main/java/com/example/simplebanner/MyActivity.java @@ -23,6 +23,7 @@ import android.os.Handler; import android.widget.RelativeLayout; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.ANClickThroughAction; import com.appnexus.opensdk.AdListener; import com.appnexus.opensdk.AdView; diff --git a/examples/java/SimpleSRM/app/src/main/java/com/example/simplesrm/MyActivity.java b/examples/java/SimpleSRM/app/src/main/java/com/example/simplesrm/MyActivity.java index 6f2701473..7fa36a0c3 100644 --- a/examples/java/SimpleSRM/app/src/main/java/com/example/simplesrm/MyActivity.java +++ b/examples/java/SimpleSRM/app/src/main/java/com/example/simplesrm/MyActivity.java @@ -36,6 +36,7 @@ import androidx.test.espresso.idling.CountingIdlingResource; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.ANClickThroughAction; import com.appnexus.opensdk.ANMultiAdRequest; import com.appnexus.opensdk.AdListener; @@ -164,7 +165,7 @@ public void onAdClicked(AdView adView, String clickUrl) { // adUnitList.add(adRequest); // adUnitList.add(videoAd); - anMultiAdRequest2 = new ANMultiAdRequest(this, 0, new MultiAdRequestListener() { + anMultiAdRequest2 = new ANMultiAdRequest(this, 0, 0, new MultiAdRequestListener() { @Override public void onMultiAdRequestCompleted() { msg += "MAR 2 Load Completed"; @@ -188,7 +189,7 @@ public void onMultiAdRequestFailed(ResultCode code) { // anMultiAdRequest2.load(); - anMultiAdRequest = new ANMultiAdRequest(this, 0, + anMultiAdRequest = new ANMultiAdRequest(this, 0, 0, new MultiAdRequestListener() { @Override public void onMultiAdRequestCompleted() { @@ -208,14 +209,14 @@ public void onMultiAdRequestFailed(ResultCode code) { }); anMultiAdRequest.addAdUnit(bav); anMultiAdRequest.addAdUnit(iav); -// anMultiAdRequest.addAdUnit(adRequest); -// anMultiAdRequest.addAdUnit(videoAd); + anMultiAdRequest.addAdUnit(adRequest); + anMultiAdRequest.addAdUnit(videoAd); // anMultiAdRequest.addAdUnit(setupVideoAd()); // bav = null; // System.gc(); // iav = null; // adRequest = null; -// load(); + load(); // bav.loadAd(); tv.setOnClickListener(new View.OnClickListener() { @@ -293,7 +294,7 @@ public void onAdLoaded(NativeAdResponse response) { } @Override - public void onAdFailed(ResultCode errorcode) { + public void onAdFailed(ResultCode errorcode, ANAdResponseInfo adResponseInfo) { msg += "Native Ad Failed:" + errorcode + "\n"; toast(); } diff --git a/examples/java/SimpleVideo/app/src/main/java/com/appnexus/example/simplevideo/MainActivity.java b/examples/java/SimpleVideo/app/src/main/java/com/appnexus/example/simplevideo/MainActivity.java index a6c485c3a..5bd88875c 100644 --- a/examples/java/SimpleVideo/app/src/main/java/com/appnexus/example/simplevideo/MainActivity.java +++ b/examples/java/SimpleVideo/app/src/main/java/com/appnexus/example/simplevideo/MainActivity.java @@ -32,6 +32,7 @@ import android.widget.Toast; import android.widget.VideoView; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.ResultCode; import com.appnexus.opensdk.SDKSettings; import com.appnexus.opensdk.instreamvideo.ResultCallback; diff --git a/examples/kotlin/SimpleDemo/app/src/main/java/appnexus/example/kotlinsample/NativeActivity.kt b/examples/kotlin/SimpleDemo/app/src/main/java/appnexus/example/kotlinsample/NativeActivity.kt index 9efba6823..c6ddee348 100644 --- a/examples/kotlin/SimpleDemo/app/src/main/java/appnexus/example/kotlinsample/NativeActivity.kt +++ b/examples/kotlin/SimpleDemo/app/src/main/java/appnexus/example/kotlinsample/NativeActivity.kt @@ -52,7 +52,7 @@ class NativeActivity : AppCompatActivity(),NativeAdRequestListener,NativeAdEvent NativeAdSDK.registerTracking(nativeAdResponse, nativeContainer, mutableListOf(clickThrough) as List?,this) } - override fun onAdFailed(errorcode: ResultCode) { + override fun onAdFailed(errorcode: ResultCode, adResponseinfo:ANAdResponseInfo) { log("Native Ad Failed: " + errorcode.name) } // NativeAdRequestListener - End diff --git a/examples/kotlin/SimpleDemo/app/src/main/java/appnexus/example/kotlinsample/VideoActivity.kt b/examples/kotlin/SimpleDemo/app/src/main/java/appnexus/example/kotlinsample/VideoActivity.kt index 9b124c71e..6be4b5103 100644 --- a/examples/kotlin/SimpleDemo/app/src/main/java/appnexus/example/kotlinsample/VideoActivity.kt +++ b/examples/kotlin/SimpleDemo/app/src/main/java/appnexus/example/kotlinsample/VideoActivity.kt @@ -6,6 +6,7 @@ import android.util.Log import android.view.View import android.widget.* import androidx.appcompat.app.AppCompatActivity +import com.appnexus.opensdk.ANAdResponseInfo import com.appnexus.opensdk.ResultCode import com.appnexus.opensdk.instreamvideo.* diff --git a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/InstreamVideoView.java b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/InstreamVideoView.java index b6319a173..3c224c878 100644 --- a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/InstreamVideoView.java +++ b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/InstreamVideoView.java @@ -196,15 +196,6 @@ public int getVideoAdDuration () { return 0; } - - - public String getCreativeId () { - if(this.videoWebView != null){ - return this.videoWebView.getCreativeId(); - } - return ""; - } - public String getVastXML () { if(this.videoWebView != null){ return this.videoWebView.getVastXML(); diff --git a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAd.java b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAd.java index d3772016b..8cf9fe237 100644 --- a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAd.java +++ b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAd.java @@ -17,11 +17,10 @@ import android.content.Context; -import android.graphics.drawable.Drawable; -import android.util.Pair; import android.widget.FrameLayout; import android.widget.RelativeLayout; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.ANClickThroughAction; import com.appnexus.opensdk.ANMultiAdRequest; import com.appnexus.opensdk.Ad; @@ -30,8 +29,8 @@ import com.appnexus.opensdk.AdType; import com.appnexus.opensdk.AdView; import com.appnexus.opensdk.AdViewRequestManager; -import com.appnexus.opensdk.MultiAd; import com.appnexus.opensdk.MediaType; +import com.appnexus.opensdk.MultiAd; import com.appnexus.opensdk.ResultCode; import com.appnexus.opensdk.VideoOrientation; import com.appnexus.opensdk.ut.UTAdRequester; @@ -61,6 +60,7 @@ public class VideoAd implements Ad, MultiAd { private InstreamVideoView videoAdView; boolean doesLoadingInBackground = true; private boolean showLoadingIndicator = true; + private ANAdResponseInfo adResponseInfo; public VideoAd(Context context, String placementID) { weakContext = new WeakReference(context); @@ -177,6 +177,24 @@ public int getMemberID() { return requestParameters.getMemberID(); } + /** + * Retrieve the Publisher ID. + * + * @return the Publisher id that this NativeAdRequest belongs to. + */ + public int getPublisherId() { + return requestParameters.getPublisherId(); + } + + /** + * Retrieve the Publisher ID. + * + * @@param publisherId the Publisher id that this NativeAdRequest belongs to. + */ + public void setPublisherId(int publisherId) { + requestParameters.setPublisherId(publisherId); + } + /** * Retrieve the inventory code. * @@ -399,7 +417,7 @@ public boolean loadAd() { protected void loadAdFromVAST(String VASTXML,int width,int height) { // load an ad directly from VASTXML VideoWebView output = new VideoWebView(this.getContext(),this, null); - RTBVASTAdResponse response = new RTBVASTAdResponse(width,height, AdType.VIDEO.toString(), null,null,"1"); + RTBVASTAdResponse response = new RTBVASTAdResponse(width,height, AdType.VIDEO.toString(), null,null, getAdResponseInfo()); response.setAdContent(VASTXML); response.setContentSource(UTConstants.RTB); response.addToExtras(UTConstants.EXTRAS_KEY_MRAID, true); @@ -498,9 +516,10 @@ public void onAdLoaded() { } @Override - public void onAdFailed(ResultCode errorCode) { + public void onAdFailed(ResultCode errorCode, ANAdResponseInfo adResponseInfo) { isLoading = false; validAdExists = false; + setAdResponseInfo(adResponseInfo); if (adLoadListener != null) { adLoadListener.onAdRequestFailed(VideoAd.this, errorCode); } @@ -730,15 +749,16 @@ public int getVideoAdDuration() { return 0; } + @Deprecated /** * Retrieve the Creative Id of the creative . * * @return the creativeId + * @deprecated use ({@link ANAdResponseInfo}.getCreativeId) */ - public String getCreativeId() { - if (videoAdView != null) { - return videoAdView.getCreativeId(); + if (getAdResponseInfo() != null) { + return getAdResponseInfo().getCreativeId(); } return ""; } @@ -778,6 +798,7 @@ public void getAdPlayElapsedTime(ResultCallback resultCallback) { @Override public void initiateVastAdView(BaseAdResponse response, AdViewRequestManager adViewRequestManager) { Clog.d(Clog.videoLogTag, "Creating WebView for::" + response.getContentSource()); + setAdResponseInfo(response.getAdResponseInfo()); VideoWebView adVideoView = new VideoWebView(getContext(), this, adViewRequestManager); getVideoAdView().setVideoWebView(adVideoView); adVideoView.loadAd(response); @@ -792,4 +813,13 @@ public void setRequestManager(UTAdRequester requester) { public MultiAd getMultiAd() { return this; } + + public ANAdResponseInfo getAdResponseInfo() { + return adResponseInfo; + } + + private void setAdResponseInfo(ANAdResponseInfo adResponseInfo) { + this.adResponseInfo = adResponseInfo; + } + } diff --git a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAdDispatcher.java b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAdDispatcher.java index ecbea04d9..ae0dd7c29 100644 --- a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAdDispatcher.java +++ b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAdDispatcher.java @@ -15,9 +15,7 @@ */ package com.appnexus.opensdk.instreamvideo; -import com.appnexus.opensdk.ANClickThroughAction; import com.appnexus.opensdk.BaseAdDispatcher; -import com.appnexus.opensdk.ResultCode; /** * This interface is used by internal classes of SDK to send callbacks to the VideoAd class. diff --git a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAdLoadListener.java b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAdLoadListener.java index b4c792354..fd4166d02 100644 --- a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAdLoadListener.java +++ b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoAdLoadListener.java @@ -29,8 +29,7 @@ public interface VideoAdLoadListener { /** * Called when an ad request has failed. Ad requests can fail * because no ad is available, or because of networking errors. - * - * @param videoAd The {@link VideoAd} that loaded the ad. + * @param videoAd The {@link VideoAd} that loaded the ad. * @param errorCode the ResultCode describing the failure. */ public void onAdRequestFailed(VideoAd videoAd, ResultCode errorCode); diff --git a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoWebView.java b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoWebView.java index 755da3bbd..95453c002 100644 --- a/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoWebView.java +++ b/instreamvideo/src/main/java/com/appnexus/opensdk/instreamvideo/VideoWebView.java @@ -74,7 +74,6 @@ class VideoWebView extends WebView { private static final int WAIT_INTERVAL_MILLES = 300; private int adDuration = 0; - private String creativeId = ""; private String creativeUrl = ""; private String vastURLContent = ""; private String vastXMLContent = ""; @@ -530,7 +529,6 @@ protected void loadAd(BaseAdResponse baseAdResponse) { return; } this.baseAdResponse = baseAdResponse; - this.creativeId = baseAdResponse.getCreativeId(); this.loadUrl(Settings.getVideoHtmlPage()); } @@ -554,11 +552,6 @@ protected VideoOrientation getVideoOrientation() { return ViewUtil.getVideoOrientation(aspectRatio); } - protected String getCreativeId() { - return this.creativeId; - } - - private class RedirectWebView extends WebView { @SuppressLint("SetJavaScriptEnabled") diff --git a/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/ANAdResponseInfoVideoTests.java b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/ANAdResponseInfoVideoTests.java new file mode 100644 index 000000000..09486400f --- /dev/null +++ b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/ANAdResponseInfoVideoTests.java @@ -0,0 +1,119 @@ +/* + * Copyright 2018 APPNEXUS INC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.appnexus.opensdk.instreamvideo; + +import com.appnexus.opensdk.ANAdResponseInfo; +import com.appnexus.opensdk.AdType; +import com.appnexus.opensdk.ResultCode; +import com.appnexus.opensdk.instreamvideo.shadows.ShadowAsyncTaskNoExecutor; +import com.appnexus.opensdk.instreamvideo.shadows.ShadowCustomWebView; +import com.appnexus.opensdk.instreamvideo.shadows.ShadowSettings; +import com.appnexus.opensdk.instreamvideo.shadows.ShadowWebSettings; +import com.appnexus.opensdk.ut.UTConstants; +import com.squareup.okhttp.mockwebserver.MockResponse; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowLog; + +import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +@Config(sdk = 21, + shadows = {ShadowAsyncTaskNoExecutor.class, + ShadowCustomWebView.class, ShadowWebSettings.class, ShadowSettings.class, ShadowLog.class}) +@RunWith(RobolectricTestRunner.class) +public class ANAdResponseInfoVideoTests extends BaseRoboTest implements VideoAdLoadListener { + + VideoAd videoAd; + ANAdResponseInfo adResponseInfo; + + @Override + public void setup() { + super.setup(); + videoAd = new VideoAd(activity,"12345"); + videoAd.setAdLoadListener(this); + } + + @Override + public void tearDown() { + super.tearDown(); + videoAd = null; + adResponseInfo = null; + } + + @Test + public void testAdResponseInfoRTBVideo() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestUTResponses.video())); // First queue a regular HTML banner response + assertNull(videoAd.getAdResponseInfo()); + executeVideoRequest(); + assertNotNull(videoAd.getAdResponseInfo()); + assertEquals(videoAd.getAdResponseInfo().getAdType(), AdType.VIDEO); + assertEquals(videoAd.getAdResponseInfo().getCreativeId(), "6332753"); + assertEquals(videoAd.getAdResponseInfo().getTagId(), "123456"); + assertEquals(videoAd.getAdResponseInfo().getBuyMemberId(), 123); + assertEquals(videoAd.getAdResponseInfo().getContentSource(), UTConstants.RTB); + assertEquals(videoAd.getAdResponseInfo().getNetworkName(), ""); + } + + @Test + public void testAdResponseInfoRTBVideoNoBid() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestUTResponses.NO_BID)); + assertNull(videoAd.getAdResponseInfo()); + executeVideoRequest(); + assertNotNull(videoAd.getAdResponseInfo()); + assertEquals(videoAd.getAdResponseInfo().getAdType(), null); + assertEquals(videoAd.getAdResponseInfo().getCreativeId(), ""); + assertEquals(videoAd.getAdResponseInfo().getTagId(), "123456789"); + assertEquals(videoAd.getAdResponseInfo().getBuyMemberId(), 0); + assertEquals(videoAd.getAdResponseInfo().getContentSource(), ""); + assertEquals(videoAd.getAdResponseInfo().getNetworkName(), ""); + } + + @Test + public void testAdResponseInfoRTBVideoBlank() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestUTResponses.blank())); + assertNull(videoAd.getAdResponseInfo()); + executeVideoRequest(); + assertNull(videoAd.getAdResponseInfo()); + } + + private void executeVideoRequest() { + videoAd.loadAd(); + + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + } + + @Override + public void onAdLoaded(VideoAd videoAd) { + + } + + @Override + public void onAdRequestFailed(VideoAd videoAd, ResultCode errorCode) { + } +} diff --git a/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/BaseRoboTest.java b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/BaseRoboTest.java index c93d35b4f..54cd6a182 100644 --- a/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/BaseRoboTest.java +++ b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/BaseRoboTest.java @@ -3,6 +3,7 @@ import android.app.Activity; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.instreamvideo.shadows.ShadowSettings; import com.appnexus.opensdk.instreamvideo.util.Lock; import com.appnexus.opensdk.ut.UTConstants; diff --git a/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/TestANClickThroughAction.java b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/TestANClickThroughAction.java index c6ad556de..021581fe3 100644 --- a/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/TestANClickThroughAction.java +++ b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/TestANClickThroughAction.java @@ -17,7 +17,6 @@ package com.appnexus.opensdk.instreamvideo; import com.appnexus.opensdk.ANClickThroughAction; -import com.appnexus.opensdk.BuildConfig; import com.appnexus.opensdk.ResultCode; import com.appnexus.opensdk.instreamvideo.shadows.ShadowAsyncTaskNoExecutor; import com.appnexus.opensdk.instreamvideo.shadows.ShadowCustomClickThroughWebView; diff --git a/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/TestUTResponses.java b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/TestUTResponses.java index d8a604380..8d52cb016 100644 --- a/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/TestUTResponses.java +++ b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/TestUTResponses.java @@ -20,8 +20,7 @@ public class TestUTResponses { public static String NO_AD_URL = "http://mobile.devnxs.net/no_ad_url?"; public static final String NO_BID_FALSE = "false"; - - + public static final String NO_BID = "{\"version\":\"3.0.0\",\"tags\":[{\"tag_id\":123456789,\"auction_id\":\"3552547938089377051000000\",\"nobid\":true,\"ad_profile_id\":2707239}]}"; private static final String DUMMY_VIDEO_CONTENT = "\n" + "\n" + diff --git a/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/VideoAdTest.java b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/VideoAdTest.java index f375275db..e2766d9d1 100644 --- a/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/VideoAdTest.java +++ b/instreamvideo/src/test/java/com/appnexus/opensdk/instreamvideo/VideoAdTest.java @@ -16,8 +16,6 @@ package com.appnexus.opensdk.instreamvideo; -import android.widget.FrameLayout; - import com.appnexus.opensdk.ResultCode; import com.appnexus.opensdk.VideoOrientation; import com.appnexus.opensdk.instreamvideo.shadows.ShadowAsyncTaskNoExecutor; diff --git a/sdk/src/com/appnexus/opensdk/ANAdResponseInfo.java b/sdk/src/com/appnexus/opensdk/ANAdResponseInfo.java new file mode 100644 index 000000000..d31767df1 --- /dev/null +++ b/sdk/src/com/appnexus/opensdk/ANAdResponseInfo.java @@ -0,0 +1,107 @@ +/* + * Copyright 2019 APPNEXUS INC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.appnexus.opensdk; + +public class ANAdResponseInfo { + + String creativeId = ""; + AdType adType; + String tagId = ""; + int memberID; + String contentSource = ""; + String networkName = ""; + + + /** + * Retrieve the Creative Id of the creative . + * + * @return the creativeId, returns empty string if not set + */ + public String getCreativeId() { + return creativeId; + } + + public void setCreativeId(String creativeId) { + this.creativeId = creativeId; + } + + /** + * Retrieve the AdType of the Ad being served + * AdType can be UNKNOWN/BANNER/VIDEO/NATIVE + * + * @return AdType of the Creative, default value is set to UNKNOWN if not set + */ + public AdType getAdType() { + return adType; + } + + public void setAdType(AdType adType) { + this.adType = adType; + } + + /** + * Retrieve the Tag Id + * @return the tag id of the Ad being served. + * */ + public String getTagId() { + return tagId; + } + + public void setTagId(String tagId) { + this.tagId = tagId; + } + + /** + * Retrieve the member ID. + * + * @return the member id that this Ad belongs to. + */ + public int getBuyMemberId() { + return memberID; + } + + public void setBuyMemberId(int memberID) { + this.memberID = memberID; + } + + /** + * Retrieve the content source + * + * @return the content source of the Ad being served. + */ + public String getContentSource() { + return contentSource; + } + + public void setContentSource(String contentSource) { + this.contentSource = contentSource; + } + + /** + * Retrieve the Network Name + * + * @return the network name of the mediated Ad being served. + */ + public String getNetworkName() { + return networkName; + } + + public void setNetworkName(String networkName) { + this.networkName = networkName; + } + +} diff --git a/sdk/src/com/appnexus/opensdk/ANMultiAdRequest.java b/sdk/src/com/appnexus/opensdk/ANMultiAdRequest.java index 0fb08508f..10fffd9cc 100644 --- a/sdk/src/com/appnexus/opensdk/ANMultiAdRequest.java +++ b/sdk/src/com/appnexus/opensdk/ANMultiAdRequest.java @@ -43,16 +43,18 @@ private ANMultiAdRequest() { * * @param context * @param memberId + * @param publisherId * @param ads * @param multiAdRequestListener * @param loadOnInit */ - public ANMultiAdRequest(Context context, int memberId, MultiAdRequestListener multiAdRequestListener, boolean loadOnInit, Ad... ads) { + public ANMultiAdRequest(Context context, int memberId, int publisherId, MultiAdRequestListener multiAdRequestListener, boolean loadOnInit, Ad... ads) { mAdFetcher = new AdFetcher(this); this.context = context; this.multiAdRequestListener = multiAdRequestListener; utRequestParameters = new UTRequestParameters(context); utRequestParameters.setMemberID(memberId); + utRequestParameters.setPublisherId(publisherId); if (ads != null && ads.length > 0) { for (Ad ad : ads) { addAdUnit(ad); @@ -80,10 +82,11 @@ public ANMultiAdRequest(Context context, int memberId, MultiAdRequestListener mu * * @param context * @param memberId + * @param publisherId * @param multiAdRequestListener */ - public ANMultiAdRequest(Context context, int memberId, MultiAdRequestListener multiAdRequestListener) { - this(context, memberId, multiAdRequestListener, false); + public ANMultiAdRequest(Context context, int memberId, int publisherId, MultiAdRequestListener multiAdRequestListener) { + this(context, memberId, publisherId, multiAdRequestListener, false); } @@ -108,6 +111,14 @@ public boolean addAdUnit(Ad adUnit) { Clog.e(Clog.SRMLogTag, "addAdUnit Failed: Member ID mismatch"); return false; } + + //Reads properties of AdUnit: Publisher ID + //Reject AdUnit if Publisher ID is set, but does not match MAR Publisher ID or if it does not match the Publisher ID of any other AdUnit. + if (utRequestParameters.getPublisherId() != 0 && adUnit.getRequestParameters().getPublisherId() != 0 && adUnit.getRequestParameters().getPublisherId() != utRequestParameters.getPublisherId()) { + Clog.e(Clog.SRMLogTag, "addAdUnit Failed: Publisher ID mismatch"); + return false; + } + //Reject AdUnit if delegate is already set if (adUnit.getMultiAdRequest() == null) { if (adUnit instanceof BannerAdView) { @@ -169,6 +180,20 @@ public ArrayList> getAdUnitList() { return utRequestParameters.getAdUnitList(); } + /** + * @return Member Id that is set to this instance of ANMultiAdRequest + * */ + public int getMemberId() { + return utRequestParameters.getMemberID(); + } + + /** + * @return Publisher Id that is set to this instance of ANMultiAdRequest + * */ + public int getPublisherId() { + return utRequestParameters.getPublisherId(); + } + // For End User public void setAge(String age) { utRequestParameters.setAge(age); @@ -259,12 +284,6 @@ public UTRequestParameters getRequestParameters() { return utRequestParameters; } - // TODO: check for the robustness of the MAR code when an AdUnit goes out of scope (UI Test) - // DONE: Mark the completion on receiving the UTResponse - // DONE: MAR load() can be called at anytime (cancel already running request) - // DONE: Removing the Internal Listener - // DONE: Passing the MAR instance to the Ad Units - public void onMARLoadCompleted() { if (ANMultiAdRequest.this.multiAdRequestListener != null) { ANMultiAdRequest.this.multiAdRequestListener.onMultiAdRequestCompleted(); diff --git a/sdk/src/com/appnexus/opensdk/Ad.java b/sdk/src/com/appnexus/opensdk/Ad.java index 152ed26b2..1b195831e 100644 --- a/sdk/src/com/appnexus/opensdk/Ad.java +++ b/sdk/src/com/appnexus/opensdk/Ad.java @@ -69,4 +69,5 @@ public interface Ad { * Not intended to be used outside of SDK. * */ MultiAd getMultiAd(); + } diff --git a/sdk/src/com/appnexus/opensdk/AdFetcher.java b/sdk/src/com/appnexus/opensdk/AdFetcher.java index 47127feb7..061df6304 100644 --- a/sdk/src/com/appnexus/opensdk/AdFetcher.java +++ b/sdk/src/com/appnexus/opensdk/AdFetcher.java @@ -219,7 +219,7 @@ synchronized public void handleMessage(Message msg) { fetcher.requestManager = new AdViewRequestManager(fetcher.owner); fetcher.requestManager.execute(); } else { - fetcher.owner.getAdDispatcher().onAdFailed(ResultCode.INVALID_REQUEST); + fetcher.owner.getAdDispatcher().onAdFailed(ResultCode.INVALID_REQUEST, null); } } } diff --git a/sdk/src/com/appnexus/opensdk/AdListener.java b/sdk/src/com/appnexus/opensdk/AdListener.java index 35ca34324..c9af394d8 100644 --- a/sdk/src/com/appnexus/opensdk/AdListener.java +++ b/sdk/src/com/appnexus/opensdk/AdListener.java @@ -39,8 +39,7 @@ public interface AdListener { /** * Called when an ad request has failed. Ad requests can fail * because no ad is available, or because of networking errors. - * - * @param adView The {@link com.appnexus.opensdk.AdView} that loaded the ad. + * @param adView The {@link AdView} that loaded the ad. * @param errorCode the error code describing the failure. */ public void onAdRequestFailed(AdView adView, ResultCode errorCode); diff --git a/sdk/src/com/appnexus/opensdk/AdView.java b/sdk/src/com/appnexus/opensdk/AdView.java index 244cf9c16..0699fa8ec 100644 --- a/sdk/src/com/appnexus/opensdk/AdView.java +++ b/sdk/src/com/appnexus/opensdk/AdView.java @@ -24,7 +24,6 @@ import android.content.MutableContextWrapper; import android.graphics.Color; import android.graphics.Point; -import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Handler; import android.os.Looper; @@ -82,6 +81,7 @@ public abstract class AdView extends FrameLayout implements Ad, MultiAd { UTRequestParameters requestParameters; protected ArrayList impressionTrackers; + private ANAdResponseInfo adResponseInfo; /** * Begin Construction @@ -222,7 +222,7 @@ protected void loadAdFromHtml(String html, int width, int height) { // load an ad directly from html loadedOffscreen = true; AdWebView output = new AdWebView(this, null); - RTBHTMLAdResponse response = new RTBHTMLAdResponse(width, height, getMediaType().toString(), null, getCreativeId()); + RTBHTMLAdResponse response = new RTBHTMLAdResponse(width, height, getMediaType().toString(), null, getAdResponseInfo()); response.setAdContent(html); output.loadAd(response); display(output); @@ -233,7 +233,7 @@ protected void loadAdFromVAST(String VASTXML, int width, int height) { // load an ad directly from VASTXML loadedOffscreen = true; AdWebView output = new AdWebView(this, null); - RTBVASTAdResponse response = new RTBVASTAdResponse(width, height, AdType.VIDEO.toString(), null, null, "1"); + RTBVASTAdResponse response = new RTBVASTAdResponse(width,height,AdType.VIDEO.toString(), null,null,getAdResponseInfo()); response.setAdContent(VASTXML); response.setContentSource(UTConstants.RTB); response.addToExtras(UTConstants.EXTRAS_KEY_MRAID, true); @@ -302,15 +302,35 @@ public void setInventoryCodeAndMemberID(int memberID, String inventoryCode) { requestParameters.setInventoryCodeAndMemberID(memberID, inventoryCode); } + @Deprecated /** * Retrieve the member ID. * * @return the member id that this AdView belongs to. + * @deprecated use {{@link ANAdResponseInfo}.getBuyMemberId} */ public int getMemberID() { return requestParameters.getMemberID(); } + /** + * Retrieve the Publisher ID. + * + * @return the Publisher id that this AdView belongs to. + */ + public int getPublisherId() { + return requestParameters.getPublisherId(); + } + + /** + * Retrieve the Publisher ID. + * + * @@param publisherId the Publisher id that this AdView belongs to. + */ + public void setPublisherId(int publisherId) { + requestParameters.setPublisherId(publisherId); + } + /** * Retrieve the inventory code. * @@ -937,12 +957,13 @@ public int getCreativeWidth() { } + @Deprecated /** * Retrieve the Creative Id of the creative . * * @return the creativeId + * @deprecated see ({@link ANAdResponseInfo}.getCreativeId) */ - public String getCreativeId() { return creativeId; } @@ -971,13 +992,14 @@ void setAdType(AdType type) { adType = type; } + @Deprecated /** * Retrieve the AdType being served on the AdView * AdType can be Banner/Video * * @return AdType of the Creative + * @deprecated Use ({@link ANAdResponseInfo}.getAdType) */ - public AdType getAdType() { return adType; } @@ -1025,7 +1047,7 @@ public void onAdLoaded(final AdResponse ad) { handleNativeAd(ad); } else { Clog.e(Clog.baseLogTag, "UNKNOWN media type::" + ad.getMediaType()); - onAdFailed(ResultCode.INTERNAL_ERROR); + onAdFailed(ResultCode.INTERNAL_ERROR, null); } } @@ -1035,10 +1057,11 @@ public void onAdLoaded() { } @Override - public void onAdFailed(final ResultCode code) { + public void onAdFailed(final ResultCode code, final ANAdResponseInfo adResponseInfo) { handler.post(new Runnable() { @Override public void run() { + setAdResponseInfo(adResponseInfo); if (adListener != null) { adListener.onAdRequestFailed(AdView.this, code); } @@ -1118,10 +1141,12 @@ public void run() { private void handleNativeAd(AdResponse ad) { setAdType(AdType.NATIVE); - setCreativeId(ad.getResponseData().getCreativeId()); + setCreativeId(ad.getResponseData().getAdResponseInfo().getCreativeId()); final NativeAdResponse response = ad.getNativeAdResponse(); - response.setCreativeId(ad.getResponseData().getCreativeId()); - if (adListener != null) { + response.setAdResponseInfo(ad.getResponseData().getAdResponseInfo()); +// setAdResponseInfo(ad.getResponseData().getAdResponseInfo()); + response.setCreativeId(ad.getResponseData().getAdResponseInfo().getCreativeId()); + if(adListener != null) { adListener.onAdLoaded(response); } } @@ -1132,7 +1157,8 @@ private void handleBannerOrInterstitialAd(final AdResponse ad) { public void run() { setCreativeWidth(ad.getDisplayable().getCreativeWidth()); setCreativeHeight(ad.getDisplayable().getCreativeHeight()); - setCreativeId(ad.getResponseData().getCreativeId()); + setCreativeId(ad.getResponseData().getAdResponseInfo().getCreativeId()); + setAdResponseInfo(ad.getResponseData().getAdResponseInfo()); if (ad.isMediated() && ad.getResponseData().getContentSource() == UTConstants.CSM) { try { displayMediated((MediatedDisplayable) ad.getDisplayable()); @@ -1298,4 +1324,12 @@ public void setRequestManager(UTAdRequester requester) { public MultiAd getMultiAd() { return this; } + + public ANAdResponseInfo getAdResponseInfo() { + return adResponseInfo; + } + + private void setAdResponseInfo(ANAdResponseInfo adResponseInfo) { + this.adResponseInfo = adResponseInfo; + } } diff --git a/sdk/src/com/appnexus/opensdk/AdViewRequestManager.java b/sdk/src/com/appnexus/opensdk/AdViewRequestManager.java index dec29e1ad..8561447f0 100644 --- a/sdk/src/com/appnexus/opensdk/AdViewRequestManager.java +++ b/sdk/src/com/appnexus/opensdk/AdViewRequestManager.java @@ -97,13 +97,13 @@ public UTRequestParameters getRequestParams() { } @Override - public void failed(ResultCode code) { + public void failed(ResultCode code, ANAdResponseInfo responseInfo) { printMediatedClasses(); Clog.e("AdViewRequestManager", code.name()); Ad owner = this.owner.get(); fireTracker(noAdUrl, Clog.getString(R.string.no_ad_url)); if (owner != null) { - owner.getAdDispatcher().onAdFailed(code); + owner.getAdDispatcher().onAdFailed(code, responseInfo); } } @@ -111,7 +111,7 @@ public void failed(ResultCode code) { public void continueWaterfall(ResultCode reason) { Clog.d(Clog.baseLogTag, "Waterfall continueWaterfall"); if (getAdList() == null || getAdList().isEmpty()) { - failed(reason); + failed(reason, currentAd != null ? currentAd.getAdResponseInfo() : null); } else { // Process next available ad response processNextAd(); @@ -175,7 +175,7 @@ public void onReceiveUTResponse(UTAdResponse response) { processNextAd(); } else { Clog.w(Clog.httpRespLogTag, Clog.getString(R.string.response_no_ads)); - failed(ResultCode.UNABLE_TO_FILL); + failed(ResultCode.UNABLE_TO_FILL, response.getAdResponseInfo()); } } @@ -266,7 +266,6 @@ private void handleRTBResponse(Ad ownerAd, BaseAdResponse rtbAdResponse) { if (UTConstants.AD_TYPE_VIDEO.equalsIgnoreCase(rtbAdResponse.getAdType())) { // Vast ads handleRTBVASTResponse(ownerAd, (RTBVASTAdResponse) rtbAdResponse); - } else { continueWaterfall(ResultCode.UNABLE_TO_FILL); } @@ -372,4 +371,5 @@ private void initiateWebview(final Ad owner, final BaseAdResponse response) { public ANMultiAdRequest getMultiAdRequest() { return anMultiAdRequest; } + } \ No newline at end of file diff --git a/sdk/src/com/appnexus/opensdk/BaseAdDispatcher.java b/sdk/src/com/appnexus/opensdk/BaseAdDispatcher.java index 6c23df997..446c98d5f 100644 --- a/sdk/src/com/appnexus/opensdk/BaseAdDispatcher.java +++ b/sdk/src/com/appnexus/opensdk/BaseAdDispatcher.java @@ -28,7 +28,7 @@ public interface BaseAdDispatcher { * * @param errorCode the error code describing the failure. */ - public void onAdFailed(ResultCode errorCode); + public void onAdFailed(ResultCode errorCode, ANAdResponseInfo responseInfo); /** * Called when the ad being clicked diff --git a/sdk/src/com/appnexus/opensdk/BaseNativeAdResponse.java b/sdk/src/com/appnexus/opensdk/BaseNativeAdResponse.java index 7faaa5bc1..d3b6c496a 100644 --- a/sdk/src/com/appnexus/opensdk/BaseNativeAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/BaseNativeAdResponse.java @@ -29,6 +29,7 @@ public abstract class BaseNativeAdResponse implements NativeAdResponse { protected ANOmidAdSession anOmidAdSession = new ANOmidAdSession(); + private ANAdResponseInfo adResponseInfo; protected abstract boolean registerView(View view, NativeAdEventListener listener); @@ -36,6 +37,17 @@ public abstract class BaseNativeAdResponse implements NativeAdResponse { protected abstract void unregisterViews(); + @Override + public ANAdResponseInfo getAdResponseInfo() { + return adResponseInfo; + } + + @Override + public void setAdResponseInfo(ANAdResponseInfo adResponseInfo) { + this.adResponseInfo = adResponseInfo; + } + + protected void registerViewforOMID(View view){ if(anOmidAdSession!=null && anOmidAdSession.isVerificationResourcesPresent()) { diff --git a/sdk/src/com/appnexus/opensdk/NativeAdRequest.java b/sdk/src/com/appnexus/opensdk/NativeAdRequest.java index 1c37dd704..5df3e88e9 100644 --- a/sdk/src/com/appnexus/opensdk/NativeAdRequest.java +++ b/sdk/src/com/appnexus/opensdk/NativeAdRequest.java @@ -223,6 +223,24 @@ public int getMemberID() { return requestParameters.getMemberID(); } + /** + * Retrieve the Publisher ID. + * + * @return the Publisher id that this NativeAdRequest belongs to. + */ + public int getPublisherId() { + return requestParameters.getPublisherId(); + } + + /** + * Retrieve the Publisher ID. + * + * @@param publisherId the Publisher id that this NativeAdRequest belongs to. + */ + public void setPublisherId(int publisherId) { + requestParameters.setPublisherId(publisherId); + } + /** * Retrieve the inventory code. * @@ -431,11 +449,12 @@ public void onAllImageDownloadsFinish() { @Override public void onAdLoaded(final AdResponse ad) { if (!ad.getMediaType().equals(MediaType.NATIVE)) { - onAdFailed(ResultCode.INTERNAL_ERROR); + onAdFailed(ResultCode.INTERNAL_ERROR, null); } else { final String IMAGE_URL = "image", ICON_URL = "icon"; final NativeAdResponse response = ad.getNativeAdResponse(); - response.setCreativeId(ad.getResponseData().getCreativeId()); + response.setAdResponseInfo(ad.getResponseData().getAdResponseInfo()); + response.setCreativeId(ad.getResponseData().getAdResponseInfo().getCreativeId()); if (!loadImage && !loadIcon) { if (listener != null) { listener.onAdLoaded(response); @@ -478,9 +497,9 @@ public void onAdLoaded() { } @Override - public void onAdFailed(ResultCode resultCode) { + public void onAdFailed(ResultCode resultCode, ANAdResponseInfo adResponseInfo) { if (listener != null) { - listener.onAdFailed(resultCode); + listener.onAdFailed(resultCode, adResponseInfo); } isLoading = false; } @@ -549,4 +568,5 @@ public void setRequestManager(UTAdRequester requestManager) { public MultiAd getMultiAd() { return this; } + } diff --git a/sdk/src/com/appnexus/opensdk/NativeAdRequestListener.java b/sdk/src/com/appnexus/opensdk/NativeAdRequestListener.java index 39a19d3d3..e79368931 100644 --- a/sdk/src/com/appnexus/opensdk/NativeAdRequestListener.java +++ b/sdk/src/com/appnexus/opensdk/NativeAdRequestListener.java @@ -35,6 +35,7 @@ public interface NativeAdRequestListener { /** * Called when a native ad call has failed * @param errorcode reason the call failed. Error codes TBD + * @param adResponseInfo */ - public void onAdFailed(ResultCode errorcode); + public void onAdFailed(ResultCode errorcode, ANAdResponseInfo adResponseInfo); } diff --git a/sdk/src/com/appnexus/opensdk/NativeAdResponse.java b/sdk/src/com/appnexus/opensdk/NativeAdResponse.java index 7da5ae461..eec9da999 100644 --- a/sdk/src/com/appnexus/opensdk/NativeAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/NativeAdResponse.java @@ -80,14 +80,16 @@ public enum Network{ public void setImage(Bitmap bitmap); + @Deprecated /** * Retrieve the creative Id * * @return empty string "" if not present - * + * @deprecated Use ({@link ANAdResponseInfo}.getCreativeId) */ public String getCreativeId(); + @Deprecated /** * Set the creative Id * @@ -95,6 +97,20 @@ public enum Network{ */ public void setCreativeId(String creativeId); + /** + * Retrieve the AdResponseInfo + * + * @return {@link ANAdResponseInfo} + */ + public ANAdResponseInfo getAdResponseInfo(); + + /** + * Set the AdResponseInfo + * + * @param adResponseInfo + */ + public void setAdResponseInfo(ANAdResponseInfo adResponseInfo); + /** * The URL of the Icon image resource can be used to manage image resources manually diff --git a/sdk/src/com/appnexus/opensdk/RequestManager.java b/sdk/src/com/appnexus/opensdk/RequestManager.java index 07491ee6c..52dbd5811 100644 --- a/sdk/src/com/appnexus/opensdk/RequestManager.java +++ b/sdk/src/com/appnexus/opensdk/RequestManager.java @@ -46,7 +46,7 @@ public abstract class RequestManager implements UTAdRequester { * * @param code reason why the request fails. */ - public abstract void failed(ResultCode code); + public abstract void failed(ResultCode code, ANAdResponseInfo responseInfo); public abstract void onReceiveAd(AdResponse ad); diff --git a/sdk/src/com/appnexus/opensdk/ut/UTAdRequest.java b/sdk/src/com/appnexus/opensdk/ut/UTAdRequest.java index 6a9b1dfef..0c898c783 100644 --- a/sdk/src/com/appnexus/opensdk/ut/UTAdRequest.java +++ b/sdk/src/com/appnexus/opensdk/ut/UTAdRequest.java @@ -86,7 +86,7 @@ private void fail(ResultCode code) { if (ad != null) { UTAdRequester requester = new AdViewRequestManager(ad); if (requester != null) { - requester.failed(code); + requester.failed(code, null); } } } @@ -94,7 +94,7 @@ private void fail(ResultCode code) { } UTAdRequester requester = this.requester.get(); if (requester != null) { - requester.failed(code); + requester.failed(code, null); } Clog.clearLastResponse(); } @@ -240,15 +240,10 @@ protected void onPostExecute(HashMap adResponseMap) { Clog.d(Clog.SRMLogTag, "RECIEVED: " + ad); if (ad != null) { UTAdRequester requester = new AdViewRequestManager(ad); - // TODO: find other way for this ad.getMultiAd().setRequestManager(requester); -// ad.getAdFetcher().getRequestManager(); - //TODO: To be discussed for using new AdViewRequestManager(), - // Although this will initialize new RequestManager for every request - // but it prevents exposing the AdFetcher and RequestManager. if (adResponseMap == null) { Clog.e(Clog.SRMLogTag, "FAILED: " + ResultCode.INVALID_REQUEST); - requester.failed(ResultCode.INVALID_REQUEST); + requester.failed(ResultCode.INVALID_REQUEST, null); continue; } UTAdResponse result = adResponseMap.get(ad.getRequestParameters().getUUID()); @@ -257,12 +252,12 @@ protected void onPostExecute(HashMap adResponseMap) { if (requester != null) { if (result == null) { Clog.e(Clog.SRMLogTag, "FAILED: " + ResultCode.NETWORK_ERROR); - requester.failed(ResultCode.NETWORK_ERROR); + requester.failed(ResultCode.NETWORK_ERROR, null); continue; } if (result.isHttpError()) { Clog.e(Clog.SRMLogTag, "FAILED: " + ResultCode.NETWORK_ERROR); - requester.failed(ResultCode.NETWORK_ERROR); + requester.failed(ResultCode.NETWORK_ERROR, null); continue; } Clog.e(Clog.SRMLogTag, "SUCCESS: " + ad); diff --git a/sdk/src/com/appnexus/opensdk/ut/UTAdRequester.java b/sdk/src/com/appnexus/opensdk/ut/UTAdRequester.java index ef9035c56..0d117d942 100644 --- a/sdk/src/com/appnexus/opensdk/ut/UTAdRequester.java +++ b/sdk/src/com/appnexus/opensdk/ut/UTAdRequester.java @@ -16,6 +16,7 @@ package com.appnexus.opensdk.ut; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.AdResponse; import com.appnexus.opensdk.ResultCode; import com.appnexus.opensdk.ut.adresponse.BaseAdResponse; @@ -29,7 +30,7 @@ public interface UTAdRequester { * * @param code reason why the request fails. */ - void failed(ResultCode code); + void failed(ResultCode code, ANAdResponseInfo responseInfo); void onReceiveAd(AdResponse ad); diff --git a/sdk/src/com/appnexus/opensdk/ut/UTAdResponse.java b/sdk/src/com/appnexus/opensdk/ut/UTAdResponse.java index 785c62e0a..df05b3921 100644 --- a/sdk/src/com/appnexus/opensdk/ut/UTAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/ut/UTAdResponse.java @@ -17,7 +17,9 @@ import android.text.TextUtils; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.ANNativeAdResponse; +import com.appnexus.opensdk.AdType; import com.appnexus.opensdk.MediaType; import com.appnexus.opensdk.R; import com.appnexus.opensdk.ut.adresponse.BaseAdResponse; @@ -79,6 +81,7 @@ public class UTAdResponse { private static final String RESPONSE_KEY_TAG_ID = "tag_id"; private static final String RESPONSE_KEY_AUCTION_ID = "auction_id"; private static final String RESPONSE_KEY_SECOND_PRICE = "second_price"; + private static final String RESPONSE_KEY_BUYER_MEMBER_ID = "buyer_member_id"; private JSONObject tag; private Integer key = null; @@ -92,7 +95,7 @@ public class UTAdResponse { private int timeout; private MediaType mediaType; private String orientation; - + private ANAdResponseInfo adResponseInfo; public UTAdResponse(String body, Map> headers, MediaType requestMediaType, String orientation) { this(body, null, headers, requestMediaType, orientation); @@ -163,6 +166,9 @@ private void parseResponseV2(String body) { } // If it contains nobid response, don't parse further. if (JsonUtil.getJSONBoolean(tagObject, RESPONSE_KEY_NO_BID)) { + adResponseInfo = new ANAdResponseInfo(); + tagId = JsonUtil.getJSONInt(tagObject, RESPONSE_KEY_TAG_ID); + adResponseInfo.setTagId(String.valueOf(tagId)); return; } @@ -197,15 +203,23 @@ private void handleAdResponse(JSONObject response) throws Exception { String notifyUrl = JsonUtil.getJSONString(ad, RESPONSE_KEY_NOTIFY_URL); String contentSource = JsonUtil.getJSONString(ad, RESPONSE_KEY_CONTENT_SOURCE); String creativeId = JsonUtil.getJSONString(ad, RESPONSE_KEY_CREATIVE_ID); + int memberId = JsonUtil.getJSONInt(ad, RESPONSE_KEY_BUYER_MEMBER_ID); + + ANAdResponseInfo adResponseInfo = new ANAdResponseInfo(); + adResponseInfo.setAdType(getAdType(adType)); + adResponseInfo.setTagId(String.valueOf(tagId)); + adResponseInfo.setCreativeId(creativeId); + adResponseInfo.setContentSource(contentSource); + adResponseInfo.setBuyMemberId(memberId); if (contentSource != null && contentSource.equalsIgnoreCase(UTConstants.CSM)) { - handleCSM(ad, adType, notifyUrl, creativeId); + handleCSM(ad, adType, notifyUrl, adResponseInfo); } else if (contentSource != null && contentSource.equalsIgnoreCase(UTConstants.SSM)) { - handleSSM(ad, adType, creativeId); + handleSSM(ad, adType, adResponseInfo); } else if (contentSource != null && contentSource.equalsIgnoreCase(UTConstants.RTB)) { - handleRTB(ad, adType, notifyUrl, creativeId); + handleRTB(ad, adType, notifyUrl, adResponseInfo); } else if (UTConstants.CSR.equalsIgnoreCase(contentSource)) { - handleCSR(ad, adType, notifyUrl, creativeId); + handleCSR(ad, adType, notifyUrl, adResponseInfo); } else { Clog.e(Clog.httpRespLogTag, "handleAdResponse unknown content_source"); } @@ -213,14 +227,14 @@ private void handleAdResponse(JSONObject response) throws Exception { } } - private void handleCSR(JSONObject adObject, String adType, String notifyUrl, String createivId) { + private void handleCSR(JSONObject adObject, String adType, String notifyUrl, ANAdResponseInfo adResponseInfo) { JSONObject csrObject = JsonUtil.getJSONObject(adObject, UTConstants.CSR); if (csrObject != null) { - parseCSRNativeBannerAd(adObject, createivId, adType, notifyUrl); + parseCSRNativeBannerAd(adObject, adResponseInfo, adType, notifyUrl); } } - private void parseCSRNativeBannerAd(JSONObject adObject, String creativeId, String adType, String notifyUrl) { + private void parseCSRNativeBannerAd(JSONObject adObject, ANAdResponseInfo adResponseInfo, String adType, String notifyUrl) { JSONObject csr = JsonUtil.getJSONObject(adObject, UTConstants.CSR); if (csr != null) { @@ -248,7 +262,7 @@ private void parseCSRNativeBannerAd(JSONObject adObject, String creativeId, Stri if (!StringUtil.isEmpty(className)) { - CSRAdResponse csrAd = new CSRAdResponse(width, height, adType, responseUrl, impressionUrls, creativeId, adObject); + CSRAdResponse csrAd = new CSRAdResponse(width, height, adType, responseUrl, impressionUrls, adResponseInfo, adObject); csrAd.setClassName(className); csrAd.setClickUrls(clickeUrls); csrAd.setPayload(payload); @@ -262,18 +276,18 @@ private void parseCSRNativeBannerAd(JSONObject adObject, String creativeId, Stri } } - private void handleRTB(JSONObject adObject, String adType, String notifyUrl, String creativeId) throws Exception { + private void handleRTB(JSONObject adObject, String adType, String notifyUrl, ANAdResponseInfo adResponseInfo) throws Exception { JSONObject rtbObject = JsonUtil.getJSONObject(adObject, UTConstants.RTB); if (rtbObject != null) { if (rtbObject.has(UTConstants.AD_TYPE_BANNER)) { Clog.i(Clog.httpRespLogTag, "it's an HTML Ad"); - parseHtmlAdResponse(rtbObject, adType, creativeId); + parseHtmlAdResponse(rtbObject, adType, adResponseInfo); } else if (rtbObject.has(UTConstants.AD_TYPE_VIDEO)) { Clog.i(Clog.httpRespLogTag, "it's a Video Ad"); - parseVideoAdResponse(rtbObject, adType, notifyUrl, creativeId); + parseVideoAdResponse(rtbObject, adType, notifyUrl, adResponseInfo); } else if (rtbObject.has(UTConstants.AD_TYPE_NATIVE)) { Clog.i(Clog.httpRespLogTag, "it's a NATIVE Ad"); - parseNativeAds(adObject, creativeId, adType); + parseNativeAds(adObject, adResponseInfo, adType); } else { Clog.e(Clog.httpRespLogTag, "handleRTB UNKNOWN AD_TYPE"); } @@ -281,7 +295,7 @@ private void handleRTB(JSONObject adObject, String adType, String notifyUrl, Str } - private void parseHtmlAdResponse(JSONObject rtbObject, String adType, String creativeId) throws Exception { + private void parseHtmlAdResponse(JSONObject rtbObject, String adType, ANAdResponseInfo adResponseInfo) throws Exception { JSONObject bannerObject = JsonUtil.getJSONObject(rtbObject, UTConstants.AD_TYPE_BANNER); if (bannerObject != null) { int height = JsonUtil.getJSONInt(bannerObject, RESPONSE_KEY_HEIGHT); @@ -291,7 +305,7 @@ private void parseHtmlAdResponse(JSONObject rtbObject, String adType, String cre if (StringUtil.isEmpty(content)) { Clog.e(Clog.httpRespLogTag, Clog.getString(R.string.blank_ad)); } else { - RTBHTMLAdResponse rtbAd = new RTBHTMLAdResponse(width, height, adType, getImpressionUrls(rtbObject), creativeId); + RTBHTMLAdResponse rtbAd = new RTBHTMLAdResponse(width, height, adType, getImpressionUrls(rtbObject), adResponseInfo); rtbAd.setAdContent(content); rtbAd.setContentSource(UTConstants.RTB); if (content.contains(UTConstants.MRAID_JS_FILENAME)) { @@ -309,9 +323,10 @@ private void parseHtmlAdResponse(JSONObject rtbObject, String adType, String cre * * @param rtbObject (JSONObject) * @param adType (String) + * @param adResponseInfo * @throws Exception */ - private void parseVideoAdResponse(JSONObject rtbObject, String adType, String notifyUrl, String creativeId) throws Exception { + private void parseVideoAdResponse(JSONObject rtbObject, String adType, String notifyUrl, ANAdResponseInfo adResponseInfo) throws Exception { JSONObject videoObject = JsonUtil.getJSONObject(rtbObject, UTConstants.AD_TYPE_VIDEO); if (videoObject != null) { @@ -320,7 +335,7 @@ private void parseVideoAdResponse(JSONObject rtbObject, String adType, String no int width = JsonUtil.getJSONInt(videoObject, RESPONSE_KEY_PLAYER_WIDTH); //String vastResponse = JsonUtil.getJSONString(videoObject,RESPONSE_KEY_ASSET_URL); if (!StringUtil.isEmpty(vastResponse)) { - RTBVASTAdResponse rtbAd = new RTBVASTAdResponse(width, height, adType, notifyUrl, getImpressionUrls(rtbObject), creativeId); + RTBVASTAdResponse rtbAd = new RTBVASTAdResponse(width, height, adType, notifyUrl, getImpressionUrls(rtbObject), adResponseInfo); rtbAd.setAdContent(vastResponse); rtbAd.setContentSource(UTConstants.RTB); rtbAd.addToExtras(UTConstants.EXTRAS_KEY_MRAID, true); @@ -330,31 +345,31 @@ private void parseVideoAdResponse(JSONObject rtbObject, String adType, String no } // returns true if response contains a native response, false if not - private void parseNativeAds(JSONObject response, String creativeId, String adType) { + private void parseNativeAds(JSONObject response, ANAdResponseInfo adResponseInfo, String adType) { if (response != null) { ANNativeAdResponse anNativeAdResponse = ANNativeAdResponse.create(response); if (anNativeAdResponse != null) { - RTBNativeAdResponse nativeRTB = new RTBNativeAdResponse(1, 1, adType, anNativeAdResponse, null, creativeId); + RTBNativeAdResponse nativeRTB = new RTBNativeAdResponse(1, 1, adType, anNativeAdResponse, null, adResponseInfo); nativeRTB.setContentSource(UTConstants.RTB); adList.add(nativeRTB); } } } - private void handleCSM(JSONObject ad, String adType, String notifyUrl, String creativeId) { + private void handleCSM(JSONObject ad, String adType, String notifyUrl, ANAdResponseInfo adResponseInfo) { if (adType.equalsIgnoreCase(UTConstants.AD_TYPE_BANNER)) { Clog.i(Clog.httpRespLogTag, "Parsing SDK Mediation Ad"); - parseCSMSDKMediation(ad, adType, creativeId); + parseCSMSDKMediation(ad, adType, adResponseInfo); } else if (adType.equalsIgnoreCase(UTConstants.AD_TYPE_NATIVE)) { Clog.i(Clog.httpRespLogTag, "Parsing Native Mediation Ad"); - parseCSMSDKMediation(ad, adType, creativeId); + parseCSMSDKMediation(ad, adType, adResponseInfo); } else if (adType.equalsIgnoreCase(UTConstants.AD_TYPE_VIDEO)) { Clog.i(Clog.httpRespLogTag, "Parsing Video CSM Ad"); - parseVideoCSMResponse(ad, adType, notifyUrl, creativeId); + parseVideoCSMResponse(ad, adType, notifyUrl, adResponseInfo); } } - private void parseCSMSDKMediation(JSONObject ad, String adType, String creativeId) { + private void parseCSMSDKMediation(JSONObject ad, String adType, ANAdResponseInfo adResponseInfo) { JSONObject csm = JsonUtil.getJSONObject(ad, UTConstants.CSM); @@ -381,7 +396,6 @@ private void parseCSMSDKMediation(JSONObject ad, String adType, String creativeI String adId = JsonUtil.getJSONString(handlerElement, RESPONSE_KEY_ID); String secondPrice = JsonUtil.getJSONString(handlerElement, RESPONSE_KEY_SECOND_PRICE); - if (param.contains("\"optimized\":true") && !StringUtil.isEmpty(secondPrice)) { try { JSONObject paramObj = new JSONObject(param); @@ -393,7 +407,8 @@ private void parseCSMSDKMediation(JSONObject ad, String adType, String creativeI } if (!StringUtil.isEmpty(className)) { - CSMSDKAdResponse csmAd = new CSMSDKAdResponse(width, height, adType, responseUrl, impressionUrls, creativeId, ad); + adResponseInfo.setNetworkName(className); + CSMSDKAdResponse csmAd = new CSMSDKAdResponse(width, height, adType, responseUrl, impressionUrls, adResponseInfo, ad); csmAd.setClassName(className); csmAd.setId(adId); csmAd.setParam(param); @@ -407,12 +422,12 @@ private void parseCSMSDKMediation(JSONObject ad, String adType, String creativeI } } - private void parseVideoCSMResponse(JSONObject ad, String adType, String notifyUrl, String creativeId) { + private void parseVideoCSMResponse(JSONObject ad, String adType, String notifyUrl, ANAdResponseInfo adResponseInfo) { JSONObject csm = JsonUtil.getJSONObject(ad, UTConstants.CSM); if (csm != null) { JSONArray handler = JsonUtil.getJSONArray(csm, RESPONSE_KEY_HANDLER); if (handler != null) { - CSMVASTAdResponse csmVideoAd = new CSMVASTAdResponse(-1, -1, adType, null, creativeId, uuid); + CSMVASTAdResponse csmVideoAd = new CSMVASTAdResponse(-1, -1, adType, null, adResponseInfo, uuid); csmVideoAd.setAdJSONContent(ad); csmVideoAd.setAuction_id(String.valueOf(auctionID)); csmVideoAd.setTag_id(tagId); @@ -424,7 +439,7 @@ private void parseVideoCSMResponse(JSONObject ad, String adType, String notifyUr } - private void handleSSM(JSONObject ad, String adType, String creativeId) { + private void handleSSM(JSONObject ad, String adType, ANAdResponseInfo adResponseInfo) { JSONObject ssm = JsonUtil.getJSONObject(ad, UTConstants.SSM); if (ssm != null) { JSONArray handler = JsonUtil.getJSONArray(ssm, RESPONSE_KEY_HANDLER); @@ -441,7 +456,7 @@ private void handleSSM(JSONObject ad, String adType, String creativeId) { String handlerUrl = JsonUtil.getJSONString(handlerElement, RESPONSE_KEY_HANDLER_URL); if (!StringUtil.isEmpty(handlerUrl)) { String responseUrl = JsonUtil.getJSONString(ssm, RESPONSE_KEY_RESPONSE_URL); - SSMHTMLAdResponse ssmAd = new SSMHTMLAdResponse(width, height, adType, responseUrl, getImpressionUrls(ssm), creativeId); + SSMHTMLAdResponse ssmAd = new SSMHTMLAdResponse(width, height, adType, responseUrl, getImpressionUrls(ssm), adResponseInfo); ssmAd.setAdUrl(handlerUrl); ssmAd.setSsmTimeout(ssmTimeout); ssmAd.setContentSource(UTConstants.SSM); @@ -518,4 +533,18 @@ public String getNoAdUrl() { return noAdUrl; } + public ANAdResponseInfo getAdResponseInfo() { + return adResponseInfo; + } + + private AdType getAdType(String adTypeString){ + if (adTypeString.equalsIgnoreCase(UTConstants.AD_TYPE_BANNER)) { + return AdType.BANNER; + } else if (adTypeString.equalsIgnoreCase(UTConstants.AD_TYPE_VIDEO)) { + return AdType.VIDEO; + } else if (adTypeString.equalsIgnoreCase(UTConstants.AD_TYPE_NATIVE)) { + return AdType.NATIVE; + } + return AdType.UNKNOWN; + } } \ No newline at end of file diff --git a/sdk/src/com/appnexus/opensdk/ut/UTRequestParameters.java b/sdk/src/com/appnexus/opensdk/ut/UTRequestParameters.java index efae5f7c3..6202f0068 100644 --- a/sdk/src/com/appnexus/opensdk/ut/UTRequestParameters.java +++ b/sdk/src/com/appnexus/opensdk/ut/UTRequestParameters.java @@ -61,6 +61,7 @@ public class UTRequestParameters { private String placementID; private String externalUid; private int memberID; + private int publisherId; private String invCode; private boolean doesLoadingInBackground = true; private AdSize primarySize; @@ -127,6 +128,7 @@ public class UTRequestParameters { private static final String APP_ID = "appid"; private static final String KEYWORDS = "keywords"; private static final String MEMBER_ID = "member_id"; + private static final String PUBLISHER_ID = "publisher_id"; private static final String KEYVAL_KEY = "key"; private static final String KEYVAL_VALUE = "value"; private static final String SDK_VERSION = "sdkver"; @@ -488,9 +490,11 @@ String getPostData() { ANMultiAdRequest multiAdRequest = anMultiAdRequest.get(); ArrayList> customKeywords = getCustomKeywords(); int memberID = getMemberID(); + int publisherId = getPublisherId(); if (multiAdRequest != null) { customKeywords = multiAdRequest.getCustomKeywords(); memberID = multiAdRequest.getRequestParameters().getMemberID(); + publisherId = multiAdRequest.getRequestParameters().getPublisherId(); } JSONArray keywordsArray = getCustomKeywordsArray(customKeywords); if (keywordsArray != null && keywordsArray.length() > 0) { @@ -501,6 +505,10 @@ String getPostData() { if (memberID > 0) { postData.put(MEMBER_ID, memberID); } + + if (publisherId > 0) { + postData.put(PUBLISHER_ID, publisherId); + } } // add GDPR Consent @@ -561,6 +569,10 @@ private JSONArray getTagsObject(JSONObject postData) { tag.put(TAG_UUID, uuid); } + if ((anMultiAdRequest.get() == null) && utRequestParameters.getPublisherId() > 0) { + postData.put(PUBLISHER_ID, utRequestParameters.getPublisherId()); + } + JSONObject primesize = new JSONObject(); primesize.put(SIZE_WIDTH, utRequestParameters.primarySize.width()); primesize.put(SIZE_HEIGHT, utRequestParameters.primarySize.height()); @@ -1010,4 +1022,12 @@ public void associateWithMultiAdRequest(ANMultiAdRequest anMultiAdRequest) { public void disassociateFromMultiAdRequest() { this.anMultiAdRequest = new WeakReference<>(null); } + + public int getPublisherId() { + return publisherId; + } + + public void setPublisherId(int publisherId) { + this.publisherId = publisherId; + } } \ No newline at end of file diff --git a/sdk/src/com/appnexus/opensdk/ut/adresponse/BaseAdResponse.java b/sdk/src/com/appnexus/opensdk/ut/adresponse/BaseAdResponse.java index 718c99909..5197671af 100644 --- a/sdk/src/com/appnexus/opensdk/ut/adresponse/BaseAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/ut/adresponse/BaseAdResponse.java @@ -16,6 +16,8 @@ package com.appnexus.opensdk.ut.adresponse; +import com.appnexus.opensdk.ANAdResponseInfo; + import java.util.ArrayList; import java.util.HashMap; @@ -24,7 +26,7 @@ public abstract class BaseAdResponse { private int width; private int height; - private String creativeId = ""; + private ANAdResponseInfo adResponseInfo; private String adType; private String contentSource; @@ -33,22 +35,14 @@ public abstract class BaseAdResponse { private HashMap extras = new HashMap(); - public BaseAdResponse(int width, int height, String adType, ArrayList impressionURLs , String creativeId) { + public BaseAdResponse(int width, int height, String adType, ArrayList impressionURLs , ANAdResponseInfo adResponseInfo) { this.width = width; this.height = height; this.adType = adType; - this.creativeId = creativeId; + this.adResponseInfo = adResponseInfo; this.impressionURLs = impressionURLs; } - public String getCreativeId() { - return creativeId; - } - - public void setCreativeId(String creativeId) { - this.creativeId = creativeId; - } - public int getWidth() { return width; } @@ -110,4 +104,11 @@ public void setAdContent(String adContent) { this.adContent = adContent; } + public ANAdResponseInfo getAdResponseInfo() { + return adResponseInfo; + } + + public void setAdResponseInfo(ANAdResponseInfo adResponseInfo) { + this.adResponseInfo = adResponseInfo; + } } diff --git a/sdk/src/com/appnexus/opensdk/ut/adresponse/CSMSDKAdResponse.java b/sdk/src/com/appnexus/opensdk/ut/adresponse/CSMSDKAdResponse.java index a212d3771..ff8f76922 100644 --- a/sdk/src/com/appnexus/opensdk/ut/adresponse/CSMSDKAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/ut/adresponse/CSMSDKAdResponse.java @@ -16,6 +16,8 @@ package com.appnexus.opensdk.ut.adresponse; +import com.appnexus.opensdk.ANAdResponseInfo; + import org.json.JSONObject; import java.util.ArrayList; @@ -29,8 +31,8 @@ public class CSMSDKAdResponse extends BaseAdResponse { private String responseUrl; private JSONObject adObject; - public CSMSDKAdResponse(int width, int height, String adType, String responseUrl, ArrayList impressionURLs , String creativeId,JSONObject adObject) { - super(width, height, adType, impressionURLs , creativeId); + public CSMSDKAdResponse(int width, int height, String adType, String responseUrl, ArrayList impressionURLs , ANAdResponseInfo adResponseInfo, JSONObject adObject) { + super(width, height, adType, impressionURLs , adResponseInfo); this.responseUrl = responseUrl; this.adObject = adObject; } diff --git a/sdk/src/com/appnexus/opensdk/ut/adresponse/CSMVASTAdResponse.java b/sdk/src/com/appnexus/opensdk/ut/adresponse/CSMVASTAdResponse.java index 409017ac4..9dee7fcb5 100644 --- a/sdk/src/com/appnexus/opensdk/ut/adresponse/CSMVASTAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/ut/adresponse/CSMVASTAdResponse.java @@ -16,6 +16,8 @@ package com.appnexus.opensdk.ut.adresponse; +import com.appnexus.opensdk.ANAdResponseInfo; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -34,8 +36,8 @@ public class CSMVASTAdResponse extends BaseAdResponse { private JSONObject adJSONContent; private String uuid; - public CSMVASTAdResponse(int width, int height, String adType, ArrayList impressionURLs, String creativeId, String uuid) { - super(width, height, adType, impressionURLs , creativeId); + public CSMVASTAdResponse(int width, int height, String adType, ArrayList impressionURLs, ANAdResponseInfo adResponseInfo, String uuid) { + super(width, height, adType, impressionURLs , adResponseInfo); this.uuid = uuid; } diff --git a/sdk/src/com/appnexus/opensdk/ut/adresponse/CSRAdResponse.java b/sdk/src/com/appnexus/opensdk/ut/adresponse/CSRAdResponse.java index e59787b7a..72986153b 100644 --- a/sdk/src/com/appnexus/opensdk/ut/adresponse/CSRAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/ut/adresponse/CSRAdResponse.java @@ -16,6 +16,8 @@ package com.appnexus.opensdk.ut.adresponse; +import com.appnexus.opensdk.ANAdResponseInfo; + import org.json.JSONObject; import java.util.ArrayList; @@ -28,8 +30,8 @@ public class CSRAdResponse extends BaseAdResponse { private JSONObject adObject; private ArrayList clickUrls; - public CSRAdResponse(int width, int height, String adType, String responseUrl, ArrayList impressionURLs, String creativeId, JSONObject adObject) { - super(width, height, adType, impressionURLs, creativeId); + public CSRAdResponse(int width, int height, String adType, String responseUrl, ArrayList impressionURLs, ANAdResponseInfo adResponseInfo, JSONObject adObject) { + super(width, height, adType, impressionURLs, adResponseInfo); this.adObject = adObject; this.responseUrl = responseUrl; } diff --git a/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBHTMLAdResponse.java b/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBHTMLAdResponse.java index 2ca55e036..4bc7b16b7 100644 --- a/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBHTMLAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBHTMLAdResponse.java @@ -16,11 +16,13 @@ package com.appnexus.opensdk.ut.adresponse; +import com.appnexus.opensdk.ANAdResponseInfo; + import java.util.ArrayList; public class RTBHTMLAdResponse extends BaseAdResponse { - public RTBHTMLAdResponse(int width, int height, String adType, ArrayList impressionURLs, String creativeId) { - super(width, height, adType, impressionURLs, creativeId); + public RTBHTMLAdResponse(int width, int height, String adType, ArrayList impressionURLs, ANAdResponseInfo adResponseInfo) { + super(width, height, adType, impressionURLs, adResponseInfo); } } diff --git a/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBNativeAdResponse.java b/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBNativeAdResponse.java index 84792b126..8d30176fd 100644 --- a/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBNativeAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBNativeAdResponse.java @@ -16,6 +16,7 @@ package com.appnexus.opensdk.ut.adresponse; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.ANNativeAdResponse; import java.util.ArrayList; @@ -24,8 +25,8 @@ public class RTBNativeAdResponse extends BaseAdResponse { private ANNativeAdResponse nativeAdResponse; - public RTBNativeAdResponse(int width, int height, String adType, ANNativeAdResponse nativeAdResponse, ArrayList impressionURLs, String creativeId) { - super(width, height, adType, impressionURLs, creativeId); + public RTBNativeAdResponse(int width, int height, String adType, ANNativeAdResponse nativeAdResponse, ArrayList impressionURLs, ANAdResponseInfo adResponseInfo) { + super(width, height, adType, impressionURLs, adResponseInfo); this.nativeAdResponse = nativeAdResponse; } diff --git a/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBVASTAdResponse.java b/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBVASTAdResponse.java index 09edae04f..1c158b1b9 100644 --- a/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBVASTAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/ut/adresponse/RTBVASTAdResponse.java @@ -17,6 +17,8 @@ package com.appnexus.opensdk.ut.adresponse; +import com.appnexus.opensdk.ANAdResponseInfo; + import java.util.ArrayList; public class RTBVASTAdResponse extends BaseAdResponse { @@ -24,8 +26,8 @@ public class RTBVASTAdResponse extends BaseAdResponse { private String notifyUrl; - public RTBVASTAdResponse(int width, int height, String adType, String notifyUrl, ArrayList impressionURLs, String creativeId) { - super(width, height, adType, impressionURLs, creativeId); + public RTBVASTAdResponse(int width, int height, String adType, String notifyUrl, ArrayList impressionURLs, ANAdResponseInfo adResponseInfo) { + super(width, height, adType, impressionURLs, adResponseInfo); this.notifyUrl = notifyUrl; } diff --git a/sdk/src/com/appnexus/opensdk/ut/adresponse/SSMHTMLAdResponse.java b/sdk/src/com/appnexus/opensdk/ut/adresponse/SSMHTMLAdResponse.java index a3a04b2cb..e0315f29f 100644 --- a/sdk/src/com/appnexus/opensdk/ut/adresponse/SSMHTMLAdResponse.java +++ b/sdk/src/com/appnexus/opensdk/ut/adresponse/SSMHTMLAdResponse.java @@ -16,6 +16,8 @@ package com.appnexus.opensdk.ut.adresponse; +import com.appnexus.opensdk.ANAdResponseInfo; + import java.util.ArrayList; @@ -24,8 +26,8 @@ public class SSMHTMLAdResponse extends BaseAdResponse { private int ssmTimeout; private String responseURL; - public SSMHTMLAdResponse(int width, int height, String adType, String responseURL, ArrayList impressionURLs, String creativeId) { - super(width, height, adType, impressionURLs, creativeId); + public SSMHTMLAdResponse(int width, int height, String adType, String responseURL, ArrayList impressionURLs, ANAdResponseInfo adResponseInfo) { + super(width, height, adType, impressionURLs, adResponseInfo); this.responseURL = responseURL; } diff --git a/sdk/test/com/appnexus/opensdk/ANAdResponseInfoBannerTests.java b/sdk/test/com/appnexus/opensdk/ANAdResponseInfoBannerTests.java new file mode 100644 index 000000000..54d0dd5be --- /dev/null +++ b/sdk/test/com/appnexus/opensdk/ANAdResponseInfoBannerTests.java @@ -0,0 +1,178 @@ +/* + * Copyright 2018 APPNEXUS INC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.appnexus.opensdk; + +import com.appnexus.opensdk.shadows.ShadowAsyncTaskNoExecutor; +import com.appnexus.opensdk.shadows.ShadowCustomWebView; +import com.appnexus.opensdk.shadows.ShadowSettings; +import com.appnexus.opensdk.shadows.ShadowWebSettings; +import com.appnexus.opensdk.ut.UTConstants; +import com.squareup.okhttp.mockwebserver.MockResponse; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowLog; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +@Config(sdk = 21, + shadows = {ShadowAsyncTaskNoExecutor.class, + ShadowCustomWebView.class, ShadowWebSettings.class, ShadowSettings.class, ShadowLog.class}) +@RunWith(RobolectricTestRunner.class) +public class ANAdResponseInfoBannerTests extends BaseViewAdTest { + + @Override + public void setup() { + super.setup(); + } + + @Override + public void tearDown() { + super.tearDown(); + } + + @Test + public void testAdResponseInfoRTBBanner() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.banner())); // First queue a regular HTML banner response + assertNull(bannerAdView.getAdResponseInfo()); + assertTrue(bannerAdView.getAdType() == AdType.UNKNOWN); // First tests if ad_type is UNKNOWN initially + executeBannerRequest(); + assertTrue(bannerAdView.getAdType() == AdType.BANNER); // If a HTML banner is served then BANNER + assertNotNull(bannerAdView.getAdResponseInfo()); + assertEquals(bannerAdView.getAdResponseInfo().getAdType(), AdType.BANNER); + assertEquals(bannerAdView.getAdResponseInfo().getCreativeId(), "6332753"); + assertEquals(bannerAdView.getAdResponseInfo().getTagId(), "123456"); + assertEquals(bannerAdView.getAdResponseInfo().getBuyMemberId(), 123); + assertEquals(bannerAdView.getAdResponseInfo().getContentSource(), UTConstants.RTB); + assertEquals(bannerAdView.getAdResponseInfo().getNetworkName(), ""); + } + +// @Test +// public void testAdResponseInfoCSMBanner() { +// server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.noFillCSMBanner())); // First queue a regular HTML banner response +// assertNull(bannerAdView.getAdResponseInfo()); +// assertTrue(bannerAdView.getAdType() == AdType.UNKNOWN); // First tests if ad_type is UNKNOWN initially +// executeBannerRequest(); +// assertTrue(bannerAdView.getAdType() == AdType.BANNER); // If a HTML banner is served then BANNER +// assertNotNull(bannerAdView.getAdResponseInfo()); +// assertEquals(bannerAdView.getAdResponseInfo().getAdType(), AdType.BANNER); +// assertEquals(bannerAdView.getAdResponseInfo().getCreativeId(), "44863345"); +// assertEquals(bannerAdView.getAdResponseInfo().getTagId(), "123456"); +// assertEquals(bannerAdView.getAdResponseInfo().getBuyMemberId(), 123); +// assertEquals(bannerAdView.getAdResponseInfo().getContentSource(), UTConstants.CSM); +// assertEquals(bannerAdView.getAdResponseInfo().getNetworkName(), "com.appnexus.opensdk.testviews.MediatedBannerNoFillView"); +// } + + @Test + public void testAdResponseInfoRTBBannerNative() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.anNative())); // First queue a regular HTML banner response + assertNull(bannerAdView.getAdResponseInfo()); + assertTrue(bannerAdView.getAdType() == AdType.UNKNOWN); // First tests if ad_type is UNKNOWN initially + executeBannerRequest(); + assertTrue(bannerAdView.getAdType() == AdType.NATIVE); // If a HTML banner is served then BANNER + assertNull(bannerAdView.getAdResponseInfo()); + + assertNotNull(nativeAdResponse.getAdResponseInfo()); + assertEquals(nativeAdResponse.getAdResponseInfo().getAdType(), AdType.NATIVE); + assertEquals(nativeAdResponse.getAdResponseInfo().getCreativeId(), "47772560"); + assertEquals(nativeAdResponse.getAdResponseInfo().getTagId(), "123456"); + assertEquals(nativeAdResponse.getAdResponseInfo().getBuyMemberId(), 958); + assertEquals(nativeAdResponse.getAdResponseInfo().getContentSource(), UTConstants.RTB); + assertEquals(nativeAdResponse.getAdResponseInfo().getNetworkName(), ""); + } + + public void testAdResponseInfoRTBBannerNativeRenderer() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.anNativeRenderer())); // First queue a regular HTML banner response + bannerAdView.enableNativeRendering(true); + assertNull(bannerAdView.getAdResponseInfo()); + assertTrue(bannerAdView.getAdType() == AdType.UNKNOWN); // First tests if ad_type is UNKNOWN initially + executeBannerRequest(); + assertNotNull(bannerAdView.getAdResponseInfo()); + assertEquals(bannerAdView.getAdResponseInfo().getAdType(), AdType.NATIVE); + assertEquals(bannerAdView.getAdResponseInfo().getCreativeId(), "47772560"); + assertEquals(bannerAdView.getAdResponseInfo().getTagId(), "123456"); + assertEquals(bannerAdView.getAdResponseInfo().getBuyMemberId(), 958); + assertEquals(bannerAdView.getAdResponseInfo().getContentSource(), UTConstants.RTB); + assertEquals(bannerAdView.getAdResponseInfo().getNetworkName(), ""); + assertNull(nativeAdResponse); + } + + @Test + public void testAdResponseInfoRTBBannerNoBid() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.NO_BID)); + assertNull(bannerAdView.getAdResponseInfo()); + executeBannerRequest(); + assertNotNull(bannerAdView.getAdResponseInfo()); + assertEquals(bannerAdView.getAdResponseInfo().getAdType(), null); + assertEquals(bannerAdView.getAdResponseInfo().getCreativeId(), ""); + assertEquals(bannerAdView.getAdResponseInfo().getTagId(), "123456789"); + assertEquals(bannerAdView.getAdResponseInfo().getBuyMemberId(), 0); + assertEquals(bannerAdView.getAdResponseInfo().getContentSource(), ""); + assertEquals(bannerAdView.getAdResponseInfo().getNetworkName(), ""); + } + + @Test + public void testAdResponseInfoRTBBannerBlank() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blank())); + assertNull(bannerAdView.getAdResponseInfo()); + executeBannerRequest(); + assertNull(bannerAdView.getAdResponseInfo()); + } + + @Test + public void testGetCreativeIdBanner() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.banner())); // First queue a regular HTML banner response + assertNull(bannerAdView.getAdResponseInfo()); + executeBannerRequest(); + assertEquals("6332753", bannerAdView.getAdResponseInfo().getCreativeId()); + } + + @Test + public void testGetCreativeIdBannerNativeCreativeId() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.anNative())); // First queue a banner Native response + assertNull(bannerAdView.getAdResponseInfo()); + executeBannerRequest(); + assertEquals("47772560", nativeAdResponse.getAdResponseInfo().getCreativeId()); + } + + @Test + public void testGetAdResponseInfoNullForBlankBannerResponse() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blankBanner())); // First queue a regular HTML banner response + assertNull(bannerAdView.getAdResponseInfo()); + executeBannerRequest(); + assertNull(bannerAdView.getAdResponseInfo()); + } + + private void executeBannerRequest() { + bannerAdView.setAutoRefreshInterval(15000); + bannerAdView.loadAd(); + + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + } +} diff --git a/sdk/test/com/appnexus/opensdk/ANAdResponseInfoBannerVideoTests.java b/sdk/test/com/appnexus/opensdk/ANAdResponseInfoBannerVideoTests.java new file mode 100644 index 000000000..57ecfb7dd --- /dev/null +++ b/sdk/test/com/appnexus/opensdk/ANAdResponseInfoBannerVideoTests.java @@ -0,0 +1,101 @@ +/* + * Copyright 2018 APPNEXUS INC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.appnexus.opensdk; + +import com.appnexus.opensdk.shadows.ShadowAsyncTaskNoExecutor; +import com.appnexus.opensdk.shadows.ShadowCustomVideoWebView; +import com.appnexus.opensdk.shadows.ShadowSettings; +import com.appnexus.opensdk.shadows.ShadowWebSettings; +import com.appnexus.opensdk.ut.UTConstants; +import com.squareup.okhttp.mockwebserver.MockResponse; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowLog; +import org.robolectric.shadows.ShadowLooper; + +import static android.os.Looper.getMainLooper; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.robolectric.Shadows.shadowOf; + +@Config(sdk = 21, + shadows = {ShadowAsyncTaskNoExecutor.class, + ShadowCustomVideoWebView.class, ShadowWebSettings.class, ShadowSettings.class, ShadowLog.class}) +@RunWith(RobolectricTestRunner.class) +public class ANAdResponseInfoBannerVideoTests extends BaseViewAdTest { + + @Override + public void setup() { + super.setup(); + } + + @Override + public void tearDown() { + super.tearDown(); + } + + @Test + public void testAdResponseInfoRTBBannerVideo() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.rtbVASTVideo())); // First queue a regular HTML banner response + assertNull(bannerAdView.getAdResponseInfo()); + assertTrue(bannerAdView.getAdType() == AdType.UNKNOWN); // First tests if ad_type is UNKNOWN initially + executeBannerRequest(); + assertCallbacks(true); + assertTrue(bannerAdView.getAdType() == AdType.VIDEO); // If a HTML banner is served then BANNER + assertNotNull(bannerAdView.getAdResponseInfo()); + assertEquals(bannerAdView.getAdResponseInfo().getAdType(), AdType.VIDEO); + assertEquals(bannerAdView.getAdResponseInfo().getCreativeId(), "6332753"); + assertEquals(bannerAdView.getAdResponseInfo().getTagId(), "123456"); + assertEquals(bannerAdView.getAdResponseInfo().getBuyMemberId(), 123); + assertEquals(bannerAdView.getAdResponseInfo().getContentSource(), UTConstants.RTB); + assertEquals(bannerAdView.getAdResponseInfo().getNetworkName(), ""); + } + + @Test + public void testgetCreativeIdBannerVideoCreativeId() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.rtbVASTVideo())); // First queue a banner Native response + assertNull(bannerAdView.getAdResponseInfo()); + executeBannerRequest(); + assertEquals("6332753", bannerAdView.getAdResponseInfo().getCreativeId()); + } + + private void executeBannerRequest() { + bannerAdView.setAutoRefreshInterval(15000); + bannerAdView.loadAd(); + + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + + ShadowLooper shadowLooper = shadowOf(getMainLooper()); + if (!shadowLooper.isIdle()) { + shadowLooper.idle(); + } + RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable(); + } +} diff --git a/sdk/test/com/appnexus/opensdk/ANAdResponseInfoInterstitialTests.java b/sdk/test/com/appnexus/opensdk/ANAdResponseInfoInterstitialTests.java new file mode 100644 index 000000000..24b15d5b4 --- /dev/null +++ b/sdk/test/com/appnexus/opensdk/ANAdResponseInfoInterstitialTests.java @@ -0,0 +1,129 @@ +/* + * Copyright 2018 APPNEXUS INC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.appnexus.opensdk; + +import com.appnexus.opensdk.shadows.ShadowAsyncTaskNoExecutor; +import com.appnexus.opensdk.shadows.ShadowCustomWebView; +import com.appnexus.opensdk.shadows.ShadowSettings; +import com.appnexus.opensdk.shadows.ShadowWebSettings; +import com.appnexus.opensdk.ut.UTConstants; +import com.squareup.okhttp.mockwebserver.MockResponse; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowLog; +import org.robolectric.shadows.ShadowLooper; + +import static android.os.Looper.getMainLooper; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.robolectric.Shadows.shadowOf; + +@Config(sdk = 21, + shadows = {ShadowAsyncTaskNoExecutor.class, + ShadowCustomWebView.class, ShadowWebSettings.class, ShadowSettings.class, ShadowLog.class}) +@RunWith(RobolectricTestRunner.class) +public class ANAdResponseInfoInterstitialTests extends BaseViewAdTest { + + @Override + public void setup() { + super.setup(); + } + + @Override + public void tearDown() { + super.tearDown(); + } + + @Test + public void testAdResponseInfoRTBInterstitial() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.banner())); // First queue a regular HTML banner response + assertNull(interstitialAdView.getAdResponseInfo()); + assertTrue(interstitialAdView.getAdType() == AdType.UNKNOWN); // First tests if ad_type is UNKNOWN initially + executeBannerRequest(); + assertTrue(interstitialAdView.getAdType() == AdType.BANNER); // If a HTML banner is served then BANNER + assertNotNull(interstitialAdView.getAdResponseInfo()); + assertEquals(interstitialAdView.getAdResponseInfo().getAdType(), AdType.BANNER); + assertEquals(interstitialAdView.getAdResponseInfo().getCreativeId(), "6332753"); + assertEquals(interstitialAdView.getAdResponseInfo().getTagId(), "123456"); + assertEquals(interstitialAdView.getAdResponseInfo().getBuyMemberId(), 123); + assertEquals(interstitialAdView.getAdResponseInfo().getContentSource(), UTConstants.RTB); + assertEquals(interstitialAdView.getAdResponseInfo().getNetworkName(), ""); + } + + @Test + public void testAdResponseInfoRTBInterstitialNoBid() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.NO_BID)); + assertNull(interstitialAdView.getAdResponseInfo()); + executeBannerRequest(); + assertNotNull(interstitialAdView.getAdResponseInfo()); + assertEquals(interstitialAdView.getAdResponseInfo().getAdType(), null); + assertEquals(interstitialAdView.getAdResponseInfo().getCreativeId(), ""); + assertEquals(interstitialAdView.getAdResponseInfo().getTagId(), "123456789"); + assertEquals(interstitialAdView.getAdResponseInfo().getBuyMemberId(), 0); + assertEquals(interstitialAdView.getAdResponseInfo().getContentSource(), ""); + assertEquals(interstitialAdView.getAdResponseInfo().getNetworkName(), ""); + } + + @Test + public void testAdResponseInfoRTBInterstitialBlank() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blank())); + assertNull(interstitialAdView.getAdResponseInfo()); + executeBannerRequest(); + assertNull(interstitialAdView.getAdResponseInfo()); + } + + @Test + public void testgetCreativeIdInterstitial() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.banner())); // First queue a regular HTML banner response + assertNull(interstitialAdView.getAdResponseInfo()); + executeBannerRequest(); + assertEquals("6332753", interstitialAdView.getAdResponseInfo().getCreativeId()); + } + + @Test + public void testGetAdResponseInfoNullForBlankInterstitialResponse() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blankBanner())); // First queue a regular HTML banner response + assertNull(interstitialAdView.getAdResponseInfo()); + executeBannerRequest(); + assertNull(interstitialAdView.getAdResponseInfo()); + } + + private void executeBannerRequest() { + interstitialAdView.loadAd(); + + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + + ShadowLooper shadowLooper = shadowOf(getMainLooper()); + if (!shadowLooper.isIdle()) { + shadowLooper.idle(); + } + RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable(); + } +} diff --git a/sdk/test/com/appnexus/opensdk/ANAdResponseInfoNativeTest.java b/sdk/test/com/appnexus/opensdk/ANAdResponseInfoNativeTest.java new file mode 100644 index 000000000..ab4815c48 --- /dev/null +++ b/sdk/test/com/appnexus/opensdk/ANAdResponseInfoNativeTest.java @@ -0,0 +1,118 @@ +/* + * Copyright 2015 APPNEXUS INC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.appnexus.opensdk; + + +import com.appnexus.opensdk.shadows.ShadowAsyncTaskNoExecutor; +import com.appnexus.opensdk.shadows.ShadowSettings; +import com.appnexus.opensdk.shadows.ShadowWebSettings; +import com.appnexus.opensdk.ut.UTConstants; +import com.appnexus.opensdk.utils.Settings; +import com.squareup.okhttp.mockwebserver.MockResponse; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowLog; +import org.robolectric.shadows.ShadowWebView; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +@Config(sdk = 21, + shadows = {ShadowAsyncTaskNoExecutor.class, + ShadowWebView.class, ShadowWebSettings.class, ShadowSettings.class, ShadowLog.class}) +@RunWith(RobolectricTestRunner.class) +public class ANAdResponseInfoNativeTest extends BaseNativeTest { + + public void assertAdLoaded(Boolean loaded) { + assertTrue(adLoaded || adFailed); + assertTrue(loaded | !adFailed); + assertTrue(loaded | adLoaded); + } + + public void assertAdFailed(Boolean loadFailed) { + assertTrue(adLoaded || adFailed); + assertTrue(loadFailed | !adLoaded); + assertTrue(loadFailed | adFailed); + } + + @Override + public void setup() { + super.setup(); + Settings.getSettings().ua = ""; + } + + @Test + public void requestNativeSuccessAdResponseInfo() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.anNative())); + assertNull(response); + executeNativeRequest(); + assertNotNull(response.getAdResponseInfo()); + assertEquals(response.getAdResponseInfo().getAdType(), AdType.NATIVE); + assertEquals(response.getAdResponseInfo().getCreativeId(), "47772560"); + assertEquals(response.getAdResponseInfo().getTagId(), "123456"); + assertEquals(response.getAdResponseInfo().getBuyMemberId(), 958); + assertEquals(response.getAdResponseInfo().getContentSource(), UTConstants.RTB); + assertEquals(response.getAdResponseInfo().getNetworkName(), ""); + } + + @Test + public void requestNativeNoBidAdResponseInfo() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.NO_BID)); + assertNull(adResponseInfo); + executeNativeRequest(); + assertNotNull(adResponseInfo); + assertEquals(adResponseInfo.getAdType(), null); + assertEquals(adResponseInfo.getCreativeId(), ""); + assertEquals(adResponseInfo.getTagId(), "123456789"); + assertEquals(adResponseInfo.getBuyMemberId(), 0); + assertEquals(adResponseInfo.getContentSource(), ""); + assertEquals(adResponseInfo.getNetworkName(), ""); + } + + @Test + public void requestNativeBlankAdResponseInfo() { + server.enqueue(new MockResponse().setResponseCode(200).setBody(TestResponsesUT.blank())); + assertNull(adResponseInfo); + executeNativeRequest(); + assertNull(adResponseInfo); + } + + public void executeNativeRequest() { + adRequest.loadAd(); + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + waitForTasks(); + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + } + + + @Override + public void tearDown() { + super.tearDown(); + if (response != null) { + response.destroy(); + } + } +} diff --git a/sdk/test/com/appnexus/opensdk/ANMultiAdRequestApiTest.java b/sdk/test/com/appnexus/opensdk/ANMultiAdRequestApiTest.java index 2c15d8924..311dec2b3 100644 --- a/sdk/test/com/appnexus/opensdk/ANMultiAdRequestApiTest.java +++ b/sdk/test/com/appnexus/opensdk/ANMultiAdRequestApiTest.java @@ -51,10 +51,20 @@ public class ANMultiAdRequestApiTest extends BaseRoboTest { @Override public void setup() { super.setup(); - anMultiAdRequest = new ANMultiAdRequest(activity, 100, null); + anMultiAdRequest = new ANMultiAdRequest(activity, 100, 1234, null); bav = new BannerAdView(activity); } + @Test + public void testGetMemberId() { + assertEquals(100, anMultiAdRequest.getMemberId()); + } + + @Test + public void testGetPublisherID() { + assertEquals(1234, anMultiAdRequest.getPublisherId()); + } + @Test public void testAddAdUnit() { assertEquals(anMultiAdRequest.getAdUnitList().size(), 0); diff --git a/sdk/test/com/appnexus/opensdk/ANMultiAdRequestLoadTests.java b/sdk/test/com/appnexus/opensdk/ANMultiAdRequestLoadTests.java index 7b5629d65..a415c7f3d 100644 --- a/sdk/test/com/appnexus/opensdk/ANMultiAdRequestLoadTests.java +++ b/sdk/test/com/appnexus/opensdk/ANMultiAdRequestLoadTests.java @@ -53,7 +53,7 @@ public class ANMultiAdRequestLoadTests extends BaseViewAdTest { @Override public void setup() { super.setup(); - anMultiAdRequest = new ANMultiAdRequest(activity, 0, this); + anMultiAdRequest = new ANMultiAdRequest(activity, 0, 1234, this); anMultiAdRequest.addAdUnit(bannerAdView); anMultiAdRequest.addAdUnit(interstitialAdView); } @@ -330,7 +330,7 @@ private void initMARWithConvenience() { interstitialAdView.setPlacementID("0"); interstitialAdView.setAdListener(this); - new ANMultiAdRequest(activity, 0, this, true, bannerAdView, interstitialAdView); + new ANMultiAdRequest(activity, 0, 1234, this, true, bannerAdView, interstitialAdView); waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); @@ -360,7 +360,7 @@ public void testReloadMARSuccess() { } private void executeSecondMARRequest() { - ANMultiAdRequest anMultiAdRequest = new ANMultiAdRequest(activity, 123, new MultiAdRequestListener() { + ANMultiAdRequest anMultiAdRequest = new ANMultiAdRequest(activity, 123, 1234, new MultiAdRequestListener() { @Override public void onMultiAdRequestCompleted() { secondMarCompleted = true; diff --git a/sdk/test/com/appnexus/opensdk/ANMultiAdRequestToRequestParametersTest.java b/sdk/test/com/appnexus/opensdk/ANMultiAdRequestToRequestParametersTest.java index 48160689a..6b184d3a5 100644 --- a/sdk/test/com/appnexus/opensdk/ANMultiAdRequestToRequestParametersTest.java +++ b/sdk/test/com/appnexus/opensdk/ANMultiAdRequestToRequestParametersTest.java @@ -56,7 +56,7 @@ public class ANMultiAdRequestToRequestParametersTest extends BaseRoboTest { @Override public void setup() { super.setup(); - anMultiAdRequest = new ANMultiAdRequest(activity, 100, null); + anMultiAdRequest = new ANMultiAdRequest(activity, 100, 123, null); bav = new BannerAdView(activity); bav.setAdSize(320, 50); anMultiAdRequest.addAdUnit(bav); @@ -68,6 +68,11 @@ public void testMemberId() { assertMARGlobalMemberIdInUTRequestParams(); } + @Test + public void testPublisherId() { + assertMARGlobalPublisherIdInUTRequestParams(); + } + @Test public void testUserInfo() { @@ -148,6 +153,21 @@ public void testAttachedThenRemoveBannerAdViewMemberIdToMarIndividualAdLoad() { assertRemovedBannerGlobalMemberIdInUTRequestParams(); } + @Test + public void testAttachedBannerAdViewPublisherIdToMarIndividualAdLoad() { + anMultiAdRequest.addAdUnit(bav); + assertAttachedBannerGlobalPublisherIdInUTRequestParams(); + } + + @Test + public void testAttachedThenRemoveBannerAdViewPublisherToMarIndividualAdLoad() { + anMultiAdRequest.addAdUnit(bav); + bav.setPublisherId(1234); + assertAttachedBannerGlobalPublisherIdInUTRequestParams(); + anMultiAdRequest.removeAdUnit(bav); + assertRemovedBannerGlobalPublisherIdInUTRequestParams(); + } + @Test public void testAttachedThenKilledBannerAdViewMemberIdToMarIndividualAdLoad() { anMultiAdRequest.addAdUnit(bav); @@ -302,6 +322,19 @@ private void assertMARGlobalMemberIdInUTRequestParams() { assertEquals(100, memberId); } + private void assertMARGlobalPublisherIdInUTRequestParams() { + String postData = getRequestParametersPostData(); + assertTrue(postData.contains("\"publisher_id\":123")); + int publisherId = -1; + try { + JSONObject json = new JSONObject(postData); + publisherId = json.getInt("publisher_id"); + } catch (JSONException e) { + e.printStackTrace(); + } + assertEquals(123, publisherId); + } + private void assertAttachedBannerGlobalMemberIdInUTRequestParams() { String postData = getBannerRequestParametersPostData(); assertTrue(postData.contains("\"member_id\":100")); @@ -315,6 +348,12 @@ private void assertAttachedBannerGlobalMemberIdInUTRequestParams() { assertEquals(100, memberId); } + private void assertAttachedBannerGlobalPublisherIdInUTRequestParams() { + String postData = getBannerRequestParametersPostData(); + assertFalse(postData.contains("\"publisher_id\":1234")); + assertMARGlobalPublisherIdInUTRequestParams(); + } + private void assertRemovedBannerGlobalMemberIdInUTRequestParams() { String postData = getBannerRequestParametersPostData(); assertTrue(postData.contains("\"member_id\":2000")); @@ -328,6 +367,19 @@ private void assertRemovedBannerGlobalMemberIdInUTRequestParams() { assertEquals(2000, memberId); } + private void assertRemovedBannerGlobalPublisherIdInUTRequestParams() { + String postData = getBannerRequestParametersPostData(); + assertTrue(postData.contains("\"publisher_id\":1234")); + int publisherId = -1; + try { + JSONObject json = new JSONObject(postData); + publisherId = json.getInt("publisher_id"); + } catch (JSONException e) { + e.printStackTrace(); + } + assertEquals(1234, publisherId); + } + private void assertTagCountInUTRequestParams() { String postData = getRequestParametersPostData(); JSONObject tag1 = null, tag2 = null; diff --git a/sdk/test/com/appnexus/opensdk/ANOmidViewabiltyTests.java b/sdk/test/com/appnexus/opensdk/ANOmidViewabiltyTests.java index b418f0510..069d31d5e 100644 --- a/sdk/test/com/appnexus/opensdk/ANOmidViewabiltyTests.java +++ b/sdk/test/com/appnexus/opensdk/ANOmidViewabiltyTests.java @@ -16,7 +16,6 @@ package com.appnexus.opensdk; -import android.app.Activity; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; diff --git a/sdk/test/com/appnexus/opensdk/AdRequestToAdRequesterTest.java b/sdk/test/com/appnexus/opensdk/AdRequestToAdRequesterTest.java index 228c6d8c5..46869f94b 100644 --- a/sdk/test/com/appnexus/opensdk/AdRequestToAdRequesterTest.java +++ b/sdk/test/com/appnexus/opensdk/AdRequestToAdRequesterTest.java @@ -210,13 +210,13 @@ public void onReceiveUTResponse(UTAdResponse response){ if(response!=null && response.getAdList() != null && !response.getAdList().isEmpty()) { requesterReceivedServerResponse = true; }else{ - failed(ResultCode.UNABLE_TO_FILL); + failed(ResultCode.UNABLE_TO_FILL, response.getAdResponseInfo()); } this.response = response; } @Override - public void failed(ResultCode code) { + public void failed(ResultCode code, ANAdResponseInfo adResponseInfo) { requesterFailed = true; } diff --git a/sdk/test/com/appnexus/opensdk/AdViewRequestManagerTest.java b/sdk/test/com/appnexus/opensdk/AdViewRequestManagerTest.java index 1616365d1..42ddcdf6a 100644 --- a/sdk/test/com/appnexus/opensdk/AdViewRequestManagerTest.java +++ b/sdk/test/com/appnexus/opensdk/AdViewRequestManagerTest.java @@ -97,6 +97,11 @@ public void testNotifyURLForVideo() throws Exception { Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); + waitForTasks(); + // execute main ad request + Robolectric.flushBackgroundThreadScheduler(); + Robolectric.flushForegroundThreadScheduler(); + ShadowLooper shadowLooper = shadowOf(getMainLooper()); if (!shadowLooper.isIdle()) { shadowLooper.idle(); diff --git a/sdk/test/com/appnexus/opensdk/BannerAdViewVideoLoadAdTest.java b/sdk/test/com/appnexus/opensdk/BannerAdViewVideoLoadAdTest.java index 1d023a2e2..5c781c2a3 100644 --- a/sdk/test/com/appnexus/opensdk/BannerAdViewVideoLoadAdTest.java +++ b/sdk/test/com/appnexus/opensdk/BannerAdViewVideoLoadAdTest.java @@ -27,12 +27,16 @@ import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowLog; +import org.robolectric.shadows.ShadowLooper; +import static android.os.Looper.getMainLooper; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotSame; import static junit.framework.Assert.assertTrue; +import static org.robolectric.Shadows.shadowOf; @Config(sdk = 21, shadows = {ShadowAsyncTaskNoExecutor.class, @@ -91,6 +95,12 @@ private void executeBannerRequest() { waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); + + ShadowLooper shadowLooper = shadowOf(getMainLooper()); + if (!shadowLooper.isIdle()) { + shadowLooper.idle(); + } + RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable(); } diff --git a/sdk/test/com/appnexus/opensdk/BaseNativeTest.java b/sdk/test/com/appnexus/opensdk/BaseNativeTest.java index a46bd246d..07a3eb33f 100644 --- a/sdk/test/com/appnexus/opensdk/BaseNativeTest.java +++ b/sdk/test/com/appnexus/opensdk/BaseNativeTest.java @@ -27,7 +27,6 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertTrue; -import static junit.framework.Assert.fail; @RunWith(RobolectricTestRunner.class) @Config(sdk = 21) @@ -38,6 +37,7 @@ public class BaseNativeTest extends BaseRoboTest implements NativeAdRequestListe protected boolean adLoaded, adFailed; NativeAdResponse nativeAdResponse; ResultCode failErrorCode; + protected ANAdResponseInfo adResponseInfo; @Override @@ -49,6 +49,8 @@ public void setup() { nativeAdResponse = null; failErrorCode = null; + adResponseInfo = null; + adRequest = new NativeAdRequest(activity, "0"); adRequest.setListener(this); } @@ -75,9 +77,10 @@ public void onAdLoaded(NativeAdResponse response) { } @Override - public void onAdFailed(ResultCode errorcode) { + public void onAdFailed(ResultCode errorcode, ANAdResponseInfo adResponseInfo) { adFailed = true; failErrorCode = errorcode; + this.adResponseInfo = adResponseInfo; Clog.w(TestUtil.testLogTag, "BaseNativeTest onAdFailed"); } diff --git a/sdk/test/com/appnexus/opensdk/BaseViewAdTest.java b/sdk/test/com/appnexus/opensdk/BaseViewAdTest.java index 7e6dfc1b6..613401606 100644 --- a/sdk/test/com/appnexus/opensdk/BaseViewAdTest.java +++ b/sdk/test/com/appnexus/opensdk/BaseViewAdTest.java @@ -68,6 +68,7 @@ public void tearDown() { bannerAdView = null; interstitialAdView = null; requestManager = null; + nativeAdResponse = null; super.tearDown(); } diff --git a/sdk/test/com/appnexus/opensdk/ExecutorForBackgroundTasksTests.java b/sdk/test/com/appnexus/opensdk/ExecutorForBackgroundTasksTests.java index a4ea40e6e..9d70d571e 100644 --- a/sdk/test/com/appnexus/opensdk/ExecutorForBackgroundTasksTests.java +++ b/sdk/test/com/appnexus/opensdk/ExecutorForBackgroundTasksTests.java @@ -125,7 +125,7 @@ public void testInstantiateNewMediatedSSMAdExecutorForBackgroundTasks() { try { ArrayList stringArrayList = new ArrayList<>(); stringArrayList.add(url); - SSMHTMLAdResponse ssmhtmlAdResponse = new SSMHTMLAdResponse(300, 250, "banner", url, stringArrayList, "12345"); + SSMHTMLAdResponse ssmhtmlAdResponse = new SSMHTMLAdResponse(300, 250, "banner", url, stringArrayList, new ANAdResponseInfo()); Class aClass = Class.forName("com.appnexus.opensdk.MediatedSSMAdViewController"); Method met = aClass.getDeclaredMethod("instantiateNewMediatedSSMAd", null); met.setAccessible(true); @@ -139,6 +139,7 @@ public void testInstantiateNewMediatedSSMAdExecutorForBackgroundTasks() { } catch (ClassNotFoundException e) { e.printStackTrace(); } + waitForTasks(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertEquals(ShadowAsyncTaskNoExecutor.getExecutor(), MockDefaultExecutorSupplier.getInstance().forBackgroundTasks()); @@ -223,7 +224,7 @@ public void testMediatedNativeAdControllerFireTrackerExecutorForBackgroundTasks( try { ArrayList stringArrayList = new ArrayList<>(); stringArrayList.add(url); - CSMSDKAdResponse csmsdkAdResponse = new CSMSDKAdResponse(300, 250, "banner", url, stringArrayList, "12345", null); + CSMSDKAdResponse csmsdkAdResponse = new CSMSDKAdResponse(300, 250, "banner", url, stringArrayList, new ANAdResponseInfo(), null); MediatedNativeAdController mediatedNativeAdController = MediatedNativeAdController.create(csmsdkAdResponse, new AdViewRequestManager(bannerAdView)); Class aClass = Class.forName("com.appnexus.opensdk.MediatedNativeAdController"); Method met = aClass.getDeclaredMethod("fireTracker", String.class); @@ -250,7 +251,8 @@ public void testCSRNativeBannerControllerFireTrackerExecutorForBackgroundTasks() try { ArrayList stringArrayList = new ArrayList<>(); stringArrayList.add(url); - CSRAdResponse csrAdResponse = new CSRAdResponse(300, 250, "banner", url, stringArrayList, "12345", null); + // Create a RTB Banner Ad + CSRAdResponse csrAdResponse = new CSRAdResponse(300, 250, "banner", url, stringArrayList, new ANAdResponseInfo(), null); CSRNativeBannerController csrNativeBannerController = new CSRNativeBannerController(csrAdResponse, new AdViewRequestManager(bannerAdView)); Class aClass = Class.forName("com.appnexus.opensdk.CSRNativeBannerController"); Method met = aClass.getDeclaredMethod("fireTracker", String.class); diff --git a/sdk/test/com/appnexus/opensdk/InterstitialAdViewLoadAdTest.java b/sdk/test/com/appnexus/opensdk/InterstitialAdViewLoadAdTest.java index 5eec468ce..43d36b5b1 100644 --- a/sdk/test/com/appnexus/opensdk/InterstitialAdViewLoadAdTest.java +++ b/sdk/test/com/appnexus/opensdk/InterstitialAdViewLoadAdTest.java @@ -124,10 +124,10 @@ private void executeInterstitialRequest() { Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); -// ShadowLooper shadowLooper = shadowOf(getMainLooper()); -// if (!shadowLooper.isIdle()) { -// shadowLooper.idle(); -// } -// RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable(); + ShadowLooper shadowLooper = shadowOf(getMainLooper()); + if (!shadowLooper.isIdle()) { + shadowLooper.idle(); + } + RuntimeEnvironment.getMasterScheduler().advanceToNextPostedRunnable(); } } diff --git a/sdk/test/com/appnexus/opensdk/MRAIDImplementationTest.java b/sdk/test/com/appnexus/opensdk/MRAIDImplementationTest.java index c0f4506e7..28e1ac5b5 100644 --- a/sdk/test/com/appnexus/opensdk/MRAIDImplementationTest.java +++ b/sdk/test/com/appnexus/opensdk/MRAIDImplementationTest.java @@ -344,6 +344,7 @@ static class MockAdDispatcher implements AdDispatcher { boolean adLoaded, adFailed, adExpanded, adCollapsed, adClicked, appEventOccurred, adClickedWithUrl; String eventName, eventData; + private ANAdResponseInfo mAdResponseInfo; @Override public void onAdLoaded(AdResponse ad) { @@ -356,7 +357,8 @@ public void onAdLoaded() { } @Override - public void onAdFailed(ResultCode errorCode) { + public void onAdFailed(ResultCode errorCode, ANAdResponseInfo adResponseInfo) { +// setAdResponseInfo(adResponseInfo); adFailed = true; } diff --git a/sdk/test/com/appnexus/opensdk/MediatedNativeAdViewControllerTest.java b/sdk/test/com/appnexus/opensdk/MediatedNativeAdViewControllerTest.java index 9cc5cabd8..d9771014c 100644 --- a/sdk/test/com/appnexus/opensdk/MediatedNativeAdViewControllerTest.java +++ b/sdk/test/com/appnexus/opensdk/MediatedNativeAdViewControllerTest.java @@ -397,8 +397,8 @@ public void onAdLoaded(NativeAdResponse response) { } @Override - public void onAdFailed(ResultCode errorcode) { - super.onAdFailed(errorcode); + public void onAdFailed(ResultCode errorcode, ANAdResponseInfo adResponseInfo) { + super.onAdFailed(errorcode, adResponseInfo); Lock.unpause(); } diff --git a/sdk/test/com/appnexus/opensdk/MediatedSSMAdViewControllerTest.java b/sdk/test/com/appnexus/opensdk/MediatedSSMAdViewControllerTest.java index 209c90a70..ecdef767c 100644 --- a/sdk/test/com/appnexus/opensdk/MediatedSSMAdViewControllerTest.java +++ b/sdk/test/com/appnexus/opensdk/MediatedSSMAdViewControllerTest.java @@ -20,8 +20,6 @@ import com.appnexus.opensdk.shadows.ShadowAsyncTaskNoExecutor; import com.appnexus.opensdk.shadows.ShadowCustomWebView; -import com.appnexus.opensdk.shadows.ShadowSettings; -import com.appnexus.opensdk.shadows.ShadowWebSettings; import com.appnexus.opensdk.util.Lock; import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.RecordedRequest; @@ -31,7 +29,6 @@ import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -import org.robolectric.shadows.ShadowLog; import java.util.concurrent.TimeUnit; diff --git a/sdk/test/com/appnexus/opensdk/NativeRequestTest.java b/sdk/test/com/appnexus/opensdk/NativeRequestTest.java index b85efbb21..46cc5ed69 100644 --- a/sdk/test/com/appnexus/opensdk/NativeRequestTest.java +++ b/sdk/test/com/appnexus/opensdk/NativeRequestTest.java @@ -43,10 +43,6 @@ import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; import static org.junit.Assert.assertNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; @Config(sdk = 21, shadows = {ShadowAsyncTaskNoExecutor.class, @@ -119,7 +115,7 @@ public void onAdLoaded(NativeAdResponse response) { } @Override - public void onAdFailed(ResultCode errorcode) { + public void onAdFailed(ResultCode errorcode, ANAdResponseInfo adResponseInfo) { } }; adRequest.setListener(listener); diff --git a/sdk/test/com/appnexus/opensdk/TestResponsesUT.java b/sdk/test/com/appnexus/opensdk/TestResponsesUT.java index 9b9b9c507..5434d9caf 100644 --- a/sdk/test/com/appnexus/opensdk/TestResponsesUT.java +++ b/sdk/test/com/appnexus/opensdk/TestResponsesUT.java @@ -223,6 +223,17 @@ public static String noFillCSM_RTBBanner() { return templateMediatedWaterFallResponses(adsArray.toArray(new String[adsArray.size()])); } + public static String noFillCSMBanner() { + //Create a CSM - Ad + String csmAd = templateSingleCSMAdResponseBannerInterstitial(createClassName("MediatedBannerNoFillView"), 320, 50, IMPRESSION_URL, REQUEST_URL, RESPONSE_URL, "", "", "android"); + + ArrayList adsArray = new ArrayList(1); + adsArray.add(csmAd); + + //Return a WaterFall response + return templateMediatedWaterFallResponses(adsArray.toArray(new String[adsArray.size()])); + } + public static String noFillCSM_RTBInterstitial() { //Create a CSM - Ad String csmAd = templateSingleCSMAdResponseBannerInterstitial(createClassName("MediatedInterstitialNoFillView"), 320, 480, IMPRESSION_URL, REQUEST_URL, RESPONSE_URL, "", "", "android"); diff --git a/sdk/test/com/appnexus/opensdk/UTAdRequestTest.java b/sdk/test/com/appnexus/opensdk/UTAdRequestTest.java index ac2268977..c28538896 100644 --- a/sdk/test/com/appnexus/opensdk/UTAdRequestTest.java +++ b/sdk/test/com/appnexus/opensdk/UTAdRequestTest.java @@ -52,6 +52,7 @@ public class UTAdRequestTest extends BaseRoboTest implements UTAdRequester { UTAdResponse response; boolean requesterReceivedServerResponse = false; public static final int PLACEMENT_ID = 123456; + public static final int PUBLISHER_ID = 9876; public static final String EXTERNAL_UID = "b865df7e-097f-4167-8a5c-44d778e75ee6"; // IAB USPrivacy @@ -104,6 +105,31 @@ public void testPlacementId() throws Exception { inspectRequestForValidId(false); } + /** + * Tests PublisherId validity in the request + * + * @throws Exception + */ + @Test + public void testPublisherId() throws Exception { + utRequestParameters.setPublisherId(PUBLISHER_ID); + executionSteps(); + inspectPublisherId(PUBLISHER_ID); + } + + /** + * Tests Invalid PublisherId validity in the request + * + * @throws Exception + */ + @Test + public void testInvalidPublisherId() throws Exception { + utRequestParameters.setPublisherId(-1); + executionSteps(); + JSONObject postData = inspectPostData(); + assertFalse(postData.has("publisher_id")); + } + /** * Tests CustomKeyWords validity in the request * Single Key and single Value @@ -511,6 +537,14 @@ private void inspectMaxDuration(int maxDuration, JSONObject tagData) throws JSON } + private void inspectPublisherId(int publisherId) throws JSONException, InterruptedException { + JSONObject postData = inspectPostData(); + System.out.println("Checking Publisher ID..."); + assertTrue(postData.has("publisher_id")); + assertEquals(publisherId, postData.getInt("publisher_id")); + System.out.println("Publisher ID test passed!"); + } + private void inspectMinDuration(int minDuration, JSONObject tagData) throws JSONException { System.out.println("Checking min duration..."); @@ -645,7 +679,7 @@ private int getGenderInt(AdView.GENDER genderToTest) { long time; @Override - public void failed(ResultCode code) { + public void failed(ResultCode code, ANAdResponseInfo adResponseInfo) { } @@ -683,8 +717,8 @@ public boolean isHttpsEnabled() { public void onReceiveUTResponse(UTAdResponse response) { if (response != null && response.getAdList() != null && !response.getAdList().isEmpty()) { requesterReceivedServerResponse = true; - } else { - failed(ResultCode.UNABLE_TO_FILL); + }else{ + failed(ResultCode.UNABLE_TO_FILL, response.getAdResponseInfo()); } this.response = response; } diff --git a/sdk/test/com/appnexus/opensdk/UTAdResponseTest.java b/sdk/test/com/appnexus/opensdk/UTAdResponseTest.java index 4360fea17..b8c308226 100644 --- a/sdk/test/com/appnexus/opensdk/UTAdResponseTest.java +++ b/sdk/test/com/appnexus/opensdk/UTAdResponseTest.java @@ -103,7 +103,7 @@ public void testBannerResponse() throws Exception { while (!list.isEmpty()) { BaseAdResponse baseAdResponse = (BaseAdResponse) list.removeFirst(); assertEquals("rtb", baseAdResponse.getContentSource()); - assertEquals("6332753", baseAdResponse.getCreativeId()); + assertEquals("6332753", baseAdResponse.getAdResponseInfo().getCreativeId()); } } @@ -156,7 +156,7 @@ public void testBannerNativeResponse() throws Exception { assertTrue(nativeAdResponse.getNativeAdResponse().getImageUrl().contains("http://path_to_main.com")); assertEquals(false, nativeAdResponse.getNativeAdResponse().isOpenNativeBrowser()); assertEquals(true, nativeAdResponse.getNativeAdResponse().getLoadsInBackground()); - assertEquals("47772560", nativeAdResponse.getCreativeId()); + assertEquals("47772560", nativeAdResponse.getAdResponseInfo().getCreativeId()); } } @@ -177,11 +177,11 @@ public void testBannerCSMResponse() throws Exception { System.out.println("Printing first"); CSMSDKAdResponse baseCSMSDKAdResponse = (CSMSDKAdResponse) list.getFirst(); assertEquals("csm", baseCSMSDKAdResponse.getContentSource()); - assertEquals("44863345", baseCSMSDKAdResponse.getCreativeId()); + assertEquals("44863345", baseCSMSDKAdResponse.getAdResponseInfo().getCreativeId()); System.out.println("Printing second"); BaseAdResponse baseAdResponse = (BaseAdResponse) list.getLast(); assertEquals("rtb", baseAdResponse.getContentSource()); - assertEquals("6332753", baseAdResponse.getCreativeId()); + assertEquals("6332753", baseAdResponse.getAdResponseInfo().getCreativeId()); } @@ -202,7 +202,7 @@ public void testNoBannerCSMResponse() throws Exception { CSMSDKAdResponse baseCSMSDKAdResponse = (CSMSDKAdResponse) list.getFirst(); assertEquals("csm", baseCSMSDKAdResponse.getContentSource()); assertNull(baseCSMSDKAdResponse.getAdContent()); - assertEquals("44863345", baseCSMSDKAdResponse.getCreativeId()); + assertEquals("44863345", baseCSMSDKAdResponse.getAdResponseInfo().getCreativeId()); } @@ -224,7 +224,7 @@ public void testBannerSSMResponse() throws Exception { SSMHTMLAdResponse baseSSMHTMLAdResponse = (SSMHTMLAdResponse) list.getFirst(); assertEquals("ssm", baseSSMHTMLAdResponse.getContentSource()); assertEquals((TestResponsesUT.SSM_URL), baseSSMHTMLAdResponse.getAdUrl()); - assertEquals("44863345", baseSSMHTMLAdResponse.getCreativeId()); + assertEquals("44863345", baseSSMHTMLAdResponse.getAdResponseInfo().getCreativeId()); } /** diff --git a/sdk/test/com/appnexus/opensdk/mocks/MockFBNativeBannerAdResponse.java b/sdk/test/com/appnexus/opensdk/mocks/MockFBNativeBannerAdResponse.java index b555db011..e8b54ccbf 100644 --- a/sdk/test/com/appnexus/opensdk/mocks/MockFBNativeBannerAdResponse.java +++ b/sdk/test/com/appnexus/opensdk/mocks/MockFBNativeBannerAdResponse.java @@ -3,6 +3,7 @@ import android.graphics.Bitmap; import android.view.View; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.BaseNativeAdResponse; import com.appnexus.opensdk.CSRController; import com.appnexus.opensdk.NativeAdEventListener; @@ -12,6 +13,7 @@ public class MockFBNativeBannerAdResponse extends BaseNativeAdResponse { private CSRController callback; + private ANAdResponseInfo adResponseInfo; public MockFBNativeBannerAdResponse(CSRController callback) { this.callback = callback; @@ -85,6 +87,16 @@ public void setCreativeId(String creativeId) { } + @Override + public ANAdResponseInfo getAdResponseInfo() { + return adResponseInfo; + } + + @Override + public void setAdResponseInfo(ANAdResponseInfo adResponseInfo) { + this.adResponseInfo = adResponseInfo; + } + @Override public String getIconUrl() { return null; diff --git a/sdk/test/com/appnexus/opensdk/testviews/MediatedNativeSuccessful.java b/sdk/test/com/appnexus/opensdk/testviews/MediatedNativeSuccessful.java index 2fe1e2e7d..f41f4b66d 100644 --- a/sdk/test/com/appnexus/opensdk/testviews/MediatedNativeSuccessful.java +++ b/sdk/test/com/appnexus/opensdk/testviews/MediatedNativeSuccessful.java @@ -20,6 +20,7 @@ import android.graphics.Bitmap; import android.view.View; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.BaseNativeAdResponse; import com.appnexus.opensdk.MediatedNativeAd; import com.appnexus.opensdk.MediatedNativeAdController; @@ -42,6 +43,7 @@ public class MediatedNativeSuccessful implements MediatedNativeAd { public static String uid; private NativeAdResponse.ImageSize mainImageSize = new NativeAdResponse.ImageSize(-1, -1); private NativeAdResponse.ImageSize iconSize = new NativeAdResponse.ImageSize(-1, -1); + private ANAdResponseInfo adResponseInfo; @Override public void requestNativeAd(Context context, String parameterString, String uid, MediatedNativeAdController mBC, TargetingParameters tp) { @@ -91,6 +93,16 @@ public void setCreativeId(String creativeId) { } + @Override + public ANAdResponseInfo getAdResponseInfo() { + return adResponseInfo; + } + + @Override + public void setAdResponseInfo(ANAdResponseInfo adResponseInfo) { + MediatedNativeSuccessful.this.adResponseInfo = adResponseInfo; + } + @Override public String getIconUrl() { return null; diff --git a/sdk/test/com/appnexus/opensdk/testviews/MediatedNativeSuccessful2.java b/sdk/test/com/appnexus/opensdk/testviews/MediatedNativeSuccessful2.java index ff4a2ea55..f6b7719f2 100644 --- a/sdk/test/com/appnexus/opensdk/testviews/MediatedNativeSuccessful2.java +++ b/sdk/test/com/appnexus/opensdk/testviews/MediatedNativeSuccessful2.java @@ -20,6 +20,7 @@ import android.graphics.Bitmap; import android.view.View; +import com.appnexus.opensdk.ANAdResponseInfo; import com.appnexus.opensdk.BaseNativeAdResponse; import com.appnexus.opensdk.MediatedNativeAd; import com.appnexus.opensdk.MediatedNativeAdController; @@ -40,6 +41,7 @@ public class MediatedNativeSuccessful2 implements MediatedNativeAd { public static boolean didPass; private NativeAdResponse.ImageSize mainImageSize = new NativeAdResponse.ImageSize(-1, -1); private NativeAdResponse.ImageSize iconSize = new NativeAdResponse.ImageSize(-1, -1); + private ANAdResponseInfo adResponseInfo; @Override public void requestNativeAd(Context context, String parameterString, String uid, MediatedNativeAdController mBC, TargetingParameters tp) { @@ -87,6 +89,16 @@ public void setCreativeId(String creativeId) { } + @Override + public ANAdResponseInfo getAdResponseInfo() { + return adResponseInfo; + } + + @Override + public void setAdResponseInfo(ANAdResponseInfo adResponseInfo) { + MediatedNativeSuccessful2.this.adResponseInfo = adResponseInfo; + } + @Override public String getIconUrl() { return null; diff --git a/tests/AppNexusSDKTestApp/app/src/main/java/appnexus/com/appnexussdktestapp/NativeActivity.kt b/tests/AppNexusSDKTestApp/app/src/main/java/appnexus/com/appnexussdktestapp/NativeActivity.kt index 62b750682..05081e4bb 100644 --- a/tests/AppNexusSDKTestApp/app/src/main/java/appnexus/com/appnexussdktestapp/NativeActivity.kt +++ b/tests/AppNexusSDKTestApp/app/src/main/java/appnexus/com/appnexussdktestapp/NativeActivity.kt @@ -23,7 +23,7 @@ class NativeActivity : AppCompatActivity(), NativeAdRequestListener { handleNativeResponse(nativeAdResponse) } - override fun onAdFailed(errorcode: ResultCode?) { + override fun onAdFailed(errorcode: ResultCode?, adResponseinfo:ANAdResponseInfo) { if (!idlingResource.isIdleNow) idlingResource.decrement() }