Home | History | Annotate | Download | only in device
      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.tv.settings.device;
     18 
     19 import static org.mockito.ArgumentMatchers.any;
     20 import static org.mockito.Mockito.atLeastOnce;
     21 import static org.mockito.Mockito.doReturn;
     22 import static org.mockito.Mockito.mock;
     23 import static org.mockito.Mockito.never;
     24 import static org.mockito.Mockito.verify;
     25 import static org.robolectric.Shadows.shadowOf;
     26 import static org.robolectric.shadow.api.Shadow.extract;
     27 
     28 import android.content.Intent;
     29 import android.content.pm.ActivityInfo;
     30 import android.content.pm.ApplicationInfo;
     31 import android.content.pm.PackageInfo;
     32 import android.content.pm.PackageManager;
     33 import android.content.pm.ResolveInfo;
     34 import android.os.UserManager;
     35 import android.provider.Settings;
     36 import android.support.v7.preference.Preference;
     37 
     38 import com.android.settingslib.development.DevelopmentSettingsEnabler;
     39 import com.android.tv.settings.R;
     40 import com.android.tv.settings.TvSettingsRobolectricTestRunner;
     41 import com.android.tv.settings.testutils.ShadowUserManager;
     42 import com.android.tv.settings.testutils.TvShadowActivityThread;
     43 import com.android.tv.settings.testutils.Utils;
     44 
     45 import org.junit.Before;
     46 import org.junit.Test;
     47 import org.junit.runner.RunWith;
     48 import org.mockito.MockitoAnnotations;
     49 import org.mockito.Spy;
     50 import org.robolectric.RuntimeEnvironment;
     51 import org.robolectric.annotation.Config;
     52 import org.robolectric.shadows.ShadowPackageManager;
     53 import org.robolectric.shadows.ShadowSettings;
     54 
     55 @RunWith(TvSettingsRobolectricTestRunner.class)
     56 @Config(shadows = {ShadowUserManager.class})
     57 public class DevicePrefFragmentTest {
     58     @Spy
     59     private DevicePrefFragment mDevicePrefFragment;
     60 
     61     private ShadowUserManager mUserManager;
     62     @Before
     63     public void setUp() {
     64         MockitoAnnotations.initMocks(this);
     65         mUserManager = extract(RuntimeEnvironment.application.getSystemService(UserManager.class));
     66         mUserManager.setIsAdminUser(true);
     67         doReturn(RuntimeEnvironment.application).when(mDevicePrefFragment).getContext();
     68         mDevicePrefFragment.onAttach(RuntimeEnvironment.application);
     69     }
     70 
     71     @Test
     72     public void testUpdateDeveloperOptions_developerDisabled() {
     73         DevelopmentSettingsEnabler
     74                 .setDevelopmentSettingsEnabled(RuntimeEnvironment.application, false);
     75         final Preference developerPref = mock(Preference.class);
     76         doReturn(developerPref).when(mDevicePrefFragment)
     77                 .findPreference(mDevicePrefFragment.KEY_DEVELOPER);
     78         mDevicePrefFragment.updateDeveloperOptions();
     79         verify(developerPref, atLeastOnce()).setVisible(false);
     80         verify(developerPref, never()).setVisible(true);
     81     }
     82 
     83     @Test
     84     public void testUpdateDeveloperOptions_notAdmin() {
     85         DevelopmentSettingsEnabler
     86                 .setDevelopmentSettingsEnabled(RuntimeEnvironment.application, true);
     87         mUserManager.setIsAdminUser(false);
     88 
     89         final Preference developerPref = mock(Preference.class);
     90         doReturn(developerPref).when(mDevicePrefFragment)
     91                     .findPreference(DevicePrefFragment.KEY_DEVELOPER);
     92         mDevicePrefFragment.updateDeveloperOptions();
     93         verify(developerPref, atLeastOnce()).setVisible(false);
     94         verify(developerPref, never()).setVisible(true);
     95     }
     96 
     97     @Test
     98     public void testUpdateDeveloperOptions_developerEnabled() {
     99         DevelopmentSettingsEnabler
    100                 .setDevelopmentSettingsEnabled(RuntimeEnvironment.application, true);
    101         final Preference developerPref = mock(Preference.class);
    102         doReturn(developerPref).when(mDevicePrefFragment)
    103                 .findPreference(mDevicePrefFragment.KEY_DEVELOPER);
    104         mDevicePrefFragment.updateDeveloperOptions();
    105         verify(developerPref, atLeastOnce()).setVisible(true);
    106         verify(developerPref, never()).setVisible(false);
    107     }
    108 
    109     @Test
    110     public void testUpdateCastSettings() {
    111         final Preference castPref = mock(Preference.class);
    112         doReturn(castPref).when(mDevicePrefFragment)
    113                     .findPreference(DevicePrefFragment.KEY_CAST_SETTINGS);
    114         final Intent intent = new Intent("com.google.android.settings.CAST_RECEIVER_SETTINGS");
    115         doReturn(intent).when(castPref).getIntent();
    116 
    117         final ResolveInfo resolveInfo = new ResolveInfo();
    118         resolveInfo.resolvePackageName = "com.test.CastPackage";
    119         final ActivityInfo activityInfo = mock(ActivityInfo.class);
    120         doReturn("Test Name").when(activityInfo).loadLabel(any(PackageManager.class));
    121         resolveInfo.activityInfo = activityInfo;
    122         final ApplicationInfo applicationInfo = new ApplicationInfo();
    123         applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
    124         activityInfo.applicationInfo = applicationInfo;
    125         final ShadowPackageManager shadowPackageManager = shadowOf(
    126                 RuntimeEnvironment.application.getPackageManager());
    127         final PackageInfo castPackageInfo = new PackageInfo();
    128         castPackageInfo.packageName = "com.test.CastPackage";
    129         shadowPackageManager.addPackage(castPackageInfo);
    130         shadowPackageManager.addResolveInfoForIntent(intent, resolveInfo);
    131 
    132         mDevicePrefFragment.updateCastSettings();
    133 
    134         verify(castPref, atLeastOnce()).setTitle("Test Name");
    135     }
    136 
    137     @Config(shadows = TvShadowActivityThread.class)
    138     @Test
    139     public void testUpdateAutofillSettings_noCandiate() {
    140         final Preference autofillPref = mock(Preference.class);
    141         doReturn(autofillPref).when(mDevicePrefFragment).findPreference(
    142                 DevicePrefFragment.KEY_KEYBOARD);
    143 
    144         mDevicePrefFragment.updateKeyboardAutofillSettings();
    145 
    146         verify(autofillPref, atLeastOnce()).setTitle(R.string.system_keyboard);
    147         verify(autofillPref, never()).setTitle(R.string.system_keyboard_autofill);
    148 
    149         verify(autofillPref, never()).setSummary("com.test.AutofillPackage.MyService");
    150         verify(autofillPref, atLeastOnce()).setSummary("");
    151     }
    152 
    153     @Config(shadows = TvShadowActivityThread.class)
    154     @Test
    155     public void testUpdateAutofillSettings_selected() {
    156         final Preference autofillPref = mock(Preference.class);
    157         doReturn(autofillPref).when(mDevicePrefFragment).findPreference(
    158                 DevicePrefFragment.KEY_KEYBOARD);
    159 
    160         Utils.addAutofill("com.test.AutofillPackage", "com.test.AutofillPackage.MyService");
    161 
    162         ShadowSettings.ShadowGlobal.putString(mDevicePrefFragment.getContext().getContentResolver(),
    163                 Settings.Secure.AUTOFILL_SERVICE,
    164                 "com.test.AutofillPackage/com.test.AutofillPackage.MyService");
    165 
    166         mDevicePrefFragment.updateKeyboardAutofillSettings();
    167 
    168         verify(autofillPref, atLeastOnce()).setTitle(R.string.system_keyboard_autofill);
    169         verify(autofillPref, never()).setTitle(R.string.system_keyboard);
    170         // unfortunately we are unable to test lableRes as
    171         // 1. ShadowPackageManager did not implement getText(int textResId)
    172         // 2. Mock up serviceInfo.loadLabel() does not work either as the package manager is calling
    173         //    method in a copy of ServiceInfo.
    174         verify(autofillPref, atLeastOnce()).setSummary("com.test.AutofillPackage.MyService");
    175     }
    176 
    177     @Config(shadows = TvShadowActivityThread.class)
    178     @Test
    179     public void testUpdateAutofillSettings_selectedNone() {
    180         final Preference autofillPref = mock(Preference.class);
    181         doReturn(autofillPref).when(mDevicePrefFragment).findPreference(
    182                 DevicePrefFragment.KEY_KEYBOARD);
    183 
    184         Utils.addAutofill("com.test.AutofillPackage", "com.test.AutofillPackage.MyService");
    185 
    186         ShadowSettings.ShadowGlobal.putString(mDevicePrefFragment.getContext().getContentResolver(),
    187                 Settings.Secure.AUTOFILL_SERVICE, null);
    188 
    189         mDevicePrefFragment.updateKeyboardAutofillSettings();
    190 
    191         verify(autofillPref, atLeastOnce()).setTitle(R.string.system_keyboard_autofill);
    192         verify(autofillPref, never()).setTitle(R.string.system_keyboard);
    193 
    194         verify(autofillPref, never()).setSummary("com.test.AutofillPackage.MyService");
    195         verify(autofillPref, atLeastOnce()).setSummary("");
    196     }
    197 }
    198