|
1 | 1 | package org.prebid.mobile;
|
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.hasItem; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertNull; |
| 7 | +import static org.junit.Assert.assertThat; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import android.app.Application; |
| 12 | +import android.content.Context; |
| 13 | +import android.view.View; |
| 14 | + |
3 | 15 | import org.junit.Test;
|
4 | 16 | import org.junit.runner.RunWith;
|
| 17 | +import org.prebid.mobile.reflection.Reflection; |
| 18 | +import org.prebid.mobile.test.utils.ResourceUtils; |
5 | 19 | import org.robolectric.RobolectricTestRunner;
|
6 | 20 |
|
| 21 | +import java.io.IOException; |
7 | 22 | import java.util.ArrayList;
|
8 |
| - |
9 |
| -import static org.hamcrest.Matchers.hasItem; |
10 |
| -import static org.junit.Assert.*; |
| 23 | +import java.util.List; |
11 | 24 |
|
12 | 25 | @RunWith(RobolectricTestRunner.class)
|
13 | 26 | public class PrebidNativeAdTest {
|
14 | 27 |
|
15 | 28 | @Test
|
16 |
| - public void testNativeAdParser() { |
17 |
| - String cacheId = CacheManager.save(getResponse()); |
18 |
| - PrebidNativeAd nativeAd = PrebidNativeAd.create(cacheId); |
| 29 | + public void registerView_withAllTrackers() { |
| 30 | + PrebidNativeAd nativeAd = nativeAdFromFile("PrebidNativeAdTest/Full.json"); |
| 31 | + |
| 32 | + assertEquals("https://prebid.qa.openx.net//event?t=win&b=5f6bec03-a3ae-4084-b2ae-dedfb0ac01ff&a=b4eb1475-4e3d-4186-97b7-25b6a6cf8618&bidder=openx&ts=1643899069308", nativeAd.getWinEvent()); |
| 33 | + assertEquals("https://prebid.qa.openx.net//event?t=imp&b=5f6bec03-a3ae-4084-b2ae-dedfb0ac01ff&a=b4eb1475-4e3d-4186-97b7-25b6a6cf8618&bidder=openx&ts=1643899069308", nativeAd.getImpEvent()); |
| 34 | + |
| 35 | + ArrayList<String> admImpressionTrackers = reflectAdmImpressionTrackers(nativeAd); |
| 36 | + assertNotNull(admImpressionTrackers); |
| 37 | + assertEquals(1, admImpressionTrackers.size()); |
| 38 | + assertThat(admImpressionTrackers, hasItem("https://s3-us-west-2.amazonaws.com/omsdk-files/compliance-js/omid-validation-verification-script-v1.js")); |
| 39 | + |
| 40 | + |
| 41 | + nativeAd.registerView(createViewMock(), mock(List.class), mock(PrebidNativeAdEventListener.class)); |
| 42 | + |
| 43 | + |
| 44 | + ArrayList<ImpressionTracker> trackerObjects = reflectImpressionTrackerObjects(nativeAd); |
| 45 | + assertEquals(2, trackerObjects.size()); |
| 46 | + assertEquals("https://s3-us-west-2.amazonaws.com/omsdk-files/compliance-js/omid-validation-verification-script-v1.js", reflectImpressionTrackerUrl(trackerObjects.get(0))); |
| 47 | + assertEquals("https://prebid.qa.openx.net//event?t=imp&b=5f6bec03-a3ae-4084-b2ae-dedfb0ac01ff&a=b4eb1475-4e3d-4186-97b7-25b6a6cf8618&bidder=openx&ts=1643899069308", reflectImpressionTrackerUrl(trackerObjects.get(1))); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void registerView_withoutTrackers() { |
| 52 | + PrebidNativeAd nativeAd = nativeAdFromFile("PrebidNativeAdTest/WithoutTrackers.json"); |
| 53 | + |
| 54 | + assertNull(nativeAd.getWinEvent()); |
| 55 | + assertNull(nativeAd.getImpEvent()); |
| 56 | + assertNull(reflectAdmImpressionTrackers(nativeAd)); |
| 57 | + |
| 58 | + |
| 59 | + nativeAd.registerView(createViewMock(), mock(List.class), mock(PrebidNativeAdEventListener.class)); |
| 60 | + |
| 61 | + |
| 62 | + ArrayList<ImpressionTracker> trackerObjects = reflectImpressionTrackerObjects(nativeAd); |
| 63 | + assertEquals(0, trackerObjects.size()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void nativeAdParser() { |
| 68 | + PrebidNativeAd nativeAd = nativeAdFromFile("PrebidNativeAdTest/Full.json"); |
19 | 69 |
|
20 | 70 | assertNotNull(nativeAd);
|
21 | 71 |
|
@@ -56,69 +106,36 @@ public void testNativeAdParser() {
|
56 | 106 | }
|
57 | 107 | }
|
58 | 108 |
|
59 |
| - private String getResponse() { |
60 |
| - return "{\n" + |
61 |
| - " \"id\": \"5f6bec03-a3ae-4084-b2ae-dedfb0ac01ff\",\n" + |
62 |
| - " \"impid\": \"PrebidMobile\",\n" + |
63 |
| - " \"price\": 0.11259999999999999,\n" + |
64 |
| - " \"adm\": \"{\\\"assets\\\":[{\\\"required\\\":1,\\\"data\\\":{\\\"value\\\":\\\"Sample value 2\\\"}},{\\\"required\\\":1,\\\"data\\\":{\\\"type\\\":500,\\\"value\\\":\\\"Sample value\\\"}},{\\\"required\\\":1,\\\"img\\\":{\\\"type\\\":500,\\\"url\\\":\\\"https://test.com/test.png\\\"}},{\\\"required\\\":1,\\\"img\\\":{\\\"url\\\":\\\"https://test2.com/test.png\\\"}},\n{\\\"required\\\":1,\\\"title\\\":{\\\"text\\\":\\\"OpenX (Title)\\\"}},{\\\"required\\\":1,\\\"img\\\":{\\\"type\\\":1,\\\"url\\\":\\\"https:\\/\\/www.saashub.com\\/images\\/app\\/service_logos\\/5\\/1df363c9a850\\/large.png?1525414023\\\"}},{\\\"required\\\":1,\\\"img\\\":{\\\"type\\\":3,\\\"url\\\":\\\"https:\\/\\/ssl-i.cdn.openx.com\\/mobile\\/demo-creatives\\/mobile-demo-banner-640x100.png\\\"}},{\\\"required\\\":1,\\\"data\\\":{\\\"type\\\":1,\\\"value\\\":\\\"OpenX (Brand)\\\"}},{\\\"required\\\":1,\\\"data\\\":{\\\"type\\\":2,\\\"value\\\":\\\"Learn all about this awesome story of someone using out OpenX SDK.\\\"}},{\\\"required\\\":1,\\\"data\\\":{\\\"type\\\":12,\\\"value\\\":\\\"Click here to visit our site!\\\"}}],\\\"link\\\":{\\\"url\\\":\\\"https:\\/\\/www.openx.com\\/\\\"},\\\"eventtrackers\\\":[{\\\"event\\\":555,\\\"method\\\":2,\\\"url\\\":\\\"https:\\/\\/s3-us-west-2.amazonaws.com\\/omsdk-files\\/compliance-js\\/omid-validation-verification-script-v1.js\\\",\\\"ext\\\":{\\\"vendorKey\\\":\\\"iabtechlab.com-omid\\\",\\\"verification_parameters\\\":\\\"iabtechlab-Openx\\\"}}]}\",\n" + |
65 |
| - " \"adid\": \"test-ad-id-12345\",\n" + |
66 |
| - " \"adomain\": [\n" + |
67 |
| - " \"openx.com\"\n" + |
68 |
| - " ],\n" + |
69 |
| - " \"crid\": \"test-creative-id-1\",\n" + |
70 |
| - " \"w\": 300,\n" + |
71 |
| - " \"h\": 250,\n" + |
72 |
| - " \"ext\": {\n" + |
73 |
| - " \"ad_ox_cats\": [\n" + |
74 |
| - " 2\n" + |
75 |
| - " ],\n" + |
76 |
| - " \"agency_id\": \"agency_10\",\n" + |
77 |
| - " \"brand_id\": \"brand_10\",\n" + |
78 |
| - " \"buyer_id\": \"buyer_10\",\n" + |
79 |
| - " \"matching_ad_id\": {\n" + |
80 |
| - " \"campaign_id\": 1,\n" + |
81 |
| - " \"creative_id\": 3,\n" + |
82 |
| - " \"placement_id\": 2\n" + |
83 |
| - " },\n" + |
84 |
| - " \"next_highest_bid_price\": 0.099,\n" + |
85 |
| - " \"prebid\": {\n" + |
86 |
| - " \"cache\": {\n" + |
87 |
| - " \"key\": \"\",\n" + |
88 |
| - " \"url\": \"\",\n" + |
89 |
| - " \"bids\": {\n" + |
90 |
| - " \"url\": \"prebid.qa.openx.net\\/cache?uuid=feb0b9c0-7064-4dd4-8607-bef8a41f7a2c\",\n" + |
91 |
| - " \"cacheId\": \"feb0b9c0-7064-4dd4-8607-bef8a41f7a2c\"\n" + |
92 |
| - " }\n" + |
93 |
| - " },\n" + |
94 |
| - " \"targeting\": {\n" + |
95 |
| - " \"hb_bidder\": \"openx\",\n" + |
96 |
| - " \"hb_bidder_openx\": \"openx\",\n" + |
97 |
| - " \"hb_cache_host\": \"prebid.qa.openx.net\",\n" + |
98 |
| - " \"hb_cache_host_openx\": \"prebid.qa.openx.net\",\n" + |
99 |
| - " \"hb_cache_id\": \"feb0b9c0-7064-4dd4-8607-bef8a41f7a2c\",\n" + |
100 |
| - " \"hb_cache_id_openx\": \"feb0b9c0-7064-4dd4-8607-bef8a41f7a2c\",\n" + |
101 |
| - " \"hb_cache_path\": \"\\/cache\",\n" + |
102 |
| - " \"hb_cache_path_openx\": \"\\/cache\",\n" + |
103 |
| - " \"hb_env\": \"mobile-app\",\n" + |
104 |
| - " \"hb_env_openx\": \"mobile-app\",\n" + |
105 |
| - " \"hb_pb\": \"0.10\",\n" + |
106 |
| - " \"hb_pb_openx\": \"0.10\",\n" + |
107 |
| - " \"hb_size\": \"300x250\",\n" + |
108 |
| - " \"hb_size_openx\": \"300x250\"\n" + |
109 |
| - " },\n" + |
110 |
| - " \"type\": \"banner\",\n" + |
111 |
| - " \"video\": {\n" + |
112 |
| - " \"duration\": 0,\n" + |
113 |
| - " \"primary_category\": \"\"\n" + |
114 |
| - " },\n" + |
115 |
| - " \"events\": {\n" + |
116 |
| - " \"win\": \"https:\\/\\/prebid.qa.openx.net\\/\\/event?t=win&b=5f6bec03-a3ae-4084-b2ae-dedfb0ac01ff&a=b4eb1475-4e3d-4186-97b7-25b6a6cf8618&bidder=openx&ts=1643899069308\",\n" + |
117 |
| - " \"imp\": \"https:\\/\\/prebid.qa.openx.net\\/\\/event?t=imp&b=5f6bec03-a3ae-4084-b2ae-dedfb0ac01ff&a=b4eb1475-4e3d-4186-97b7-25b6a6cf8618&bidder=openx&ts=1643899069308\"\n" + |
118 |
| - " }\n" + |
119 |
| - " }\n" + |
120 |
| - " }\n" + |
121 |
| - "}"; |
| 109 | + |
| 110 | + private PrebidNativeAd nativeAdFromFile(String path) { |
| 111 | + try { |
| 112 | + String resource = ResourceUtils.convertResourceToString(path); |
| 113 | + String cacheId = CacheManager.save(resource); |
| 114 | + return PrebidNativeAd.create(cacheId); |
| 115 | + } catch (IOException e) { |
| 116 | + throw new NullPointerException(e.getMessage()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private View createViewMock() { |
| 121 | + Context contextMock = mock(Context.class); |
| 122 | + when(contextMock.getApplicationContext()).thenReturn(mock(Application.class)); |
| 123 | + |
| 124 | + View mainMock = mock(View.class); |
| 125 | + when(mainMock.getContext()).thenReturn(contextMock); |
| 126 | + return mainMock; |
| 127 | + } |
| 128 | + |
| 129 | + private ArrayList<String> reflectAdmImpressionTrackers(PrebidNativeAd ad) { |
| 130 | + return Reflection.getFieldOf(ad, "imp_trackers"); |
| 131 | + } |
| 132 | + |
| 133 | + private ArrayList<ImpressionTracker> reflectImpressionTrackerObjects(PrebidNativeAd ad) { |
| 134 | + return Reflection.getFieldOf(ad, "impressionTrackers"); |
| 135 | + } |
| 136 | + |
| 137 | + private String reflectImpressionTrackerUrl(ImpressionTracker tracker) { |
| 138 | + return Reflection.getFieldOf(tracker, "url"); |
122 | 139 | }
|
123 | 140 |
|
124 | 141 | }
|
0 commit comments