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
17package com.android.managedprovisioning.preprovisioning;
18
19import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
20import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
21import static android.app.admin.DevicePolicyManager.ACTION_START_ENCRYPTION;
22import static android.support.test.espresso.Espresso.onView;
23import static android.support.test.espresso.action.ViewActions.click;
24import static android.support.test.espresso.assertion.ViewAssertions.matches;
25import static android.support.test.espresso.matcher.ViewMatchers.withId;
26import static android.support.test.espresso.matcher.ViewMatchers.withText;
27import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertTrue;
29import static org.mockito.Mockito.verify;
30
31import android.app.Activity;
32import android.content.ComponentName;
33import android.content.Context;
34import android.content.Intent;
35import android.graphics.Color;
36import android.provider.Settings;
37import android.support.test.InstrumentationRegistry;
38import android.support.test.filters.SmallTest;
39import android.support.test.rule.ActivityTestRule;
40
41import com.android.managedprovisioning.R;
42import com.android.managedprovisioning.TestInstrumentationRunner;
43import com.android.managedprovisioning.common.CustomizationVerifier;
44import com.android.managedprovisioning.model.ProvisioningParams;
45
46import org.junit.After;
47import org.junit.AfterClass;
48import org.junit.Before;
49import org.junit.BeforeClass;
50import org.junit.Rule;
51import org.junit.Test;
52import org.mockito.Mock;
53import org.mockito.MockitoAnnotations;
54
55/**
56 * Unit tests for {@link EncryptDeviceActivity}.
57 */
58@SmallTest
59public 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
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
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
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
118    public void tearDown() {
119        TestInstrumentationRunner.unregisterReplacedActivity(EncryptDeviceActivity.class);
120    }
121
122    @Test
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
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
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