package org.sharetomail.test.intent;

import org.sharetomail.MainActivity;
import org.sharetomail.R;
import org.sharetomail.test.Util;
import org.sharetomail.util.Constants;

import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.test.ActivityInstrumentationTestCase2;
import android.view.Window;
import android.view.WindowManager;

import com.robotium.solo.Solo;

public class NonExistentSpecifiedEmailApp extends
		ActivityInstrumentationTestCase2<MainActivity> {

	private static final String TEST_SUBJECT = "test subject";
	private static final String TEST_LINK = "http://test.lnk";

	private SharedPreferences sharedPreferences;

	private Solo solo;
	private String defaultEmail = "default.text@mail.org";
	private String defaultEmailConfigLine = "{\"EMAIL_APP_PACKAGE_NAME\":\"testAppPackage\",\"EMAIL_APP_NAME\":\"testAppName\",\"EMAIL_ADDRESS\":\""
			+ defaultEmail + "\"}";

	public NonExistentSpecifiedEmailApp() {
		super(MainActivity.class);
	}

	@Override
	protected void setUp() {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.putExtra(Intent.EXTRA_TEXT, TEST_LINK);
		intent.putExtra(Intent.EXTRA_SUBJECT, TEST_SUBJECT);
		setActivityIntent(intent);

		sharedPreferences = getInstrumentation()
				.getTargetContext()
				.getApplicationContext()
				.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME,
						Context.MODE_PRIVATE);

		clearSharedPreferences();

		addDefaultEmail();

		solo = new Solo(getInstrumentation());

		KeyguardManager myKM = (KeyguardManager) getActivity()
				.getSystemService(Context.KEYGUARD_SERVICE);
		if (myKM.inKeyguardRestrictedInputMode()) {
			final Window win = getActivity().getWindow();
			win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
					| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
			win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
					| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
		}
	}

	@Override
	public void tearDown() throws Exception {
		solo.finishOpenedActivities();
		clearSharedPreferences();

		super.tearDown();
	}

	private void clearSharedPreferences() {
		Editor editor = sharedPreferences.edit();
		editor.clear();
		editor.commit();
	}

	private void addDefaultEmail() {
		Editor editor = sharedPreferences.edit();

		editor.putString(Constants.EMAIL_ADDRESSES_SHARED_PREFERENCES_KEY,
				defaultEmailConfigLine);

		editor.commit();
	}

	public void testNonExistentSpecifiedEmailApp() throws InterruptedException {
		String emailAppNotFountText = solo.getCurrentActivity().getString(
				R.string.email_app_not_found);
		solo.clickOnView(Util.getEmailAddressesListView(solo).getChildAt(0));

		assertTrue(solo.waitForText(emailAppNotFountText));
	}
}

@@ -31,16 +31,70 @@
 ntent;%0A%0A
+import org.sharetomail.AddModifyEmailAddressActivity;%0A
 import o
@@ -2581,81 +2581,62 @@
 %0A%0A%09p
-ublic void testNonExistentSpecifiedEmailApp() throws InterruptedException
+rivate void clickOnEmailAddressAndWaitForErrorDialog()
  %7B%0A%09
@@ -2811,16 +2811,16 @@
 t(0));%0A%0A
-
 %09%09assert
@@ -2867,11 +2867,613 @@
 xt));%0A%09%7D
+%0A%0A%09public void testNonExistentSpecifiedEmailApp_Modify()%0A%09%09%09throws InterruptedException %7B%0A%09%09clickOnEmailAddressAndWaitForErrorDialog();%0A%0A%09%09solo.clickOnButton(solo.getCurrentActivity().getString(%0A%09%09%09%09R.string.modify_button));%0A%0A%09%09assertTrue(solo.waitForActivity(AddModifyEmailAddressActivity.class,%0A%09%09%09%095000));%0A%09%7D%0A%0A%09public void testNonExistentSpecifiedEmailApp_Cancel()%0A%09%09%09throws InterruptedException %7B%0A%09%09clickOnEmailAddressAndWaitForErrorDialog();%0A%0A%09%09solo.clickOnButton(solo.getCurrentActivity().getString(%0A%09%09%09%09android.R.string.cancel));%0A%0A%09%09assertTrue(solo.waitForActivity(MainActivity.class, 5000));%0A%09%7D
 %0A%7D%0A

test for cancel and modify when trying to use an email address with non existent specified email app