From 19c5f7ae71bd8ba0f36ad7f070f68b49ffd988b3 Mon Sep 17 00:00:00 2001 From: Stefan Andonian Date: Tue, 16 Jul 2024 20:20:16 +0000 Subject: [PATCH] [Test Week] Unit test for WidgetManagerHelperTest. 6 public methods covered. Bug: 353303621 Test: Unit Test Flag: EXEMPT unit test Change-Id: I549d97f5b57f0e6e9af44bd1695d94ebfa0538f2 --- .../launcher3/widget/WidgetManagerHelper.java | 8 +- .../widget/WidgetManagerHelperTest.kt | 128 ++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 tests/multivalentTests/src/com/android/launcher3/widget/WidgetManagerHelperTest.kt diff --git a/src/com/android/launcher3/widget/WidgetManagerHelper.java b/src/com/android/launcher3/widget/WidgetManagerHelper.java index 9132b4f18c..23d05852f2 100644 --- a/src/com/android/launcher3/widget/WidgetManagerHelper.java +++ b/src/com/android/launcher3/widget/WidgetManagerHelper.java @@ -32,6 +32,7 @@ import android.widget.RemoteViews; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; +import androidx.annotation.VisibleForTesting; import com.android.launcher3.logging.FileLog; import com.android.launcher3.model.data.LauncherAppWidgetInfo; @@ -57,8 +58,13 @@ public class WidgetManagerHelper { final Context mContext; public WidgetManagerHelper(Context context) { + this(context, AppWidgetManager.getInstance(context)); + } + + @VisibleForTesting + public WidgetManagerHelper(Context context, AppWidgetManager appWidgetManager) { mContext = context; - mAppWidgetManager = AppWidgetManager.getInstance(context); + mAppWidgetManager = appWidgetManager; } /** diff --git a/tests/multivalentTests/src/com/android/launcher3/widget/WidgetManagerHelperTest.kt b/tests/multivalentTests/src/com/android/launcher3/widget/WidgetManagerHelperTest.kt new file mode 100644 index 0000000000..f1cfb79d22 --- /dev/null +++ b/tests/multivalentTests/src/com/android/launcher3/widget/WidgetManagerHelperTest.kt @@ -0,0 +1,128 @@ +/* + * 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 com.android.launcher3.widget + +import android.appwidget.AppWidgetManager +import android.content.Context +import android.content.pm.ActivityInfo +import android.os.Bundle +import android.os.Process +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.SmallTest +import androidx.test.platform.app.InstrumentationRegistry +import com.android.launcher3.util.ActivityContextWrapper +import com.android.launcher3.util.PackageUserKey +import com.google.common.truth.Truth +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.Mockito.mock +import org.mockito.Mockito.verify +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.whenever + +@SmallTest +@RunWith(AndroidJUnit4::class) +class WidgetManagerHelperTest { + + private val context: Context + get() = ActivityContextWrapper(InstrumentationRegistry.getInstrumentation().targetContext) + + private val info = + LauncherAppWidgetProviderInfo().apply { + provider = InstrumentationRegistry.getInstrumentation().componentName + providerInfo = + mock(ActivityInfo::class.java).apply { applicationInfo = context.applicationInfo } + } + + @Mock private lateinit var appWidgetManager: AppWidgetManager + + private lateinit var underTest: WidgetManagerHelper + + @Before + fun setup() { + MockitoAnnotations.initMocks(this) + underTest = WidgetManagerHelper(context, appWidgetManager) + } + + @Test + fun getAllProviders_returnsCorrectWidgetProviderInfo() { + val packageUserKey = + mock(PackageUserKey::class.java).apply { + mPackageName = context.packageName + mUser = Process.myUserHandle() + } + val desiredResult = listOf(info) + whenever( + appWidgetManager.getInstalledProvidersForPackage( + packageUserKey.mPackageName, + packageUserKey.mUser + ) + ) + .thenReturn(desiredResult) + Truth.assertThat(underTest.getAllProviders(packageUserKey)).isSameInstanceAs(desiredResult) + } + + @Test + fun getLauncherAppWidgetInfo_returnsCorrectInfo_ifWidgetExists() { + val id = 123 + whenever(appWidgetManager.getAppWidgetInfo(id)).thenReturn(info) + val componentName = InstrumentationRegistry.getInstrumentation().componentName + Truth.assertThat(underTest.getLauncherAppWidgetInfo(id, componentName)) + .isSameInstanceAs(info) + } + + @Test + fun bindAppWidgetIdIfAllowed_correctly_forwardsBindCommandToAppWidgetManager() { + val id = 124 + val options = Bundle() + underTest.bindAppWidgetIdIfAllowed(id, info, options) + verify(appWidgetManager).bindAppWidgetIdIfAllowed(id, info.profile, info.provider, options) + } + + @Test + fun findProvider_returnsNull_ifNoProviderExists() { + val info = + underTest.getLauncherAppWidgetInfo( + 1, + InstrumentationRegistry.getInstrumentation().componentName + ) + Truth.assertThat(info).isNull() + } + + @Test + fun isAppWidgetRestored_returnsTrue_ifWidgetIsRestored() { + val id = 126 + whenever(appWidgetManager.getAppWidgetOptions(id)) + .thenReturn( + Bundle().apply { + putBoolean(WidgetManagerHelper.WIDGET_OPTION_RESTORE_COMPLETED, true) + } + ) + Truth.assertThat(underTest.isAppWidgetRestored(id)).isTrue() + } + + @Test + fun loadGeneratedPreview_returnsWidgetPreview_fromAppWidgetManager() { + val widgetCategory = 130 + with(info) { + underTest.loadGeneratedPreview(this, widgetCategory) + verify(appWidgetManager).getWidgetPreview(provider, profile, widgetCategory) + } + } +}