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

import java.util.LinkedList;
import java.util.List;

import android.test.AndroidTestCase;

import com.adobe.mediacore.MediaResource;
import com.adobe.mediacore.TextFormat;
import com.adobe.mediacore.TextFormat.Color;
import com.adobe.mediacore.TextFormat.Font;
import com.adobe.mediacore.TextFormat.FontEdge;
import com.adobe.mediacore.TextFormat.Size;
import com.adobe.mediacore.info.ClosedCaptionsTrack;
import com.adobe.primetime.reference.config.ICCConfig;
import com.adobe.primetime.reference.manager.CCManagerOn;

//import com.adobe.primetime.reference.manager.test.PlaybackManagerTest.AdConfig;

/**
 * Unit tests for the CCManager
 * 
 * @author narain
 * 
 */

public class CCManagerTest extends AndroidTestCase {

	// Media player setup metadata
	private MockMediaPlayer mediaPlayer = null;
	private ICCConfig ccConfig;
	private CCManagerOn ccManager;
	private LinkedList<ClosedCaptionsTrack> cctracks;
	private long duration = 30000;
	private boolean isLive = false;
	private long width = 640;
	private long height = 480;
	private String label = "has activity";
	List<String> list = null;
	TextFormat tf, newtf = null;
	private String url = "test_url";

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		cctracks = new LinkedList<ClosedCaptionsTrack>();
		ccConfig = new CCConfig();
		cctracks.add(new ClosedCaptionsTrack("CC1", "english", true, true, false, ClosedCaptionsTrack.ServiceType.Service608Captions));
		cctracks.add(new ClosedCaptionsTrack("CC2", "french", false, false, false, ClosedCaptionsTrack.ServiceType.Service608Captions));
		cctracks.add(new ClosedCaptionsTrack("CC3", "spanish", false, true, false, ClosedCaptionsTrack.ServiceType.Service608Captions));
		mediaPlayer = new MockMediaPlayer(getContext(), duration, isLive, width, height, cctracks);
		ccManager = new CCManagerOn(ccConfig, mediaPlayer);
		MediaResource playerResource = new MediaResource(url, MediaResource.Type.HLS, null);

		// TODO replace current item is not implemented in glue layer
		//mediaPlayer.replaceCurrentItem(playerResource);
		mediaPlayer.replaceCurrentResource(playerResource);

	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();

		mediaPlayer = null;
		ccManager = null;
	}

	/**
	 * Test hasClosedCaptioned function to verify the media item has closed
	 * captioning.
	 */

	public void testHasClosedCaptioned() {
		assertTrue(ccManager.hasClosedCaptions());
	}

	/**
	 * Test selectedClosedCaptions function to verify that the trackIndex in the
	 * argument is the track that gets selected. If the trackIndex is less than
	 * 0 then index value of -1 is set.
	 */

	public void testSelectClosedCaptions() {
		int trackIndex = 1;
		ccManager.selectClosedCaptionTrack(trackIndex);
		int result = ccManager.getSelectedClosedCaptionsIndex();

		assertEquals(trackIndex, result);

		ccManager.selectClosedCaptionTrack(-1);
		int result1 = ccManager.getSelectedClosedCaptionsIndex();

		assertEquals(-1, result1);
	}

	/**
	 * Test getCCArray function to verify that it returns the correct size of
	 * array and the correct tracks.
	 */
	public void testGetCCsAsArray() {
		list = ccManager.getClosedCaptionTracks(label);

		assertEquals(list.size(), 3);
		assertTrue(list.contains("CC1 : english (has activity)"));
		assertTrue(list.contains("CC2 : french"));
		assertTrue(list.contains("CC3 : spanish (has activity)"));
	}

	/**
	 * Customized CC config for testing purpose
	 * 
	 */
	private class CCConfig implements ICCConfig {

		@Override
		public boolean getCCVisibility() {
			return true;
		}

		@Override
		public Font getCCFont() {
			return Font.DEFAULT;
		}

		@Override
		public FontEdge getCCFontEdge() {
			return FontEdge.DEFAULT;
		}

		@Override
		public Color getCCFontColor() {
			return Color.BLACK;
		}

		@Override
		public int getCCFontOpacity() {
			return 0;
		}

		@Override
		public Color getCCBackgroundColor() {
			return Color.WHITE;
		}

		@Override
		public Color getCCEdgeColor() {
			return Color.BLACK;
		}

		@Override
		public int getCCBackgroundOpacity() {
			return 0;
		}

		@Override
		public Size getCCSize() {
			return Size.DEFAULT;
		}

	}
}
