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