From 7af48d3232d5c8d8fe3c7a805e88f8ea25b0c42f Mon Sep 17 00:00:00 2001 From: Vadim Caen Date: Tue, 14 Jun 2022 15:49:36 +0200 Subject: Fix IME test to be compatible with WindowOnBackInvokedDispatcher WindowOnBackInvokedDispatcher is intilized with the value of enableOnBackOnvokedCallback from the ApplicationInfo. If the flag is disabled in the manifest, callback cannot be registered anymore. Fix the IME tests relying on manually setting the value of enableOnBackInvokedCallback on the ApplicationInfo to set the value before the MockIme and TestActvities gets created Test: android.view.inputmethod.cts.KeyboardVisibilityControlTest#testHideImeAfterBackPressed_legacyAppLegacyIme \ android.view.inputmethod.cts.KeyboardVisibilityControlTest#testHideImeAfterBackPressed_migratedAppLegacyIme \ android.view.inputmethod.cts.KeyboardVisibilityControlTest#testHideImeAfterBackPressed_migratedAppMigratedIme \ android.view.inputmethod.cts.KeyboardVisibilityControlTest#testHideImeAfterBackPressed_legacyAppMigratedIme Bug: 235206960 Change-Id: Ie9b5c0be705db835a346f3208922a03680e66877 Merged-In: Ie9b5c0be705db835a346f3208922a03680e66877 (cherry picked from commit 403dd4e5dac320908e8ce273c8f356e62cb6d484) --- .../src/com/android/cts/mockime/ImeSettings.java | 19 ++++++++++ .../src/com/android/cts/mockime/MockIme.java | 8 ++--- .../cts/KeyboardVisibilityControlTest.java | 42 +++++++++++++++------- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeSettings.java b/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeSettings.java index be709b454c6..cfc30ec673e 100644 --- a/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeSettings.java +++ b/tests/inputmethod/mockime/src/com/android/cts/mockime/ImeSettings.java @@ -61,6 +61,11 @@ public class ImeSettings { private static final String STRICT_MODE_ENABLED = "StrictModeEnabled"; private static final String VERIFY_CONTEXT_APIS_IN_ON_CREATE = "VerifyContextApisInOnCreate"; + /** + * Simulate the manifest flag enableOnBackInvokedCallback being true for the IME. + */ + private static final String ON_BACK_CALLBACK_ENABLED = "onBackCallbackEnabled"; + @NonNull private final PersistableBundle mBundle; @@ -182,6 +187,10 @@ public class ImeSettings { return mBundle.getBoolean(VERIFY_CONTEXT_APIS_IN_ON_CREATE, false); } + public boolean isOnBackCallbackEnabled() { + return mBundle.getBoolean(ON_BACK_CALLBACK_ENABLED, false); + } + static Bundle serializeToBundle(@NonNull String eventCallbackActionName, @Nullable Builder builder) { final Bundle result = new Bundle(); @@ -363,5 +372,15 @@ public class ImeSettings { mBundle.putBoolean(VERIFY_CONTEXT_APIS_IN_ON_CREATE, enabled); return this; } + + /** + * Sets whether the IME's + * {@link android.content.pm.ApplicationInfo#isOnBackInvokedCallbackEnabled()} + * should be set to {@code true}. + */ + public Builder setOnBackCallbackEnabled(boolean enabled) { + mBundle.putBoolean(ON_BACK_CALLBACK_ENABLED, enabled); + return this; + } } } diff --git a/tests/inputmethod/mockime/src/com/android/cts/mockime/MockIme.java b/tests/inputmethod/mockime/src/com/android/cts/mockime/MockIme.java index fc4c05e31a2..528da58c7b7 100644 --- a/tests/inputmethod/mockime/src/com/android/cts/mockime/MockIme.java +++ b/tests/inputmethod/mockime/src/com/android/cts/mockime/MockIme.java @@ -426,10 +426,6 @@ public final class MockIme extends InputMethodService { return e; } } - case "setEnableOnBackInvokedCallback": - boolean isEnabled = command.getExtras().getBoolean("isEnabled"); - getApplicationInfo().setEnableOnBackInvokedCallback(isEnabled); - return ImeEvent.RETURN_VALUE_UNAVAILABLE; case "getDisplayId": return getDisplay().getDisplayId(); case "verifyLayoutInflaterContext": @@ -679,6 +675,10 @@ public final class MockIme extends InputMethodService { .build()); } + if (mSettings.isOnBackCallbackEnabled()) { + getApplicationInfo().setEnableOnBackInvokedCallback(true); + } + getTracer().onCreate(() -> { super.onCreate(); mHandlerThread.start(); diff --git a/tests/inputmethod/src/android/view/inputmethod/cts/KeyboardVisibilityControlTest.java b/tests/inputmethod/src/android/view/inputmethod/cts/KeyboardVisibilityControlTest.java index ac95dc89c94..d79dccae84c 100644 --- a/tests/inputmethod/src/android/view/inputmethod/cts/KeyboardVisibilityControlTest.java +++ b/tests/inputmethod/src/android/view/inputmethod/cts/KeyboardVisibilityControlTest.java @@ -51,6 +51,7 @@ import static org.junit.Assume.assumeTrue; import android.app.AlertDialog; import android.app.Instrumentation; import android.app.compat.CompatChanges; +import android.content.Context; import android.content.pm.PackageManager; import android.graphics.Color; import android.os.SystemClock; @@ -245,27 +246,38 @@ public class KeyboardVisibilityControlTest extends EndToEndImeTestBase { } private void verifyHideImeBackPressed( - boolean appRequestsLegacy, boolean imeRequestsLegacy) throws Exception { + boolean appRequestsBackCallback, boolean imeRequestsBackCallback) throws Exception { final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); - final InputMethodManager imm = InstrumentationRegistry.getInstrumentation() - .getTargetContext().getSystemService(InputMethodManager.class); + final Context context = instrumentation.getTargetContext(); + final InputMethodManager imm = context.getSystemService(InputMethodManager.class); + + // Whether 'OnBackInvokedCallback' or 'onBackPressed' (legacy back) is used is defined by + // the 'enableOnBackInvokedCallback' flag in the Application manifest. + // Registering a callback is only authorized if the flag is set to true. Since the + // WindowOnBackDispatcher is created at the same time as the ViewRootImpl, for test purpose, + // we need to manually set the flag on ApplicationInfo before the window is created which + // happens during the MockIme creation and TestActivity creation. try (MockImeSession imeSession = MockImeSession.create( instrumentation.getContext(), instrumentation.getUiAutomation(), - new ImeSettings.Builder())) { + new ImeSettings.Builder() + .setOnBackCallbackEnabled(imeRequestsBackCallback) + )) { final ImeEventStream stream = imeSession.openEventStream(); final String marker = getTestMarker(); + + if (appRequestsBackCallback) { + context.getApplicationInfo().setEnableOnBackInvokedCallback(true); + } + final EditText editText = launchTestActivity(marker); final TestActivity testActivity = (TestActivity) editText.getContext(); - if (appRequestsLegacy) { - testActivity.getApplicationInfo().setEnableOnBackInvokedCallback(true); + + if (!appRequestsBackCallback) { testActivity.setIgnoreBackKey(true); } - if (imeRequestsLegacy) { - imeSession.callSetEnableOnBackInvokedCallback(true); - } expectEvent(stream, editorMatcher("onStartInput", marker), TIMEOUT); notExpectEvent(stream, editorMatcher("onStartInputView", marker), TIMEOUT); @@ -296,22 +308,26 @@ public class KeyboardVisibilityControlTest extends EndToEndImeTestBase { @Test public void testHideImeAfterBackPressed_legacyAppLegacyIme() throws Exception { - verifyHideImeBackPressed(true /* appRequestsLegacy */, true /* imeRequestsLegacy */); + verifyHideImeBackPressed(false/* appRequestsBackCallback */, + false/* imeRequestsBackCallback */); } @Test public void testHideImeAfterBackPressed_migratedAppLegacyIme() throws Exception { - verifyHideImeBackPressed(false /* appRequestsLegacy */, true /* imeRequestsLegacy */); + verifyHideImeBackPressed(true/* appRequestsBackCallback */, + false/* imeRequestsBackCallback */); } @Test public void testHideImeAfterBackPressed_migratedAppMigratedIme() throws Exception { - verifyHideImeBackPressed(false /* appRequestsLegacy */, false /* imeRequestsLegacy */); + verifyHideImeBackPressed(true/* appRequestsBackCallback */, + true/* imeRequestsBackCallback */); } @Test public void testHideImeAfterBackPressed_legacyAppMigratedIme() throws Exception { - verifyHideImeBackPressed(true /* appRequestsLegacy */, false /* imeRequestsLegacy */); + verifyHideImeBackPressed(false/* appRequestsBackCallback */, + true/* imeRequestsBackCallback */); } @Test -- cgit v1.2.3 From b4572b47400268c653cbc0f18161775b391c5b8a Mon Sep 17 00:00:00 2001 From: Issei Suzuki Date: Tue, 21 Jun 2022 12:10:09 +0000 Subject: Remove tests for unused APIs. ActivityOptions#setDismissKeyguardIfInsecure is not used in T, and is replaced by different API in device branch. Test: atest KeyguardLockedTests KeyguardTransitionTests Bug: 233839989 Merged-In: I6cb6d9bd91970bb538036763a3a13480f07bcadd Change-Id: I9e15d5d73199c66ee93e092d4caabc1086f0ea95 --- .../src/android/server/wm/KeyguardLockedTests.java | 14 -------------- .../android/server/wm/KeyguardTransitionTests.java | 21 --------------------- 2 files changed, 35 deletions(-) diff --git a/tests/framework/base/windowmanager/src/android/server/wm/KeyguardLockedTests.java b/tests/framework/base/windowmanager/src/android/server/wm/KeyguardLockedTests.java index 784003b2452..08b8204cd98 100644 --- a/tests/framework/base/windowmanager/src/android/server/wm/KeyguardLockedTests.java +++ b/tests/framework/base/windowmanager/src/android/server/wm/KeyguardLockedTests.java @@ -173,20 +173,6 @@ public class KeyguardLockedTests extends KeyguardTestBase { mWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true); } - @Test - public void testDismissKeyguardIfInsecure_notAllowed() { - final LockScreenSession lockScreenSession = createManagedLockScreenSession(); - lockScreenSession.setLockCredential().gotoKeyguard(); - - mWmState.assertKeyguardShowingAndNotOccluded(); - launchActivityWithDismissKeyguardIfInsecure(SHOW_WHEN_LOCKED_ACTIVITY); - mWmState.computeState(SHOW_WHEN_LOCKED_ACTIVITY); - mWmState.assertVisibility(SHOW_WHEN_LOCKED_ACTIVITY, true); - - // Make sure we stay on Keyguard. - mWmState.assertKeyguardShowingAndOccluded(); - } - @Test public void testDismissKeyguardActivity_method() { final LockScreenSession lockScreenSession = createManagedLockScreenSession(); diff --git a/tests/framework/base/windowmanager/src/android/server/wm/KeyguardTransitionTests.java b/tests/framework/base/windowmanager/src/android/server/wm/KeyguardTransitionTests.java index bac67d28dc0..b585c048c4b 100644 --- a/tests/framework/base/windowmanager/src/android/server/wm/KeyguardTransitionTests.java +++ b/tests/framework/base/windowmanager/src/android/server/wm/KeyguardTransitionTests.java @@ -105,15 +105,6 @@ public class KeyguardTransitionTests extends ActivityManagerTestBase { mWmState.getDefaultDisplayLastTransition()); } - @Test - public void testDismissKeyguardIfInsecure() { - createManagedLockScreenSession().gotoKeyguard(); - launchActivityWithDismissKeyguardIfInsecure(SHOW_WHEN_LOCKED_NO_PREVIEW_ACTIVITY); - mWmState.computeState(SHOW_WHEN_LOCKED_NO_PREVIEW_ACTIVITY); - assertEquals("Picked wrong transition", TRANSIT_KEYGUARD_GOING_AWAY, - mWmState.getDefaultDisplayLastTransition()); - } - @Test public void testNewActivityDuringOccluded() { final LockScreenSession lockScreenSession = createManagedLockScreenSession(); @@ -125,18 +116,6 @@ public class KeyguardTransitionTests extends ActivityManagerTestBase { mWmState.getDefaultDisplayLastTransition()); } - @Test - public void testNewDismissKeyguardIfInsecureActivityDuringOccluded() { - final LockScreenSession lockScreenSession = createManagedLockScreenSession(); - launchActivity(SHOW_WHEN_LOCKED_NO_PREVIEW_ACTIVITY); - lockScreenSession.gotoKeyguard(SHOW_WHEN_LOCKED_NO_PREVIEW_ACTIVITY); - launchActivityWithDismissKeyguardIfInsecure( - SHOW_WHEN_LOCKED_WITH_DIALOG_NO_PREVIEW_ACTIVITY); - mWmState.computeState(SHOW_WHEN_LOCKED_WITH_DIALOG_NO_PREVIEW_ACTIVITY); - assertEquals("Picked wrong transition", TRANSIT_ACTIVITY_OPEN, - mWmState.getDefaultDisplayLastTransition()); - } - @Test public void testOccludeManifestAttr() { final LockScreenSession lockScreenSession = createManagedLockScreenSession(); -- cgit v1.2.3 From 1bbb8107fffd5f21f184205ac47156f979b8fba7 Mon Sep 17 00:00:00 2001 From: Shawn Willden Date: Mon, 20 Jun 2022 06:57:38 -0600 Subject: Adjust KeyAttestation test for RKP StrongBox tests required every intermediate cert in the attestation chain to contain "strongbox" in the subject, but this isn't the case for RKP-issued chains. This changes the test to require at least one cert with "strongbox". Bug: 234395804 Test: CtsKeystoreTestCases Change-Id: I28e7e3d345929853b8fc90fd57021758bb739e03 --- .../src/android/keystore/cts/KeyAttestationTest.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/tests/keystore/src/android/keystore/cts/KeyAttestationTest.java b/tests/tests/keystore/src/android/keystore/cts/KeyAttestationTest.java index ae87bdc7d91..f864e52812d 100644 --- a/tests/tests/keystore/src/android/keystore/cts/KeyAttestationTest.java +++ b/tests/tests/keystore/src/android/keystore/cts/KeyAttestationTest.java @@ -1489,6 +1489,7 @@ public class KeyAttestationTest { public static void verifyCertificateChain(Certificate[] certChain, boolean expectStrongBox) throws GeneralSecurityException { assertNotNull(certChain); + boolean strongBoxSubjectFound = false; for (int i = 1; i < certChain.length; ++i) { try { PublicKey pubKey = certChain[i].getPublicKey(); @@ -1515,19 +1516,19 @@ public class KeyAttestationTest { if (i == 1) { // First cert should have subject "CN=Android Keystore Key". assertEquals(signedCertSubject, new X500Name("CN=Android Keystore Key")); - } else { - // Only strongbox implementations should have strongbox in the subject line - assertEquals(expectStrongBox, signedCertSubject.toString() - .toLowerCase() - .contains("strongbox")); + } else if (signedCertSubject.toString().toLowerCase().contains("strongbox")) { + strongBoxSubjectFound = true; } } catch (InvalidKeyException | CertificateException | NoSuchAlgorithmException | NoSuchProviderException | SignatureException e) { throw new GeneralSecurityException("Using StrongBox: " + expectStrongBox + "\n" - + "Failed to verify certificate " - + certChain[i - 1] + " with public key " + certChain[i].getPublicKey(), e); + + "Failed to verify certificate " + certChain[i - 1] + + " with public key " + certChain[i].getPublicKey(), + e); } } + // At least one intermediate in a StrongBox chain must have "strongbox" in the subject. + assertEquals(expectStrongBox, strongBoxSubjectFound); } private void testDeviceIdAttestationFailure(int idType, -- cgit v1.2.3