• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.security;
18 
19 import static com.android.internal.R.string.config_defaultContentProtectionService;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.spy;
25 
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.platform.test.flag.junit.SetFlagsRule;
29 import android.provider.DeviceConfig;
30 import android.view.contentcapture.ContentCaptureManager;
31 
32 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
33 
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 import org.robolectric.annotation.Config;
42 
43 @RunWith(RobolectricTestRunner.class)
44 @Config(
45         shadows = {
46             ShadowDeviceConfig.class,
47         })
48 public class ContentProtectionPreferenceControllerTest {
49 
50     private static final String PACKAGE_NAME = "com.test.package";
51 
52     private static final ComponentName COMPONENT_NAME =
53             new ComponentName(PACKAGE_NAME, "TestClass");
54 
55     @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
56 
57     private Context mContext;
58 
59     private String mConfigDefaultContentProtectionService = COMPONENT_NAME.flattenToString();
60 
61     private ContentProtectionPreferenceController mController;
62 
63     @Before
setUp()64     public void setUp() {
65         mContext = spy(RuntimeEnvironment.application);
66         mController = new ContentProtectionPreferenceController(mContext, "key");
67     }
68 
69     @After
tearDown()70     public void tearDown() {
71         ShadowDeviceConfig.reset();
72     }
73 
74     @Test
isAvailable_isFalse()75     public void isAvailable_isFalse() {
76         assertThat(mController.isAvailable()).isFalse();
77     }
78 
79     @Test
isAvailable_isTrue()80     public void isAvailable_isTrue() {
81         doReturn(COMPONENT_NAME.flattenToString())
82                 .when(mContext)
83                 .getString(config_defaultContentProtectionService);
84 
85         DeviceConfig.setProperty(
86                 DeviceConfig.NAMESPACE_CONTENT_CAPTURE,
87                 ContentCaptureManager.DEVICE_CONFIG_PROPERTY_ENABLE_CONTENT_PROTECTION_RECEIVER,
88                 "true",
89                 /* makeDefault= */ false);
90 
91         assertThat(mController.isAvailable()).isTrue();
92     }
93 }
94