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.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.os.SystemProperties; 24 import android.support.v14.preference.SwitchPreference; 25 import android.support.v7.preference.PreferenceScreen; 26 import android.view.ThreadedRenderer; 27 28 import com.android.settings.testutils.SettingsRobolectricTestRunner; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RuntimeEnvironment; 36 37 @RunWith(SettingsRobolectricTestRunner.class) 38 public class HardwareLayersUpdatesPreferenceControllerTest { 39 40 @Mock 41 private SwitchPreference mPreference; 42 @Mock 43 private PreferenceScreen mPreferenceScreen; 44 45 private HardwareLayersUpdatesPreferenceController mController; 46 47 @Before 48 public void setup() { 49 MockitoAnnotations.initMocks(this); 50 mController = new HardwareLayersUpdatesPreferenceController(RuntimeEnvironment.application); 51 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 52 .thenReturn(mPreference); 53 mController.displayPreference(mPreferenceScreen); 54 } 55 56 @Test 57 public void onPreferenceChanged_settingEnabled_turnOnHardwareLayersUpdates() { 58 mController.onPreferenceChange(mPreference, true /* new value */); 59 60 final boolean mode = SystemProperties 61 .getBoolean(ThreadedRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY, false /* default */); 62 63 assertThat(mode).isTrue(); 64 } 65 66 @Test 67 public void onPreferenceChanged_settingDisabled_turnOffHardwareLayersUpdates() { 68 mController.onPreferenceChange(mPreference, false /* new value */); 69 70 final boolean mode = SystemProperties 71 .getBoolean(ThreadedRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY, false /* default */); 72 73 assertThat(mode).isFalse(); 74 } 75 76 @Test 77 public void updateState_settingEnabled_preferenceShouldBeChecked() { 78 SystemProperties 79 .set(ThreadedRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY, Boolean.toString(true)); 80 mController.updateState(mPreference); 81 82 verify(mPreference).setChecked(true); 83 } 84 85 @Test 86 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 87 SystemProperties 88 .set(ThreadedRenderer.DEBUG_SHOW_LAYERS_UPDATES_PROPERTY, Boolean.toString(false)); 89 mController.updateState(mPreference); 90 91 verify(mPreference).setChecked(false); 92 } 93 94 @Test 95 public void onDeveloperOptionsDisabled_shouldDisablePreference() { 96 mController.onDeveloperOptionsDisabled(); 97 98 verify(mPreference).setEnabled(false); 99 verify(mPreference).setChecked(false); 100 } 101 } 102