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 private static final String WALLPAPER_LAUNCH_SOURCE = "com.android.wallpaper.LAUNCH_SOURCE"; 60 61 @Before setUp()62 public void setUp() { 63 MockitoAnnotations.initMocks(this); 64 65 final Application application = RuntimeEnvironment.application; 66 WallpaperManager wallpaperManager = WallpaperManager.getInstance(application); 67 ShadowApplication shadowApplication = Shadows.shadowOf(application); 68 shadowApplication.setSystemService(Context.WALLPAPER_SERVICE, wallpaperManager); 69 } 70 71 @After tearDown()72 public void tearDown() { 73 ShadowWallpaperManager.reset(); 74 } 75 76 @Test wallpaperServiceEnabled_no_shouldReturnTrue()77 public void wallpaperServiceEnabled_no_shouldReturnTrue() { 78 when(mContext.getResources()).thenReturn(mResources); 79 when(mResources.getBoolean(com.android.internal.R.bool.config_enableWallpaperService)) 80 .thenReturn(false); 81 82 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(mContext)).isTrue(); 83 } 84 85 @Test 86 @Config(shadows = ShadowWallpaperManager.class) hasWallpaperSet_no_shouldReturnFalse()87 public void hasWallpaperSet_no_shouldReturnFalse() { 88 ShadowWallpaperManager.setWallpaperId(0); 89 90 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(RuntimeEnvironment.application)) 91 .isFalse(); 92 } 93 94 @Test 95 @Config(shadows = ShadowWallpaperManager.class) hasWallpaperSet_yes_shouldReturnTrue()96 public void hasWallpaperSet_yes_shouldReturnTrue() { 97 ShadowWallpaperManager.setWallpaperId(100); 98 99 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(RuntimeEnvironment.application)) 100 .isTrue(); 101 } 102 103 @Test addExtras_intentFromSetupWizard_extrasHasWallpaperOnlyAndLaunchedSuw()104 public void addExtras_intentFromSetupWizard_extrasHasWallpaperOnlyAndLaunchedSuw() { 105 WallpaperSuggestionActivity activity = 106 Robolectric.buildActivity(WallpaperSuggestionActivity.class, new Intent( 107 Intent.ACTION_MAIN).setComponent( 108 new ComponentName(RuntimeEnvironment.application, 109 PACKAGE_WALLPAPER_ACTIVITY)).putExtra( 110 WizardManagerHelper.EXTRA_IS_FIRST_RUN, true).putExtra( 111 WizardManagerHelper.EXTRA_IS_SETUP_FLOW, true)).setup().get(); 112 Intent intent = Shadows.shadowOf(activity).getNextStartedActivity(); 113 114 assertThat(intent).isNotNull(); 115 assertThat(intent.getStringExtra(WALLPAPER_FLAVOR)).isEqualTo("wallpaper_only"); 116 assertThat(intent.getStringExtra(WALLPAPER_LAUNCH_SOURCE)) 117 .isEqualTo("app_launched_suw"); 118 } 119 120 @Test addExtras_intentNotFromSetupWizard_extrasHasFocusWallpaperAndLaunchedSettingsSearch()121 public void addExtras_intentNotFromSetupWizard_extrasHasFocusWallpaperAndLaunchedSettingsSearch() { 122 WallpaperSuggestionActivity activity = Robolectric.buildActivity( 123 WallpaperSuggestionActivity.class, new Intent(Intent.ACTION_MAIN).setComponent( 124 new ComponentName(RuntimeEnvironment.application, 125 PACKAGE_WALLPAPER_ACTIVITY))).setup().get(); 126 Intent intent = Shadows.shadowOf(activity).getNextStartedActivity(); 127 128 assertThat(intent).isNotNull(); 129 assertThat(intent.getStringExtra(WALLPAPER_FLAVOR)).isEqualTo("focus_wallpaper"); 130 assertThat(intent.getStringExtra(WALLPAPER_LAUNCH_SOURCE)) 131 .isEqualTo("app_launched_settings_search"); 132 } 133 134 135 @Implements(WallpaperManager.class) 136 public static class ShadowWallpaperManager extends 137 org.robolectric.shadows.ShadowWallpaperManager { 138 139 private static int sWallpaperId; 140 setWallpaperId(int id)141 private static void setWallpaperId(int id) { 142 sWallpaperId = id; 143 } 144 145 @Resetter reset()146 public static void reset() { 147 sWallpaperId = 0; 148 } 149 150 @Implementation isWallpaperServiceEnabled()151 protected boolean isWallpaperServiceEnabled() { 152 return true; 153 } 154 155 @Implementation getWallpaperId(int which)156 protected int getWallpaperId(int which) { 157 return sWallpaperId; 158 } 159 } 160 } 161