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
17package com.android.settings.applications;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Matchers.anyInt;
21import static org.mockito.Matchers.anyString;
22import static org.mockito.Matchers.eq;
23import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
24import static org.mockito.Mockito.mock;
25import static org.mockito.Mockito.never;
26import static org.mockito.Mockito.spy;
27import static org.mockito.Mockito.verify;
28import static org.mockito.Mockito.when;
29
30import android.content.Context;
31import android.content.pm.PackageManager;
32import android.os.UserManager;
33import android.telephony.TelephonyManager;
34
35import com.android.settings.R;
36import com.android.settings.TestConfig;
37import com.android.settings.applications.defaultapps.DefaultBrowserPreferenceController;
38import com.android.settings.applications.defaultapps.DefaultPhonePreferenceController;
39import com.android.settings.applications.defaultapps.DefaultSmsPreferenceController;
40import com.android.settings.dashboard.SummaryLoader;
41import com.android.settings.testutils.SettingsRobolectricTestRunner;
42import com.android.settings.testutils.XmlTestUtils;
43
44import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.MockitoAnnotations;
48import org.robolectric.RuntimeEnvironment;
49import org.robolectric.annotation.Config;
50import org.robolectric.util.ReflectionHelpers;
51
52import java.util.List;
53
54@RunWith(SettingsRobolectricTestRunner.class)
55@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
56public class DefaultAppSettingsTest {
57
58    private Context mContext;
59
60    private DefaultAppSettings mFragment;
61
62    @Before
63    public void setUp() {
64        MockitoAnnotations.initMocks(this);
65        mContext = RuntimeEnvironment.application;
66        mFragment = new DefaultAppSettings();
67        mFragment.onAttach(mContext);
68    }
69
70    @Test
71    public void getPreferenceScreenResId_shouldUseAppDefaultSettingPrefLayout() {
72        assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(
73                R.xml.app_default_settings);
74    }
75
76    @Test
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
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