diff options
7 files changed, 181 insertions, 194 deletions
diff --git a/java/tests/Android.bp b/java/tests/Android.bp index bb287eb2..3936b38e 100644 --- a/java/tests/Android.bp +++ b/java/tests/Android.bp @@ -28,6 +28,7 @@ android_test { "androidx.lifecycle_lifecycle-common-java8", "androidx.lifecycle_lifecycle-extensions", "androidx.lifecycle_lifecycle-runtime-ktx", + "androidx.lifecycle_lifecycle-runtime-testing", "kotlinx_coroutines_test", "mockito-target-minus-junit4", "testables", diff --git a/java/tests/src/com/android/intentresolver/EnterTransitionAnimationDelegateTest.kt b/java/tests/src/com/android/intentresolver/EnterTransitionAnimationDelegateTest.kt index 9ea9dfa7..c7d20000 100644 --- a/java/tests/src/com/android/intentresolver/EnterTransitionAnimationDelegateTest.kt +++ b/java/tests/src/com/android/intentresolver/EnterTransitionAnimationDelegateTest.kt @@ -21,6 +21,7 @@ import android.view.View import android.view.Window import androidx.activity.ComponentActivity import androidx.lifecycle.Lifecycle +import androidx.lifecycle.testing.TestLifecycleOwner import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher @@ -44,35 +45,34 @@ class EnterTransitionAnimationDelegateTest { private val dispatcher = StandardTestDispatcher(scheduler) private val lifecycleOwner = TestLifecycleOwner() - private val transitionTargetView = mock<View> { - // avoid the request-layout path in the delegate - whenever(isInLayout).thenReturn(true) - } + private val transitionTargetView = + mock<View> { + // avoid the request-layout path in the delegate + whenever(isInLayout).thenReturn(true) + } private val windowMock = mock<Window>() - private val resourcesMock = mock<Resources> { - whenever(getInteger(anyInt())).thenReturn(TIMEOUT_MS) - } - private val activity = mock<ComponentActivity> { - whenever(lifecycle).thenReturn(lifecycleOwner.lifecycle) - whenever(resources).thenReturn(resourcesMock) - whenever(isActivityTransitionRunning).thenReturn(true) - whenever(window).thenReturn(windowMock) - } - - private val testSubject = EnterTransitionAnimationDelegate(activity) { - transitionTargetView - } + private val resourcesMock = + mock<Resources> { whenever(getInteger(anyInt())).thenReturn(TIMEOUT_MS) } + private val activity = + mock<ComponentActivity> { + whenever(lifecycle).thenReturn(lifecycleOwner.lifecycle) + whenever(resources).thenReturn(resourcesMock) + whenever(isActivityTransitionRunning).thenReturn(true) + whenever(window).thenReturn(windowMock) + } + + private val testSubject = EnterTransitionAnimationDelegate(activity) { transitionTargetView } @Before fun setup() { Dispatchers.setMain(dispatcher) - lifecycleOwner.state = Lifecycle.State.CREATED + lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) } @After fun cleanup() { - lifecycleOwner.state = Lifecycle.State.DESTROYED + lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) Dispatchers.resetMain() } diff --git a/java/tests/src/com/android/intentresolver/TestLifecycleOwner.kt b/java/tests/src/com/android/intentresolver/TestLifecycleOwner.kt deleted file mode 100644 index 7e588f98..00000000 --- a/java/tests/src/com/android/intentresolver/TestLifecycleOwner.kt +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2023 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 com.android.intentresolver - -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.LifecycleRegistry - -internal class TestLifecycleOwner : LifecycleOwner { - private val lifecycleRegistry = LifecycleRegistry.createUnsafe(this) - - override val lifecycle: Lifecycle get() = lifecycleRegistry - - var state: Lifecycle.State - get() = lifecycle.currentState - set(value) { - lifecycleRegistry.currentState = value - } -}
\ No newline at end of file diff --git a/java/tests/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUiTest.kt b/java/tests/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUiTest.kt index 008cc162..dab1a956 100644 --- a/java/tests/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUiTest.kt +++ b/java/tests/src/com/android/intentresolver/contentpreview/ChooserContentPreviewUiTest.kt @@ -20,7 +20,7 @@ import android.content.Intent import android.graphics.Bitmap import android.net.Uri import androidx.lifecycle.Lifecycle -import com.android.intentresolver.TestLifecycleOwner +import androidx.lifecycle.testing.TestLifecycleOwner import com.android.intentresolver.contentpreview.ChooserContentPreviewUi.ActionFactory import com.android.intentresolver.mock import com.android.intentresolver.whenever diff --git a/java/tests/src/com/android/intentresolver/contentpreview/FilesPlusTextContentPreviewUiTest.kt b/java/tests/src/com/android/intentresolver/contentpreview/FilesPlusTextContentPreviewUiTest.kt index 06ade1ce..fe13a215 100644 --- a/java/tests/src/com/android/intentresolver/contentpreview/FilesPlusTextContentPreviewUiTest.kt +++ b/java/tests/src/com/android/intentresolver/contentpreview/FilesPlusTextContentPreviewUiTest.kt @@ -20,10 +20,10 @@ import android.net.Uri import android.view.LayoutInflater import android.view.ViewGroup import android.widget.TextView +import androidx.lifecycle.testing.TestLifecycleOwner import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation import com.android.intentresolver.R -import com.android.intentresolver.TestLifecycleOwner import com.android.intentresolver.mock import com.android.intentresolver.whenever import com.android.intentresolver.widget.ActionRow diff --git a/java/tests/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoaderTest.kt b/java/tests/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoaderTest.kt index 6e57c289..b5fd1fa6 100644 --- a/java/tests/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoaderTest.kt +++ b/java/tests/src/com/android/intentresolver/contentpreview/ImagePreviewImageLoaderTest.kt @@ -22,7 +22,7 @@ import android.net.Uri import android.util.Size import androidx.lifecycle.Lifecycle import androidx.lifecycle.coroutineScope -import com.android.intentresolver.TestLifecycleOwner +import androidx.lifecycle.testing.TestLifecycleOwner import com.android.intentresolver.any import com.android.intentresolver.anyOrNull import com.android.intentresolver.mock @@ -78,7 +78,7 @@ class ImagePreviewImageLoaderTest { @Before fun setup() { Dispatchers.setMain(dispatcher) - lifecycleOwner.state = Lifecycle.State.CREATED + lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) // create test subject after we've updated the lifecycle dispatcher testSubject = ImagePreviewImageLoader( @@ -91,7 +91,7 @@ class ImagePreviewImageLoaderTest { @After fun cleanup() { - lifecycleOwner.state = Lifecycle.State.DESTROYED + lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) Dispatchers.resetMain() } @@ -164,7 +164,7 @@ class ImagePreviewImageLoaderTest { @Test(expected = CancellationException::class) fun invoke_onClosedImageLoaderScope_throwsCancellationException() = runTest { - lifecycleOwner.state = Lifecycle.State.DESTROYED + lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) testSubject(uriOne) } @@ -181,7 +181,7 @@ class ImagePreviewImageLoaderTest { ) coroutineScope { val deferred = async(start = UNDISPATCHED) { testSubject(uriOne, false) } - lifecycleOwner.state = Lifecycle.State.DESTROYED + lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) scheduler.advanceUntilIdle() deferred.await() } diff --git a/java/tests/src/com/android/intentresolver/shortcuts/ShortcutLoaderTest.kt b/java/tests/src/com/android/intentresolver/shortcuts/ShortcutLoaderTest.kt index 742aac71..9b4a8057 100644 --- a/java/tests/src/com/android/intentresolver/shortcuts/ShortcutLoaderTest.kt +++ b/java/tests/src/com/android/intentresolver/shortcuts/ShortcutLoaderTest.kt @@ -27,8 +27,8 @@ import android.content.pm.ShortcutManager import android.os.UserHandle import android.os.UserManager import androidx.lifecycle.Lifecycle +import androidx.lifecycle.testing.TestLifecycleOwner import androidx.test.filters.SmallTest -import com.android.intentresolver.TestLifecycleOwner import com.android.intentresolver.any import com.android.intentresolver.argumentCaptor import com.android.intentresolver.capture @@ -38,6 +38,7 @@ import com.android.intentresolver.createShareShortcutInfo import com.android.intentresolver.createShortcutInfo import com.android.intentresolver.mock import com.android.intentresolver.whenever +import java.util.function.Consumer import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestCoroutineScheduler @@ -56,28 +57,31 @@ import org.mockito.Mockito.atLeastOnce import org.mockito.Mockito.never import org.mockito.Mockito.times import org.mockito.Mockito.verify -import java.util.function.Consumer @OptIn(ExperimentalCoroutinesApi::class) @SmallTest class ShortcutLoaderTest { - private val appInfo = ApplicationInfo().apply { - enabled = true - flags = 0 - } - private val pm = mock<PackageManager> { - whenever(getApplicationInfo(any(), any<ApplicationInfoFlags>())).thenReturn(appInfo) - } - private val userManager = mock<UserManager> { - whenever(isUserRunning(any<UserHandle>())).thenReturn(true) - whenever(isUserUnlocked(any<UserHandle>())).thenReturn(true) - whenever(isQuietModeEnabled(any<UserHandle>())).thenReturn(false) - } - private val context = mock<Context> { - whenever(packageManager).thenReturn(pm) - whenever(createContextAsUser(any(), anyInt())).thenReturn(this) - whenever(getSystemService(Context.USER_SERVICE)).thenReturn(userManager) - } + private val appInfo = + ApplicationInfo().apply { + enabled = true + flags = 0 + } + private val pm = + mock<PackageManager> { + whenever(getApplicationInfo(any(), any<ApplicationInfoFlags>())).thenReturn(appInfo) + } + private val userManager = + mock<UserManager> { + whenever(isUserRunning(any<UserHandle>())).thenReturn(true) + whenever(isUserUnlocked(any<UserHandle>())).thenReturn(true) + whenever(isQuietModeEnabled(any<UserHandle>())).thenReturn(false) + } + private val context = + mock<Context> { + whenever(packageManager).thenReturn(pm) + whenever(createContextAsUser(any(), anyInt())).thenReturn(this) + whenever(getSystemService(Context.USER_SERVICE)).thenReturn(userManager) + } private val scheduler = TestCoroutineScheduler() private val dispatcher = UnconfinedTestDispatcher(scheduler) private val lifecycleOwner = TestLifecycleOwner() @@ -85,47 +89,48 @@ class ShortcutLoaderTest { private val appPredictor = mock<ShortcutLoader.AppPredictorProxy>() private val callback = mock<Consumer<ShortcutLoader.Result>>() private val componentName = ComponentName("pkg", "Class") - private val appTarget = mock<DisplayResolveInfo> { - whenever(resolvedComponentName).thenReturn(componentName) - } + private val appTarget = + mock<DisplayResolveInfo> { whenever(resolvedComponentName).thenReturn(componentName) } private val appTargets = arrayOf(appTarget) private val matchingShortcutInfo = createShortcutInfo("id-0", componentName, 1) @Before fun setup() { Dispatchers.setMain(dispatcher) - lifecycleOwner.state = Lifecycle.State.CREATED + lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) } @After fun cleanup() { - lifecycleOwner.state = Lifecycle.State.DESTROYED + lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) Dispatchers.resetMain() } @Test fun test_loadShortcutsWithAppPredictor_resultIntegrity() { - val testSubject = ShortcutLoader( - context, - lifecycleOwner.lifecycle, - appPredictor, - UserHandle.of(0), - true, - intentFilter, - dispatcher, - callback - ) + val testSubject = + ShortcutLoader( + context, + lifecycleOwner.lifecycle, + appPredictor, + UserHandle.of(0), + true, + intentFilter, + dispatcher, + callback + ) testSubject.updateAppTargets(appTargets) val matchingAppTarget = createAppTarget(matchingShortcutInfo) - val shortcuts = listOf( - matchingAppTarget, - // an AppTarget that does not belong to any resolved application; should be ignored - createAppTarget( - createShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) + val shortcuts = + listOf( + matchingAppTarget, + // an AppTarget that does not belong to any resolved application; should be ignored + createAppTarget( + createShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) + ) ) - ) val appPredictorCallbackCaptor = argumentCaptor<AppPredictor.Callback>() verify(appPredictor, atLeastOnce()) .registerPredictionUpdates(any(), capture(appPredictorCallbackCaptor)) @@ -155,25 +160,28 @@ class ShortcutLoaderTest { @Test fun test_loadShortcutsWithShortcutManager_resultIntegrity() { - val shortcutManagerResult = listOf( - ShortcutManager.ShareShortcutInfo(matchingShortcutInfo, componentName), - // mismatching shortcut - createShareShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) - ) - val shortcutManager = mock<ShortcutManager> { - whenever(getShareTargets(intentFilter)).thenReturn(shortcutManagerResult) - } + val shortcutManagerResult = + listOf( + ShortcutManager.ShareShortcutInfo(matchingShortcutInfo, componentName), + // mismatching shortcut + createShareShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) + ) + val shortcutManager = + mock<ShortcutManager> { + whenever(getShareTargets(intentFilter)).thenReturn(shortcutManagerResult) + } whenever(context.getSystemService(Context.SHORTCUT_SERVICE)).thenReturn(shortcutManager) - val testSubject = ShortcutLoader( - context, - lifecycleOwner.lifecycle, - null, - UserHandle.of(0), - true, - intentFilter, - dispatcher, - callback - ) + val testSubject = + ShortcutLoader( + context, + lifecycleOwner.lifecycle, + null, + UserHandle.of(0), + true, + intentFilter, + dispatcher, + callback + ) testSubject.updateAppTargets(appTargets) @@ -200,25 +208,28 @@ class ShortcutLoaderTest { @Test fun test_appPredictorReturnsEmptyList_fallbackToShortcutManager() { - val shortcutManagerResult = listOf( - ShortcutManager.ShareShortcutInfo(matchingShortcutInfo, componentName), - // mismatching shortcut - createShareShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) - ) - val shortcutManager = mock<ShortcutManager> { - whenever(getShareTargets(intentFilter)).thenReturn(shortcutManagerResult) - } + val shortcutManagerResult = + listOf( + ShortcutManager.ShareShortcutInfo(matchingShortcutInfo, componentName), + // mismatching shortcut + createShareShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) + ) + val shortcutManager = + mock<ShortcutManager> { + whenever(getShareTargets(intentFilter)).thenReturn(shortcutManagerResult) + } whenever(context.getSystemService(Context.SHORTCUT_SERVICE)).thenReturn(shortcutManager) - val testSubject = ShortcutLoader( - context, - lifecycleOwner.lifecycle, - appPredictor, - UserHandle.of(0), - true, - intentFilter, - dispatcher, - callback - ) + val testSubject = + ShortcutLoader( + context, + lifecycleOwner.lifecycle, + appPredictor, + UserHandle.of(0), + true, + intentFilter, + dispatcher, + callback + ) testSubject.updateAppTargets(appTargets) @@ -251,27 +262,30 @@ class ShortcutLoaderTest { @Test fun test_appPredictor_requestPredictionUpdateFailure_fallbackToShortcutManager() { - val shortcutManagerResult = listOf( - ShortcutManager.ShareShortcutInfo(matchingShortcutInfo, componentName), - // mismatching shortcut - createShareShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) - ) - val shortcutManager = mock<ShortcutManager> { - whenever(getShareTargets(intentFilter)).thenReturn(shortcutManagerResult) - } + val shortcutManagerResult = + listOf( + ShortcutManager.ShareShortcutInfo(matchingShortcutInfo, componentName), + // mismatching shortcut + createShareShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) + ) + val shortcutManager = + mock<ShortcutManager> { + whenever(getShareTargets(intentFilter)).thenReturn(shortcutManagerResult) + } whenever(context.getSystemService(Context.SHORTCUT_SERVICE)).thenReturn(shortcutManager) whenever(appPredictor.requestPredictionUpdate()) .thenThrow(IllegalStateException("Test exception")) - val testSubject = ShortcutLoader( - context, - lifecycleOwner.lifecycle, - appPredictor, - UserHandle.of(0), - true, - intentFilter, - dispatcher, - callback - ) + val testSubject = + ShortcutLoader( + context, + lifecycleOwner.lifecycle, + appPredictor, + UserHandle.of(0), + true, + intentFilter, + dispatcher, + callback + ) testSubject.updateAppTargets(appTargets) @@ -317,25 +331,28 @@ class ShortcutLoaderTest { @Test fun test_ShortcutLoader_noResultsWithoutAppTargets() { - val shortcutManagerResult = listOf( - ShortcutManager.ShareShortcutInfo(matchingShortcutInfo, componentName), - // mismatching shortcut - createShareShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) - ) - val shortcutManager = mock<ShortcutManager> { - whenever(getShareTargets(intentFilter)).thenReturn(shortcutManagerResult) - } + val shortcutManagerResult = + listOf( + ShortcutManager.ShareShortcutInfo(matchingShortcutInfo, componentName), + // mismatching shortcut + createShareShortcutInfo("id-1", ComponentName("mismatching.pkg", "Class"), 1) + ) + val shortcutManager = + mock<ShortcutManager> { + whenever(getShareTargets(intentFilter)).thenReturn(shortcutManagerResult) + } whenever(context.getSystemService(Context.SHORTCUT_SERVICE)).thenReturn(shortcutManager) - val testSubject = ShortcutLoader( - context, - lifecycleOwner.lifecycle, - null, - UserHandle.of(0), - true, - intentFilter, - dispatcher, - callback - ) + val testSubject = + ShortcutLoader( + context, + lifecycleOwner.lifecycle, + null, + UserHandle.of(0), + true, + intentFilter, + dispatcher, + callback + ) verify(shortcutManager, times(1)).getShareTargets(any()) verify(callback, never()).accept(any()) @@ -366,7 +383,7 @@ class ShortcutLoaderTest { verify(appPredictor, never()).unregisterPredictionUpdates(any()) - lifecycleOwner.state = Lifecycle.State.DESTROYED + lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) verify(appPredictor, times(1)).unregisterPredictionUpdates(any()) } @@ -412,19 +429,20 @@ class ShortcutLoaderTest { whenever(isUserUnlocked(userHandle)).thenReturn(isUserUnlocked) whenever(isQuietModeEnabled(userHandle)).thenReturn(isQuietModeEnabled) } - whenever(context.getSystemService(Context.USER_SERVICE)).thenReturn(userManager); + whenever(context.getSystemService(Context.USER_SERVICE)).thenReturn(userManager) val appPredictor = mock<ShortcutLoader.AppPredictorProxy>() val callback = mock<Consumer<ShortcutLoader.Result>>() - val testSubject = ShortcutLoader( - context, - lifecycleOwner.lifecycle, - appPredictor, - userHandle, - false, - intentFilter, - dispatcher, - callback - ) + val testSubject = + ShortcutLoader( + context, + lifecycleOwner.lifecycle, + appPredictor, + userHandle, + false, + intentFilter, + dispatcher, + callback + ) testSubject.updateAppTargets(arrayOf<DisplayResolveInfo>(mock())) @@ -442,19 +460,20 @@ class ShortcutLoaderTest { whenever(isUserUnlocked(userHandle)).thenReturn(isUserUnlocked) whenever(isQuietModeEnabled(userHandle)).thenReturn(isQuietModeEnabled) } - whenever(context.getSystemService(Context.USER_SERVICE)).thenReturn(userManager); + whenever(context.getSystemService(Context.USER_SERVICE)).thenReturn(userManager) val appPredictor = mock<ShortcutLoader.AppPredictorProxy>() val callback = mock<Consumer<ShortcutLoader.Result>>() - val testSubject = ShortcutLoader( - context, - lifecycleOwner.lifecycle, - appPredictor, - userHandle, - true, - intentFilter, - dispatcher, - callback - ) + val testSubject = + ShortcutLoader( + context, + lifecycleOwner.lifecycle, + appPredictor, + userHandle, + true, + intentFilter, + dispatcher, + callback + ) testSubject.updateAppTargets(arrayOf<DisplayResolveInfo>(mock())) |