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