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.development; 18 19 import static android.provider.Settings.Global.DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS; 20 21 import static com.android.settings.development.DesktopModePreferenceController.SETTING_VALUE_OFF; 22 import static com.android.settings.development.DesktopModePreferenceController.SETTING_VALUE_ON; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import static org.mockito.ArgumentMatchers.any; 27 import static org.mockito.Mockito.doReturn; 28 import static org.mockito.Mockito.spy; 29 import static org.mockito.Mockito.verify; 30 import static org.mockito.Mockito.when; 31 32 import android.content.Context; 33 import android.provider.Settings; 34 35 import androidx.fragment.app.FragmentActivity; 36 import androidx.fragment.app.FragmentManager; 37 import androidx.fragment.app.FragmentTransaction; 38 import androidx.preference.PreferenceScreen; 39 import androidx.preference.SwitchPreference; 40 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.RuntimeEnvironment; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class DesktopModePreferenceControllerTest { 51 52 private static final String ENG_BUILD_TYPE = "eng"; 53 private static final String USER_BUILD_TYPE = "user"; 54 55 @Mock 56 private SwitchPreference mPreference; 57 @Mock 58 private PreferenceScreen mScreen; 59 @Mock 60 private DevelopmentSettingsDashboardFragment mFragment; 61 @Mock 62 private FragmentActivity mActivity; 63 @Mock 64 private FragmentManager mFragmentManager; 65 @Mock 66 private FragmentTransaction mTransaction; 67 68 private Context mContext; 69 private DesktopModePreferenceController mController; 70 71 @Before setup()72 public void setup() { 73 MockitoAnnotations.initMocks(this); 74 mContext = RuntimeEnvironment.application; 75 doReturn(mTransaction).when(mFragmentManager).beginTransaction(); 76 doReturn(mFragmentManager).when(mActivity).getSupportFragmentManager(); 77 doReturn(mActivity).when(mFragment).getActivity(); 78 mController = new DesktopModePreferenceController(mContext, mFragment); 79 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 80 mController.displayPreference(mScreen); 81 } 82 83 @Test isAvailable_engBuild_shouldBeTrue()84 public void isAvailable_engBuild_shouldBeTrue() { 85 mController = spy(mController); 86 doReturn(ENG_BUILD_TYPE).when(mController).getBuildType(); 87 88 assertThat(mController.isAvailable()).isTrue(); 89 } 90 91 @Test isAvaiable_userBuild_shouldBeTrue()92 public void isAvaiable_userBuild_shouldBeTrue() { 93 mController = spy(mController); 94 doReturn(USER_BUILD_TYPE).when(mController).getBuildType(); 95 96 assertThat(mController.isAvailable()).isTrue(); 97 } 98 99 @Test onPreferenceChange_switchEnabled_shouldEnableDesktopMode()100 public void onPreferenceChange_switchEnabled_shouldEnableDesktopMode() { 101 mController.onPreferenceChange(mPreference, true /* new value */); 102 103 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 104 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, -1 /* default */); 105 assertThat(mode).isEqualTo(SETTING_VALUE_ON); 106 107 verify(mTransaction).add(any(RebootConfirmationDialogFragment.class), any()); 108 } 109 110 @Test onPreferenceChange_switchDisabled_shouldDisableDesktopMode()111 public void onPreferenceChange_switchDisabled_shouldDisableDesktopMode() { 112 mController.onPreferenceChange(mPreference, false /* new value */); 113 114 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 115 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, -1 /* default */); 116 assertThat(mode).isEqualTo(SETTING_VALUE_OFF); 117 } 118 119 @Test updateState_settingEnabled_preferenceShouldBeChecked()120 public void updateState_settingEnabled_preferenceShouldBeChecked() { 121 Settings.Global.putInt(mContext.getContentResolver(), 122 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, SETTING_VALUE_ON); 123 124 mController.updateState(mPreference); 125 126 verify(mPreference).setChecked(true); 127 } 128 129 @Test updateState_settingDisabled_preferenceShouldNotBeChecked()130 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 131 Settings.Global.putInt(mContext.getContentResolver(), 132 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, SETTING_VALUE_OFF); 133 134 mController.updateState(mPreference); 135 136 verify(mPreference).setChecked(false); 137 } 138 139 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()140 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { 141 mController.onDeveloperOptionsSwitchDisabled(); 142 143 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 144 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, -1 /* default */); 145 assertThat(mode).isEqualTo(SETTING_VALUE_OFF); 146 verify(mPreference).setEnabled(false); 147 } 148 } 149