1 /*
2  * Copyright 2016, 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.preprovisioning;
18 
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
21 import static android.app.admin.DevicePolicyManager.ACTION_START_ENCRYPTION;
22 import static android.support.test.espresso.Espresso.onView;
23 import static android.support.test.espresso.action.ViewActions.click;
24 import static android.support.test.espresso.assertion.ViewAssertions.matches;
25 import static android.support.test.espresso.matcher.ViewMatchers.withId;
26 import static android.support.test.espresso.matcher.ViewMatchers.withText;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.verify;
30 
31 import android.app.Activity;
32 import android.content.ComponentName;
33 import android.content.Context;
34 import android.content.Intent;
35 import android.graphics.Color;
36 import android.provider.Settings;
37 import android.support.test.InstrumentationRegistry;
38 import android.support.test.filters.SmallTest;
39 import android.support.test.rule.ActivityTestRule;
40 
41 import com.android.managedprovisioning.R;
42 import com.android.managedprovisioning.TestInstrumentationRunner;
43 import com.android.managedprovisioning.common.CustomizationVerifier;
44 import com.android.managedprovisioning.model.ProvisioningParams;
45 
46 import org.junit.After;
47 import org.junit.AfterClass;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Rule;
51 import org.junit.Test;
52 import org.mockito.Mock;
53 import org.mockito.MockitoAnnotations;
54 
55 /**
56  * Unit tests for {@link EncryptDeviceActivity}.
57  */
58 @SmallTest
59 public class EncryptDeviceActivityTest {
60 
61     private static final ComponentName ADMIN = new ComponentName("com.test.admin", ".Receiver");
62     private static final int SAMPLE_COLOR = Color.parseColor("#d40000");
63     private static final ProvisioningParams PROFILE_OWNER_PARAMS = new ProvisioningParams.Builder()
64             .setProvisioningAction(ACTION_PROVISION_MANAGED_PROFILE)
65             .setMainColor(SAMPLE_COLOR)
66             .setDeviceAdminComponentName(ADMIN)
67             .build();
68     private static final ProvisioningParams DEVICE_OWNER_PARAMS = new ProvisioningParams.Builder()
69             .setProvisioningAction(ACTION_PROVISION_MANAGED_DEVICE)
70             .setMainColor(SAMPLE_COLOR)
71             .setDeviceAdminComponentName(ADMIN)
72             .build();
73     private static final Intent PROFILE_OWNER_INTENT = new Intent()
74             .putExtra(ProvisioningParams.EXTRA_PROVISIONING_PARAMS, PROFILE_OWNER_PARAMS);
75     private static final Intent DEVICE_OWNER_INTENT = new Intent()
76             .putExtra(ProvisioningParams.EXTRA_PROVISIONING_PARAMS, DEVICE_OWNER_PARAMS);
77 
78     @Rule
79     public ActivityTestRule<EncryptDeviceActivity> mActivityRule = new ActivityTestRule<>(
80             EncryptDeviceActivity.class, true /* Initial touch mode  */,
81             false /* Lazily launch activity */);
82 
83     @Mock EncryptionController mController;
84     private static int sRotationLocked;
85 
86     @BeforeClass
setUpClass()87     public static void setUpClass() {
88         // Stop the activity from rotating in order to keep hold of the context
89         Context context = InstrumentationRegistry.getTargetContext();
90 
91         sRotationLocked = Settings.System.getInt(context.getContentResolver(),
92                 Settings.System.ACCELEROMETER_ROTATION, 0);
93         Settings.System.putInt(context.getContentResolver(),
94                 Settings.System.ACCELEROMETER_ROTATION, 0);
95     }
96 
97     @AfterClass
tearDownClass()98     public static void tearDownClass() {
99         // Reset the rotation value back to what it was before the test
100         Context context = InstrumentationRegistry.getTargetContext();
101 
102         Settings.System.putInt(context.getContentResolver(),
103                 Settings.System.ACCELEROMETER_ROTATION, sRotationLocked);
104     }
105 
106     @Before
setUp()107     public void setUp() {
108         MockitoAnnotations.initMocks(this);
109 
110         TestEncryptionActivity.sController = mController;
111         TestEncryptionActivity.sLastLaunchedIntent = null;
112 
113         TestInstrumentationRunner.registerReplacedActivity(EncryptDeviceActivity.class,
114                 TestEncryptionActivity.class);
115     }
116 
117     @After
tearDown()118     public void tearDown() {
119         TestInstrumentationRunner.unregisterReplacedActivity(EncryptDeviceActivity.class);
120     }
121 
122     @Test
testProfileOwner()123     public void testProfileOwner() {
124         // WHEN launching EncryptDeviceActivity with a profile owner intent
125         Activity activity = mActivityRule.launchActivity(PROFILE_OWNER_INTENT);
126 
127         // THEN the profile owner description should be present
128         onView(withId(R.id.encrypt_main_text))
129                 .check(matches(withText(R.string.encrypt_device_text_for_profile_owner_setup)));
130 
131         // status bar color matches the one from intent parameters
132         new CustomizationVerifier(activity).assertStatusBarColorCorrect(SAMPLE_COLOR);
133 
134         // WHEN pressing the encrypt button
135         onView(withId(R.id.encrypt_button)).perform(click());
136 
137         // THEN encryption reminder should be set
138         verify(mController).setEncryptionReminder(PROFILE_OWNER_PARAMS);
139 
140         // THEN encryption activity should be started
141         assertEquals(ACTION_START_ENCRYPTION,
142                 TestEncryptionActivity.sLastLaunchedIntent.getAction());
143     }
144 
145     @Test
testDeviceOwner()146     public void testDeviceOwner() {
147         // WHEN launching EncryptDeviceActivity with a profile owner intent
148         Activity activity = mActivityRule.launchActivity(DEVICE_OWNER_INTENT);
149 
150         // THEN the profile owner description should be present
151         onView(withId(R.id.encrypt_main_text))
152                 .check(matches(withText(R.string.encrypt_device_text_for_device_owner_setup)));
153 
154         // status bar color matches the one from intent parameters
155         new CustomizationVerifier(activity).assertStatusBarColorCorrect(SAMPLE_COLOR);
156 
157         // WHEN pressing the encrypt button
158         onView(withId(R.id.encrypt_button)).perform(click());
159 
160         // THEN encryption reminder should be set
161         verify(mController).setEncryptionReminder(DEVICE_OWNER_PARAMS);
162 
163         // THEN encryption activity should be started
164         assertEquals(ACTION_START_ENCRYPTION,
165                 TestEncryptionActivity.sLastLaunchedIntent.getAction());
166     }
167 
168     @Test
testNoParams()169     public void testNoParams() {
170         // WHEN launching EncryptDeviceActivity without a params object
171         mActivityRule.launchActivity(new Intent());
172 
173         // THEN the activity should finish immediately
174         assertTrue(mActivityRule.getActivity().isFinishing());
175     }
176 }
177