1 /* 2 * Copyright (C) 2018 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.settings.wallpaper; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.app.Application; 22 import android.app.WallpaperColors; 23 import android.app.WallpaperManager; 24 import android.app.WallpaperManager.OnColorsChangedListener; 25 import android.content.Context; 26 import android.os.Handler; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.test.core.app.ApplicationProvider; 31 32 import com.android.settings.FallbackHome; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.junit.MockitoJUnit; 38 import org.mockito.junit.MockitoRule; 39 import org.robolectric.Robolectric; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 import org.robolectric.Shadows; 43 import org.robolectric.android.controller.ActivityController; 44 import org.robolectric.annotation.Config; 45 import org.robolectric.annotation.Implementation; 46 import org.robolectric.annotation.Implements; 47 import org.robolectric.shadow.api.Shadow; 48 import org.robolectric.shadows.ShadowApplication; 49 50 import java.util.ArrayList; 51 import java.util.List; 52 53 @RunWith(RobolectricTestRunner.class) 54 public class FallbackHomeActivityTest { 55 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 56 57 private ActivityController<FallbackHome> mController; 58 59 @Before setUp()60 public void setUp() { 61 final Application application = ApplicationProvider.getApplicationContext(); 62 WallpaperManager wallpaperManager = WallpaperManager.getInstance(application); 63 ShadowApplication shadowApplication = Shadows.shadowOf(application); 64 shadowApplication.setSystemService(Context.WALLPAPER_SERVICE, wallpaperManager); 65 66 mController = Robolectric.buildActivity(FallbackHome.class); 67 } 68 69 @Test 70 @Config(shadows = ShadowWallpaperManager.class) wallpaperColorsChangedListener_ensured_removed()71 public void wallpaperColorsChangedListener_ensured_removed() { 72 // onCreate adds the first color listener by WallpaperManager returning null colors 73 ActivityController controller = mController.setup(); 74 ShadowWallpaperManager shadowManager = Shadow.extract(RuntimeEnvironment.application 75 .getSystemService(WallpaperManager.class)); 76 assertThat(shadowManager.size()).isEqualTo(1); 77 78 // Assert onDestroy will remove the original listener 79 controller.destroy(); 80 assertThat(shadowManager.size()).isEqualTo(0); 81 } 82 83 @Implements(WallpaperManager.class) 84 public static class ShadowWallpaperManager extends 85 org.robolectric.shadows.ShadowWallpaperManager { 86 87 private final List<OnColorsChangedListener> mListener = new ArrayList<>(); 88 size()89 public int size() { 90 return mListener.size(); 91 } 92 93 @Implementation isWallpaperServiceEnabled()94 protected boolean isWallpaperServiceEnabled() { 95 return true; 96 } 97 98 @Implementation getWallpaperColors(int which)99 protected @Nullable WallpaperColors getWallpaperColors(int which) { 100 return null; 101 } 102 103 @Implementation addOnColorsChangedListener(@onNull OnColorsChangedListener listener, @NonNull Handler handler)104 protected void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener, 105 @NonNull Handler handler) { 106 mListener.add(listener); 107 } 108 109 @Implementation removeOnColorsChangedListener(@onNull OnColorsChangedListener listener)110 protected void removeOnColorsChangedListener(@NonNull OnColorsChangedListener listener) { 111 mListener.remove(listener); 112 } 113 } 114 } 115