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 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.os.SystemProperties; 26 import android.view.ThreadedRenderer; 27 28 import androidx.preference.ListPreference; 29 import androidx.preference.PreferenceScreen; 30 31 import com.android.settings.R; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.RuntimeEnvironment; 40 41 @RunWith(RobolectricTestRunner.class) 42 public class DebugGpuOverdrawPreferenceControllerTest { 43 44 @Mock 45 private ListPreference mPreference; 46 @Mock 47 private PreferenceScreen mScreen; 48 49 /** 50 * 0: off 51 * 1: Show overdraw areas 52 * 2: Show areas for Deuteranomaly 53 */ 54 private String[] mListValues; 55 private String[] mListSummaries; 56 private Context mContext; 57 private DebugGpuOverdrawPreferenceController mController; 58 59 @Before setup()60 public void setup() { 61 MockitoAnnotations.initMocks(this); 62 mContext = RuntimeEnvironment.application; 63 mListValues = mContext.getResources().getStringArray(R.array.debug_hw_overdraw_values); 64 mListSummaries = mContext.getResources().getStringArray( 65 R.array.debug_hw_overdraw_entries); 66 mController = new DebugGpuOverdrawPreferenceController(mContext); 67 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 68 mController.displayPreference(mScreen); 69 } 70 71 @Test onPreferenceChange_noValueSet_shouldSetEmptyString()72 public void onPreferenceChange_noValueSet_shouldSetEmptyString() { 73 mController.onPreferenceChange(mPreference, null /* new value */); 74 75 String mode = SystemProperties.get(ThreadedRenderer.DEBUG_OVERDRAW_PROPERTY); 76 assertThat(mode).isEqualTo(""); 77 } 78 79 @Test onPreferenceChange_option1Selected_shouldSetOption1()80 public void onPreferenceChange_option1Selected_shouldSetOption1() { 81 mController.onPreferenceChange(mPreference, mListValues[1]); 82 83 String mode = SystemProperties.get(ThreadedRenderer.DEBUG_OVERDRAW_PROPERTY); 84 assertThat(mode).isEqualTo(mListValues[1]); 85 } 86 87 @Test updateState_option1Set_shouldUpdatePreferenceToOption1()88 public void updateState_option1Set_shouldUpdatePreferenceToOption1() { 89 SystemProperties.set(ThreadedRenderer.DEBUG_OVERDRAW_PROPERTY, mListValues[1]); 90 91 mController.updateState(mPreference); 92 93 verify(mPreference).setValue(mListValues[1]); 94 verify(mPreference).setSummary(mListSummaries[1]); 95 } 96 97 @Test updateState_option2Set_shouldUpdatePreferenceToOption2()98 public void updateState_option2Set_shouldUpdatePreferenceToOption2() { 99 SystemProperties.set(ThreadedRenderer.DEBUG_OVERDRAW_PROPERTY, mListValues[2]); 100 101 mController.updateState(mPreference); 102 103 verify(mPreference).setValue(mListValues[2]); 104 verify(mPreference).setSummary(mListSummaries[2]); 105 } 106 107 @Test updateState_noOptionSet_shouldDefaultToOption0()108 public void updateState_noOptionSet_shouldDefaultToOption0() { 109 SystemProperties.set(ThreadedRenderer.DEBUG_OVERDRAW_PROPERTY, null); 110 111 mController.updateState(mPreference); 112 113 verify(mPreference).setValue(mListValues[0]); 114 verify(mPreference).setSummary(mListSummaries[0]); 115 } 116 } 117