1 /* 2 * Copyright (C) 2017 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 static org.mockito.Mockito.when; 22 23 import android.app.Application; 24 import android.app.WallpaperManager; 25 import android.content.Context; 26 import android.content.res.Resources; 27 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.RobolectricTestRunner; 35 import org.robolectric.RuntimeEnvironment; 36 import org.robolectric.Shadows; 37 import org.robolectric.annotation.Config; 38 import org.robolectric.annotation.Implementation; 39 import org.robolectric.annotation.Implements; 40 import org.robolectric.annotation.Resetter; 41 import org.robolectric.shadows.ShadowApplication; 42 43 @RunWith(RobolectricTestRunner.class) 44 public class WallpaperSuggestionActivityTest { 45 46 @Mock 47 private Context mContext; 48 @Mock 49 private Resources mResources; 50 51 @Before setUp()52 public void setUp() { 53 MockitoAnnotations.initMocks(this); 54 55 final Application application = RuntimeEnvironment.application; 56 WallpaperManager wallpaperManager = WallpaperManager.getInstance(application); 57 ShadowApplication shadowApplication = Shadows.shadowOf(application); 58 shadowApplication.setSystemService(Context.WALLPAPER_SERVICE, wallpaperManager); 59 } 60 61 @After tearDown()62 public void tearDown() { 63 ShadowWallpaperManager.reset(); 64 } 65 66 @Test wallpaperServiceEnabled_no_shouldReturnTrue()67 public void wallpaperServiceEnabled_no_shouldReturnTrue() { 68 when(mContext.getResources()).thenReturn(mResources); 69 when(mResources.getBoolean(com.android.internal.R.bool.config_enableWallpaperService)) 70 .thenReturn(false); 71 72 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(mContext)).isTrue(); 73 } 74 75 @Test 76 @Config(shadows = ShadowWallpaperManager.class) hasWallpaperSet_no_shouldReturnFalse()77 public void hasWallpaperSet_no_shouldReturnFalse() { 78 ShadowWallpaperManager.setWallpaperId(0); 79 80 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(RuntimeEnvironment.application)) 81 .isFalse(); 82 } 83 84 @Test 85 @Config(shadows = ShadowWallpaperManager.class) hasWallpaperSet_yes_shouldReturnTrue()86 public void hasWallpaperSet_yes_shouldReturnTrue() { 87 ShadowWallpaperManager.setWallpaperId(100); 88 89 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(RuntimeEnvironment.application)) 90 .isTrue(); 91 } 92 93 @Implements(WallpaperManager.class) 94 public static class ShadowWallpaperManager extends 95 org.robolectric.shadows.ShadowWallpaperManager { 96 97 private static int sWallpaperId; 98 setWallpaperId(int id)99 private static void setWallpaperId(int id) { 100 sWallpaperId = id; 101 } 102 103 @Resetter reset()104 public static void reset() { 105 sWallpaperId = 0; 106 } 107 108 @Implementation isWallpaperServiceEnabled()109 protected boolean isWallpaperServiceEnabled() { 110 return true; 111 } 112 113 @Implementation getWallpaperId(int which)114 protected int getWallpaperId(int which) { 115 return sWallpaperId; 116 } 117 } 118 } 119