1 /* 2 * Copyright (C) 2025 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.util 18 19 import android.app.Activity 20 import android.content.ComponentCallbacks 21 import android.content.res.Configuration 22 import androidx.test.ext.junit.runners.AndroidJUnit4 23 import androidx.test.filters.SmallTest 24 import com.android.dx.mockito.inline.extended.ExtendedMockito.any 25 import com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn 26 import com.android.dx.mockito.inline.extended.ExtendedMockito.eq 27 import com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession 28 import com.android.dx.mockito.inline.extended.ExtendedMockito.times 29 import com.android.dx.mockito.inline.extended.ExtendedMockito.verify 30 import com.android.dx.mockito.inline.extended.StaticMockitoSession 31 import com.android.launcher3.util.WallpaperThemeManager.Companion.setWallpaperDependentTheme 32 import org.junit.After 33 import org.junit.Before 34 import org.junit.Rule 35 import org.junit.Test 36 import org.junit.runner.RunWith 37 import org.mockito.ArgumentCaptor 38 import org.mockito.Captor 39 import org.mockito.Mock 40 import org.mockito.MockitoAnnotations 41 import org.mockito.kotlin.never 42 import org.mockito.kotlin.whenever 43 44 /** Tests for WallpaperThemeManager */ 45 @SmallTest 46 @RunWith(AndroidJUnit4::class) 47 class WallpaperThemeManagerTest { 48 49 @get:Rule val context = SandboxApplication() 50 51 @Mock lateinit var activity: Activity 52 @Captor lateinit var callbacksCaptor: ArgumentCaptor<ComponentCallbacks> 53 54 private lateinit var mockSession: StaticMockitoSession 55 56 @Before setupnull57 fun setup() { 58 MockitoAnnotations.initMocks(this) 59 mockSession = mockitoSession().spyStatic(Themes::class.java).startMocking() 60 61 doReturn(1).`when`<Int> { Themes.getActivityThemeRes(any()) } 62 doReturn(context).whenever(activity).applicationContext 63 } 64 65 @After tearDownnull66 fun tearDown() { 67 mockSession.finishMocking() 68 } 69 70 @Test correct theme set on activity createnull71 fun `correct theme set on activity create`() { 72 activity.setWallpaperDependentTheme() 73 verify(activity, times(1)).setTheme(eq(1)) 74 } 75 76 @Test ignores update if theme does not changenull77 fun `ignores update if theme does not change`() { 78 activity.setWallpaperDependentTheme() 79 verify(activity).registerComponentCallbacks(callbacksCaptor.capture()) 80 callbacksCaptor.value.onConfigurationChanged(Configuration()) 81 verify(activity, never()).recreate() 82 } 83 84 @Test activity recreated if theme changesnull85 fun `activity recreated if theme changes`() { 86 activity.setWallpaperDependentTheme() 87 verify(activity).registerComponentCallbacks(callbacksCaptor.capture()) 88 89 doReturn(3).`when`<Int> { Themes.getActivityThemeRes(any()) } 90 callbacksCaptor.value.onConfigurationChanged(Configuration()) 91 verify(activity, times(1)).recreate() 92 } 93 } 94