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