1 /* 2 * Copyright (C) 2021 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.privacy; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.content.ClipboardManager; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.provider.DeviceConfig; 25 import android.provider.Settings; 26 import android.provider.Settings.SettingNotFoundException; 27 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceScreen; 30 import androidx.test.core.app.ApplicationProvider; 31 32 import com.android.settings.testutils.shadow.ShadowDeviceConfig; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.annotation.Config; 41 42 @RunWith(RobolectricTestRunner.class) 43 @Config(shadows = {ShadowDeviceConfig.class}) 44 public class ShowClipAccessNotificationPreferenceControllerTest { 45 46 @Mock 47 private PreferenceScreen mScreen; 48 49 private Context mContext; 50 private ShowClipAccessNotificationPreferenceController mController; 51 private Preference mPreference; 52 53 @Before setUp()54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 mContext = ApplicationProvider.getApplicationContext(); 57 mController = new ShowClipAccessNotificationPreferenceController(mContext); 58 mPreference = new Preference(mContext); 59 mPreference.setKey(mController.getPreferenceKey()); 60 } 61 62 @Test isChecked_settingIsOff_shouldReturnFalse()63 public void isChecked_settingIsOff_shouldReturnFalse() { 64 setProperty(0); 65 66 assertThat(mController.isChecked()).isFalse(); 67 } 68 69 @Test isChecked_settingIsOn_shouldReturnTrue()70 public void isChecked_settingIsOn_shouldReturnTrue() { 71 setProperty(1); 72 73 assertThat(mController.isChecked()).isTrue(); 74 } 75 76 @Test isChecked_settingIsUnset_deviceConfigProvidesDefaultOfTrue()77 public void isChecked_settingIsUnset_deviceConfigProvidesDefaultOfTrue() { 78 clearProperty(); 79 80 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_CLIPBOARD, 81 ClipboardManager.DEVICE_CONFIG_SHOW_ACCESS_NOTIFICATIONS, 82 "true", false); 83 84 ShowClipAccessNotificationPreferenceController controller = 85 new ShowClipAccessNotificationPreferenceController(mContext); 86 87 assertThat(controller.isChecked()).isTrue(); 88 } 89 90 @Test isChecked_settingIsUnset_deviceConfigProvidesDefaultOfFalse()91 public void isChecked_settingIsUnset_deviceConfigProvidesDefaultOfFalse() { 92 clearProperty(); 93 94 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_CLIPBOARD, 95 ClipboardManager.DEVICE_CONFIG_SHOW_ACCESS_NOTIFICATIONS, 96 "false", false); 97 98 ShowClipAccessNotificationPreferenceController controller = 99 new ShowClipAccessNotificationPreferenceController(mContext); 100 101 assertThat(controller.isChecked()).isFalse(); 102 } 103 104 @Test onPreferenceChange_turnOn_shouldChangeSettingTo1()105 public void onPreferenceChange_turnOn_shouldChangeSettingTo1() throws Exception { 106 setProperty(0); 107 108 mController.onPreferenceChange(mPreference, true); 109 110 assertThat(mController.isChecked()).isTrue(); 111 assertProperty(1); 112 } 113 114 @Test onPreferenceChange_turnOff_shouldChangeSettingTo0()115 public void onPreferenceChange_turnOff_shouldChangeSettingTo0() throws Exception { 116 setProperty(1); 117 118 mController.onPreferenceChange(mPreference, false); 119 120 assertThat(mController.isChecked()).isFalse(); 121 assertProperty(0); 122 } 123 setProperty(int newValue)124 private void setProperty(int newValue) { 125 final ContentResolver contentResolver = mContext.getContentResolver(); 126 Settings.Secure.putInt( 127 contentResolver, Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, newValue); 128 } 129 clearProperty()130 private void clearProperty() { 131 final ContentResolver contentResolver = mContext.getContentResolver(); 132 Settings.Secure.putString( 133 contentResolver, Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS, null); 134 } 135 assertProperty(int expectedValue)136 private void assertProperty(int expectedValue) throws SettingNotFoundException { 137 final ContentResolver contentResolver = mContext.getContentResolver(); 138 assertThat(Settings.Secure.getInt( 139 contentResolver, Settings.Secure.CLIPBOARD_SHOW_ACCESS_NOTIFICATIONS)) 140 .isEqualTo(expectedValue); 141 } 142 } 143