1 /*
2  * Copyright (C) 2023 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.android.settings.core.BasePreferenceController.AVAILABLE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.content.ContentResolver;
24 import android.content.Context;
25 import android.provider.Settings;
26 
27 import androidx.test.core.app.ApplicationProvider;
28 import androidx.test.ext.junit.runners.AndroidJUnit4;
29 
30 import com.android.settings.privacy.CameraExtensionsFallbackPreferenceController;
31 
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 @RunWith(AndroidJUnit4.class)
38 public class CameraExtensionsFallbackPreferenceControllerTest {
39 
40     private static final String KEY = "camera_extensions_fallback";
41     private final Context mContext = ApplicationProvider.getApplicationContext();
42 
43     private int mOriginalPreference;
44     private ContentResolver mContentResolver;
45     private CameraExtensionsFallbackPreferenceController mController;
46 
47     @Before
setUp()48     public void setUp() {
49         mContentResolver = mContext.getContentResolver();
50         mOriginalPreference = Settings.Secure.getInt(mContentResolver,
51                 Settings.Secure.CAMERA_EXTENSIONS_FALLBACK, 1);
52         mController = new CameraExtensionsFallbackPreferenceController(mContext, KEY);
53     }
54 
55     @After
tearDown()56     public void tearDown() {
57         Settings.Secure.putInt(mContentResolver, Settings.Secure.CAMERA_EXTENSIONS_FALLBACK,
58                 mOriginalPreference);
59     }
60 
61     @Test
getAvailability_returnAvailable()62     public void getAvailability_returnAvailable() {
63         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
64     }
65 
66     @Test
setChecked_disable_shouldTurnOff()67     public void setChecked_disable_shouldTurnOff() {
68         Settings.Secure.putInt(mContentResolver,
69                 Settings.Secure.CAMERA_EXTENSIONS_FALLBACK, 1);
70 
71         assertThat(mController.isChecked()).isTrue();
72 
73         mController.setChecked(false);
74 
75         assertThat(Settings.Secure.getInt(mContentResolver,
76                 Settings.Secure.CAMERA_EXTENSIONS_FALLBACK, -1)).isEqualTo(0);
77     }
78 
79     @Test
setChecked_enable_shouldTurnOn()80     public void setChecked_enable_shouldTurnOn() {
81         Settings.Secure.putInt(mContentResolver,
82                 Settings.Secure.CAMERA_EXTENSIONS_FALLBACK, 0);
83 
84         assertThat(mController.isChecked()).isFalse();
85 
86         mController.setChecked(true);
87 
88         assertThat(Settings.Secure.getInt(mContentResolver,
89                 Settings.Secure.CAMERA_EXTENSIONS_FALLBACK, -1)).isEqualTo(1);
90     }
91 }
92