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 import static org.mockito.Mockito.when; 21 import static org.robolectric.Shadows.shadowOf; 22 23 import android.app.WallpaperManager; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.res.Resources; 27 28 import com.android.settings.SubSettings; 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 31 import org.junit.After; 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.Robolectric; 38 import org.robolectric.RuntimeEnvironment; 39 import org.robolectric.android.controller.ActivityController; 40 import org.robolectric.annotation.Config; 41 import org.robolectric.annotation.Implementation; 42 import org.robolectric.annotation.Implements; 43 import org.robolectric.annotation.Resetter; 44 import org.robolectric.shadows.ShadowActivity; 45 import org.robolectric.shadows.ShadowPackageManager; 46 47 @RunWith(SettingsRobolectricTestRunner.class) 48 public class WallpaperSuggestionActivityTest { 49 50 @Mock 51 private Context mContext; 52 @Mock 53 private Resources mResources; 54 55 private ActivityController<WallpaperSuggestionActivity> mController; 56 57 @Before 58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mController = Robolectric.buildActivity(WallpaperSuggestionActivity.class); 61 } 62 63 @After 64 public void tearDown() { 65 ShadowWallpaperManager.reset(); 66 } 67 68 @Test 69 public void launch_primarySuggestionActivityDoesNotExist_shouldFallback() { 70 ShadowPackageManager packageManager = 71 shadowOf(RuntimeEnvironment.application.getPackageManager()); 72 packageManager.removePackage("com.android.settings"); 73 74 ShadowActivity activity = shadowOf(mController.setup().get()); 75 final Intent intent = activity.getNextStartedActivity(); 76 77 assertThat(intent.getComponent().getClassName()).isEqualTo(SubSettings.class.getName()); 78 assertThat(intent.getFlags()).isEqualTo(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 79 assertThat(activity.isFinishing()).isTrue(); 80 } 81 82 @Test 83 public void wallpaperServiceEnabled_no_shouldReturnTrue() { 84 when(mContext.getResources()).thenReturn(mResources); 85 when(mResources.getBoolean(com.android.internal.R.bool.config_enableWallpaperService)) 86 .thenReturn(false); 87 88 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(mContext)).isTrue(); 89 } 90 91 @Test 92 @Config(shadows = ShadowWallpaperManager.class) 93 public void hasWallpaperSet_no_shouldReturnFalse() { 94 ShadowWallpaperManager.setWallpaperId(0); 95 96 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(RuntimeEnvironment.application)) 97 .isFalse(); 98 } 99 100 @Test 101 @Config(shadows = ShadowWallpaperManager.class) 102 public void hasWallpaperSet_yes_shouldReturnTrue() { 103 ShadowWallpaperManager.setWallpaperId(100); 104 105 assertThat(WallpaperSuggestionActivity.isSuggestionComplete(RuntimeEnvironment.application)) 106 .isTrue(); 107 } 108 109 @Implements(WallpaperManager.class) 110 public static class ShadowWallpaperManager { 111 112 private static int sWallpaperId; 113 114 private static void setWallpaperId(int id) { 115 sWallpaperId = id; 116 } 117 118 @Resetter 119 public static void reset() { 120 sWallpaperId = 0; 121 } 122 123 @Implementation 124 public boolean isWallpaperServiceEnabled() { 125 return true; 126 } 127 128 @Implementation 129 public int getWallpaperId(int which) { 130 return sWallpaperId; 131 } 132 } 133 } 134