Home | History | Annotate | Download | only in deviceinfo
      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 
     17 package com.android.settings.deviceinfo;
     18 
     19 import static com.android.settings.SettingsActivity.EXTRA_FRAGMENT_ARG_KEY;
     20 import static com.android.settings.deviceinfo.DeviceInfoSettings.NON_SIM_PREFERENCES_COUNT;
     21 import static com.android.settings.deviceinfo.DeviceInfoSettings.SIM_PREFERENCES_COUNT;
     22 import static com.google.common.truth.Truth.assertThat;
     23 import static org.mockito.ArgumentMatchers.any;
     24 import static org.mockito.Mockito.doNothing;
     25 import static org.mockito.Mockito.doReturn;
     26 import static org.mockito.Mockito.spy;
     27 import static org.mockito.Mockito.verify;
     28 
     29 import android.app.Activity;
     30 import android.content.Context;
     31 import android.os.Build;
     32 import android.os.Bundle;
     33 import android.support.v7.preference.PreferenceScreen;
     34 import android.telephony.TelephonyManager;
     35 
     36 import com.android.settings.R;
     37 import com.android.settings.dashboard.SummaryLoader;
     38 import com.android.settings.testutils.FakeFeatureFactory;
     39 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     40 import com.android.settings.testutils.XmlTestUtils;
     41 import com.android.settings.testutils.shadow.SettingsShadowResources;
     42 import com.android.settings.testutils.shadow.ShadowConnectivityManager;
     43 import com.android.settings.testutils.shadow.ShadowUserManager;
     44 import com.android.settings.testutils.shadow.ShadowUtils;
     45 import com.android.settingslib.DeviceInfoUtils;
     46 
     47 import org.junit.Before;
     48 import org.junit.Test;
     49 import org.junit.runner.RunWith;
     50 import org.mockito.Mock;
     51 import org.mockito.MockitoAnnotations;
     52 import org.robolectric.RuntimeEnvironment;
     53 import org.robolectric.annotation.Config;
     54 import org.robolectric.shadows.ShadowApplication;
     55 
     56 import java.util.List;
     57 
     58 @RunWith(SettingsRobolectricTestRunner.class)
     59 @Config(shadows = {ShadowUtils.class, ShadowConnectivityManager.class, ShadowUserManager.class})
     60 public 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