Search in sources :

Example 1 with VideoCreativeModel

use of org.prebid.mobile.rendering.video.VideoCreativeModel in project prebid-mobile-android by prebid.

the class CreativeModelsMakerVast method makeModelsContinued.

private void makeModelsContinued() {
    try {
        // TODO: If we want to support a VAST Buffet, we'll need to put the following in a
        // TODO: loop and make a model for each Ad object in the Buffet
        // TODO: Until then, we'll only make one model
        /**
         * We pre parse the impressions and trackings for faster reading at
         * video time. DO NOT REMOVE THESE LINES
         */
        mRootVastParser.getAllTrackings(mRootVastParser, 0);
        mRootVastParser.getImpressions(mRootVastParser, 0);
        mRootVastParser.getClickTrackings(mRootVastParser, 0);
        final String videoErrorUrl = mRootVastParser.getError(mRootVastParser, 0);
        final String vastClickThroughUrl = mRootVastParser.getClickThroughUrl(mRootVastParser, 0);
        final String videoDuration = mLatestVastWrapperParser.getVideoDuration(mLatestVastWrapperParser, 0);
        final String skipOffset = mLatestVastWrapperParser.getSkipOffset(mLatestVastWrapperParser, 0);
        final AdVerifications adVerifications = mRootVastParser.getAdVerification(mLatestVastWrapperParser, 0);
        Result result = new Result();
        result.loaderIdentifier = mAdLoaderIdentifier;
        TrackingManager trackingManager = TrackingManager.getInstance();
        OmEventTracker omEventTracker = new OmEventTracker();
        VideoCreativeModel videoModel = new VideoCreativeModel(trackingManager, omEventTracker, mAdConfiguration);
        videoModel.setName(VIDEO_CREATIVE_TAG);
        videoModel.setMediaUrl(mLatestVastWrapperParser.getMediaFileUrl(mLatestVastWrapperParser, 0));
        videoModel.setMediaDuration(Utils.getMsFrom(videoDuration));
        videoModel.setSkipOffset(Utils.getMsFrom(skipOffset));
        videoModel.setAdVerifications(adVerifications);
        videoModel.setAuid(mRootVastParser.getVast().getAds().get(0).getId());
        videoModel.setWidth(mLatestVastWrapperParser.getWidth());
        videoModel.setHeight(mLatestVastWrapperParser.getHeight());
        // put tracking urls into element.
        for (VideoAdEvent.Event videoEvent : VideoAdEvent.Event.values()) {
            videoModel.getVideoEventUrls().put(videoEvent, mRootVastParser.getTrackingByType(videoEvent));
        }
        // put impression urls into element
        ArrayList<String> impUrls = new ArrayList<>();
        for (Impression impression : mRootVastParser.getImpressions()) {
            impUrls.add(impression.getValue());
        }
        videoModel.getVideoEventUrls().put(VideoAdEvent.Event.AD_IMPRESSION, impUrls);
        // put click urls into element
        ArrayList<String> clickTrackingUrls = new ArrayList<>();
        for (ClickTracking clickTracking : mRootVastParser.getClickTrackings()) {
            clickTrackingUrls.add(clickTracking.getValue());
        }
        videoModel.getVideoEventUrls().put(VideoAdEvent.Event.AD_CLICK, clickTrackingUrls);
        // put error vastURL into element
        ArrayList<String> errorUrls = new ArrayList<>();
        errorUrls.add(videoErrorUrl);
        videoModel.getVideoEventUrls().put(VideoAdEvent.Event.AD_ERROR, errorUrls);
        // put click through url into element
        videoModel.setVastClickthroughUrl(vastClickThroughUrl);
        result.creativeModels = new ArrayList<>();
        result.creativeModels.add(videoModel);
        CreativeModel endCardModel = new CreativeModel(trackingManager, omEventTracker, mAdConfiguration);
        endCardModel.setName(HTML_CREATIVE_TAG);
        endCardModel.setHasEndCard(true);
        // Create CompanionAd object
        Companion companionAd = AdResponseParserVast.getCompanionAd(mLatestVastWrapperParser.getVast().getAds().get(0).getInline());
        if (companionAd != null) {
            switch(AdResponseParserVast.getCompanionResourceFormat(companionAd)) {
                case RESOURCE_FORMAT_HTML:
                    endCardModel.setHtml(companionAd.getHtmlResource().getValue());
                    break;
                case RESOURCE_FORMAT_IFRAME:
                    endCardModel.setHtml(companionAd.getIFrameResource().getValue());
                    break;
                case RESOURCE_FORMAT_STATIC:
                    endCardModel.setHtml(String.format("<div id=\"ad\" align=\"center\">\n" + "<a href=\"%s\">\n" + "<img src=\"%s\"></a>\n" + "</div>", companionAd.getCompanionClickThrough().getValue(), companionAd.getStaticResource().getValue()));
                    break;
            }
            if (companionAd.getCompanionClickThrough() != null) {
                endCardModel.setClickUrl(companionAd.getCompanionClickThrough().getValue());
            }
            if (companionAd.getCompanionClickTracking() != null) {
                clickTrackingUrls = new ArrayList<>();
                clickTrackingUrls.add(companionAd.getCompanionClickTracking().getValue());
                endCardModel.registerTrackingEvent(TrackingEvent.Events.CLICK, clickTrackingUrls);
            }
            Tracking creativeViewTracking = AdResponseParserVast.findTracking(companionAd.getTrackingEvents());
            if (creativeViewTracking != null && Utils.isNotBlank(creativeViewTracking.getValue())) {
                ArrayList<String> creativeViewTrackingUrls = new ArrayList<>();
                creativeViewTrackingUrls.add(creativeViewTracking.getValue());
                endCardModel.registerTrackingEvent(TrackingEvent.Events.IMPRESSION, creativeViewTrackingUrls);
            }
            endCardModel.setWidth(Integer.parseInt(companionAd.getWidth()));
            endCardModel.setHeight(Integer.parseInt(companionAd.getHeight()));
            endCardModel.setAdConfiguration(new AdConfiguration());
            endCardModel.getAdConfiguration().setAdUnitIdentifierType(AdConfiguration.AdUnitIdentifierType.INTERSTITIAL);
            endCardModel.setRequireImpressionUrl(false);
            result.creativeModels.add(endCardModel);
            // Flag that video creative has a corresponding end card
            videoModel.setHasEndCard(true);
        }
        mAdConfiguration.setInterstitialSize(videoModel.getWidth() + "x" + videoModel.getHeight());
        mListener.onCreativeModelReady(result);
    } catch (Exception e) {
        LogUtil.error(TAG, "Video failed with: " + e.getMessage());
        notifyErrorListener("Video failed: " + e.getMessage());
    }
}
Also used : ClickTracking(org.prebid.mobile.rendering.video.vast.ClickTracking) ClickTracking(org.prebid.mobile.rendering.video.vast.ClickTracking) Tracking(org.prebid.mobile.rendering.video.vast.Tracking) TrackingManager(org.prebid.mobile.rendering.networking.tracking.TrackingManager) VideoCreativeModel(org.prebid.mobile.rendering.video.VideoCreativeModel) ArrayList(java.util.ArrayList) Impression(org.prebid.mobile.rendering.video.vast.Impression) AdException(org.prebid.mobile.rendering.errors.AdException) Companion(org.prebid.mobile.rendering.video.vast.Companion) OmEventTracker(org.prebid.mobile.rendering.video.OmEventTracker) VideoAdEvent(org.prebid.mobile.rendering.video.VideoAdEvent) AdVerifications(org.prebid.mobile.rendering.video.vast.AdVerifications) VideoCreativeModel(org.prebid.mobile.rendering.video.VideoCreativeModel)

Example 2 with VideoCreativeModel

use of org.prebid.mobile.rendering.video.VideoCreativeModel in project prebid-mobile-android by prebid.

the class CreativeFactory method attemptVastCreative.

private void attemptVastCreative() {
    VideoCreativeModel videoCreativeModel = (VideoCreativeModel) mCreativeModel;
    String mediaUrl = videoCreativeModel.getMediaUrl();
    if (Utils.isBlank(mediaUrl) || mediaUrl.equals("invalid media file")) {
        mListener.onFailure(new AdException(AdException.INTERNAL_ERROR, VASTErrorCodes.NO_SUPPORTED_MEDIA_ERROR.toString()));
        return;
    }
    // get the tracking url for all event types & do the registration here.
    for (VideoAdEvent.Event videoEvent : VideoAdEvent.Event.values()) {
        videoCreativeModel.registerVideoEvent(videoEvent, videoCreativeModel.getVideoEventUrls().get(videoEvent));
    }
    VideoCreative newCreative;
    try {
        if (mCreativeModel.getAdConfiguration().isRewarded()) {
            newCreative = new RewardedVideoCreative(mContextReference.get(), videoCreativeModel, mOmAdSessionManager, mInterstitialManager);
        } else {
            newCreative = new VideoCreative(mContextReference.get(), videoCreativeModel, mOmAdSessionManager, mInterstitialManager);
        }
        newCreative.setResolutionListener(new CreativeFactoryCreativeResolutionListener(this));
        mCreative = newCreative;
        markWorkStart(VAST_TIMEOUT);
        newCreative.load();
    } catch (Exception exception) {
        LogUtil.error(TAG, "VideoCreative creation failed: " + Log.getStackTraceString(exception));
        mListener.onFailure(new AdException(AdException.INTERNAL_ERROR, "VideoCreative creation failed: " + exception.getMessage()));
    }
}
Also used : RewardedVideoCreative(org.prebid.mobile.rendering.video.RewardedVideoCreative) VideoCreative(org.prebid.mobile.rendering.video.VideoCreative) RewardedVideoCreative(org.prebid.mobile.rendering.video.RewardedVideoCreative) VideoAdEvent(org.prebid.mobile.rendering.video.VideoAdEvent) AdException(org.prebid.mobile.rendering.errors.AdException) VideoCreativeModel(org.prebid.mobile.rendering.video.VideoCreativeModel) AdException(org.prebid.mobile.rendering.errors.AdException)

Example 3 with VideoCreativeModel

use of org.prebid.mobile.rendering.video.VideoCreativeModel in project prebid-mobile-android by prebid.

the class CreativeFactoryTest method testAttemptVastCreative.

@Test
public void testAttemptVastCreative() throws Exception {
    VideoCreativeModel mockVideoModel = mock(VideoCreativeModel.class);
    AdConfiguration adConfiguration = new AdConfiguration();
    Handler mockHandler = mock(Handler.class);
    adConfiguration.setAdUnitIdentifierType(AdConfiguration.AdUnitIdentifierType.VAST);
    HashMap<VideoAdEvent.Event, ArrayList<String>> videoEventsUrls = new HashMap<>();
    videoEventsUrls.put(VideoAdEvent.Event.AD_EXPAND, new ArrayList<>(Arrays.asList("AD_EXPAND")));
    when(mockVideoModel.getVideoEventUrls()).thenReturn(videoEventsUrls);
    when(mockVideoModel.getAdConfiguration()).thenReturn(adConfiguration);
    CreativeFactory creativeFactory;
    // Blank media URL
    when(mockVideoModel.getMediaUrl()).thenReturn("");
    creativeFactory = new CreativeFactory(mMockContext, mockVideoModel, mMockListener, mMockOmAdSessionManager, mMockInterstitialManager);
    creativeFactory.start();
    assertNull(WhiteBox.getInternalState(creativeFactory, "mCreative"));
    // Valid
    when(mockVideoModel.getMediaUrl()).thenReturn("mediaUrl");
    creativeFactory = new CreativeFactory(mMockContext, mockVideoModel, mMockListener, mMockOmAdSessionManager, mMockInterstitialManager);
    WhiteBox.field(CreativeFactory.class, "mTimeoutHandler").set(creativeFactory, mockHandler);
    creativeFactory.start();
    AbstractCreative creative = creativeFactory.getCreative();
    assertNotNull(creative);
    assertTrue(creative instanceof VideoCreative);
    verify(mockHandler).postDelayed(any(Runnable.class), eq(30_000L));
}
Also used : AbstractCreative(org.prebid.mobile.rendering.models.AbstractCreative) VideoCreative(org.prebid.mobile.rendering.video.VideoCreative) HashMap(java.util.HashMap) AdConfiguration(org.prebid.mobile.rendering.models.AdConfiguration) ArrayList(java.util.ArrayList) Handler(android.os.Handler) VideoAdEvent(org.prebid.mobile.rendering.video.VideoAdEvent) VideoCreativeModel(org.prebid.mobile.rendering.video.VideoCreativeModel) Test(org.junit.Test)

Example 4 with VideoCreativeModel

use of org.prebid.mobile.rendering.video.VideoCreativeModel in project prebid-mobile-android by prebid.

the class CreativeModelsMakerVastTest method testMakeModelsWrapper.

// To test wrappers, we use a mock server to handle the vast wrapper redirect
@Test
public void testMakeModelsWrapper() throws Exception {
    String testWrapperFileName = "test_ad_response_vast_wrapper_macro_ad_tag";
    String testInlineFileName = "vast_inline_linear.xml";
    AppInfoManager.setUserAgent("user-agent");
    // Set mock server response as inline vast
    String inlineVastString = ResourceUtils.convertResourceToString(testInlineFileName);
    MockWebServer mockWebServer = new MockWebServer();
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(inlineVastString));
    // Replace vast ad tag macro inside wrapper with mock server URL
    HttpUrl baseUrl = mockWebServer.url("/vast_wrapper");
    String urlString = baseUrl.url().toString();
    String responseString = ResourceUtils.convertResourceToString(testWrapperFileName);
    responseString = responseString.replace("%%VAST_AD_TAG%%", urlString);
    // Set wrapper ad response as parameter for makeModels()
    BaseNetworkTask.GetUrlResult adResponse = new BaseNetworkTask.GetUrlResult();
    adResponse.responseString = responseString;
    List<AdResponseParserBase> parsers = getVastParsers(adResponse);
    AdResponseParserVast rootParser = (AdResponseParserVast) parsers.get(0);
    AdResponseParserVast latestParser = (AdResponseParserVast) parsers.get(1);
    // Execute makeModels()
    CreativeModelsMakerVast creativeModelsMakerVast = new CreativeModelsMakerVast(null, mMockListener);
    creativeModelsMakerVast.makeModels(mAdConfiguration, rootParser, latestParser);
    // Get result
    ArgumentCaptor<CreativeModelsMaker.Result> argumentCaptor = ArgumentCaptor.forClass(CreativeModelsMaker.Result.class);
    verify(mMockListener).onCreativeModelReady(argumentCaptor.capture());
    CreativeModelsMaker.Result result = argumentCaptor.getValue();
    VideoCreativeModel model = (VideoCreativeModel) result.creativeModels.get(0);
    // Assert wrapper specific events
    HashMap<VideoAdEvent.Event, ArrayList<String>> videoEventUrls = model.getVideoEventUrls();
    assertEquals("http://myTrackingURL/wrapper/click", videoEventUrls.get(VideoAdEvent.Event.AD_CLICK).get(0));
    videoEventUrls.get(VideoAdEvent.Event.AD_CLICK).remove(0);
    assertEquals("http://myTrackingURL/wrapper/impression", videoEventUrls.get(VideoAdEvent.Event.AD_IMPRESSION).get(0));
    videoEventUrls.get(VideoAdEvent.Event.AD_IMPRESSION).remove(0);
    assertEquals("http://myTrackingURL/wrapper/creativeView", videoEventUrls.get(VideoAdEvent.Event.AD_CREATIVEVIEW).get(0));
    videoEventUrls.get(VideoAdEvent.Event.AD_CREATIVEVIEW).remove(0);
    // Assert inline
    assertVastInline(result.creativeModels, true);
    mockWebServer.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) BaseNetworkTask(org.prebid.mobile.rendering.networking.BaseNetworkTask) AdResponseParserBase(org.prebid.mobile.rendering.parser.AdResponseParserBase) ArrayList(java.util.ArrayList) AdResponseParserVast(org.prebid.mobile.rendering.parser.AdResponseParserVast) HttpUrl(okhttp3.HttpUrl) VastExtractorResult(org.prebid.mobile.rendering.models.internal.VastExtractorResult) MockWebServer(okhttp3.mockwebserver.MockWebServer) VideoAdEvent(org.prebid.mobile.rendering.video.VideoAdEvent) VideoCreativeModel(org.prebid.mobile.rendering.video.VideoCreativeModel) Test(org.junit.Test)

Example 5 with VideoCreativeModel

use of org.prebid.mobile.rendering.video.VideoCreativeModel in project prebid-mobile-android by prebid.

the class AdViewManagerTest method creativeDidCompleteTest.

@Test
public void creativeDidCompleteTest() throws Exception {
    VideoCreative mockVideoCreative = mock(VideoCreative.class);
    when(mockVideoCreative.getCreativeModel()).thenReturn(new VideoCreativeModel(mock(TrackingManager.class), mock(OmEventTracker.class), new AdConfiguration()));
    when(mockVideoCreative.isVideo()).thenReturn(true);
    Transaction mockTransaction = mock(Transaction.class);
    ArrayList<CreativeFactory> creativeFactories = new ArrayList<>();
    TransactionManager mockTransactionManager = mock(TransactionManager.class);
    when(mockTransactionManager.getCurrentTransaction()).thenReturn(mockTransaction);
    when(mockTransaction.getCreativeFactories()).thenReturn(creativeFactories);
    WhiteBox.field(AdViewManager.class, "mTransactionManager").set(mAdViewManager, mockTransactionManager);
    WhiteBox.field(AdViewManager.class, "mAdView").set(mAdViewManager, mockAdView);
    mAdViewManager.creativeDidComplete(mockVideoCreative);
    verify(mockAdView).closeInterstitialVideo();
    verify(mMockAdViewListener, times(1)).adCompleted();
    mAdViewManager.creativeDidComplete(mockVideoCreative);
    verify(mMockAdViewListener, times(2)).adCompleted();
}
Also used : VideoCreative(org.prebid.mobile.rendering.video.VideoCreative) Transaction(org.prebid.mobile.rendering.loading.Transaction) TransactionManager(org.prebid.mobile.rendering.loading.TransactionManager) AdConfiguration(org.prebid.mobile.rendering.models.AdConfiguration) ArrayList(java.util.ArrayList) CreativeFactory(org.prebid.mobile.rendering.loading.CreativeFactory) VideoCreativeModel(org.prebid.mobile.rendering.video.VideoCreativeModel) Test(org.junit.Test)

Aggregations

VideoCreativeModel (org.prebid.mobile.rendering.video.VideoCreativeModel)11 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 AdConfiguration (org.prebid.mobile.rendering.models.AdConfiguration)5 VideoAdEvent (org.prebid.mobile.rendering.video.VideoAdEvent)5 AbstractCreative (org.prebid.mobile.rendering.models.AbstractCreative)4 VideoCreative (org.prebid.mobile.rendering.video.VideoCreative)4 Context (android.content.Context)3 AdViewManager (org.prebid.mobile.rendering.views.AdViewManager)3 AdViewManagerListener (org.prebid.mobile.rendering.views.AdViewManagerListener)3 AdException (org.prebid.mobile.rendering.errors.AdException)2 VastExtractorResult (org.prebid.mobile.rendering.models.internal.VastExtractorResult)2 BaseNetworkTask (org.prebid.mobile.rendering.networking.BaseNetworkTask)2 AdResponseParserBase (org.prebid.mobile.rendering.parser.AdResponseParserBase)2 AdResponseParserVast (org.prebid.mobile.rendering.parser.AdResponseParserVast)2 Handler (android.os.Handler)1 HashMap (java.util.HashMap)1 HttpUrl (okhttp3.HttpUrl)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 MockWebServer (okhttp3.mockwebserver.MockWebServer)1