1 /*
2  * Copyright (C) 2022 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.applications;
18 
19 import static android.provider.DeviceConfig.NAMESPACE_APP_CLONING;
20 
21 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 import android.content.res.Resources;
30 import android.provider.DeviceConfig;
31 
32 import androidx.test.core.app.ApplicationProvider;
33 
34 import com.android.settings.R;
35 import com.android.settings.Utils;
36 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.annotation.Config;
43 
44 @RunWith(RobolectricTestRunner.class)
45 @Config(shadows = {ShadowDeviceConfig.class})
46 public class ClonedAppsPreferenceControllerTest {
47 
48     private ClonedAppsPreferenceController mController;
49     private static final String KEY = "key";
50     private Context mContext;
51     private Resources mResources;
52 
53     @Before
setUp()54     public void setUp() {
55         mContext = spy(ApplicationProvider.getApplicationContext());
56 
57         mResources = spy(mContext.getResources());
58         when(mContext.getResources()).thenReturn(mResources);
59 
60         mController = new ClonedAppsPreferenceController(mContext, KEY);
61     }
62 
63     @Test
getAvailabilityStatus_featureNotEnabled_shouldNotReturnAvailable()64     public void getAvailabilityStatus_featureNotEnabled_shouldNotReturnAvailable() {
65         DeviceConfig.setProperty(NAMESPACE_APP_CLONING, Utils.PROPERTY_CLONED_APPS_ENABLED,
66                 "false", true /* makeDefault */);
67         when(mResources.getBoolean(R.bool.config_cloned_apps_page_enabled)).thenReturn(false);
68 
69         assertThat(mController.getAvailabilityStatus()).isNotEqualTo(AVAILABLE);
70     }
71 
72     @Test
getAvailabilityStatus_featureEnabled_shouldReturnAvailable()73     public void getAvailabilityStatus_featureEnabled_shouldReturnAvailable() {
74         DeviceConfig.setProperty(NAMESPACE_APP_CLONING, Utils.PROPERTY_CLONED_APPS_ENABLED,
75                 "true", true /* makeDefault */);
76         when(mResources.getBoolean(R.bool.config_cloned_apps_page_enabled)).thenReturn(true);
77 
78         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
79     }
80 
81     @Test
getAvailabilityStatus_deviceConfigFalseAndConfigEnabled_shouldNotReturnAvailable()82     public void getAvailabilityStatus_deviceConfigFalseAndConfigEnabled_shouldNotReturnAvailable() {
83         DeviceConfig.setProperty(NAMESPACE_APP_CLONING, Utils.PROPERTY_CLONED_APPS_ENABLED,
84                 "false", true /* makeDefault */);
85         when(mResources.getBoolean(R.bool.config_cloned_apps_page_enabled)).thenReturn(true);
86 
87         assertThat(mController.getAvailabilityStatus()).isNotEqualTo(AVAILABLE);
88     }
89 
90     @Test
getAvailabilityStatus_deviceConfigTrueAndConfigDisabled_shouldNotReturnAvailable()91     public void getAvailabilityStatus_deviceConfigTrueAndConfigDisabled_shouldNotReturnAvailable() {
92         DeviceConfig.setProperty(NAMESPACE_APP_CLONING, Utils.PROPERTY_CLONED_APPS_ENABLED,
93                 "true", true /* makeDefault */);
94         when(mResources.getBoolean(R.bool.config_cloned_apps_page_enabled)).thenReturn(false);
95 
96         assertThat(mController.getAvailabilityStatus()).isNotEqualTo(AVAILABLE);
97     }
98 }
99