1 /** 2 * Copyright (C) 2023 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 org.junit.Assert.assertTrue; 20 import static org.junit.Assert.assertFalse; 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.eq; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.os.IBinder; 30 import android.os.RemoteException; 31 import android.platform.test.flag.junit.SetFlagsRule; 32 33 import androidx.preference.PreferenceScreen; 34 import androidx.preference.TwoStatePreference; 35 36 import com.android.settings.flags.Flags; 37 import com.android.settings.testutils.shadow.ShadowParcel; 38 39 import org.junit.Before; 40 import org.junit.Rule; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.RuntimeEnvironment; 47 import org.robolectric.annotation.Config; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class ShowHdrSdrRatioPreferenceControllerTest { 51 52 @Mock 53 private Context mContext; 54 @Mock 55 private PreferenceScreen mScreen; 56 @Mock 57 private TwoStatePreference mPreference; 58 @Mock 59 private IBinder mSurfaceFlinger; 60 61 private ShowHdrSdrRatioPreferenceController mController; 62 63 @Rule 64 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 65 66 @Before setUp()67 public void setUp() { 68 MockitoAnnotations.initMocks(this); 69 mContext = RuntimeEnvironment.application; 70 mController = new ShowHdrSdrRatioPreferenceController(mContext, mSurfaceFlinger, true); 71 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 72 mController.displayPreference(mScreen); 73 } 74 75 @Test 76 @Config(shadows = ShadowParcel.class) onPreferenceChange_settingEnabled_shouldChecked()77 public void onPreferenceChange_settingEnabled_shouldChecked() throws RemoteException { 78 mSetFlagsRule.enableFlags(Flags.FLAG_DEVELOPMENT_HDR_SDR_RATIO); 79 assertTrue(mController.isAvailable()); 80 ShadowParcel.sReadBoolResult = true; 81 doReturn(true).when(mSurfaceFlinger) 82 .transact(anyInt(), any(), any(), eq(0 /* flags */)); 83 mController.onPreferenceChange(mPreference, true /* new value */); 84 verify(mPreference).setChecked(true); 85 } 86 87 @Test 88 @Config(shadows = ShadowParcel.class) onPreferenceChange_settingDisabled_shouldUnchecked()89 public void onPreferenceChange_settingDisabled_shouldUnchecked() throws RemoteException { 90 mSetFlagsRule.enableFlags(Flags.FLAG_DEVELOPMENT_HDR_SDR_RATIO); 91 assertTrue(mController.isAvailable()); 92 ShadowParcel.sReadBoolResult = false; 93 doReturn(true).when(mSurfaceFlinger) 94 .transact(anyInt(), any(), any(), eq(0 /* flags */)); 95 mController.onPreferenceChange(mPreference, false /* new value */); 96 verify(mPreference).setChecked(false); 97 } 98 99 @Test 100 @Config(shadows = ShadowParcel.class) updateState_settingEnabled_shouldChecked()101 public void updateState_settingEnabled_shouldChecked() throws RemoteException { 102 mSetFlagsRule.enableFlags(Flags.FLAG_DEVELOPMENT_HDR_SDR_RATIO); 103 assertTrue(mController.isAvailable()); 104 ShadowParcel.sReadBoolResult = true; 105 doReturn(true).when(mSurfaceFlinger) 106 .transact(anyInt(), any(), any(), eq(0 /* flags */)); 107 mController.updateState(mPreference); 108 verify(mPreference).setChecked(true); 109 } 110 111 @Test 112 @Config(shadows = ShadowParcel.class) updateState_settingDisabled_shouldUnchecked()113 public void updateState_settingDisabled_shouldUnchecked() throws RemoteException { 114 mSetFlagsRule.enableFlags(Flags.FLAG_DEVELOPMENT_HDR_SDR_RATIO); 115 assertTrue(mController.isAvailable()); 116 ShadowParcel.sReadBoolResult = false; 117 doReturn(true).when(mSurfaceFlinger) 118 .transact(anyInt(), any(), any(), eq(0 /* flags */)); 119 mController.updateState(mPreference); 120 verify(mPreference).setChecked(false); 121 } 122 123 @Test settingNotAvailable_isHdrSdrRatioAvailableFalse_flagsOff()124 public void settingNotAvailable_isHdrSdrRatioAvailableFalse_flagsOff() { 125 mSetFlagsRule.disableFlags(Flags.FLAG_DEVELOPMENT_HDR_SDR_RATIO); 126 mController = new ShowHdrSdrRatioPreferenceController(mContext, mSurfaceFlinger, true); 127 assertFalse(mController.isAvailable()); 128 } 129 130 @Test settingNotAvailable_isHdrSdrRatioAvailableTrue_flagsOn()131 public void settingNotAvailable_isHdrSdrRatioAvailableTrue_flagsOn() { 132 mSetFlagsRule.enableFlags(Flags.FLAG_DEVELOPMENT_HDR_SDR_RATIO); 133 mController = new ShowHdrSdrRatioPreferenceController(mContext, mSurfaceFlinger, false); 134 assertFalse(mController.isAvailable()); 135 } 136 137 @Test 138 @Config(shadows = ShadowParcel.class) onDeveloperOptionsSwitchDisabled_preferenceUnchecked_shouldNotTurnOffPreference()139 public void onDeveloperOptionsSwitchDisabled_preferenceUnchecked_shouldNotTurnOffPreference() 140 throws RemoteException { 141 mSetFlagsRule.enableFlags(Flags.FLAG_DEVELOPMENT_HDR_SDR_RATIO); 142 assertTrue(mController.isAvailable()); 143 ShadowParcel.sReadBoolResult = false; 144 doReturn(true).when(mSurfaceFlinger) 145 .transact(anyInt(), any(), any(), eq(0 /* flags */)); 146 when(mPreference.isChecked()).thenReturn(false); 147 mController.onDeveloperOptionsSwitchDisabled(); 148 149 mController.writeShowHdrSdrRatioSetting(true); 150 verify(mPreference).setChecked(false); 151 verify(mPreference).setEnabled(false); 152 } 153 154 @Test 155 @Config(shadows = ShadowParcel.class) onDeveloperOptionsSwitchDisabled_preferenceChecked_shouldTurnOffPreference()156 public void onDeveloperOptionsSwitchDisabled_preferenceChecked_shouldTurnOffPreference() 157 throws RemoteException { 158 mSetFlagsRule.enableFlags(Flags.FLAG_DEVELOPMENT_HDR_SDR_RATIO); 159 assertTrue(mController.isAvailable()); 160 ShadowParcel.sReadBoolResult = true; 161 doReturn(true).when(mSurfaceFlinger) 162 .transact(anyInt(), any(), any(), eq(0 /* flags */)); 163 when(mPreference.isChecked()).thenReturn(true); 164 mController.onDeveloperOptionsSwitchDisabled(); 165 166 mController.writeShowHdrSdrRatioSetting(false); 167 verify(mPreference).setChecked(false); 168 verify(mPreference).setEnabled(false); 169 } 170 } 171 172