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.Mockito.doReturn; 27 import static org.mockito.Mockito.spy; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.when; 30 31 import android.content.Context; 32 import android.provider.Settings; 33 34 import androidx.preference.PreferenceScreen; 35 import androidx.preference.SwitchPreference; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class DesktopModePreferenceControllerTest { 47 48 private static final String ENG_BUILD_TYPE = "eng"; 49 private static final String USER_BUILD_TYPE = "user"; 50 51 @Mock 52 private SwitchPreference mPreference; 53 @Mock 54 private PreferenceScreen mScreen; 55 56 private Context mContext; 57 private DesktopModePreferenceController mController; 58 59 @Before setup()60 public void setup() { 61 MockitoAnnotations.initMocks(this); 62 mContext = RuntimeEnvironment.application; 63 mController = new DesktopModePreferenceController(mContext); 64 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 65 mController.displayPreference(mScreen); 66 } 67 68 @Test isAvailable_engBuild_shouldBeTrue()69 public void isAvailable_engBuild_shouldBeTrue() { 70 mController = spy(mController); 71 doReturn(ENG_BUILD_TYPE).when(mController).getBuildType(); 72 73 assertThat(mController.isAvailable()).isTrue(); 74 } 75 76 @Test isAvaiable_userBuild_shouldBeTrue()77 public void isAvaiable_userBuild_shouldBeTrue() { 78 mController = spy(mController); 79 doReturn(USER_BUILD_TYPE).when(mController).getBuildType(); 80 81 assertThat(mController.isAvailable()).isTrue(); 82 } 83 84 @Test onPreferenceChange_switchEnabled_shouldEnableFreeformWindows()85 public void onPreferenceChange_switchEnabled_shouldEnableFreeformWindows() { 86 mController.onPreferenceChange(mPreference, true /* new value */); 87 88 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 89 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, -1 /* default */); 90 assertThat(mode).isEqualTo(SETTING_VALUE_ON); 91 } 92 93 @Test onPreferenceChange_switchDisabled_shouldDisableFreeformWindows()94 public void onPreferenceChange_switchDisabled_shouldDisableFreeformWindows() { 95 mController.onPreferenceChange(mPreference, false /* new value */); 96 97 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 98 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, -1 /* default */); 99 assertThat(mode).isEqualTo(SETTING_VALUE_OFF); 100 } 101 102 @Test updateState_settingEnabled_preferenceShouldBeChecked()103 public void updateState_settingEnabled_preferenceShouldBeChecked() { 104 Settings.Global.putInt(mContext.getContentResolver(), 105 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, SETTING_VALUE_ON); 106 107 mController.updateState(mPreference); 108 109 verify(mPreference).setChecked(true); 110 } 111 112 @Test updateState_settingDisabled_preferenceShouldNotBeChecked()113 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 114 Settings.Global.putInt(mContext.getContentResolver(), 115 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, SETTING_VALUE_OFF); 116 117 mController.updateState(mPreference); 118 119 verify(mPreference).setChecked(false); 120 } 121 122 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()123 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { 124 mController.onDeveloperOptionsSwitchDisabled(); 125 126 final int mode = Settings.Global.getInt(mContext.getContentResolver(), 127 DEVELOPMENT_FORCE_DESKTOP_MODE_ON_EXTERNAL_DISPLAYS, -1 /* default */); 128 assertThat(mode).isEqualTo(SETTING_VALUE_OFF); 129 verify(mPreference).setEnabled(false); 130 } 131 } 132