1 /*
2  * Copyright (C) 2021 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.managedprovisioning.provisioning;
18 
19 import static com.android.managedprovisioning.provisioning.ProvisioningActivity.PROVISIONING_MODE_FULLY_MANAGED_DEVICE;
20 import static com.android.managedprovisioning.provisioning.ProvisioningActivity.PROVISIONING_MODE_WORK_PROFILE;
21 import static com.android.managedprovisioning.provisioning.ProvisioningActivity.PROVISIONING_MODE_WORK_PROFILE_ON_ORG_OWNED_DEVICE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.junit.Assert.assertThrows;
26 
27 import android.content.ComponentName;
28 import android.content.Context;
29 
30 import androidx.test.InstrumentationRegistry;
31 import androidx.test.filters.SmallTest;
32 
33 import com.android.managedprovisioning.R;
34 import com.android.managedprovisioning.model.ProvisioningParams;
35 import com.android.managedprovisioning.provisioning.ProvisioningModeWrapperProvider.ProvisioningModeWrapper;
36 
37 import com.google.android.setupdesign.util.DeviceHelper;
38 
39 import org.junit.Test;
40 
41 /**
42  * Unit tests for {@link ProvisioningModeWrapperProvider}.
43  */
44 @SmallTest
45 public class ProvisioningModeWrapperProviderTest {
46     private static final ComponentName ADMIN = new ComponentName("com.foo", "com.bar");
47     private static final String TEST_PROVIONING_ACTION = "android.app.action.TEST";
48     private static final ProvisioningParams SIMPLE_PARAMS = new ProvisioningParams.Builder()
49             .setProvisioningAction(TEST_PROVIONING_ACTION)
50             .setDeviceAdminComponentName(ADMIN)
51             .build();
52 
53     private final Context mContext = InstrumentationRegistry.getTargetContext();
54     private final ProvisioningModeWrapperProvider mTestProvider =
55             new ProvisioningModeWrapperProvider(SIMPLE_PARAMS);
56 
57     private final CharSequence TEST_DEVICE_NAME = DeviceHelper.getDeviceName(mContext);
58 
59     @Test
testGetProvisioningModeWrapper_invalidMode()60     public void testGetProvisioningModeWrapper_invalidMode() {
61         assertThrows(IllegalStateException.class,
62                 () -> mTestProvider.getProvisioningModeWrapper(-1, TEST_DEVICE_NAME));
63     }
64 
65     @Test
testGetProvisioningModeWrapper_workProfile()66     public void testGetProvisioningModeWrapper_workProfile() {
67         String expected = mContext.getString(R.string.work_profile_provisioning_summary);
68 
69         ProvisioningModeWrapper wrapper = mTestProvider.getProvisioningModeWrapper(
70                 PROVISIONING_MODE_WORK_PROFILE, TEST_DEVICE_NAME);
71 
72         assertWrapperAsExpected(wrapper, expected);
73     }
74 
75     @Test
testGetProvisioningModeWrapper_workProfileOnOrgOwnedDevice()76     public void testGetProvisioningModeWrapper_workProfileOnOrgOwnedDevice() {
77         String expected = mContext.getString(R.string.cope_provisioning_summary, TEST_DEVICE_NAME);
78 
79         ProvisioningModeWrapper wrapper = mTestProvider.getProvisioningModeWrapper(
80                 PROVISIONING_MODE_WORK_PROFILE_ON_ORG_OWNED_DEVICE, TEST_DEVICE_NAME);
81 
82         assertWrapperAsExpected(wrapper, expected);
83     }
84 
85     @Test
testGetProvisioningModeWrapper_fullyManagedDevice_withoutSensorPermission()86     public void testGetProvisioningModeWrapper_fullyManagedDevice_withoutSensorPermission() {
87         ProvisioningParams params = new ProvisioningParams.Builder()
88                 .setProvisioningAction(TEST_PROVIONING_ACTION)
89                 .setDeviceAdminComponentName(ADMIN)
90                 .setDeviceOwnerPermissionGrantOptOut(true)
91                 .build();
92         ProvisioningModeWrapperProvider provider = new ProvisioningModeWrapperProvider(params);
93         String expected = mContext.getString(R.string.fully_managed_device_provisioning_summary,
94                 TEST_DEVICE_NAME);
95 
96         ProvisioningModeWrapper wrapper =
97                 provider.getProvisioningModeWrapper(PROVISIONING_MODE_FULLY_MANAGED_DEVICE,
98                         TEST_DEVICE_NAME);
99 
100         assertWrapperAsExpected(wrapper, expected);
101     }
102 
103     @Test
testGetProvisioningModeWrapper_fullyManagedDevice_withSensorPermission()104     public void testGetProvisioningModeWrapper_fullyManagedDevice_withSensorPermission() {
105         ProvisioningParams params = new ProvisioningParams.Builder()
106                 .setProvisioningAction(TEST_PROVIONING_ACTION)
107                 .setDeviceAdminComponentName(ADMIN)
108                 .setDeviceOwnerPermissionGrantOptOut(false)
109                 .build();
110         ProvisioningModeWrapperProvider provider = new ProvisioningModeWrapperProvider(params);
111         String expected = mContext.getString(
112                 R.string.fully_managed_device_with_permission_control_provisioning_summary,
113                 TEST_DEVICE_NAME);
114 
115         ProvisioningModeWrapper wrapper =
116                 provider.getProvisioningModeWrapper(PROVISIONING_MODE_FULLY_MANAGED_DEVICE,
117                         TEST_DEVICE_NAME);
118 
119         assertWrapperAsExpected(wrapper, expected);
120     }
121 
assertWrapperAsExpected(ProvisioningModeWrapper wrapper, String expected)122     private void assertWrapperAsExpected(ProvisioningModeWrapper wrapper, String expected) {
123         assertThat(wrapper.mSummary.value(mContext)).isEqualTo(expected);
124     }
125 }
126