• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.provider.Settings;
27 import android.provider.Settings.SettingNotFoundException;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class EnableContentCapturePreferenceControllerTest {
42 
43     @Mock
44     private PreferenceScreen mScreen;
45 
46     private Context mContext;
47     private EnableContentCapturePreferenceController mController;
48     private Preference mPreference;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53         mContext = RuntimeEnvironment.application;
54         mController = new EnableContentCapturePreferenceController(mContext, "THE_KEY_TO_SUCCESS");
55         mPreference = new Preference(mContext);
56         mPreference.setKey(mController.getPreferenceKey());
57     }
58 
59     @Test
isChecked_settingIsOff_false()60     public void isChecked_settingIsOff_false() throws Exception {
61         setProperty(0);
62 
63         assertThat(mController.isChecked()).isFalse();
64     }
65 
66     @Test
isChecked_settingIsOn_true()67     public void isChecked_settingIsOn_true() throws Exception {
68         setProperty(1);
69 
70         assertThat(mController.isChecked()).isTrue();
71     }
72 
73     @Test
changePref_turnOn_shouldChangeSettingTo1()74     public void changePref_turnOn_shouldChangeSettingTo1() throws Exception {
75         setProperty(0);
76 
77         mController.onPreferenceChange(mPreference, true);
78 
79         assertThat(mController.isChecked()).isTrue();
80         assertProperty(1);
81     }
82 
83     @Test
changePref_turnOff_shouldChangeSettingTo0()84     public void changePref_turnOff_shouldChangeSettingTo0() throws Exception {
85         setProperty(1);
86 
87         mController.onPreferenceChange(mPreference, false);
88 
89         assertThat(mController.isChecked()).isFalse();
90         assertProperty(0);
91     }
92 
setProperty(int newValue)93     private void setProperty(int newValue) throws SettingNotFoundException {
94         final ContentResolver contentResolver = mContext.getContentResolver();
95         Settings.Secure.putInt(contentResolver, Settings.Secure.CONTENT_CAPTURE_ENABLED, newValue);
96     }
97 
assertProperty(int expectedValue)98     private void assertProperty(int expectedValue) throws SettingNotFoundException {
99         final ContentResolver contentResolver = mContext.getContentResolver();
100         assertThat(Settings.Secure.getInt(contentResolver, Settings.Secure.CONTENT_CAPTURE_ENABLED))
101                 .isEqualTo(expectedValue);
102     }
103 }
104