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.ComponentName; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.res.Resources; 29 30 import com.google.android.setupcompat.util.WizardManagerHelper; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.Robolectric; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.Shadows; 42 import org.robolectric.annotation.Config; 43 import org.robolectric.annotation.Implementation; 44 import org.robolectric.annotation.Implements; 45 import org.robolectric.annotation.Resetter; 46 import org.robolectric.shadows.ShadowApplication; 47 48 @RunWith(RobolectricTestRunner.class) 49 public class WallpaperSuggestionActivityTest { 50 51 @Mock 52 private Context mContext; 53 @Mock 54 private Resources mResources; 55 56 private static final String PACKAGE_WALLPAPER_ACTIVITY = 57 "com.android.settings.wallpaper.WallpaperSuggestionActivity"; 58 private static final String WALLPAPER_FLAVOR = "com.android.launcher3.WALLPAPER_FLAVOR"; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 64 final Application application = RuntimeEnvironment.application; 65 WallpaperManager wallpaperManager = WallpaperManager.getInstance(application); 66 ShadowApplication shadowApplication = Shadows.shadowOf(application); 67 shadowApplication.setSystemService(Context.WALLPAPER_SERVICE, wallpaperManager); 68 } 69 70 @After tearDown()71 public void tearDown() { 72 ShadowWallpaperManager.reset(); 73 } 74 75 @Test wallpaperServiceEnabled_no_shouldReturnTrue()76 public void wallpaperServiceEnabled_no_shouldReturnTrue() { 77 when(mContext.getResources()).thenReturn(mResources); 78 when(mResources.getBoolean(com.android.internal.R.bool.config_enableWallpaperService)) 79 .thenReturn(false); 80 81 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(mContext)).isTrue(); 82 } 83 84 @Test 85 @Config(shadows = ShadowWallpaperManager.class) hasWallpaperSet_no_shouldReturnFalse()86 public void hasWallpaperSet_no_shouldReturnFalse() { 87 ShadowWallpaperManager.setWallpaperId(0); 88 89 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(RuntimeEnvironment.application)) 90 .isFalse(); 91 } 92 93 @Test 94 @Config(shadows = ShadowWallpaperManager.class) hasWallpaperSet_yes_shouldReturnTrue()95 public void hasWallpaperSet_yes_shouldReturnTrue() { 96 ShadowWallpaperManager.setWallpaperId(100); 97 98 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(RuntimeEnvironment.application)) 99 .isTrue(); 100 } 101 102 @Test addExtras_intentFromSetupWizard_extrasHasWallpaperOnly()103 public void addExtras_intentFromSetupWizard_extrasHasWallpaperOnly() { 104 WallpaperSuggestionActivity activity = 105 Robolectric.buildActivity(WallpaperSuggestionActivity.class, new Intent( 106 Intent.ACTION_MAIN).setComponent( 107 new ComponentName(RuntimeEnvironment.application, 108 PACKAGE_WALLPAPER_ACTIVITY)).putExtra( 109 WizardManagerHelper.EXTRA_IS_FIRST_RUN, true).putExtra( 110 WizardManagerHelper.EXTRA_IS_SETUP_FLOW, true)).setup().get(); 111 Intent intent = Shadows.shadowOf(activity).getNextStartedActivity(); 112 113 assertThat(intent).isNotNull(); 114 assertThat(intent.getStringExtra(WALLPAPER_FLAVOR)).isEqualTo("wallpaper_only"); 115 } 116 117 @Test addExtras_intentNotFromSetupWizard_extrasHasFocusWallpaper()118 public void addExtras_intentNotFromSetupWizard_extrasHasFocusWallpaper() { 119 WallpaperSuggestionActivity activity = Robolectric.buildActivity( 120 WallpaperSuggestionActivity.class, new Intent(Intent.ACTION_MAIN).setComponent( 121 new ComponentName(RuntimeEnvironment.application, 122 PACKAGE_WALLPAPER_ACTIVITY))).setup().get(); 123 Intent intent = Shadows.shadowOf(activity).getNextStartedActivity(); 124 125 assertThat(intent).isNotNull(); 126 assertThat(intent.getStringExtra(WALLPAPER_FLAVOR)).isEqualTo("focus_wallpaper"); 127 } 128 129 130 @Implements(WallpaperManager.class) 131 public static class ShadowWallpaperManager extends 132 org.robolectric.shadows.ShadowWallpaperManager { 133 134 private static int sWallpaperId; 135 setWallpaperId(int id)136 private static void setWallpaperId(int id) { 137 sWallpaperId = id; 138 } 139 140 @Resetter reset()141 public static void reset() { 142 sWallpaperId = 0; 143 } 144 145 @Implementation isWallpaperServiceEnabled()146 protected boolean isWallpaperServiceEnabled() { 147 return true; 148 } 149 150 @Implementation getWallpaperId(int which)151 protected int getWallpaperId(int which) { 152 return sWallpaperId; 153 } 154 } 155 } 156