• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STATUS_BAR_COLOR_ID;
26 
27 import static org.hamcrest.Matchers.lessThanOrEqualTo;
28 import static org.junit.Assert.assertThat;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.when;
31 
32 import android.app.Activity;
33 import android.content.Intent;
34 import android.graphics.Color;
35 import android.support.test.filters.SmallTest;
36 import android.support.test.rule.ActivityTestRule;
37 import android.view.View;
38 
39 import com.android.managedprovisioning.R;
40 import com.android.managedprovisioning.TestInstrumentationRunner;
41 import com.android.managedprovisioning.analytics.TimeLogger;
42 import com.android.managedprovisioning.common.CustomizationVerifier;
43 import com.android.managedprovisioning.common.SettingsFacade;
44 import com.android.managedprovisioning.common.UriBitmap;
45 import com.android.managedprovisioning.common.Utils;
46 import com.android.managedprovisioning.parser.MessageParser;
47 import com.android.managedprovisioning.preprovisioning.terms.TermsActivity;
48 
49 import org.junit.AfterClass;
50 import org.junit.Before;
51 import org.junit.Rule;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.Mock;
55 import org.mockito.junit.MockitoJUnitRunner;
56 
57 import java.io.IOException;
58 
59 @SmallTest
60 @RunWith(MockitoJUnitRunner.class)
61 // TODO: Currently only color and logo functionality are covered. Fill in the rest (b/32131665).
62 public class PreProvisioningActivityTest {
63     private static final int SAMPLE_COLOR = Color.parseColor("#ffd40000");
64     private static final int DEFAULT_MAIN_COLOR = Color.rgb(99, 99, 99);
65 
66     @Mock
67     private Utils mUtils;
68 
69     @Rule
70     public ActivityTestRule<PreProvisioningActivity> mActivityRule = new ActivityTestRule<>(
71             PreProvisioningActivity.class, true, false);
72 
73     @Before
setup()74     public void setup() {
75         when(mUtils.getAccentColor(any())).thenReturn(DEFAULT_MAIN_COLOR);
76 
77         TestInstrumentationRunner.registerReplacedActivity(PreProvisioningActivity.class,
78                 (classLoader, className, intent) -> new PreProvisioningActivity(
79                         activity -> new PreProvisioningController(
80                                 activity,
81                                 activity,
82                                 new TimeLogger(activity, 0 /* category */),
83                                 new MessageParser(activity),
84                                 mUtils,
85                                 new SettingsFacade(),
86                                 EncryptionController.getInstance(activity)) {
87                             @Override
88                             protected boolean checkDevicePolicyPreconditions() {
89                                 return true;
90                             }
91 
92                             @Override
93                             protected boolean verifyActionAndCaller(Intent intent, String caller) {
94                                 return true;
95                             }
96                         }, null, mUtils));
97     }
98 
99     @AfterClass
tearDownClass()100     public static void tearDownClass() {
101         TestInstrumentationRunner.unregisterReplacedActivity(TermsActivity.class);
102     }
103 
104     @Test
profileOwnerDefaultColors()105     public void profileOwnerDefaultColors() {
106         Activity activity = mActivityRule.launchActivity(
107                 createIntent(ACTION_PROVISION_MANAGED_PROFILE, null));
108         CustomizationVerifier v = new CustomizationVerifier(activity);
109         v.assertStatusBarColorCorrect(activity.getColor(DEFAULT_STATUS_BAR_COLOR_ID));
110         v.assertSwiperColorCorrect(DEFAULT_MAIN_COLOR);
111         v.assertNextButtonColorCorrect(DEFAULT_MAIN_COLOR);
112     }
113 
114     @Test
profileOwnerCustomColors()115     public void profileOwnerCustomColors() {
116         Activity activity = mActivityRule.launchActivity(
117                 createIntent(ACTION_PROVISION_MANAGED_PROFILE, SAMPLE_COLOR));
118         CustomizationVerifier v = new CustomizationVerifier(activity);
119         v.assertStatusBarColorCorrect(SAMPLE_COLOR);
120         v.assertSwiperColorCorrect(SAMPLE_COLOR);
121         v.assertNextButtonColorCorrect(SAMPLE_COLOR);
122     }
123 
124     @Test
deviceOwnerDefaultColorsAndLogo()125     public void deviceOwnerDefaultColorsAndLogo() {
126         Activity activity = mActivityRule.launchActivity(
127                 createIntent(ACTION_PROVISION_MANAGED_DEVICE, null));
128         CustomizationVerifier v = new CustomizationVerifier(activity);
129         v.assertStatusBarColorCorrect(activity.getColor(DEFAULT_STATUS_BAR_COLOR_ID));
130         v.assertDefaultLogoCorrect(DEFAULT_MAIN_COLOR);
131         v.assertNextButtonColorCorrect(DEFAULT_MAIN_COLOR);
132     }
133 
134     @Test
deviceOwnerCustomColor()135     public void deviceOwnerCustomColor() {
136         Activity activity = mActivityRule.launchActivity(
137                 createIntent(ACTION_PROVISION_MANAGED_DEVICE, SAMPLE_COLOR));
138         CustomizationVerifier v = new CustomizationVerifier(activity);
139         v.assertStatusBarColorCorrect(SAMPLE_COLOR);
140         v.assertDefaultLogoCorrect(SAMPLE_COLOR);
141         v.assertNextButtonColorCorrect(SAMPLE_COLOR);
142     }
143 
144     @Test
deviceOwnerCustomLogo()145     public void deviceOwnerCustomLogo() throws IOException {
146         UriBitmap expectedLogo = UriBitmap.createSimpleInstance();
147 
148         Activity activity = mActivityRule.launchActivity(
149                 createIntent(ACTION_PROVISION_MANAGED_DEVICE, SAMPLE_COLOR).putExtra(
150                         EXTRA_PROVISIONING_LOGO_URI, expectedLogo.getUri()));
151         CustomizationVerifier v = new CustomizationVerifier(activity);
152         v.assertCustomLogoCorrect(expectedLogo.getBitmap());
153     }
154 
155     @Test
profileOwnerWholeLayoutIsAdjusted()156     public void profileOwnerWholeLayoutIsAdjusted() {
157         Activity activity = mActivityRule.launchActivity(
158                 createIntent(ACTION_PROVISION_MANAGED_PROFILE, null));
159         View content = activity.findViewById(R.id.intro_po_content);
160         View viewport = activity.findViewById(R.id.suw_layout_content);
161         assertThat("Width", content.getWidth(), lessThanOrEqualTo(viewport.getWidth()));
162 
163         int animationHeight = activity.findViewById(R.id.animated_info).getHeight();
164         int minHeight = activity.getResources().getDimensionPixelSize(
165                 R.dimen.intro_animation_min_height);
166 
167         if (animationHeight >= minHeight) {
168             assertThat("Height", content.getHeight(), lessThanOrEqualTo(viewport.getHeight()));
169         }
170     }
171 
createIntent(String provisioningAction, Integer mainColor)172     private Intent createIntent(String provisioningAction, Integer mainColor) {
173         Intent intent = new Intent(provisioningAction).putExtra(
174                 EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, COMPONENT_NAME);
175         if (mainColor != null) {
176             intent.putExtra(EXTRA_PROVISIONING_MAIN_COLOR, mainColor);
177         }
178         return intent;
179     }
180 }