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

import junit.framework.Assert;

import org.json.JSONObject;

import com.adobe.mediacore.MediaPlayer.PlayerStatus;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.ui.player.MarkableSeekBar;

public class VodAuditudeAdsEndToEndTest extends PlaybackWithAdsEndToEndTestBase {
	private final String PLAYBACK_START = "Player state changed to ["
			+ PlayerStatus.PLAYING + "].";
	private final String AUDITUDE_AD_START = "Reporting AD-START";

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

		String json = "{\"id\":\"vid1\",\"title\":\"VOD - AUDITUDE ADS\",\"description\": \"This is an episode with primetime ads.\",\"categories\":\"documentary, educational, children\",\"keywords\":\"List, of, comma, separated, keywords\",\"isLive\": false,\"content\":[{\"format\":\"M3U8\",\"url\":\"http://d398890tia84ty.cloudfront.net/e2e-vod/cloudfront_vod_hls_tos_30fps.m3u8\",\"language\": \"en\"}],\"thumbnails\":[{\"format\":\"JPEG\",\"height\":\"90\",\"width\":\"160\",\"url\":\"http://example.com/small.jpg\"},{\"format\": \"JPEG\",\"height\":\"180\",\"width\":\"320\",\"url\":\"http://example.com/medium.jpg\"}],\"metadata\":{\"ad\":{\"type\":\"Primetime Ads\",\"details\":{\"domain\":\"auditude.com\",\"mediaid\":\"adbe_tearsofsteel2\",\"zoneid\":\"123869\"}}}}";
		jsonObject = new JSONObject(json);
	}

	/**
	 * Test player activity loads and play the video item correctly
	 */
	public void testLoadAndPlayVodAuditudeAds() throws InterruptedException {
		// Wait until video starts playing
		Assert.assertTrue(solo.waitForLogMessage(PLAYBACK_START));

		// Test pre-roll ad plays
		Assert.assertTrue(solo.waitForLogMessage(AUDITUDE_AD_START));
		solo.sleep(10000);
		solo.clearLog();

		// Test mid-roll ad plays
		MarkableSeekBar seekBar = (MarkableSeekBar) solo
				.getView(R.id.sbPlayerControlSeekBar);
		int[] location = { 0, 0 };
		seekBar.getLocationOnScreen(location);
		solo.clickOnScreen(
				(int) Math.floor(location[0] + (seekBar.getWidth() * .50)),
				location[1]);
		Assert.assertTrue(solo.waitForLogMessage(AUDITUDE_AD_START));
		solo.sleep(10000);
		solo.clearLog();

		// Test post-roll ad plays
		seekBar = (MarkableSeekBar) solo.getView(R.id.sbPlayerControlSeekBar);
		int[] endLocation = { 0, 0 };
		seekBar.getLocationOnScreen(endLocation);
		solo.clickOnScreen(
				(int) Math.floor(endLocation[0] + (seekBar.getWidth() * .99)),
				endLocation[1]);
		Assert.assertTrue(solo.waitForLogMessage(AUDITUDE_AD_START));
	}

}
