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

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.Suppress;

import com.adobe.primetime.reference.PrimetimeReference;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.manager.DrmManager;
import com.adobe.primetime.reference.ui.catalog.CatalogActivity;
import com.robotium.solo.Solo;

/**
 * Load protected content in the CatalogActivity and confirm that the DRM
 * preload completes.
 */
public class DRMLicensePreloadEndToEndTest extends
		ActivityInstrumentationTestCase2<CatalogActivity> {

	protected String TEST_CONTENT_URL = "http://d398890tia84ty.cloudfront.net/reference/ref_player_sample.json";

	public DRMLicensePreloadEndToEndTest() {
		super(CatalogActivity.class);
	}

	public Solo solo = null;
	private String oldUrl;
	private boolean preCache;
	Context context = null;

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

		context = getInstrumentation().getTargetContext();

		// // set the content url in shared prefs
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		oldUrl = prefs.getString(PrimetimeReference.SETTINGS_CONTENT_URL_KEY,
				context.getString(R.string.contentUrl));
		prefs.edit()
				.putString(PrimetimeReference.SETTINGS_CONTENT_URL_KEY, TEST_CONTENT_URL)
				.commit();

		preCache = prefs.getBoolean(DrmManager.SETTINGS_DRM_PRE_CACHE,
				DrmManager.DEFAULT_DRM_PRECACHE);
		prefs.edit().putBoolean(DrmManager.SETTINGS_DRM_PRE_CACHE, true)
				.commit();

		Intent intent = new Intent(context, CatalogActivity.class);

		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);

		setActivityIntent(intent);

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

	@Override
	protected void tearDown() throws Exception {

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		prefs.edit().putString(PrimetimeReference.SETTINGS_CONTENT_URL_KEY, oldUrl)
				.commit();
		prefs.edit().putBoolean(DrmManager.SETTINGS_DRM_PRE_CACHE, preCache)
				.commit();

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

	@Suppress
	public void testPreLoad() throws InterruptedException {
		assertTrue(solo.waitForLogMessage("DRMPreload#onLoadComplete", 20000));
	}

}
