/*
 * ***********************************************************************
 *
 *  ADOBE CONFIDENTIAL
 *  ___________________
 *
 *  Copyright 2013 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 *  NOTICE:  All information contained herein is, and remains
 *  the property of Adobe Systems Incorporated and its suppliers,
 *  if any.  The intellectual and technical concepts contained
 *  herein are proprietary to Adobe Systems Incorporated and its
 *  suppliers and are protected by trade secret or copyright law.
 *  Dissemination of this information or reproduction of this material
 *  is strictly forbidden unless prior written permission is obtained
 *  from Adobe Systems Incorporated.
 *
 * ***********************************************************************
 */

package com.adobe.primetime.reference.feature.cc;

import junit.framework.Assert;
import android.widget.ImageButton;

import com.adobe.mediacore.MediaPlayer.PlayerStatus;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.feature.playback.PlaybackEndToEndTestBase;

/**
 * Play a stream with CC track selection.
 */
public class CCEndToEndTest extends PlaybackEndToEndTestBase {

	private final String BUFFER_COMPLETE = "Buffering complete";
	private final String CC_DIALOG_OPEN = "Displaying closed captions chooser dialog.";
	private final String PLAYBACK_START = "Player state changed to ["
			+ PlayerStatus.PLAYING + "].";
	private final String TRACK = "CC1";
	private final String CC_SELECTED = "CC track:" + TRACK + "selected.";
	private final String NONE_SELECTED = "None Option Selected.";

	public CCEndToEndTest() throws Exception {
		contentUrl = "http://d398890tia84ty.cloudfront.net/e2e-vod/cloudfront_vod_hls_tos_30fps.m3u8";
	}

	/**
	 * Test player activity loads and play the video item correctly and
	 * selection of CC tracks
	 */
	public void testPlayVideoAndCC() throws InterruptedException {

		// Verify buffering on prepare stage
		Assert.assertTrue(solo.waitForLogMessage(BUFFER_COMPLETE));

		// Wait until video starts playing
		Assert.assertTrue(solo.waitForLogMessage(PLAYBACK_START));

		solo.sleep(30000);

		ImageButton imageButton = (ImageButton) solo
				.getView(R.id.sbPlayerControlCC);
		solo.clickOnView(imageButton);

		Assert.assertTrue(solo.waitForLogMessage(CC_DIALOG_OPEN));

		Assert.assertTrue(solo.searchText("None"));
		Assert.assertTrue(solo.searchText("CC1"));
		// Assert.assertTrue(solo.searchText(Pattern.quote(CC_SELECTED_STRING)));
		Assert.assertTrue(solo.searchText("CC2"));
		Assert.assertTrue(solo.searchText("CC3"));

		solo.clickLongOnText(TRACK);
		Assert.assertTrue(solo.waitForLogMessage(CC_SELECTED));

		solo.sleep(3000);

		solo.clickOnView(imageButton);

		Assert.assertTrue(solo.waitForLogMessage(CC_DIALOG_OPEN));

		solo.scrollUp();
		Assert.assertTrue(solo.searchText("None"));
		Assert.assertTrue(solo.searchText("CC1"));
		Assert.assertTrue(solo.searchText("CC2"));
		Assert.assertTrue(solo.searchText("CC3"));

		solo.clickLongOnText("None");
		Assert.assertTrue(solo.waitForLogMessage(NONE_SELECTED));

	}

}
