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