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.shadows.ShadowActivityManager;
import org.robolectric.shadows.ShadowLog;

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

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

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

	@Test
	public void shouldNotReturnTrueForNonExistentInstalledPackage() {
		assertFalse(ActivityUtils.isPackageInstalled(context,
				NON_EXISTENT_INSTALLED_PACKAGE));
	}
	
	@Test
	public void shouldGetBaseActivityPakcageName() {
		setBaseActivityPackage(BASE_ACTIVITY_PACKAGE);
		String packageName = ActivityUtils.getBaseActivityPackageName(context);
		assertThat(packageName, is(BASE_ACTIVITY_PACKAGE));
	}
	
	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));
	}
}
@@ -333,16 +333,78 @@
 Config;%0A
+import org.robolectric.res.builder.RobolectricPackageManager;%0A
 import o
@@ -649,16 +649,55 @@
 Context;
+%0Aimport android.content.pm.PackageInfo;
 %0A%0A/**%0A *
@@ -908,25 +908,16 @@
 tring NO
-N_EXISTEN
 T_INSTAL
@@ -991,29 +991,25 @@
  String 
-BASE_ACTIVITY
+INSTALLED
 _PACKAGE
@@ -1196,16 +1196,8 @@
 orNo
-nExisten
 tIns
@@ -1220,20 +1220,28 @@
 %7B%0A%09%09
-assertFalse(
+boolean installed = 
 Acti
@@ -1281,43 +1281,290 @@
 ext,
-%0A%09%09%09%09NON_EXISTENT_INSTALLED_PACKAGE
+ NOT_INSTALLED_PACKAGE);%0A%09%09assertThat(installed, is(false));%0A%09%7D%0A%0A%09@Test%0A%09public void shouldReturnTrueForInstalledPackage() %7B%0A%09%09addInstalledPackage(INSTALLED_PACKAGE);%0A%09%09boolean installed = ActivityUtils.isPackageInstalled(context, INSTALLED_PACKAGE);%0A%09%09assertThat(installed, is(true
 ));%0A
@@ -1650,29 +1650,25 @@
 Package(
-BASE_ACTIVITY
+INSTALLED
 _PACKAGE
@@ -1777,30 +1777,257 @@
  is(
-BASE_ACTIVITY_PACKAGE)
+INSTALLED_PACKAGE));%0A%09%7D%0A%09%0A%09private void addInstalledPackage(String packageName) %7B%0A%09%09RobolectricPackageManager pm = Robolectric.packageManager;%0A%09%09PackageInfo packInfo = new PackageInfo();%0A%09%09packInfo.packageName = packageName;%0A%09%09pm.addPackage(packInfo
 );%0A%09

Add a test for ActivityUtils.isPackageInstalled() returning true