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