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