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 android.content.pm.PackageManager.FEATURE_AUTOMOTIVE;
20 import static android.content.pm.PackageManager.FEATURE_LEANBACK;
21 import static android.content.pm.PackageManager.FEATURE_WATCH;
22 
23 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
24 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.doReturn;
30 
31 import android.content.Context;
32 import android.content.pm.PackageManager;
33 import android.provider.DeviceConfig;
34 import android.provider.Settings;
35 import android.safetylabel.SafetyLabelConstants;
36 
37 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
38 
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoSession;
46 import org.mockito.quality.Strictness;
47 import org.robolectric.RobolectricTestRunner;
48 import org.robolectric.annotation.Config;
49 
50 import java.util.Arrays;
51 import java.util.List;
52 
53 @RunWith(RobolectricTestRunner.class)
54 @Config(shadows = {ShadowDeviceConfig.class})
55 public class AppDataSharingUpdatesPreferenceControllerTest {
56 
57     public static final String PREFERENCE_KEY = "PREFERENCE_KEY";
58     private static final List<String> sUnsupportedFormFactors =
59             Arrays.asList(FEATURE_AUTOMOTIVE, FEATURE_LEANBACK, FEATURE_WATCH);
60     private MockitoSession mMockitoSession;
61     @Mock
62     private PackageManager mPackageManager;
63     @Mock
64     private Context mContext;
65     private AppDataSharingUpdatesPreferenceController mController;
66 
67     @Before
setUp()68     public void setUp() {
69         mMockitoSession = Mockito.mockitoSession()
70                 .initMocks(this)
71                 .strictness(Strictness.WARN)
72                 .startMocking();
73         doReturn(mPackageManager).when(mContext).getPackageManager();
74         mController = new AppDataSharingUpdatesPreferenceController(mContext, PREFERENCE_KEY);
75         for (String formFactor : sUnsupportedFormFactors) {
76             doReturn(false).when(mPackageManager).hasSystemFeature(eq(formFactor));
77         }
78     }
79 
80     @After
tearDown()81     public void tearDown() {
82         mMockitoSession.finishMocking();
83     }
84 
85     @Test
whenSafetyLabelsDisabled_thenPreferenceUnavailable()86     public void whenSafetyLabelsDisabled_thenPreferenceUnavailable()
87             throws Exception {
88         setSafetyLabelsDeviceConfigEnabled(false);
89         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
90     }
91 
92     @Test
whenSafetyLabelsEnabled_andSupportedFormFactor_thenPreferenceAvailable()93     public void whenSafetyLabelsEnabled_andSupportedFormFactor_thenPreferenceAvailable()
94             throws Exception {
95         setSafetyLabelsDeviceConfigEnabled(true);
96         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
97     }
98 
99     @Test
whenSafetyLabelsEnabled_andUnsupportedFormFactor_thenPreferenceUnavailable()100     public void whenSafetyLabelsEnabled_andUnsupportedFormFactor_thenPreferenceUnavailable()
101             throws Exception {
102         setSafetyLabelsDeviceConfigEnabled(true);
103         for (String formFactor : sUnsupportedFormFactors) {
104             doReturn(true).when(mPackageManager).hasSystemFeature(eq(formFactor));
105             assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
106             doReturn(false).when(mPackageManager).hasSystemFeature(eq(formFactor));
107         }
108     }
109 
setSafetyLabelsDeviceConfigEnabled(boolean newValue)110     private void setSafetyLabelsDeviceConfigEnabled(boolean newValue)
111             throws Settings.SettingNotFoundException {
112         DeviceConfig.setProperty(DeviceConfig.NAMESPACE_PRIVACY,
113                 SafetyLabelConstants.SAFETY_LABEL_CHANGE_NOTIFICATIONS_ENABLED,
114                 ((Boolean) newValue).toString(), /* makeDefault */ false);
115     }
116 }
117