diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/BinaryTransparencyHostTest/Android.bp | 2 | ||||
-rw-r--r-- | tests/BinaryTransparencyHostTest/src/android/transparency/test/BinaryTransparencyHostTest.java | 9 | ||||
-rw-r--r-- | tests/Input/res/xml/bookmarks.xml | 16 | ||||
-rw-r--r-- | tests/Input/res/xml/bookmarks_legacy.xml | 14 | ||||
-rw-r--r-- | tests/Input/src/com/android/server/input/KeyGestureControllerTests.kt | 64 | ||||
-rw-r--r-- | tests/broadcasts/unit/Android.bp | 44 | ||||
-rw-r--r-- | tests/broadcasts/unit/AndroidManifest.xml | 27 | ||||
-rw-r--r-- | tests/broadcasts/unit/AndroidTest.xml | 29 | ||||
-rw-r--r-- | tests/broadcasts/unit/OWNERS | 2 | ||||
-rw-r--r-- | tests/broadcasts/unit/TEST_MAPPING | 7 | ||||
-rw-r--r-- | tests/broadcasts/unit/src/android/app/BroadcastStickyCacheTest.java | 231 |
11 files changed, 373 insertions, 72 deletions
diff --git a/tests/BinaryTransparencyHostTest/Android.bp b/tests/BinaryTransparencyHostTest/Android.bp index e14e5fea001f..1c8386add1b1 100644 --- a/tests/BinaryTransparencyHostTest/Android.bp +++ b/tests/BinaryTransparencyHostTest/Android.bp @@ -31,6 +31,8 @@ java_test_host { ], static_libs: [ "truth", + "flag-junit-host", + "android.app.flags-aconfig-java-host", ], device_common_data: [ ":BinaryTransparencyTestApp", diff --git a/tests/BinaryTransparencyHostTest/src/android/transparency/test/BinaryTransparencyHostTest.java b/tests/BinaryTransparencyHostTest/src/android/transparency/test/BinaryTransparencyHostTest.java index 6e5f08a11ed8..6d8dbcb5c963 100644 --- a/tests/BinaryTransparencyHostTest/src/android/transparency/test/BinaryTransparencyHostTest.java +++ b/tests/BinaryTransparencyHostTest/src/android/transparency/test/BinaryTransparencyHostTest.java @@ -24,6 +24,9 @@ import static org.junit.Assert.fail; import android.platform.test.annotations.LargeTest; import android.platform.test.annotations.Presubmit; +import android.platform.test.annotations.RequiresFlagsDisabled; +import android.platform.test.flag.junit.CheckFlagsRule; +import android.platform.test.flag.junit.host.HostFlagsValueProvider; import com.android.tradefed.device.DeviceNotAvailableException; import com.android.tradefed.log.LogUtil.CLog; @@ -34,6 +37,7 @@ import com.android.tradefed.util.CommandResult; import com.android.tradefed.util.CommandStatus; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -49,6 +53,10 @@ public final class BinaryTransparencyHostTest extends BaseHostJUnit4Test { /** Waiting time for the job to be scheduled */ private static final int JOB_CREATION_MAX_SECONDS = 30; + @Rule + public final CheckFlagsRule mCheckFlagsRule = + HostFlagsValueProvider.createCheckFlagsRule(this::getDevice); + @Before public void setUp() throws Exception { cancelPendingJob(); @@ -123,6 +131,7 @@ public final class BinaryTransparencyHostTest extends BaseHostJUnit4Test { } } + @RequiresFlagsDisabled(android.app.Flags.FLAG_BACKGROUND_INSTALL_CONTROL_CALLBACK_API) @Test public void testPreloadUpdateTriggersJobScheduling() throws Exception { try { diff --git a/tests/Input/res/xml/bookmarks.xml b/tests/Input/res/xml/bookmarks.xml index a4c898d8159a..68ec1233cdd7 100644 --- a/tests/Input/res/xml/bookmarks.xml +++ b/tests/Input/res/xml/bookmarks.xml @@ -23,7 +23,7 @@ androidprv:modifierState="META" /> <bookmark category="android.intent.category.APP_CONTACTS" - androidprv:keycode="KEYCODE_C" + androidprv:keycode="KEYCODE_P" androidprv:modifierState="META" /> <bookmark category="android.intent.category.APP_EMAIL" @@ -31,21 +31,13 @@ androidprv:modifierState="META" /> <bookmark category="android.intent.category.APP_CALENDAR" - androidprv:keycode="KEYCODE_K" + androidprv:keycode="KEYCODE_C" androidprv:modifierState="META" /> <bookmark category="android.intent.category.APP_MAPS" androidprv:keycode="KEYCODE_M" androidprv:modifierState="META" /> <bookmark - category="android.intent.category.APP_MUSIC" - androidprv:keycode="KEYCODE_P" - androidprv:modifierState="META" /> - <bookmark - role="android.app.role.SMS" - androidprv:keycode="KEYCODE_S" - androidprv:modifierState="META" /> - <bookmark category="android.intent.category.APP_CALCULATOR" androidprv:keycode="KEYCODE_U" androidprv:modifierState="META" /> @@ -57,7 +49,7 @@ <bookmark category="android.intent.category.APP_CONTACTS" - androidprv:keycode="KEYCODE_C" + androidprv:keycode="KEYCODE_P" shift="true" /> <bookmark @@ -65,4 +57,4 @@ class="com.test.BookmarkTest" androidprv:keycode="KEYCODE_J" shift="true" /> -</bookmarks>
\ No newline at end of file +</bookmarks> diff --git a/tests/Input/res/xml/bookmarks_legacy.xml b/tests/Input/res/xml/bookmarks_legacy.xml index 8bacf490ad9e..78cc48b19416 100644 --- a/tests/Input/res/xml/bookmarks_legacy.xml +++ b/tests/Input/res/xml/bookmarks_legacy.xml @@ -22,23 +22,17 @@ shortcut="b" /> <bookmark category="android.intent.category.APP_CONTACTS" - shortcut="c" /> + shortcut="p" /> <bookmark category="android.intent.category.APP_EMAIL" shortcut="e" /> <bookmark category="android.intent.category.APP_CALENDAR" - shortcut="k" /> + shortcut="c" /> <bookmark category="android.intent.category.APP_MAPS" shortcut="m" /> <bookmark - category="android.intent.category.APP_MUSIC" - shortcut="p" /> - <bookmark - role="android.app.role.SMS" - shortcut="s" /> - <bookmark category="android.intent.category.APP_CALCULATOR" shortcut="u" /> @@ -49,7 +43,7 @@ <bookmark category="android.intent.category.APP_CONTACTS" - shortcut="c" + shortcut="p" shift="true" /> <bookmark @@ -57,4 +51,4 @@ class="com.test.BookmarkTest" shortcut="j" shift="true" /> -</bookmarks>
\ No newline at end of file +</bookmarks> diff --git a/tests/Input/src/com/android/server/input/KeyGestureControllerTests.kt b/tests/Input/src/com/android/server/input/KeyGestureControllerTests.kt index 36a89f95aa6f..662b8e562126 100644 --- a/tests/Input/src/com/android/server/input/KeyGestureControllerTests.kt +++ b/tests/Input/src/com/android/server/input/KeyGestureControllerTests.kt @@ -542,9 +542,9 @@ class KeyGestureControllerTests { ), TestData( "META + C -> Launch Default Contacts", - intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_C), + intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_P), KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, - intArrayOf(KeyEvent.KEYCODE_C), + intArrayOf(KeyEvent.KEYCODE_P), KeyEvent.META_META_ON, intArrayOf(KeyGestureEvent.ACTION_GESTURE_COMPLETE), AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_CONTACTS) @@ -560,9 +560,9 @@ class KeyGestureControllerTests { ), TestData( "META + K -> Launch Default Calendar", - intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_K), + intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_C), KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, - intArrayOf(KeyEvent.KEYCODE_K), + intArrayOf(KeyEvent.KEYCODE_C), KeyEvent.META_META_ON, intArrayOf(KeyGestureEvent.ACTION_GESTURE_COMPLETE), AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_CALENDAR) @@ -577,24 +577,6 @@ class KeyGestureControllerTests { AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_MAPS) ), TestData( - "META + P -> Launch Default Music", - intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_P), - KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, - intArrayOf(KeyEvent.KEYCODE_P), - KeyEvent.META_META_ON, - intArrayOf(KeyGestureEvent.ACTION_GESTURE_COMPLETE), - AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_MUSIC) - ), - TestData( - "META + S -> Launch Default SMS", - intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_S), - KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, - intArrayOf(KeyEvent.KEYCODE_S), - KeyEvent.META_META_ON, - intArrayOf(KeyGestureEvent.ACTION_GESTURE_COMPLETE), - AppLaunchData.createLaunchDataForRole(RoleManager.ROLE_SMS) - ), - TestData( "META + U -> Launch Default Calculator", intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_U), KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, @@ -839,10 +821,10 @@ class KeyGestureControllerTests { AppLaunchData.createLaunchDataForRole(RoleManager.ROLE_BROWSER) ), TestData( - "META + C -> Launch Default Contacts", - intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_C), + "META + P -> Launch Default Contacts", + intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_P), KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, - intArrayOf(KeyEvent.KEYCODE_C), + intArrayOf(KeyEvent.KEYCODE_P), KeyEvent.META_META_ON, intArrayOf(KeyGestureEvent.ACTION_GESTURE_COMPLETE), AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_CONTACTS) @@ -857,10 +839,10 @@ class KeyGestureControllerTests { AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_EMAIL) ), TestData( - "META + K -> Launch Default Calendar", - intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_K), + "META + C -> Launch Default Calendar", + intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_C), KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, - intArrayOf(KeyEvent.KEYCODE_K), + intArrayOf(KeyEvent.KEYCODE_C), KeyEvent.META_META_ON, intArrayOf(KeyGestureEvent.ACTION_GESTURE_COMPLETE), AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_CALENDAR) @@ -875,24 +857,6 @@ class KeyGestureControllerTests { AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_MAPS) ), TestData( - "META + P -> Launch Default Music", - intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_P), - KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, - intArrayOf(KeyEvent.KEYCODE_P), - KeyEvent.META_META_ON, - intArrayOf(KeyGestureEvent.ACTION_GESTURE_COMPLETE), - AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_MUSIC) - ), - TestData( - "META + S -> Launch Default SMS", - intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_S), - KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, - intArrayOf(KeyEvent.KEYCODE_S), - KeyEvent.META_META_ON, - intArrayOf(KeyGestureEvent.ACTION_GESTURE_COMPLETE), - AppLaunchData.createLaunchDataForRole(RoleManager.ROLE_SMS) - ), - TestData( "META + U -> Launch Default Calculator", intArrayOf(KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_U), KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, @@ -915,14 +879,14 @@ class KeyGestureControllerTests { AppLaunchData.createLaunchDataForRole(RoleManager.ROLE_BROWSER) ), TestData( - "META + SHIFT + C -> Launch Default Contacts", + "META + SHIFT + P -> Launch Default Contacts", intArrayOf( KeyEvent.KEYCODE_META_LEFT, KeyEvent.KEYCODE_SHIFT_LEFT, - KeyEvent.KEYCODE_C + KeyEvent.KEYCODE_P ), KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_APPLICATION, - intArrayOf(KeyEvent.KEYCODE_C), + intArrayOf(KeyEvent.KEYCODE_P), KeyEvent.META_META_ON or KeyEvent.META_SHIFT_ON, intArrayOf(KeyGestureEvent.ACTION_GESTURE_COMPLETE), AppLaunchData.createLaunchDataForCategory(Intent.CATEGORY_APP_CONTACTS) @@ -1627,4 +1591,4 @@ class KeyGestureControllerTests { return true } } -}
\ No newline at end of file +} diff --git a/tests/broadcasts/unit/Android.bp b/tests/broadcasts/unit/Android.bp new file mode 100644 index 000000000000..9e15ac41d84b --- /dev/null +++ b/tests/broadcasts/unit/Android.bp @@ -0,0 +1,44 @@ +// Copyright (C) 2024 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "frameworks_base_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["frameworks_base_license"], + default_team: "trendy_team_framework_backstage_power", +} + +android_test { + name: "BroadcastUnitTests", + srcs: ["src/**/*.java"], + defaults: [ + "modules-utils-extended-mockito-rule-defaults", + ], + static_libs: [ + "androidx.test.runner", + "androidx.test.rules", + "androidx.test.ext.junit", + "mockito-target-extended-minus-junit4", + "truth", + "flag-junit", + "android.app.flags-aconfig-java", + "junit-params", + ], + certificate: "platform", + platform_apis: true, + test_suites: ["device-tests"], +} diff --git a/tests/broadcasts/unit/AndroidManifest.xml b/tests/broadcasts/unit/AndroidManifest.xml new file mode 100644 index 000000000000..61eb230f7957 --- /dev/null +++ b/tests/broadcasts/unit/AndroidManifest.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2024 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.broadcasts.unit" > + + <application android:debuggable="true"> + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner" + android:targetPackage="com.android.broadcasts.unit" + android:label="Broadcasts Unit Tests"/> +</manifest>
\ No newline at end of file diff --git a/tests/broadcasts/unit/AndroidTest.xml b/tests/broadcasts/unit/AndroidTest.xml new file mode 100644 index 000000000000..b91e4783b69e --- /dev/null +++ b/tests/broadcasts/unit/AndroidTest.xml @@ -0,0 +1,29 @@ +<!-- Copyright (C) 2024 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<configuration description="Runs Broadcasts tests"> + <option name="test-suite-tag" value="apct" /> + <option name="test-tag" value="BroadcastUnitTests" /> + + <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller"> + <option name="cleanup-apks" value="true" /> + <option name="test-file-name" value="BroadcastUnitTests.apk" /> + </target_preparer> + + <test class="com.android.tradefed.testtype.AndroidJUnitTest" > + <option name="package" value="com.android.broadcasts.unit" /> + <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" /> + <option name="hidden-api-checks" value="false"/> + </test> +</configuration>
\ No newline at end of file diff --git a/tests/broadcasts/unit/OWNERS b/tests/broadcasts/unit/OWNERS new file mode 100644 index 000000000000..f1e450b7e5f9 --- /dev/null +++ b/tests/broadcasts/unit/OWNERS @@ -0,0 +1,2 @@ +# Bug component: 316181 +include platform/frameworks/base:/BROADCASTS_OWNERS
\ No newline at end of file diff --git a/tests/broadcasts/unit/TEST_MAPPING b/tests/broadcasts/unit/TEST_MAPPING new file mode 100644 index 000000000000..b920e2586c86 --- /dev/null +++ b/tests/broadcasts/unit/TEST_MAPPING @@ -0,0 +1,7 @@ +{ + "postsubmit": [ + { + "name": "BroadcastUnitTests" + } + ] +} diff --git a/tests/broadcasts/unit/src/android/app/BroadcastStickyCacheTest.java b/tests/broadcasts/unit/src/android/app/BroadcastStickyCacheTest.java new file mode 100644 index 000000000000..ad032fb2fba6 --- /dev/null +++ b/tests/broadcasts/unit/src/android/app/BroadcastStickyCacheTest.java @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.app; + +import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; + +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Intent; +import android.content.IntentFilter; +import android.media.AudioManager; +import android.os.IpcDataCache; +import android.os.RemoteException; +import android.platform.test.annotations.DisableFlags; +import android.platform.test.annotations.EnableFlags; +import android.platform.test.flag.junit.SetFlagsRule; + +import com.android.dx.mockito.inline.extended.ExtendedMockito; +import com.android.internal.annotations.Keep; +import com.android.modules.utils.testing.ExtendedMockitoRule; + +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; + +@RunWith(JUnitParamsRunner.class) +public class BroadcastStickyCacheTest { + + @Rule + public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); + @Rule + public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this) + .mockStatic(IpcDataCache.class) + .mockStatic(ActivityManager.class) + .build(); + + @Mock + private IActivityManager mActivityManagerMock; + + @Mock + private IApplicationThread mIApplicationThreadMock; + + @Keep + private static Object stickyBroadcastList() { + return BroadcastStickyCache.STICKY_BROADCAST_ACTIONS; + } + + @Before + public void setUp() { + BroadcastStickyCache.clearCacheForTest(); + + doNothing().when(() -> IpcDataCache.invalidateCache(anyString(), anyString())); + } + + @Test + @DisableFlags(Flags.FLAG_USE_STICKY_BCAST_CACHE) + public void useCache_flagDisabled_returnsFalse() { + assertFalse(BroadcastStickyCache.useCache(new IntentFilter(Intent.ACTION_BATTERY_CHANGED))); + } + + @Test + @EnableFlags(Flags.FLAG_USE_STICKY_BCAST_CACHE) + public void useCache_nullFilter_returnsFalse() { + assertFalse(BroadcastStickyCache.useCache(null)); + } + + @Test + @EnableFlags(Flags.FLAG_USE_STICKY_BCAST_CACHE) + public void useCache_filterWithoutAction_returnsFalse() { + assertFalse(BroadcastStickyCache.useCache(new IntentFilter())); + } + + @Test + @EnableFlags(Flags.FLAG_USE_STICKY_BCAST_CACHE) + public void useCache_filterWithoutStickyBroadcastAction_returnsFalse() { + assertFalse(BroadcastStickyCache.useCache(new IntentFilter(Intent.ACTION_BOOT_COMPLETED))); + } + + @Test + @DisableFlags(Flags.FLAG_USE_STICKY_BCAST_CACHE) + public void invalidateCache_flagDisabled_cacheNotInvalidated() { + final String apiName = BroadcastStickyCache.sActionApiNameMap.get( + AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); + + BroadcastStickyCache.invalidateCache( + AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION); + + ExtendedMockito.verify( + () -> IpcDataCache.invalidateCache(eq(IpcDataCache.MODULE_SYSTEM), eq(apiName)), + times(0)); + } + + @Test + @EnableFlags(Flags.FLAG_USE_STICKY_BCAST_CACHE) + public void invalidateCache_broadcastNotSticky_cacheNotInvalidated() { + BroadcastStickyCache.invalidateCache(Intent.ACTION_AIRPLANE_MODE_CHANGED); + + ExtendedMockito.verify( + () -> IpcDataCache.invalidateCache(eq(IpcDataCache.MODULE_SYSTEM), anyString()), + times(0)); + } + + @Test + @EnableFlags(Flags.FLAG_USE_STICKY_BCAST_CACHE) + public void invalidateCache_withStickyBroadcast_cacheInvalidated() { + final String apiName = BroadcastStickyCache.sActionApiNameMap.get( + Intent.ACTION_BATTERY_CHANGED); + + BroadcastStickyCache.invalidateCache(Intent.ACTION_BATTERY_CHANGED); + + ExtendedMockito.verify( + () -> IpcDataCache.invalidateCache(eq(IpcDataCache.MODULE_SYSTEM), eq(apiName)), + times(1)); + } + + @Test + public void invalidateAllCaches_cacheInvalidated() { + BroadcastStickyCache.invalidateAllCaches(); + + for (int i = BroadcastStickyCache.sActionApiNameMap.size() - 1; i > -1; i--) { + final String apiName = BroadcastStickyCache.sActionApiNameMap.valueAt(i); + ExtendedMockito.verify(() -> IpcDataCache.invalidateCache(anyString(), + eq(apiName)), times(1)); + } + } + + @Test + @Parameters(method = "stickyBroadcastList") + public void getIntent_createNewCache_verifyRegisterReceiverIsCalled(String action) + throws RemoteException { + setActivityManagerMock(action); + final IntentFilter filter = new IntentFilter(action); + final Intent intent = queryIntent(filter); + + assertNotNull(intent); + assertEquals(intent.getAction(), action); + verify(mActivityManagerMock, times(1)).registerReceiverWithFeature( + eq(mIApplicationThreadMock), anyString(), anyString(), anyString(), any(), + eq(filter), anyString(), anyInt(), anyInt()); + } + + @Test + public void getIntent_querySameValueTwice_verifyRegisterReceiverIsCalledOnce() + throws RemoteException { + setActivityManagerMock(Intent.ACTION_DEVICE_STORAGE_LOW); + final Intent intent = queryIntent(new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW)); + final Intent cachedIntent = queryIntent(new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW)); + + assertNotNull(intent); + assertEquals(intent.getAction(), Intent.ACTION_DEVICE_STORAGE_LOW); + assertNotNull(cachedIntent); + assertEquals(cachedIntent.getAction(), Intent.ACTION_DEVICE_STORAGE_LOW); + + verify(mActivityManagerMock, times(1)).registerReceiverWithFeature( + eq(mIApplicationThreadMock), anyString(), anyString(), anyString(), any(), + any(), anyString(), anyInt(), anyInt()); + } + + @Test + public void getIntent_querySameActionWithDifferentFilter_verifyRegisterReceiverCalledTwice() + throws RemoteException { + setActivityManagerMock(Intent.ACTION_DEVICE_STORAGE_LOW); + final IntentFilter filter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); + final Intent intent = queryIntent(filter); + + final IntentFilter newFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); + newFilter.addDataScheme("file"); + final Intent newIntent = queryIntent(newFilter); + + assertNotNull(intent); + assertEquals(intent.getAction(), Intent.ACTION_DEVICE_STORAGE_LOW); + assertNotNull(newIntent); + assertEquals(newIntent.getAction(), Intent.ACTION_DEVICE_STORAGE_LOW); + + verify(mActivityManagerMock, times(1)).registerReceiverWithFeature( + eq(mIApplicationThreadMock), anyString(), anyString(), anyString(), any(), + eq(filter), anyString(), anyInt(), anyInt()); + + verify(mActivityManagerMock, times(1)).registerReceiverWithFeature( + eq(mIApplicationThreadMock), anyString(), anyString(), anyString(), any(), + eq(newFilter), anyString(), anyInt(), anyInt()); + } + + private Intent queryIntent(IntentFilter filter) { + return BroadcastStickyCache.getIntent( + mIApplicationThreadMock, + "android", + "android", + filter, + "system", + 0, + 0 + ); + } + + private void setActivityManagerMock(String action) throws RemoteException { + when(ActivityManager.getService()).thenReturn(mActivityManagerMock); + when(mActivityManagerMock.registerReceiverWithFeature(any(), anyString(), + anyString(), anyString(), any(), any(), anyString(), anyInt(), + anyInt())).thenReturn(new Intent(action)); + } +} |