GpuViewUpdatesPreferenceControllerTest.java revision 4fbf04cd1056928d1f2ef3c648b11112f80915de
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 17package com.android.settings.development; 18 19import static com.google.common.truth.Truth.assertThat; 20 21import static org.mockito.Mockito.verify; 22import static org.mockito.Mockito.when; 23 24import android.content.Context; 25import android.support.v14.preference.SwitchPreference; 26import android.support.v7.preference.PreferenceScreen; 27import android.view.ThreadedRenderer; 28 29import com.android.settings.TestConfig; 30import com.android.settings.testutils.SettingsRobolectricTestRunner; 31import com.android.settings.testutils.shadow.SettingsShadowSystemProperties; 32 33import org.junit.After; 34import org.junit.Before; 35import org.junit.Test; 36import org.junit.runner.RunWith; 37import org.mockito.Mock; 38import org.mockito.MockitoAnnotations; 39import org.robolectric.RuntimeEnvironment; 40import org.robolectric.annotation.Config; 41 42@RunWith(SettingsRobolectricTestRunner.class) 43@Config(manifest = TestConfig.MANIFEST_PATH, 44 sdk = TestConfig.SDK_VERSION, 45 shadows = {SettingsShadowSystemProperties.class}) 46public class GpuViewUpdatesPreferenceControllerTest { 47 48 @Mock 49 private SwitchPreference mPreference; 50 @Mock 51 private PreferenceScreen mPreferenceScreen; 52 53 private Context mContext; 54 private GpuViewUpdatesPreferenceController mController; 55 56 @Before 57 public void setup() { 58 MockitoAnnotations.initMocks(this); 59 mContext = RuntimeEnvironment.application; 60 mController = new GpuViewUpdatesPreferenceController(mContext); 61 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( 62 mPreference); 63 mController.displayPreference(mPreferenceScreen); 64 } 65 66 @After 67 public void tearDown() { 68 SettingsShadowSystemProperties.clear(); 69 } 70 71 @Test 72 public void onPreferenceChanged_settingEnabled_turnOnGpuViewUpdates() { 73 mController.onPreferenceChange(mPreference, true /* new value */); 74 75 final boolean mode = SettingsShadowSystemProperties.getBoolean( 76 ThreadedRenderer.DEBUG_DIRTY_REGIONS_PROPERTY, false /* default */); 77 78 assertThat(mode).isTrue(); 79 } 80 81 @Test 82 public void onPreferenceChanged_settingDisabled_turnOffGpuViewUpdates() { 83 mController.onPreferenceChange(mPreference, false /* new value */); 84 85 final boolean mode = SettingsShadowSystemProperties.getBoolean( 86 ThreadedRenderer.DEBUG_DIRTY_REGIONS_PROPERTY, false /* default */); 87 88 assertThat(mode).isFalse(); 89 } 90 91 @Test 92 public void updateState_settingEnabled_preferenceShouldBeChecked() { 93 SettingsShadowSystemProperties.set(ThreadedRenderer.DEBUG_DIRTY_REGIONS_PROPERTY, 94 Boolean.toString(true)); 95 mController.updateState(mPreference); 96 97 verify(mPreference).setChecked(true); 98 } 99 100 @Test 101 public void updateState_settingDisabled_preferenceShouldNotBeChecked() { 102 SettingsShadowSystemProperties.set(ThreadedRenderer.DEBUG_DIRTY_REGIONS_PROPERTY, 103 Boolean.toString(false)); 104 mController.updateState(mPreference); 105 106 verify(mPreference).setChecked(false); 107 } 108 109 @Test 110 public void onDeveloperOptionsDisabled_shouldDisablePreference() { 111 mController.onDeveloperOptionsDisabled(); 112 113 verify(mPreference).setEnabled(false); 114 verify(mPreference).setChecked(false); 115 } 116} 117