• 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 android.content.Context;
20 
21 import com.android.settings.R;
22 import com.android.settings.SettingsRobolectricTestRunner;
23 import com.android.settings.TestConfig;
24 
25 import com.android.settings.applications.defaultapps.DefaultBrowserPreferenceController;
26 import com.android.settings.applications.defaultapps.DefaultPhonePreferenceController;
27 import com.android.settings.applications.defaultapps.DefaultSmsPreferenceController;
28 import com.android.settings.dashboard.SummaryLoader;
29 import com.android.settings.testutils.XmlTestUtils;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.RuntimeEnvironment;
35 import org.robolectric.annotation.Config;
36 import org.robolectric.util.ReflectionHelpers;
37 
38 import java.util.List;
39 
40 import static com.google.common.truth.Truth.assertThat;
41 import static org.mockito.Matchers.anyString;
42 import static org.mockito.Matchers.eq;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.never;
45 import static org.mockito.Mockito.spy;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.when;
48 
49 @RunWith(SettingsRobolectricTestRunner.class)
50 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
51 public class AdvancedAppSettingsTest {
52 
53     private Context mContext;
54 
55     private AdvancedAppSettings mFragment;
56 
57     @Before
setUp()58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60 
61         mContext = RuntimeEnvironment.application;
62         mFragment = new AdvancedAppSettings();
63         mFragment.onAttach(mContext);
64     }
65 
66     @Test
getPreferenceScreenResId_shouldUseAppDefaultSettingPrefLayout()67     public void getPreferenceScreenResId_shouldUseAppDefaultSettingPrefLayout() {
68         assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(
69                 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 AdvancedAppSettings.SummaryProvider summaryProvider =
76             new AdvancedAppSettings.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, "Sms1, Browser1, Phone1");
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, "Sms1, Phone1");
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, "Sms1, Browser1");
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 
144     @Test
testNonIndexableKeys_existInXmlLayout()145     public void testNonIndexableKeys_existInXmlLayout() {
146         final Context context = spy(RuntimeEnvironment.application);
147         final List<String> niks = AdvancedAppSettings.SEARCH_INDEX_DATA_PROVIDER
148                 .getNonIndexableKeys(context);
149         final int xmlId = (new AdvancedAppSettings()).getPreferenceScreenResId();
150 
151         final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);
152 
153         assertThat(keys).containsAllIn(niks);
154     }
155 }
156