/*
 * ***********************************************************************
 *
 *  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.lba;

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 AA track selection.
 */
public class AAEndToEndTest extends PlaybackEndToEndTestBase {

	private final String BUFFER_COMPLETE = "Buffering complete";
	private final String AA_DIALOG_OPEN = "Displaying alternate audio chooser dialog.";
	private final String PLAYBACK_START = "Player state changed to ["
			+ PlayerStatus.PLAYING + "].";;
	private final String TRACK_SPANISH = "audio_spa";
	private final String TRACK_ENG = "audio_eng";
	private final String AA_TRACK_ENG_SELECTED = "Audio track Name: audio_eng. Language: eng selected.";
	private final String AA_TRACK_SPA_SELECTED = "Audio track Name: audio_spa. Language: spa selected.";

	public AAEndToEndTest() throws Exception {
		contentUrl = "http://d398890tia84ty.cloudfront.net/cloudfront_hls.m3u8";
	}

	/**
	 * Test player plays the video item correctly and AA track selection
	 */
	public void testPlayVideoAndAA() throws InterruptedException {

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

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

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

		Assert.assertTrue(solo.waitForLogMessage(AA_DIALOG_OPEN));
		Assert.assertTrue(solo.searchText("eng"));
		Assert.assertTrue(solo.searchText("spa"));

		solo.clickLongOnText(TRACK_SPANISH);
		Assert.assertTrue(solo.waitForLogMessage(AA_TRACK_SPA_SELECTED));

		// Play video for 6 seconds to test audio track change
		solo.sleep(1000);

		solo.clickOnView(imageButton);

		// Play video for 6 seconds till tracks populate
		solo.sleep(1000);

		Assert.assertTrue(solo.waitForLogMessage(AA_DIALOG_OPEN));
		Assert.assertTrue(solo.searchText("eng"));
		Assert.assertTrue(solo.searchText("spa"));

		solo.clickLongOnText(TRACK_ENG);
		Assert.assertTrue(solo.waitForLogMessage(AA_TRACK_ENG_SELECTED));

		// Play video for 3 seconds to test audio track change
		solo.sleep(500);

	}

}
