VastManager implements VastXmlManagerAggregatorListener { public void prepareVastVideoConfiguration(final String vastXml, final VastManagerListener vastManagerListener) { if (mVastXmlManagerAggregator == null) { mVastManagerListener = vastManagerListener; mVastXmlManagerAggregator = new VastXmlManagerAggregator(this); try { AsyncTasks.safeExecuteOnExecutor(mVastXmlManagerAggregator, vastXml); } catch (Exception e) { MoPubLog.d("Failed to aggregate vast xml", e); mVastManagerListener.onVastVideoConfigurationPrepared(null); } } } VastManager(final Context context); void prepareVastVideoConfiguration(final String vastXml, final VastManagerListener vastManagerListener); void cancel(); @Override void onAggregationComplete(final List<VastXmlManager> vastXmlManagers);  }
@Test public void prepareVastVideoConfiguration_shouldNotifyTheListenerAndContainTheCorrectVastValues() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getNetworkMediaFileUrl()).isEqualTo("https: final String expectedFilePathDiskCache = CacheService.getFilePathDiskCache(vastVideoConfiguration.getNetworkMediaFileUrl()); assertThat(vastVideoConfiguration.getDiskMediaFileUrl()).isEqualTo(expectedFilePathDiskCache); assertThat(vastVideoConfiguration.getClickThroughUrl()).isEqualTo("http: assertThat(vastVideoConfiguration.getImpressionTrackers().size()).isEqualTo(5); assertThat(vastVideoConfiguration.getFractionalTrackers().size()).isEqualTo(3); assertThat(vastVideoConfiguration.getFractionalTrackers().get(0).trackingFraction()).isEqualTo(0.25f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(1).trackingFraction()).isEqualTo(0.5f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(2).trackingFraction()).isEqualTo(0.75f); assertThat(vastVideoConfiguration.getAbsoluteTrackers().size()).isEqualTo(2); assertThat(vastVideoConfiguration.getAbsoluteTrackers().get(0).getTrackingMilliseconds()).isEqualTo(2000); assertThat(vastVideoConfiguration.getAbsoluteTrackers().get(1).getTrackingMilliseconds()).isEqualTo(3100); assertThat(vastVideoConfiguration.getCompleteTrackers().size()).isEqualTo(1); assertThat(vastVideoConfiguration.getCloseTrackers().size()).isEqualTo(2); assertThat(vastVideoConfiguration.getSkipTrackers().size()).isEqualTo(2); assertThat(vastVideoConfiguration.getClickTrackers().size()).isEqualTo(1); final VastCompanionAd vastCompanionAd = vastVideoConfiguration.getVastCompanionAd(); assertThat(vastCompanionAd.getWidth()).isEqualTo(300); assertThat(vastCompanionAd.getHeight()).isEqualTo(250); assertThat(vastCompanionAd.getImageUrl()).isEqualTo("http: assertThat(vastCompanionAd.getClickThroughUrl()).isEqualTo("http: assertThat(vastCompanionAd.getClickTrackers()) .containsOnly("http: }
@Test public void prepareVastVideoConfiguration_shouldHandleMultipleRedirects() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_VAST_XML_STRING); mFakeHttpLayer.addPendingHttpResponse(200, TEST_VAST_XML_STRING); mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getNetworkMediaFileUrl()).isEqualTo("https: final String expectedFilePathDiskCache = CacheService.getFilePathDiskCache(vastVideoConfiguration.getNetworkMediaFileUrl()); assertThat(vastVideoConfiguration.getDiskMediaFileUrl()).isEqualTo(expectedFilePathDiskCache); assertThat(vastVideoConfiguration.getClickThroughUrl()).isEqualTo("http: assertThat(vastVideoConfiguration.getImpressionTrackers().size()).isEqualTo(13); assertThat(vastVideoConfiguration.getAbsoluteTrackers().size()).isEqualTo(6); assertThat(vastVideoConfiguration.getAbsoluteTrackers().get(0).getTrackingMilliseconds()).isEqualTo(2000); assertThat(vastVideoConfiguration.getAbsoluteTrackers().get(1).getTrackingMilliseconds()).isEqualTo(2000); assertThat(vastVideoConfiguration.getAbsoluteTrackers().get(2).getTrackingMilliseconds()).isEqualTo(2000); assertThat(vastVideoConfiguration.getAbsoluteTrackers().get(3).getTrackingMilliseconds()).isEqualTo(3100); assertThat(vastVideoConfiguration.getAbsoluteTrackers().get(4).getTrackingMilliseconds()).isEqualTo(3100); assertThat(vastVideoConfiguration.getAbsoluteTrackers().get(5).getTrackingMilliseconds()).isEqualTo(3100); assertThat(vastVideoConfiguration.getFractionalTrackers().size()).isEqualTo(9); assertThat(vastVideoConfiguration.getFractionalTrackers().get(0).trackingFraction()).isEqualTo(0.25f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(1).trackingFraction()).isEqualTo(0.25f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(2).trackingFraction()).isEqualTo(0.25f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(3).trackingFraction()).isEqualTo(0.5f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(4).trackingFraction()).isEqualTo(0.5f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(5).trackingFraction()).isEqualTo(0.5f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(6).trackingFraction()).isEqualTo(0.75f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(7).trackingFraction()).isEqualTo(0.75f); assertThat(vastVideoConfiguration.getFractionalTrackers().get(8).trackingFraction()).isEqualTo(0.75f); assertThat(vastVideoConfiguration.getCompleteTrackers().size()).isEqualTo(3); assertThat(vastVideoConfiguration.getCloseTrackers().size()).isEqualTo(4); assertThat(vastVideoConfiguration.getSkipTrackers().size()).isEqualTo(4); assertThat(vastVideoConfiguration.getClickTrackers().size()).isEqualTo(3); final VastCompanionAd vastCompanionAd = vastVideoConfiguration.getVastCompanionAd(); assertThat(vastCompanionAd.getWidth()).isEqualTo(300); assertThat(vastCompanionAd.getHeight()).isEqualTo(250); assertThat(vastCompanionAd.getImageUrl()).isEqualTo("http: assertThat(vastCompanionAd.getClickThroughUrl()).isEqualTo("http: assertThat(vastCompanionAd.getClickTrackers()) .containsOnly("http: }
@Test public void prepareVastVideoConfiguration_shouldReturnCorrectVastValuesWhenAVastRedirectFails() throws Exception { mFakeHttpLayer.addPendingHttpResponse(404, ""); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getNetworkMediaFileUrl()).isEqualTo("https: final String expectedFilePathDiskCache = CacheService.getFilePathDiskCache(vastVideoConfiguration.getNetworkMediaFileUrl()); assertThat(vastVideoConfiguration.getDiskMediaFileUrl()).isEqualTo(expectedFilePathDiskCache); assertThat(vastVideoConfiguration.getClickThroughUrl()).isEqualTo(null); assertThat(vastVideoConfiguration.getImpressionTrackers().size()).isEqualTo(4); assertThat(vastVideoConfiguration.getFractionalTrackers().size()).isEqualTo(3); }
@Test public void prepareVastVideoConfiguration_withNoExtensions_shouldContainTheCorrectDefaultExtensionValues() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getCustomCtaText()).isNull(); assertThat(vastVideoConfiguration.getCustomSkipText()).isNull(); assertThat(vastVideoConfiguration.getCustomCloseIconUrl()).isNull(); assertThat(vastVideoConfiguration.getCustomForceOrientation()).isEqualTo(DeviceUtils.ForceOrientation.FORCE_LANDSCAPE); }
@Test public void prepareVastVideoConfiguration_withExtensionsUnderWrapper_shouldContainTheCorrectCustomExtensionValues() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); subject.prepareVastVideoConfiguration( TEST_VAST_XML_STRING.replace(EXTENSIONS_SNIPPET_PLACEHOLDER, "<Extensions>" + "<Extension type=\"MoPub\">" + "<MoPubCtaText>custom CTA text</MoPubCtaText>" + "<MoPubSkipText>skip</MoPubSkipText>" + "<MoPubCloseIcon>http: "<MoPubForceOrientation>device</MoPubForceOrientation>" + "</Extension>" + "</Extensions>"), vastManagerListener); Robolectric.runBackgroundTasks(); Robolectric.runUiThreadTasks(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getCustomCtaText()).isEqualTo("custom CTA text"); assertThat(vastVideoConfiguration.getCustomSkipText()).isEqualTo("skip"); assertThat(vastVideoConfiguration.getCustomCloseIconUrl()).isEqualTo("http: assertThat(vastVideoConfiguration.getCustomForceOrientation()).isEqualTo(DeviceUtils.ForceOrientation.DEVICE_ORIENTATION); }
@Test public void prepareVastVideoConfiguration_withExtensionsUnderInline_shouldContainTheCorrectCustomExtensionValues() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace(EXTENSIONS_SNIPPET_PLACEHOLDER, "<Extensions>" + "<Extension type=\"MoPub\">" + "<MoPubCtaText>custom CTA text</MoPubCtaText>" + "<MoPubSkipText>skip</MoPubSkipText>" + "<MoPubCloseIcon>http: "<MoPubForceOrientation>device</MoPubForceOrientation>" + "</Extension>" + "</Extensions>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getCustomCtaText()).isEqualTo("custom CTA text"); assertThat(vastVideoConfiguration.getCustomSkipText()).isEqualTo("skip"); assertThat(vastVideoConfiguration.getCustomCloseIconUrl()).isEqualTo("http: assertThat(vastVideoConfiguration.getCustomForceOrientation()).isEqualTo(DeviceUtils.ForceOrientation.DEVICE_ORIENTATION); }
@Test public void prepareVastVideoConfiguration_withExtensionsUnderBothWrapperAndInline_shouldContainLastParsedCustomExtensionValues() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace(EXTENSIONS_SNIPPET_PLACEHOLDER, "<Extensions>" + "<Extension type=\"MoPub\">" + "<MoPubCtaText>CTA 2</MoPubCtaText>" + "<MoPubSkipText>skip 2</MoPubSkipText>" + "<MoPubCloseIcon>http: "<MoPubForceOrientation>landscape</MoPubForceOrientation>" + "</Extension>" + "</Extensions>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); subject.prepareVastVideoConfiguration( TEST_VAST_XML_STRING.replace(EXTENSIONS_SNIPPET_PLACEHOLDER, "<Extensions>" + "<Extension type=\"MoPub\">" + "<MoPubCtaText>CTA 1</MoPubCtaText>" + "<MoPubSkipText>skip 1</MoPubSkipText>" + "<MoPubCloseIcon>http: "<MoPubForceOrientation>device orientation</MoPubForceOrientation>" + "</Extension>" + "</Extensions>"), vastManagerListener); Robolectric.runBackgroundTasks(); Robolectric.runUiThreadTasks(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getCustomCtaText()).isEqualTo("CTA 2"); assertThat(vastVideoConfiguration.getCustomSkipText()).isEqualTo("skip 2"); assertThat(vastVideoConfiguration.getCustomCloseIconUrl()).isEqualTo("http: assertThat(vastVideoConfiguration.getCustomForceOrientation()).isEqualTo(DeviceUtils.ForceOrientation.FORCE_LANDSCAPE); }
@Test public void prepareVastVideoConfiguration_withCustomCtaTextAsSingleSpace_shouldReturnEmptyString() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace(EXTENSIONS_SNIPPET_PLACEHOLDER, "<Extensions>" + "<Extension type=\"MoPub\">" + "<MoPubCtaText> </MoPubCtaText>" + "</Extension>" + "</Extensions>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getCustomCtaText()).isEmpty(); }
@Test public void prepareVastVideoConfiguration_withCustomCtaTextLongerThan15Chars_shouldReturnNull() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace(EXTENSIONS_SNIPPET_PLACEHOLDER, "<Extensions>" + "<Extension type=\"MoPub\">" + "<MoPubCtaText>1234567890123456</MoPubCtaText>" + "</Extension>" + "</Extensions>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getCustomCtaText()).isNull(); }
@Test public void prepareVastVideoConfiguration_withCustomSkipTextLongerThan8Chars_shouldReturnNull() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace(EXTENSIONS_SNIPPET_PLACEHOLDER, "<Extensions>" + "<Extension type=\"MoPub\">" + "<MoPubSkipText>123456789</MoPubSkipText>" + "</Extension>" + "</Extensions>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getCustomSkipText()).isNull(); }
@Test public void prepareVastVideoConfiguration_withInvalidCustomForceOrientation_shouldReturnDefaultForceLandscapeOrientation() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace(EXTENSIONS_SNIPPET_PLACEHOLDER, "<Extensions>" + "<Extension type=\"MoPub\">" + "<MoPubForceOrientation>abcd</MoPubForceOrientation>" + "</Extension>" + "</Extensions>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getCustomForceOrientation()).isEqualTo(DeviceUtils.ForceOrientation.FORCE_LANDSCAPE); }
@Test public void prepareVastVideoConfiguration_withCustomForceOrientationInMixedCaseAndUntrimmed_shouldReturnCustomForceOrientation() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace(EXTENSIONS_SNIPPET_PLACEHOLDER, "<Extensions>" + "<Extension type=\"MoPub\">" + "<MoPubForceOrientation> PortRAIT </MoPubForceOrientation>" + "</Extension>" + "</Extensions>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getCustomForceOrientation()).isEqualTo(DeviceUtils.ForceOrientation.FORCE_PORTRAIT); }
@Test public void prepareVastVideoConfiguration_withValidPercentSkipOffset_shouldReturnCorrectValue() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace("<Linear>", "<Linear skipoffset='25%'>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getSkipOffset()).isEqualTo("25%"); }
@Test public void prepareVastVideoConfiguration_withValidAbsoluteSkipOffset_shouldReturnCorrectValue() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace("<Linear>", "<Linear skipoffset=' 00:03:14 '>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getSkipOffset()).isEqualTo("00:03:14"); }
@Test public void prepareVastVideoConfiguration_withValidAbsoluteSkipOffsetWithExtraSpace_shouldReturnCorrectTrimmedValue() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace("<Linear>", "<Linear skipoffset=' 00:03:14.159 '>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getSkipOffset()).isEqualTo("00:03:14.159"); }
@Test public void prepareVastVideoConfiguration_withSkipOffsets_shouldReturnLastParsedValue() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace("<Linear>", "<Linear skipoffset='25%'>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); subject.prepareVastVideoConfiguration( TEST_VAST_XML_STRING.replace("<Linear>", "<Linear skipoffset='00:03:14'>"), vastManagerListener); Robolectric.runBackgroundTasks(); Robolectric.runUiThreadTasks(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getSkipOffset()).isEqualTo("25%"); }
@Test public void prepareVastVideoConfiguration_withEmptySkipOffset_shouldReturnNull() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING.replace("<Linear>", "<Linear skipoffset=' '>")); mFakeHttpLayer.addPendingHttpResponse(200, "video_data"); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getSkipOffset()).isNull(); }
@Test public void prepareVastVideoConfiguration_withNoMediaUrlInXml_shouldReturnNull() throws Exception { subject.prepareVastVideoConfiguration(TEST_VAST_BAD_NEST_URL_XML_STRING, vastManagerListener); Robolectric.runBackgroundTasks(); Robolectric.runUiThreadTasks(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(null); assertThat(vastVideoConfiguration).isEqualTo(null); }
@Test public void prepareVastVideoConfiguration_withNullXml_shouldReturnNull() throws Exception { subject.prepareVastVideoConfiguration(null, vastManagerListener); Robolectric.runBackgroundTasks(); Robolectric.runUiThreadTasks(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(null); assertThat(vastVideoConfiguration).isEqualTo(null); }
@Test public void prepareVastVideoConfiguration_withEmptyXml_shouldReturnNull() throws Exception { subject.prepareVastVideoConfiguration("", vastManagerListener); Robolectric.runBackgroundTasks(); Robolectric.runUiThreadTasks(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(null); assertThat(vastVideoConfiguration).isEqualTo(null); }
@Test public void prepareVastVideoConfiguration_withVideoInDiskCache_shouldNotDownloadVideo() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING); CacheService.putToDiskCache("https: prepareVastVideoConfiguration(); semaphore.acquire(); assertThat(mFakeHttpLayer.getSentHttpRequestInfos().size()).isEqualTo(1); verify(vastManagerListener).onVastVideoConfigurationPrepared(any(VastVideoConfiguration.class)); assertThat(vastVideoConfiguration.getDiskMediaFileUrl()) .isEqualTo(CacheService.getFilePathDiskCache("https: }
@Test public void prepareVastVideoConfiguration_withUninitializedDiskCache_shouldReturnNull() throws Exception { mFakeHttpLayer.addPendingHttpResponse(200, TEST_NESTED_VAST_XML_STRING); prepareVastVideoConfiguration(); semaphore.acquire(); verify(vastManagerListener).onVastVideoConfigurationPrepared(null); assertThat(vastVideoConfiguration).isEqualTo(null); }