package com.navercorp.utilset.ui;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.res.builder.RobolectricPackageManager;
import org.robolectric.shadows.ShadowActivityManager;
import org.robolectric.shadows.ShadowLog;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;

/**
 *
 * @author jaemin.woo
 * @author sanghyuk.jung
 */
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class ActivityUtilsTest {
	private Context context;
	private static final String NOT_INSTALLED_PACKAGE = "seventh.son.of.a.seventh.son";
	private static final String INSTALLED_PACKAGE = "com.navercorp.utilset";

	@Before
	public void setUp() {
		ShadowLog.stream = System.out;
		this.context = Robolectric.application;
	}

	@Test
	public void shouldNotReturnTrueForNotInstalledPackage() {
		boolean installed = ActivityUtils.isPackageInstalled(context, NOT_INSTALLED_PACKAGE);
		assertThat(installed, is(false));
	}

	@Test
	public void shouldReturnTrueForInstalledPackage() {
		addInstalledPackage(INSTALLED_PACKAGE);
		boolean installed = ActivityUtils.isPackageInstalled(context, INSTALLED_PACKAGE);
		assertThat(installed, is(true));
	}
	
	@Test
	public void shouldGetBaseActivityPakcageName() {
		setBaseActivityPackage(INSTALLED_PACKAGE);
		String packageName = ActivityUtils.getBaseActivityPackageName(context);
		assertThat(packageName, is(INSTALLED_PACKAGE));
	}
	
	private void addInstalledPackage(String packageName) {
		RobolectricPackageManager pm = Robolectric.packageManager;
		PackageInfo packInfo = new PackageInfo();
		packInfo.packageName = packageName;
		pm.addPackage(packInfo);
	}
	
	private void setBaseActivityPackage(String packageName) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		ShadowActivityManager shadowAm = Robolectric.shadowOf(am);
		RunningTaskInfo info = new RunningTaskInfo();
		info.baseActivity = new ComponentName(packageName, "test");
		shadowAm.setTasks(Arrays.asList(info));
	}
}
@@ -491,16 +491,138 @@
 dowLog;%0A
+import org.robolectric.shadows.ShadowWindow;%0Aimport org.robolectric.util.ActivityController;%0A%0Aimport android.app.Activity;
 %0Aimport 
@@ -810,16 +810,51 @@
 ageInfo;
+%0Aimport android.view.WindowManager;
 %0A%0A/**%0A *
@@ -1725,16 +1725,632 @@
 );%0A%09%7D%0A%09%0A
+%09@Test%0A%09public void shouldAddKeepScreenOnFlag() %7B%0A%09%09// given%0A%09%09TestActivity activity = createActivity(TestActivity.class);%0A%09%09%0A%09%09// when%0A%09%09ActivityUtils.setScreenOn(activity);%0A%0A%09%09// then %0A%09%09boolean flag = getFlag(activity, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);%0A%09%09assertThat(flag, is(true));%0A%09%7D%0A%09%0A%09@Test%0A%09public void shouldClearKeepScreenOnFlag() %7B%0A%09%09// given%0A%09%09TestActivity activity = createActivity(TestActivity.class);%0A%09%09%0A%09%09// when%0A%09%09ActivityUtils.clearScreenOn(activity);%0A%0A%09%09// then %0A%09%09boolean flag = getFlag(activity, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);%0A%09%09assertThat(flag, is(false));%0A%09%7D%0A%0A
 %09@Test%0A%09
@@ -3171,9 +3171,443 @@
 o));%0A%09%7D%0A
+%0A%0A%09private %3CT extends Activity%3E T createActivity(Class%3CT%3E activityClass) %7B%0A%09%09ActivityController%3CT%3E controller = Robolectric.buildActivity(activityClass);%0A%09%09controller.create();%0A%09%09return controller.get();%0A%09%7D%0A%0A%09private boolean getFlag(TestActivity activity, int flagToMatch) %7B%0A%09%09ShadowWindow window = Robolectric.shadowOf(activity.getWindow());%0A%09%09return window.getFlag(flagToMatch);%0A%09%7D%0A%0A%09static class TestActivity extends Activity %7B%0A%09%7D%0A
 %7D

Add tests for ActivityUtils.setScreenOn(), clearScreenOn()