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