1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.widget 18 19 import android.appwidget.AppWidgetManager 20 import android.content.Context 21 import android.content.pm.ActivityInfo 22 import android.os.Bundle 23 import android.os.Process 24 import androidx.test.ext.junit.runners.AndroidJUnit4 25 import androidx.test.filters.SmallTest 26 import androidx.test.platform.app.InstrumentationRegistry 27 import com.android.launcher3.util.ActivityContextWrapper 28 import com.android.launcher3.util.PackageUserKey 29 import com.google.common.truth.Truth 30 import org.junit.Before 31 import org.junit.Test 32 import org.junit.runner.RunWith 33 import org.mockito.Mock 34 import org.mockito.Mockito.mock 35 import org.mockito.Mockito.verify 36 import org.mockito.MockitoAnnotations 37 import org.mockito.kotlin.whenever 38 39 @SmallTest 40 @RunWith(AndroidJUnit4::class) 41 class WidgetManagerHelperTest { 42 43 private val context: Context 44 get() = ActivityContextWrapper(InstrumentationRegistry.getInstrumentation().targetContext) 45 46 private val info = <lambda>null47 LauncherAppWidgetProviderInfo().apply { 48 provider = InstrumentationRegistry.getInstrumentation().componentName 49 providerInfo = 50 mock(ActivityInfo::class.java).apply { applicationInfo = context.applicationInfo } 51 } 52 53 @Mock private lateinit var appWidgetManager: AppWidgetManager 54 55 private lateinit var underTest: WidgetManagerHelper 56 57 @Before setupnull58 fun setup() { 59 MockitoAnnotations.initMocks(this) 60 underTest = WidgetManagerHelper(context, appWidgetManager) 61 } 62 63 @Test getAllProviders_returnsCorrectWidgetProviderInfonull64 fun getAllProviders_returnsCorrectWidgetProviderInfo() { 65 val packageUserKey = 66 mock(PackageUserKey::class.java).apply { 67 mPackageName = context.packageName 68 mUser = Process.myUserHandle() 69 } 70 val desiredResult = listOf(info) 71 whenever( 72 appWidgetManager.getInstalledProvidersForPackage( 73 packageUserKey.mPackageName, 74 packageUserKey.mUser 75 ) 76 ) 77 .thenReturn(desiredResult) 78 Truth.assertThat(underTest.getAllProviders(packageUserKey)).isSameInstanceAs(desiredResult) 79 } 80 81 @Test getLauncherAppWidgetInfo_returnsCorrectInfo_ifWidgetExistsnull82 fun getLauncherAppWidgetInfo_returnsCorrectInfo_ifWidgetExists() { 83 val id = 123 84 whenever(appWidgetManager.getAppWidgetInfo(id)).thenReturn(info) 85 val componentName = InstrumentationRegistry.getInstrumentation().componentName 86 Truth.assertThat(underTest.getLauncherAppWidgetInfo(id, componentName)) 87 .isSameInstanceAs(info) 88 } 89 90 @Test bindAppWidgetIdIfAllowed_correctly_forwardsBindCommandToAppWidgetManagernull91 fun bindAppWidgetIdIfAllowed_correctly_forwardsBindCommandToAppWidgetManager() { 92 val id = 124 93 val options = Bundle() 94 underTest.bindAppWidgetIdIfAllowed(id, info, options) 95 verify(appWidgetManager).bindAppWidgetIdIfAllowed(id, info.profile, info.provider, options) 96 } 97 98 @Test findProvider_returnsNull_ifNoProviderExistsnull99 fun findProvider_returnsNull_ifNoProviderExists() { 100 val info = 101 underTest.getLauncherAppWidgetInfo( 102 1, 103 InstrumentationRegistry.getInstrumentation().componentName 104 ) 105 Truth.assertThat(info).isNull() 106 } 107 108 @Test isAppWidgetRestored_returnsTrue_ifWidgetIsRestorednull109 fun isAppWidgetRestored_returnsTrue_ifWidgetIsRestored() { 110 val id = 126 111 whenever(appWidgetManager.getAppWidgetOptions(id)) 112 .thenReturn( 113 Bundle().apply { 114 putBoolean(WidgetManagerHelper.WIDGET_OPTION_RESTORE_COMPLETED, true) 115 } 116 ) 117 Truth.assertThat(underTest.isAppWidgetRestored(id)).isTrue() 118 } 119 120 @Test loadGeneratedPreview_returnsWidgetPreview_fromAppWidgetManagernull121 fun loadGeneratedPreview_returnsWidgetPreview_fromAppWidgetManager() { 122 val widgetCategory = 130 123 with(info) { 124 underTest.loadGeneratedPreview(this, widgetCategory) 125 verify(appWidgetManager).getWidgetPreview(provider, profile, widgetCategory) 126 } 127 } 128 } 129