• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.applications;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Matchers.anyInt;
21 import static org.mockito.Matchers.anyString;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.Context;
31 import android.content.pm.PackageManager;
32 import android.os.UserManager;
33 import android.telephony.TelephonyManager;
34 
35 import com.android.settings.R;
36 import com.android.settings.applications.defaultapps.DefaultBrowserPreferenceController;
37 import com.android.settings.applications.defaultapps.DefaultPhonePreferenceController;
38 import com.android.settings.applications.defaultapps.DefaultSmsPreferenceController;
39 import com.android.settings.dashboard.SummaryLoader;
40 import com.android.settings.testutils.SettingsRobolectricTestRunner;
41 import com.android.settings.testutils.XmlTestUtils;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.util.ReflectionHelpers;
49 
50 import java.util.List;
51 
52 @RunWith(SettingsRobolectricTestRunner.class)
53 public class DefaultAppSettingsTest {
54 
55     private Context mContext;
56 
57     private DefaultAppSettings mFragment;
58 
59     @Before
setUp()60     public void setUp() {
61         MockitoAnnotations.initMocks(this);
62         mContext = RuntimeEnvironment.application;
63         mFragment = new DefaultAppSettings();
64         mFragment.onAttach(mContext);
65     }
66 
67     @Test
getPreferenceScreenResId_shouldUseAppDefaultSettingPrefLayout()68     public void getPreferenceScreenResId_shouldUseAppDefaultSettingPrefLayout() {
69         assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(R.xml.app_default_settings);
70     }
71 
72     @Test
setListening_shouldUpdateSummary()73     public void setListening_shouldUpdateSummary() {
74         final SummaryLoader summaryLoader = mock(SummaryLoader.class);
75         final DefaultAppSettings.SummaryProvider summaryProvider =
76                 new DefaultAppSettings.SummaryProvider(mContext, summaryLoader);
77         final DefaultSmsPreferenceController defaultSms =
78                 mock(DefaultSmsPreferenceController.class);
79         final DefaultBrowserPreferenceController defaultBrowser =
80                 mock(DefaultBrowserPreferenceController.class);
81         final DefaultPhonePreferenceController defaultPhone =
82                 mock(DefaultPhonePreferenceController.class);
83         ReflectionHelpers.setField(summaryProvider, "mDefaultSmsPreferenceController", defaultSms);
84         ReflectionHelpers.setField(
85                 summaryProvider, "mDefaultBrowserPreferenceController", defaultBrowser);
86         ReflectionHelpers.setField(
87                 summaryProvider, "mDefaultPhonePreferenceController", defaultPhone);
88 
89         // all available
90         when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
91         when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
92         when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
93         summaryProvider.setListening(true);
94         verify(summaryLoader).setSummary(summaryProvider, "Browser1, Phone1, Sms1");
95 
96         // 2 available
97         when(defaultSms.getDefaultAppLabel()).thenReturn(null);
98         when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
99         when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
100         summaryProvider.setListening(true);
101         verify(summaryLoader).setSummary(summaryProvider, "Browser1, Phone1");
102 
103         when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
104         when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
105         when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
106         summaryProvider.setListening(true);
107         verify(summaryLoader).setSummary(summaryProvider, "Phone1, Sms1");
108 
109         when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
110         when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
111         when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
112         summaryProvider.setListening(true);
113         verify(summaryLoader).setSummary(summaryProvider, "Phone1, Sms1");
114 
115         // 1 available
116         when(defaultSms.getDefaultAppLabel()).thenReturn(null);
117         when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1");
118         when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
119         summaryProvider.setListening(true);
120         verify(summaryLoader).setSummary(summaryProvider, "Browser1");
121 
122         when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1");
123         when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
124         when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
125         summaryProvider.setListening(true);
126         verify(summaryLoader).setSummary(summaryProvider, "Sms1");
127 
128         when(defaultSms.getDefaultAppLabel()).thenReturn(null);
129         when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
130         when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1");
131         summaryProvider.setListening(true);
132         verify(summaryLoader).setSummary(summaryProvider, "Phone1");
133 
134         // None available
135         when(defaultSms.getDefaultAppLabel()).thenReturn(null);
136         when(defaultBrowser.getDefaultAppLabel()).thenReturn(null);
137         when(defaultPhone.getDefaultAppLabel()).thenReturn(null);
138         summaryProvider.setListening(true);
139         verify(summaryLoader, never()).setSummary(summaryProvider, eq(anyString()));
140 
141     }
142 
143     @Test
testNonIndexableKeys_existInXmlLayout()144     public void testNonIndexableKeys_existInXmlLayout() {
145         final Context context = spy(RuntimeEnvironment.application);
146         when(context.getApplicationContext()).thenReturn(context);
147         final UserManager userManager = mock(UserManager.class, RETURNS_DEEP_STUBS);
148 
149         when(context.getSystemService(Context.USER_SERVICE))
150                 .thenReturn(userManager);
151         when(userManager.getUserInfo(anyInt()).isRestricted()).thenReturn(true);
152 
153         when(context.getSystemService(Context.TELEPHONY_SERVICE))
154                 .thenReturn(mock(TelephonyManager.class));
155         when(context.getPackageManager())
156                 .thenReturn(mock(PackageManager.class));
157         final List<String> niks = DefaultAppSettings.SEARCH_INDEX_DATA_PROVIDER
158                 .getNonIndexableKeys(context);
159 
160         final int xmlId = (new DefaultAppSettings()).getPreferenceScreenResId();
161 
162         final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);
163 
164         assertThat(keys).containsAllIn(niks);
165     }
166 }
167