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 17package com.android.settings.display; 18 19import static com.google.common.truth.Truth.assertThat; 20 21import static org.mockito.Matchers.any; 22import static org.mockito.Matchers.anyInt; 23import static org.mockito.Mockito.mock; 24import static org.mockito.Mockito.when; 25 26import android.content.Context; 27import android.content.Intent; 28import android.content.pm.PackageManager; 29import android.content.pm.ResolveInfo; 30 31import com.android.settings.R; 32import com.android.settings.TestConfig; 33import com.android.settings.testutils.SettingsRobolectricTestRunner; 34 35import org.junit.Before; 36import org.junit.Test; 37import org.junit.runner.RunWith; 38import org.mockito.Mock; 39import org.mockito.MockitoAnnotations; 40import org.robolectric.annotation.Config; 41 42import java.util.ArrayList; 43import java.util.List; 44 45@RunWith(SettingsRobolectricTestRunner.class) 46@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 47public class WallpaperPreferenceControllerTest { 48 49 private static final String WALLPAPER_PACKAGE = "TestPkg"; 50 private static final String WALLPAPER_CLASS = "TestCls"; 51 52 @Mock 53 private Context mContext; 54 @Mock 55 private PackageManager mPackageManager; 56 57 private WallpaperPreferenceController mController; 58 59 @Before 60 public void setUp() throws PackageManager.NameNotFoundException { 61 MockitoAnnotations.initMocks(this); 62 when(mContext.getString(R.string.config_wallpaper_picker_package)) 63 .thenReturn(WALLPAPER_PACKAGE); 64 when(mContext.getString(R.string.config_wallpaper_picker_class)) 65 .thenReturn(WALLPAPER_CLASS); 66 when(mContext.getPackageManager()).thenReturn(mPackageManager); 67 68 mController = new WallpaperPreferenceController(mContext); 69 } 70 71 @Test 72 public void isAvailable_wallpaerPickerEnabled_shouldReturnTrue() { 73 final List<ResolveInfo> resolveInfos = new ArrayList<>(); 74 resolveInfos.add(mock(ResolveInfo.class)); 75 when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) 76 .thenReturn(resolveInfos); 77 78 assertThat(mController.isAvailable()).isTrue(); 79 } 80 81 @Test 82 public void isAvailable_wallpaerPickerDisbled_shouldReturnFalseAndNoCrash() { 83 when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(null); 84 85 assertThat(mController.isAvailable()).isFalse(); 86 87 final List<ResolveInfo> resolveInfos = new ArrayList<>(); 88 when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) 89 .thenReturn(resolveInfos); 90 91 assertThat(mController.isAvailable()).isFalse(); 92 // should not crash 93 } 94} 95