1 /*
2  * Copyright (C) 2017 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 package com.android.managedprovisioning.preprovisioning;
17 
18 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME;
21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LOGO_URI;
22 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_MAIN_COLOR;
23 
24 import static com.android.managedprovisioning.e2eui.ManagedProfileAdminReceiver.COMPONENT_NAME;
25 
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.when;
28 
29 import android.app.Activity;
30 import android.content.Intent;
31 import android.graphics.Color;
32 
33 import androidx.test.filters.SmallTest;
34 import androidx.test.rule.ActivityTestRule;
35 
36 import com.android.managedprovisioning.TestInstrumentationRunner;
37 import com.android.managedprovisioning.analytics.TimeLogger;
38 import com.android.managedprovisioning.common.CustomizationVerifier;
39 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
40 import com.android.managedprovisioning.common.SettingsFacade;
41 import com.android.managedprovisioning.common.UriBitmap;
42 import com.android.managedprovisioning.common.Utils;
43 import com.android.managedprovisioning.parser.MessageParser;
44 import com.android.managedprovisioning.preprovisioning.terms.TermsActivity;
45 
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.Mock;
52 import org.mockito.junit.MockitoJUnitRunner;
53 
54 import java.io.IOException;
55 
56 @SmallTest
57 @RunWith(MockitoJUnitRunner.class)
58 // TODO: Currently only color and logo functionality are covered. Fill in the rest (b/32131665).
59 public class PreProvisioningActivityTest {
60     private static final int SAMPLE_COLOR = Color.parseColor("#ffd40000");
61     private static final int DEFAULT_MAIN_COLOR = Color.rgb(99, 99, 99);
62 
63     @Mock
64     private Utils mUtils;
65 
66     @Rule
67     public ActivityTestRule<PreProvisioningActivity> mActivityRule = new ActivityTestRule<>(
68             PreProvisioningActivity.class, true, false);
69 
70     @Before
setup()71     public void setup() {
72         when(mUtils.getAccentColor(any())).thenReturn(DEFAULT_MAIN_COLOR);
73         when(mUtils.alreadyHasManagedProfile(any())).thenReturn(-1);
74 
75         TestInstrumentationRunner.registerReplacedActivity(PreProvisioningActivity.class,
76                 (classLoader, className, intent) -> new PreProvisioningActivity(
77                         activity -> new PreProvisioningController(
78                                 activity,
79                                 activity,
80                                 new TimeLogger(activity, 0 /* category */),
81                                 new MessageParser(activity),
82                                 mUtils,
83                                 new SettingsFacade(),
84                                 EncryptionController.getInstance(activity),
85                                 new ManagedProvisioningSharedPreferences(activity)) {
86                             @Override
87                             protected boolean checkDevicePolicyPreconditions() {
88                                 return true;
89                             }
90 
91                             @Override
92                             protected boolean verifyActionAndCaller(Intent intent, String caller) {
93                                 return true;
94                             }
95                         }, null, mUtils));
96     }
97 
98     @AfterClass
tearDownClass()99     public static void tearDownClass() {
100         TestInstrumentationRunner.unregisterReplacedActivity(TermsActivity.class);
101     }
102 
103     @Test
profileOwnerDefaultColors()104     public void profileOwnerDefaultColors() {
105         Activity activity = mActivityRule.launchActivity(
106                 createIntent(ACTION_PROVISION_MANAGED_PROFILE, null));
107         CustomizationVerifier v = new CustomizationVerifier(activity);
108         v.assertStatusBarColorCorrect(Color.TRANSPARENT);
109         v.assertSwiperColorCorrect(DEFAULT_MAIN_COLOR);
110     }
111 
112     @Test
profileOwnerCustomColors()113     public void profileOwnerCustomColors() {
114         Activity activity = mActivityRule.launchActivity(
115                 createIntent(ACTION_PROVISION_MANAGED_PROFILE, SAMPLE_COLOR));
116 
117         CustomizationVerifier v = new CustomizationVerifier(activity);
118         v.assertStatusBarColorCorrect(SAMPLE_COLOR);
119         v.assertSwiperColorCorrect(SAMPLE_COLOR);
120     }
121 
122     @Test
deviceOwnerDefaultColorsAndLogo()123     public void deviceOwnerDefaultColorsAndLogo() {
124         Activity activity = mActivityRule.launchActivity(
125                 createIntent(ACTION_PROVISION_MANAGED_DEVICE, null));
126         CustomizationVerifier v = new CustomizationVerifier(activity);
127         v.assertStatusBarColorCorrect(Color.TRANSPARENT);
128         v.assertDefaultLogoCorrect(DEFAULT_MAIN_COLOR);
129     }
130 
131     @Test
deviceOwnerCustomColor()132     public void deviceOwnerCustomColor() {
133         Activity activity = mActivityRule.launchActivity(
134                 createIntent(ACTION_PROVISION_MANAGED_DEVICE, SAMPLE_COLOR));
135         CustomizationVerifier v = new CustomizationVerifier(activity);
136         v.assertStatusBarColorCorrect(SAMPLE_COLOR);
137         v.assertDefaultLogoCorrect(SAMPLE_COLOR);
138     }
139 
140     @Test
deviceOwnerCustomLogo()141     public void deviceOwnerCustomLogo() throws IOException {
142         UriBitmap expectedLogo = UriBitmap.createSimpleInstance();
143 
144         Activity activity = mActivityRule.launchActivity(
145                 createIntent(ACTION_PROVISION_MANAGED_DEVICE, SAMPLE_COLOR).putExtra(
146                         EXTRA_PROVISIONING_LOGO_URI, expectedLogo.getUri()));
147         CustomizationVerifier v = new CustomizationVerifier(activity);
148         v.assertCustomLogoCorrect(expectedLogo.getBitmap());
149     }
150 
createIntent(String provisioningAction, Integer mainColor)151     private Intent createIntent(String provisioningAction, Integer mainColor) {
152         Intent intent = new Intent(provisioningAction).putExtra(
153                 EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, COMPONENT_NAME);
154         if (mainColor != null) {
155             intent.putExtra(EXTRA_PROVISIONING_MAIN_COLOR, mainColor);
156         }
157         return intent;
158     }
159 }