1 /* 2 * Copyright (C) 2020 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.display; 18 19 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 21 import static com.android.settings.display.PeakRefreshRatePreferenceController.DEFAULT_REFRESH_RATE; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import android.content.Context; 26 import android.provider.Settings; 27 28 import androidx.preference.SwitchPreference; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.robolectric.RobolectricTestRunner; 34 import org.robolectric.RuntimeEnvironment; 35 import org.robolectric.annotation.Config; 36 37 @RunWith(RobolectricTestRunner.class) 38 public class PeakRefreshRatePreferenceControllerTest { 39 40 private Context mContext; 41 private PeakRefreshRatePreferenceController mController; 42 private SwitchPreference mPreference; 43 44 @Before setUp()45 public void setUp() { 46 mContext = RuntimeEnvironment.application; 47 mController = new PeakRefreshRatePreferenceController(mContext, "key"); 48 mPreference = new SwitchPreference(RuntimeEnvironment.application); 49 } 50 51 @Test 52 @Config(qualifiers = "mcc999") getAvailabilityStatus_withConfigNoShow_returnUnsupported()53 public void getAvailabilityStatus_withConfigNoShow_returnUnsupported() { 54 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 55 } 56 57 @Test getAvailabilityStatus_refreshRateLargerThanDefault_returnAvailable()58 public void getAvailabilityStatus_refreshRateLargerThanDefault_returnAvailable() { 59 mController.mPeakRefreshRate = DEFAULT_REFRESH_RATE + 1; 60 61 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 62 } 63 64 @Test getAvailabilityStatus_refreshRateEqualToDefault_returnUnsupported()65 public void getAvailabilityStatus_refreshRateEqualToDefault_returnUnsupported() { 66 mController.mPeakRefreshRate = DEFAULT_REFRESH_RATE; 67 68 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 69 } 70 71 @Test setChecked_enableSmoothDisplay_setCurrentRefreshRate()72 public void setChecked_enableSmoothDisplay_setCurrentRefreshRate() { 73 mController.mPeakRefreshRate = 88f; 74 mController.setChecked(true); 75 76 assertThat(Settings.System.getFloat(mContext.getContentResolver(), 77 Settings.System.PEAK_REFRESH_RATE, DEFAULT_REFRESH_RATE)) 78 .isEqualTo(88.0f); 79 } 80 81 @Test setChecked_disableSmoothDisplay_setDefaultRefreshRate()82 public void setChecked_disableSmoothDisplay_setDefaultRefreshRate() { 83 mController.mPeakRefreshRate = 88f; 84 mController.setChecked(false); 85 86 assertThat(Settings.System.getFloat(mContext.getContentResolver(), 87 Settings.System.PEAK_REFRESH_RATE, DEFAULT_REFRESH_RATE)) 88 .isEqualTo(DEFAULT_REFRESH_RATE); 89 } 90 91 @Test isChecked_enableSmoothDisplay_returnTrue()92 public void isChecked_enableSmoothDisplay_returnTrue() { 93 enableSmoothDisplayPreference(); 94 95 assertThat(mController.isChecked()).isTrue(); 96 } 97 98 @Test isChecked_disableSmoothDisplay_returnFalse()99 public void isChecked_disableSmoothDisplay_returnFalse() { 100 disableSmoothDisplayPreference(); 101 102 assertThat(mController.isChecked()).isFalse(); 103 } 104 enableSmoothDisplayPreference()105 private void enableSmoothDisplayPreference() { 106 mController.mPeakRefreshRate = 88f; 107 108 Settings.System.putFloat( 109 mContext.getContentResolver(), 110 Settings.System.PEAK_REFRESH_RATE, 111 mController.mPeakRefreshRate); 112 } 113 disableSmoothDisplayPreference()114 private void disableSmoothDisplayPreference() { 115 mController.mPeakRefreshRate = 88f; 116 117 Settings.System.putFloat( 118 mContext.getContentResolver(), 119 Settings.System.PEAK_REFRESH_RATE, 120 DEFAULT_REFRESH_RATE); 121 } 122 } 123