package com.example.wordbook;

import android.app.Instrumentation.ActivityMonitor;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.test.ActivityInstrumentationTestCase2;

/**
 * ヘルプ画面テストクラス
 */
public class HelpActivityTest extends
		ActivityInstrumentationTestCase2<HelpActivity> {

	/**
	 * コンストラクタ
	 */
	public HelpActivityTest() {
		super(HelpActivity.class);
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see android.test.ActivityInstrumentationTestCase2#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		super.setUp();
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see android.test.ActivityInstrumentationTestCase2#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * パラメータを与えない場合でも起動することを確認する
	 */
	public void testLaunch() {
		HelpActivity activity = getActivity();

		// 作成できることを確認する
		assertNotNull("Activity should launch successfully.", activity);
		// 終了しないことを確認する
		assertFalse("Activity should not be finishing.", activity.isFinishing());
	}

	/**
	 * オリエンテーション時の動作を確認する
	 * 
	 * @throws Throwable
	 */
	public void testOrientation() throws Throwable {
		final HelpActivity activity = getActivity();

		ActivityMonitor monitor = new ActivityMonitor(
				HelpActivity.class.getName(), null, false);

		// 設定
		getInstrumentation().addMonitor(monitor);
		runTestOnUiThread(new Runnable() {
			@Override
			public void run() {
				activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			}
		});
		// 反映待ち
		getInstrumentation().waitForIdleSync();
		getInstrumentation().waitForMonitor(monitor);
		// 設定されていることを確認する
		assertEquals("Activity should be LANDSCAPE mode",
				Configuration.ORIENTATION_LANDSCAPE, activity.getResources()
						.getConfiguration().orientation);

		// 設定
		getInstrumentation().addMonitor(monitor);
		runTestOnUiThread(new Runnable() {
			@Override
			public void run() {
				activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			}
		});
		// 反映待ち
		getInstrumentation().waitForIdleSync();
		getInstrumentation().waitForMonitor(monitor);
		// 設定されていることを確認する
		assertEquals("Activity should be PORTRAIT mode",
				Configuration.ORIENTATION_PORTRAIT, activity.getResources()
						.getConfiguration().orientation);
	}

}
