1 /*
2  * Copyright (C) 2018 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.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.Context;
31 import android.content.Intent;
32 import android.content.pm.PackageManager;
33 import android.content.pm.ResolveInfo;
34 
35 import com.android.settings.R;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.annotation.Config;
45 
46 import java.util.ArrayList;
47 import java.util.List;
48 
49 @RunWith(RobolectricTestRunner.class)
50 public class TopLevelDisplayPreferenceControllerTest {
51     private Context mContext;
52     @Mock
53     private PackageManager mPackageManager;
54 
55     private TopLevelDisplayPreferenceController mController;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60         mContext = spy(RuntimeEnvironment.application);
61         when(mContext.getPackageManager()).thenReturn(mPackageManager);
62         when(mContext.getString(R.string.config_wallpaper_picker_package)).thenReturn("pkg");
63         when(mContext.getString(R.string.config_wallpaper_picker_class)).thenReturn("cls");
64 
65         mController = new TopLevelDisplayPreferenceController(mContext, "test_key");
66     }
67 
68     @Test
getAvailibilityStatus_availableByDefault()69     public void getAvailibilityStatus_availableByDefault() {
70         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
71     }
72 
73     @Test
74     @Config(qualifiers = "mcc999")
getAvailabilityStatus_unsupportedWhenSet()75     public void getAvailabilityStatus_unsupportedWhenSet() {
76         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
77     }
78 
79     @Test
getSummary_hasWallpaper_shouldReturnWallpaperSummary()80     public void getSummary_hasWallpaper_shouldReturnWallpaperSummary() {
81         final List<ResolveInfo> resolveInfos = new ArrayList<>();
82         resolveInfos.add(mock(ResolveInfo.class));
83         when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt()))
84                 .thenReturn(resolveInfos);
85 
86         assertThat(mController.getSummary())
87                 .isEqualTo(mContext.getText(R.string.display_dashboard_summary));
88     }
89 
90     @Test
getSummary_hasWallpaperWithStyles_shouldReturnWallpaperSummary()91     public void getSummary_hasWallpaperWithStyles_shouldReturnWallpaperSummary() {
92         when(mContext.getString(R.string.config_styles_and_wallpaper_picker_class))
93                 .thenReturn("any.nonempty.class");
94         final List<ResolveInfo> resolveInfos = new ArrayList<>();
95         resolveInfos.add(mock(ResolveInfo.class));
96         when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt()))
97                 .thenReturn(resolveInfos);
98 
99         assertThat(mController.getSummary())
100                 .isEqualTo(mContext.getText(R.string.display_dashboard_summary_with_style));
101     }
102 
103     @Test
getSummary_hasWallpaper_shouldReturnNoWallpaperSummary()104     public void getSummary_hasWallpaper_shouldReturnNoWallpaperSummary() {
105         final List<ResolveInfo> resolveInfos = new ArrayList<>();
106         when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt()))
107                 .thenReturn(resolveInfos);
108 
109         assertThat(mController.getSummary())
110                 .isEqualTo(mContext.getText(R.string.display_dashboard_nowallpaper_summary));
111     }
112 }
113