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.display; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.provider.Settings; 24 25 import androidx.preference.Preference; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.mockito.Mock; 31 import org.mockito.MockitoAnnotations; 32 import org.robolectric.RobolectricTestRunner; 33 import org.robolectric.RuntimeEnvironment; 34 35 @RunWith(RobolectricTestRunner.class) 36 public class LockscreenClockPreferenceControllerTest { 37 38 private static final String TEST_KEY = "test_key"; 39 private static final String SETTING_KEY = Settings.Secure.LOCKSCREEN_USE_DOUBLE_LINE_CLOCK; 40 41 private Context mContext; 42 private ContentResolver mContentResolver; 43 private LockscreenClockPreferenceController mController; 44 45 @Mock 46 private Preference mPreference; 47 48 @Before setUp()49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 mContext = RuntimeEnvironment.application; 52 mContentResolver = mContext.getContentResolver(); 53 mController = new LockscreenClockPreferenceController(mContext, TEST_KEY); 54 } 55 56 @Test isChecked_SettingIs1_returnTrue()57 public void isChecked_SettingIs1_returnTrue() { 58 Settings.Secure.putInt(mContentResolver, SETTING_KEY, 1); 59 60 assertThat(mController.isChecked()).isTrue(); 61 } 62 63 @Test isChecked_SettingIs0_returnFalse()64 public void isChecked_SettingIs0_returnFalse() { 65 Settings.Secure.putInt(mContentResolver, SETTING_KEY, 0); 66 67 assertThat(mController.isChecked()).isFalse(); 68 } 69 70 @Test isChecked_SettingIsNotSet_returnTrue()71 public void isChecked_SettingIsNotSet_returnTrue() { 72 Settings.Secure.putString(mContentResolver, SETTING_KEY, null); 73 74 assertThat(mController.isChecked()).isTrue(); 75 } 76 77 @Test setChecked_true_SettingIsNot0()78 public void setChecked_true_SettingIsNot0() { 79 mController.setChecked(true); 80 81 assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isNotEqualTo(0); 82 } 83 84 @Test setChecked_false_SettingIs0()85 public void setChecked_false_SettingIs0() { 86 mController.setChecked(false); 87 88 assertThat(Settings.Secure.getInt(mContentResolver, SETTING_KEY, 0)).isEqualTo(0); 89 } 90 } 91