/*
 * ***********************************************************************
 *
 *  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.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.test.AndroidTestCase;

import com.adobe.mediacore.MediaPlayerItem;
import com.adobe.mediacore.MediaPlayerItemConfig;
import com.adobe.mediacore.MediaResource;
import com.adobe.mediacore.info.AudioTrack;
import com.adobe.primetime.reference.config.IAAConfig;
import com.adobe.primetime.reference.manager.AAManagerOn;

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

public class AAManagerTest extends AndroidTestCase {

	private MockMediaPlayer mediaPlayer = null;
	private AAManagerOn aaManager;
	private IAAConfig aaConfig;
	private LinkedList<AudioTrack> aatracks;
	List<String> list = null;
	private List<Integer> failedTracks = new ArrayList<Integer>();

	// Media player setup metadata
	private String url = "test_url";

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		aatracks = new LinkedList<AudioTrack>();
		aatracks.add(new AudioTrack("MainAudio", "English", true, true, 1));
		aatracks.add(new AudioTrack("Spanish", "Spanish", false, false, 2));
		failedTracks.add(3);
		mediaPlayer = new MockMediaPlayer(getContext(), aatracks, failedTracks);
		aaManager = new AAManagerOn(aaConfig, mediaPlayer);
		MediaResource playerResource = new MediaResource(url, MediaResource.Type.HLS, null);
		// TODO replaceCurrentItem is not currently implemented
		//mediaPlayer.replaceCurrentItem(playerResource);
		mediaPlayer.replaceCurrentResource(playerResource);
	}

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

		mediaPlayer = null;
		aaManager = null;
	}

	/**
	 * Test hasAlternateAudio function to verify that the mediaItem has audio
	 * tracks.
	 */
	public void testHasAlternateAudio() {
		assertTrue(aaManager.hasAlternateAudio());
	}

	/**
	 * Test selectSelectedAudioTrack function to test whether the index passed
	 * returns true for track selection. Test getSelectedAudioTrack function to
	 * check if the currently selected track is returned. Test
	 * getSelectedAudioTrackIndex function to verify the index returned by this
	 * function is not -1.
	 */
	public void testSelectSelectedAudioTrack() {
		int result = -1;
		assertTrue(aaManager.selectAlternateAudioTrack(1));
		AudioTrack aa = aaManager.getSelectedAudioTrack();
		assertEquals(aa, this.aatracks.get(0));
		result = aaManager.getSelectedAudioTrackIndex();
		assertEquals(result, 0);
	}

	/**
	 * Test getAudioTracksAsArray function to verify an array of tracks is being
	 * returned and it contains the tracks we set up.
	 */
	public void testGetAudioTracksAsArray() {
		String[] list = aaManager.getAudioTracks();
		assertEquals(list.length, 2);
		assertTrue(list[0].toString().equalsIgnoreCase("MainAudio[ English]"));
		assertTrue(list[1].toString().equalsIgnoreCase("Spanish[ Spanish]"));
	}

}