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