1 /* 2 * Copyright (C) 2018 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.accessibility; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.junit.Assert.assertTrue; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.ContentResolver; 27 import android.content.Context; 28 import android.provider.Settings; 29 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.R; 34 import com.android.settingslib.core.lifecycle.Lifecycle; 35 import com.android.settingslib.widget.RadioButtonPreference; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class AccessibilityTimeoutControllerTest 47 implements AccessibilityTimeoutController.OnChangeListener { 48 private static final String PREF_KEY = "accessibility_control_timeout_30secs"; 49 50 private AccessibilityTimeoutController mController; 51 52 @Mock 53 private RadioButtonPreference mMockPref; 54 private Context mContext; 55 private ContentResolver mContentResolver; 56 57 @Mock 58 private PreferenceScreen mScreen; 59 60 @Before setup()61 public void setup() { 62 MockitoAnnotations.initMocks(this); 63 mContext = RuntimeEnvironment.application; 64 mController = new AccessibilityTimeoutController(mContext, mock(Lifecycle.class), PREF_KEY); 65 mController.setOnChangeListener(this); 66 mContentResolver = mContext.getContentResolver(); 67 String prefTitle = mContext.getResources().getString(R.string.accessibility_timeout_30secs); 68 69 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mMockPref); 70 when(mMockPref.getKey()).thenReturn(PREF_KEY); 71 when(mMockPref.getTitle()).thenReturn(prefTitle); 72 mController.displayPreference(mScreen); 73 } 74 75 @Override onCheckedChanged(Preference preference)76 public void onCheckedChanged(Preference preference) { 77 mController.updateState(preference); 78 } 79 80 @Test isAvailable()81 public void isAvailable() { 82 assertTrue(mController.isAvailable()); 83 } 84 85 @Test updateState_notChecked()86 public void updateState_notChecked() { 87 Settings.Secure.putString(mContentResolver, 88 Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS, "0"); 89 90 mController.updateState(mMockPref); 91 92 // the first checked state is set to false by control 93 verify(mMockPref).setChecked(false); 94 verify(mMockPref).setChecked(false); 95 } 96 97 @Test updateState_checked()98 public void updateState_checked() { 99 Settings.Secure.putString(mContentResolver, 100 Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS, "30000"); 101 102 mController.updateState(mMockPref); 103 104 // the first checked state is set to false by control 105 verify(mMockPref).setChecked(false); 106 verify(mMockPref).setChecked(true); 107 } 108 109 @Test onRadioButtonClick()110 public void onRadioButtonClick() { 111 mController.onRadioButtonClicked(mMockPref); 112 113 String accessibilityUiTimeoutValue = Settings.Secure.getString(mContentResolver, 114 Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS); 115 116 assertThat(accessibilityUiTimeoutValue).isEqualTo("30000"); 117 } 118 } 119