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