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.HardwareOverlaysPreferenceController 20 .SURFACE_FLINGER_READ_CODE; 21 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.ArgumentMatchers.anyBoolean; 24 import static org.mockito.ArgumentMatchers.eq; 25 import static org.mockito.Mockito.doNothing; 26 import static org.mockito.Mockito.doReturn; 27 import static org.mockito.Mockito.never; 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.os.IBinder; 34 import android.os.RemoteException; 35 36 import androidx.preference.PreferenceScreen; 37 import androidx.preference.SwitchPreference; 38 39 import com.android.settings.testutils.shadow.ShadowParcel; 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.annotation.Config; 48 import org.robolectric.util.ReflectionHelpers; 49 50 @RunWith(RobolectricTestRunner.class) 51 public class HardwareOverlaysPreferenceControllerTest { 52 53 @Mock 54 private Context mContext; 55 @Mock 56 private PreferenceScreen mScreen; 57 @Mock 58 private SwitchPreference mPreference; 59 @Mock 60 private IBinder mSurfaceFlinger; 61 62 private HardwareOverlaysPreferenceController mController; 63 64 @Before setUp()65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 mController = spy(new HardwareOverlaysPreferenceController(mContext)); 68 ReflectionHelpers.setField(mController, "mSurfaceFlinger", mSurfaceFlinger); 69 doNothing().when(mController).writeHardwareOverlaysSetting(anyBoolean()); 70 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 71 mController.displayPreference(mScreen); 72 } 73 74 @Test onPreferenceChange_settingToggledOn_shouldWriteTrueToHardwareOverlaysSetting()75 public void onPreferenceChange_settingToggledOn_shouldWriteTrueToHardwareOverlaysSetting() { 76 mController.onPreferenceChange(mPreference, true /* new value */); 77 78 verify(mController).writeHardwareOverlaysSetting(true); 79 } 80 81 @Test onPreferenceChange_settingToggledOff_shouldWriteFalseToHardwareOverlaysSetting()82 public void onPreferenceChange_settingToggledOff_shouldWriteFalseToHardwareOverlaysSetting() { 83 mController.onPreferenceChange(mPreference, false /* new value */); 84 85 verify(mController).writeHardwareOverlaysSetting(false); 86 } 87 88 @Test 89 @Config(shadows = {ShadowParcel.class}) updateState_settingEnabled_shouldCheckPreference()90 public void updateState_settingEnabled_shouldCheckPreference() throws RemoteException { 91 ShadowParcel.sReadIntResult = 1; 92 doReturn(true).when(mSurfaceFlinger) 93 .transact(eq(SURFACE_FLINGER_READ_CODE), any(), any(), eq(0 /* flags */)); 94 mController.updateState(mPreference); 95 96 verify(mPreference).setChecked(true); 97 } 98 99 @Test 100 @Config(shadows = {ShadowParcel.class}) updateState_settingDisabled_shouldUnCheckPreference()101 public void updateState_settingDisabled_shouldUnCheckPreference() throws RemoteException { 102 ShadowParcel.sReadIntResult = 0; 103 doReturn(true).when(mSurfaceFlinger) 104 .transact(eq(SURFACE_FLINGER_READ_CODE), any(), any(), eq(0 /* flags */)); 105 mController.updateState(mPreference); 106 107 verify(mPreference).setChecked(false); 108 } 109 110 @Test onDeveloperOptionsSwitchDisabled_preferenceChecked_shouldTurnOffPreference()111 public void onDeveloperOptionsSwitchDisabled_preferenceChecked_shouldTurnOffPreference() { 112 when(mPreference.isChecked()).thenReturn(true); 113 mController.onDeveloperOptionsSwitchDisabled(); 114 115 verify(mController).writeHardwareOverlaysSetting(false); 116 verify(mPreference).setChecked(false); 117 verify(mPreference).setEnabled(false); 118 } 119 120 @Test onDeveloperOptionsSwitchDisabled_preferenceUnchecked_shouldNotTurnOffPreference()121 public void onDeveloperOptionsSwitchDisabled_preferenceUnchecked_shouldNotTurnOffPreference() { 122 when(mPreference.isChecked()).thenReturn(false); 123 mController.onDeveloperOptionsSwitchDisabled(); 124 125 verify(mController, never()).writeHardwareOverlaysSetting(anyBoolean()); 126 verify(mPreference, never()).setChecked(anyBoolean()); 127 verify(mPreference).setEnabled(false); 128 } 129 } 130