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

import junit.framework.Assert;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.os.Build;
import android.preference.PreferenceManager;
import android.test.ActivityInstrumentationTestCase2;
import android.view.Display;

import com.adobe.primetime.reference.PrimetimeReference;
import com.adobe.primetime.reference.feeds.ContentRenditionInfo.ContentFormat;
import com.adobe.primetime.reference.ui.catalog.VideoItem;
import com.adobe.primetime.reference.ui.player.PlayerActivity;
import com.robotium.solo.Solo;

/**
 * Base class for Playback end to end tests which use the PlayerActivity to load
 * HLS video item. Loads a media URL with specified in the contentUrl, which
 * must be set in the constructor of derived classes. Uses the Robotium
 * framework to enable test interaction with the UI.
 */
public abstract class PlaybackEndToEndTestBase extends
		ActivityInstrumentationTestCase2<PlayerActivity> {

	public PlaybackEndToEndTestBase() {
		super(PlayerActivity.class);
	}

	protected String contentUrl;
	protected ContentFormat contentFormat = ContentFormat.hls;

	public Solo solo = null;
	private VideoItem content = null;
	private boolean autoHide;

	/**
	 * Return the size of a display
	 * @param display
	 * @return size of the display as Point 
	 */
	@SuppressWarnings("deprecation")
	protected static Point getDisplaySize(final Display display) 
	{
		Point point = new Point();
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
		{   
			//API LEVEL 13
			display.getSize(point);
		}
		else
		{    
		    point.x = display.getWidth();
		    point.y = display.getHeight();
		}
		return point;
	}

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

		Context context = getInstrumentation().getTargetContext();
		Intent intent = new Intent(context, PlayerActivity.class);

		Assert.assertNotNull(contentUrl);
		content = new VideoItem(new SimpleFeedItem(contentFormat, contentUrl));

		intent.putExtra("CONTENT_INFO", content);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);

		setActivityIntent(intent);

		solo = new Solo(getInstrumentation(), getActivity());
		solo.clearLog();

		// set the control bar to not autohide
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		autoHide = prefs.getBoolean(PrimetimeReference.SETTINGS_CONTROLBAR_AUTOHIDE,
				PrimetimeReference.DEFAULT_AUTOHIDE_CONTROL_BAR);
		prefs.edit().putBoolean(PrimetimeReference.SETTINGS_CONTROLBAR_AUTOHIDE, false)
				.commit();

	}

	@Override
	protected void tearDown() throws Exception {

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(getInstrumentation()
						.getTargetContext());
		prefs.edit().putBoolean(PrimetimeReference.SETTINGS_CONTROLBAR_AUTOHIDE, autoHide)
				.commit();

		solo.finishOpenedActivities();
		super.tearDown();
	}

}
