From e9919377ee02958da4f2d2dde3b65c93505b4343 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Mon, 18 Oct 2010 13:47:56 -0700 Subject: [PATCH] Skip testCommaInserting if screen is off or locked. sendKey() doesn't work if the screen is off or locked. We could probably use a WakeLock and KeyguardLock to force the screen to be on, but that'd require adding unnecessary permissions to the email app. (not the test apk) Bug 1737038 Change-Id: Ic036dc4d12770e82bc5de1133d6dd34cd8465f30 --- tests/src/com/android/email/TestUtils.java | 19 +++++++++++++++++++ .../email/activity/MessageComposeTests.java | 19 ++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/tests/src/com/android/email/TestUtils.java b/tests/src/com/android/email/TestUtils.java index 062bc145e..0697ed83a 100644 --- a/tests/src/com/android/email/TestUtils.java +++ b/tests/src/com/android/email/TestUtils.java @@ -16,6 +16,9 @@ package com.android.email; +import android.app.KeyguardManager; +import android.content.Context; +import android.os.PowerManager; import android.test.MoreAsserts; import android.test.suitebuilder.annotation.LargeTest; import android.util.Log; @@ -140,4 +143,20 @@ public class TestUtils extends TestCase /* It tests itself */ { } }, AssertionFailedError.class); } + + /** + * @return true if the screen is on and not locked; false otherwise, in which case tests that + * send key events will fail. + */ + public static boolean isScreenOnAndNotLocked(Context context) { + PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); + if (!pm.isScreenOn()) { + return false; + } + KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); + if (km.inKeyguardRestrictedInputMode()) { + return false; + } + return true; + } } diff --git a/tests/src/com/android/email/activity/MessageComposeTests.java b/tests/src/com/android/email/activity/MessageComposeTests.java index 1a6c1c033..76ad46c5d 100644 --- a/tests/src/com/android/email/activity/MessageComposeTests.java +++ b/tests/src/com/android/email/activity/MessageComposeTests.java @@ -19,6 +19,7 @@ package com.android.email.activity; import com.android.email.Email; import com.android.email.EmailAddressValidator; import com.android.email.R; +import com.android.email.TestUtils; import com.android.email.mail.Address; import com.android.email.mail.MessagingException; import com.android.email.provider.EmailContent.Account; @@ -31,6 +32,7 @@ import android.net.Uri; import android.test.ActivityInstrumentationTestCase2; import android.test.UiThreadTest; import android.test.suitebuilder.annotation.LargeTest; +import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.MultiAutoCompleteTextView; @@ -48,6 +50,8 @@ import android.widget.MultiAutoCompleteTextView; public class MessageComposeTests extends ActivityInstrumentationTestCase2 { + private Context mContext; + private MultiAutoCompleteTextView mToView; private MultiAutoCompleteTextView mCcView; private EditText mSubjectView; @@ -119,21 +123,21 @@ public class MessageComposeTests @Override protected void setUp() throws Exception { super.setUp(); - Context context = getInstrumentation().getTargetContext(); + mContext = getInstrumentation().getTargetContext(); // Force assignment of a default account - long accountId = Account.getDefaultAccountId(context); + long accountId = Account.getDefaultAccountId(mContext); if (accountId == -1) { Account account = new Account(); account.mSenderName = "Bob Sender"; account.mEmailAddress = "bob@sender.com"; - account.save(context); + account.save(mContext); accountId = account.mId; mCreatedAccountId = accountId; } - Account account = Account.restoreAccountWithId(context, accountId); + Account account = Account.restoreAccountWithId(mContext, accountId); mSignature = account.getSignature(); - Email.setServicesEnabled(context); + Email.setServicesEnabled(mContext); Intent intent = new Intent(Intent.ACTION_VIEW); setActivityIntent(intent); @@ -889,6 +893,11 @@ public class MessageComposeTests * but we only run the full set on To: */ public void testCommaInserting() throws Throwable { + if (!TestUtils.isScreenOnAndNotLocked(mContext)) { + Log.w(Email.LOG_TAG, "SKIP testCommaInserting: Screen off or locked"); + return; + } + // simple appending cases checkCommaInsert("a", "", false); checkCommaInsert("a@", "", false);