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