package com.brightcove.samples.android.closedcaptioning.dfxp;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.brightcove.player.event.Event;
import com.brightcove.player.event.EventEmitter;
import com.brightcove.player.event.EventListener;
import com.brightcove.player.event.EventType;
import com.brightcove.player.view.BrightcoveVideoView;
import com.robotium.solo.Solo;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * White box style tests for DFXP/TTML closed captioning in the Brightcove SDK.
 */
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    private final String TAG = this.getClass().getSimpleName();

    private Solo solo;
    private BrightcoveVideoView brightcoveVideoView;
    private EventEmitter eventEmitter;
    private MainActivity mainActivity;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        setActivityInitialTouchMode(false);
        mainActivity = getActivity();
        solo = new Solo(getInstrumentation(), mainActivity);
        brightcoveVideoView = (BrightcoveVideoView) mainActivity.findViewById(R.id.brightcove_video_view);
        eventEmitter = brightcoveVideoView.getEventEmitter();

        eventEmitter.once(EventType.DID_SET_VIDEO, new EventListener() {
            @Override
            public void processEvent(Event event) {
                brightcoveVideoView.start();
            }
        });
    }

    /**
     * Test
     */
    public void testCaptionsLoaded() throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        Log.v(TAG, "testCaptionsLoaded");
        eventEmitter.once(EventType.DID_LOAD_CAPTIONS, new EventListener() {
            @Override
            public void processEvent(Event event) {
                Log.v(TAG, "DERP CAPS LOADED");
                latch.countDown();
            }
        });
        assertTrue("Timeout occurred.", latch.await(30, TimeUnit.SECONDS));
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
    }
}
@@ -134,16 +134,42 @@
 til.Log;
+%0Aimport android.view.View;
 %0A%0Aimport
@@ -349,16 +349,82 @@
 ntType;%0A
+import com.brightcove.player.view.BrightcoveClosedCaptioningView;%0A
 import c
@@ -502,16 +502,44 @@
 .Solo;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -1719,20 +1719,145 @@
 %0A     * 
-Test
+Verify that upon receiving DFXP captions that a single BrightcoveClosedCaptioningView is%0A     * added to the BrightcoveVideoView.
 %0A     */
@@ -1884,20 +1884,22 @@
 tCaption
-sLoa
+ViewAd
 ded() th
@@ -2021,19 +2021,23 @@
 tion
-sLoa
+ViewAd
 ded
+:
 %22);%0A
+%0A
     
@@ -2203,37 +2203,309 @@
     
-Log.v(TAG, %22DERP CAPS LOADED%22
+View parent = solo.getView(R.id.brightcove_video_view);%0A                ArrayList%3CBrightcoveClosedCaptioningView%3E captioningViews = solo.getCurrentViews(BrightcoveClosedCaptioningView.class, parent);%0A                assertTrue(%22A BrightcoveClosedCaptioningView was added.%22, captioningViews.size() == 1
 );%0A 
@@ -2620,10 +2620,9 @@
 ait(
-30
+2
 , Ti
@@ -2632,14 +2632,14 @@
 nit.
-SECOND
+MINUTE
 S));

add captioning view test case