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
17package com.android.settings.deviceinfo;
18
19import static com.android.settings.SettingsActivity.EXTRA_FRAGMENT_ARG_KEY;
20import static com.android.settings.deviceinfo.DeviceInfoSettings.NON_SIM_PREFERENCES_COUNT;
21import static com.android.settings.deviceinfo.DeviceInfoSettings.SIM_PREFERENCES_COUNT;
22import static com.google.common.truth.Truth.assertThat;
23import static org.mockito.ArgumentMatchers.any;
24import static org.mockito.Mockito.doNothing;
25import static org.mockito.Mockito.doReturn;
26import static org.mockito.Mockito.spy;
27import static org.mockito.Mockito.verify;
28
29import android.app.Activity;
30import android.content.Context;
31import android.os.Build;
32import android.os.Bundle;
33import android.support.v7.preference.PreferenceScreen;
34import android.telephony.TelephonyManager;
35
36import com.android.settings.R;
37import com.android.settings.dashboard.SummaryLoader;
38import com.android.settings.testutils.FakeFeatureFactory;
39import com.android.settings.testutils.SettingsRobolectricTestRunner;
40import com.android.settings.testutils.XmlTestUtils;
41import com.android.settings.testutils.shadow.SettingsShadowResources;
42import com.android.settings.testutils.shadow.ShadowConnectivityManager;
43import com.android.settings.testutils.shadow.ShadowUserManager;
44import com.android.settings.testutils.shadow.ShadowUtils;
45import com.android.settingslib.DeviceInfoUtils;
46
47import org.junit.Before;
48import org.junit.Test;
49import org.junit.runner.RunWith;
50import org.mockito.Mock;
51import org.mockito.MockitoAnnotations;
52import org.robolectric.RuntimeEnvironment;
53import org.robolectric.annotation.Config;
54import org.robolectric.shadows.ShadowApplication;
55
56import java.util.List;
57
58@RunWith(SettingsRobolectricTestRunner.class)
59@Config(shadows = {ShadowUtils.class, ShadowConnectivityManager.class, ShadowUserManager.class})
60public class DeviceInfoSettingsTest {
61
62    @Mock
63    private Activity mActivity;
64    @Mock
65    private PreferenceScreen mScreen;
66    @Mock
67    private SummaryLoader mSummaryLoader;
68    @Mock
69    private TelephonyManager mTelephonyManager;
70
71    private Context mContext;
72    private DeviceInfoSettings mSettings;
73
74    @Before
75    public void setUp() {
76        MockitoAnnotations.initMocks(this);
77        FakeFeatureFactory.setupForTest();
78        mContext = RuntimeEnvironment.application;
79        mSettings = spy(new DeviceInfoSettings());
80
81        doReturn(mActivity).when(mSettings).getActivity();
82        doReturn(mContext).when(mSettings).getContext();
83        doReturn(mContext.getTheme()).when(mActivity).getTheme();
84        doReturn(mContext.getResources()).when(mSettings).getResources();
85        doNothing().when(mSettings).onCreatePreferences(any(), any());
86
87        doReturn(mScreen).when(mSettings).getPreferenceScreen();
88        ShadowApplication.getInstance()
89            .setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
90    }
91
92    @Test
93    public void getPrefXml_shouldReturnDeviceInfoXml() {
94        assertThat(mSettings.getPreferenceScreenResId()).isEqualTo(R.xml.device_info_settings);
95    }
96
97    @Test
98    public void getSummary_shouldReturnDeviceModel() {
99        final SummaryLoader.SummaryProvider mProvider =
100            DeviceInfoSettings.SUMMARY_PROVIDER_FACTORY.createSummaryProvider(null, mSummaryLoader);
101
102        mProvider.setListening(true);
103
104        verify(mSummaryLoader).setSummary(mProvider, Build.MODEL + DeviceInfoUtils.getMsvSuffix());
105    }
106
107    @Test
108    public void testNonIndexableKeys_existInXmlLayout() {
109        final Context context = RuntimeEnvironment.application;
110        final List<String> niks =
111            DeviceInfoSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context);
112        final int xmlId = (new DeviceInfoSettings()).getPreferenceScreenResId();
113
114        final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(context, xmlId);
115
116        assertThat(keys).containsAllIn(niks);
117    }
118
119    @Test
120    @Config(shadows = SettingsShadowResources.SettingsShadowTheme.class)
121    public void onCreate_fromSearch_shouldNotOverrideInitialExpandedCount() {
122        final Bundle args = new Bundle();
123        args.putString(EXTRA_FRAGMENT_ARG_KEY, "search_key");
124        mSettings.setArguments(args);
125
126        mSettings.onCreate(null /* icicle */);
127
128        verify(mScreen).setInitialExpandedChildrenCount(Integer.MAX_VALUE);
129    }
130
131    @Test
132    @Config(shadows = SettingsShadowResources.SettingsShadowTheme.class)
133    public void onCreate_singleSim_shouldAddSingleSimCount() {
134        doReturn(1).when(mTelephonyManager).getPhoneCount();
135
136        mSettings.onCreate(null /* icicle */);
137
138        verify(mScreen).setInitialExpandedChildrenCount(
139                SIM_PREFERENCES_COUNT + NON_SIM_PREFERENCES_COUNT);
140    }
141
142    @Test
143    @Config(shadows = SettingsShadowResources.SettingsShadowTheme.class)
144    public void onCreate_dualeSim_shouldAddDualSimCount() {
145        doReturn(2).when(mTelephonyManager).getPhoneCount();
146
147        mSettings.onCreate(null /* icicle */);
148
149        verify(mScreen).setInitialExpandedChildrenCount(
150                2 * SIM_PREFERENCES_COUNT + NON_SIM_PREFERENCES_COUNT);
151    }
152}
153