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 import static com.android.managedprovisioning.model.CustomizationParams.DEFAULT_COLOR_ID_BUTTON;
26 import static com.android.managedprovisioning.model.CustomizationParams.DEFAULT_COLOR_ID_DO;
27 import static com.android.managedprovisioning.model.CustomizationParams.DEFAULT_COLOR_ID_MP;
28 import static com.android.managedprovisioning.model.CustomizationParams.DEFAULT_COLOR_ID_SWIPER;
29 
30 import android.app.Activity;
31 import android.content.Intent;
32 import android.graphics.Color;
33 import android.support.test.filters.SmallTest;
34 import android.support.test.rule.ActivityTestRule;
35 
36 import com.android.managedprovisioning.TestInstrumentationRunner;
37 import com.android.managedprovisioning.common.CustomizationVerifier;
38 import com.android.managedprovisioning.common.UriBitmap;
39 import com.android.managedprovisioning.preprovisioning.terms.TermsActivity;
40 
41 import org.junit.AfterClass;
42 import org.junit.BeforeClass;
43 import org.junit.Rule;
44 import org.junit.Test;
45 
46 import java.io.IOException;
47 
48 @SmallTest
49 // TODO: Currently only color and logo functionality are covered. Fill in the rest (b/32131665).
50 public class PreProvisioningActivityTest {
51     private static final int SAMPLE_COLOR = Color.parseColor("#ffd40000");
52 
53     @Rule
54     public ActivityTestRule<PreProvisioningActivity> mActivityRule = new ActivityTestRule<>(
55             PreProvisioningActivity.class, true, false);
56 
57     @BeforeClass
setUpClass()58     public static void setUpClass() {
59         TestInstrumentationRunner.registerReplacedActivity(PreProvisioningActivity.class,
60                 (classLoader, className, intent) -> new PreProvisioningActivity(
61                         activity -> new PreProvisioningController(activity, activity) {
62                             @Override
63                             protected boolean checkDevicePolicyPreconditions() {
64                                 return true;
65                             }
66 
67                             @Override
68                             protected boolean verifyActionAndCaller(Intent intent, String caller) {
69                                 return true;
70                             }
71                         }, null));
72     }
73 
74     @AfterClass
tearDownClass()75     public static void tearDownClass() {
76         TestInstrumentationRunner.unregisterReplacedActivity(TermsActivity.class);
77     }
78 
79     @Test
profileOwnerDefaultColors()80     public void profileOwnerDefaultColors() {
81         Activity activity = mActivityRule.launchActivity(
82                 createIntent(ACTION_PROVISION_MANAGED_PROFILE, null));
83         CustomizationVerifier v = new CustomizationVerifier(activity);
84         v.assertStatusBarColorCorrect(activity.getColor(DEFAULT_COLOR_ID_MP));
85         v.assertSwiperColorCorrect(activity.getColor(DEFAULT_COLOR_ID_SWIPER));
86         v.assertNextButtonColorCorrect(activity.getColor(DEFAULT_COLOR_ID_BUTTON));
87     }
88 
89     @Test
profileOwnerCustomColors()90     public void profileOwnerCustomColors() {
91         Activity activity = mActivityRule.launchActivity(
92                 createIntent(ACTION_PROVISION_MANAGED_PROFILE, SAMPLE_COLOR));
93         CustomizationVerifier v = new CustomizationVerifier(activity);
94         v.assertStatusBarColorCorrect(SAMPLE_COLOR);
95         v.assertSwiperColorCorrect(SAMPLE_COLOR);
96         v.assertNextButtonColorCorrect(SAMPLE_COLOR);
97     }
98 
99     @Test
deviceOwnerDefaultColorsAndLogo()100     public void deviceOwnerDefaultColorsAndLogo() {
101         Activity activity = mActivityRule.launchActivity(
102                 createIntent(ACTION_PROVISION_MANAGED_DEVICE, null));
103         CustomizationVerifier v = new CustomizationVerifier(activity);
104         int color = activity.getColor(DEFAULT_COLOR_ID_DO);
105         v.assertStatusBarColorCorrect(color);
106         v.assertDefaultLogoCorrect(color);
107         v.assertNextButtonColorCorrect(color);
108     }
109 
110     @Test
deviceOwnerCustomColor()111     public void deviceOwnerCustomColor() {
112         Activity activity = mActivityRule.launchActivity(
113                 createIntent(ACTION_PROVISION_MANAGED_DEVICE, SAMPLE_COLOR));
114         CustomizationVerifier v = new CustomizationVerifier(activity);
115         v.assertStatusBarColorCorrect(SAMPLE_COLOR);
116         v.assertDefaultLogoCorrect(SAMPLE_COLOR);
117         v.assertNextButtonColorCorrect(SAMPLE_COLOR);
118     }
119 
120     @Test
deviceOwnerCustomLogo()121     public void deviceOwnerCustomLogo() throws IOException {
122         UriBitmap expectedLogo = UriBitmap.createSimpleInstance();
123 
124         Activity activity = mActivityRule.launchActivity(
125                 createIntent(ACTION_PROVISION_MANAGED_DEVICE, SAMPLE_COLOR).putExtra(
126                         EXTRA_PROVISIONING_LOGO_URI, expectedLogo.getUri()));
127         CustomizationVerifier v = new CustomizationVerifier(activity);
128         v.assertCustomLogoCorrect(expectedLogo.getBitmap());
129     }
130 
createIntent(String provisioningAction, Integer mainColor)131     private Intent createIntent(String provisioningAction, Integer mainColor) {
132         Intent intent = new Intent(provisioningAction).putExtra(
133                 EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, COMPONENT_NAME);
134         if (mainColor != null) {
135             intent.putExtra(EXTRA_PROVISIONING_MAIN_COLOR, mainColor);
136         }
137         return intent;
138     }
139 }