1 /*
2  * Copyright (C) 2019 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.provisioning;
18 
19 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_DEVICE_OWNER;
20 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED;
21 import static android.app.admin.DevicePolicyManager.FLAG_SUPPORTED_MODES_PERSONALLY_OWNED;
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.intent.Intents.intended;
27 import static androidx.test.espresso.intent.Intents.times;
28 import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
29 import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
30 import static androidx.test.espresso.matcher.ViewMatchers.withText;
31 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
32 
33 import static com.android.managedprovisioning.model.ProvisioningParams.EXTRA_PROVISIONING_PARAMS;
34 
35 import android.content.ComponentName;
36 import android.content.Intent;
37 import android.os.RemoteException;
38 
39 import androidx.test.espresso.intent.rule.IntentsTestRule;
40 import androidx.test.filters.SmallTest;
41 import androidx.test.uiautomator.UiDevice;
42 
43 import com.android.managedprovisioning.R;
44 import com.android.managedprovisioning.TestUtils;
45 import com.android.managedprovisioning.model.ProvisioningParams;
46 import com.android.managedprovisioning.model.WifiInfo;
47 
48 import org.junit.Before;
49 import org.junit.Ignore;
50 import org.junit.Rule;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.junit.runners.JUnit4;
54 
55 /**
56  * Unit tests for {@link LandingActivity}.
57  */
58 @SmallTest
59 @RunWith(JUnit4.class)
60 public class LandingActivityTest {
61 
62     private static final ComponentName TEST_COMPONENT_NAME =
63             new ComponentName("test", "test");
64 
65     @Rule
66     public IntentsTestRule<LandingActivity> mActivityRule =
67             new IntentsTestRule(LandingActivity.class, true, false);
68 
69     @Before
setup()70     public void setup() throws RemoteException {
71         TestUtils.wakeupDeviceAndPressHome(UiDevice.getInstance(getInstrumentation()));
72     }
73 
74     @Ignore("b/181323689")
75     @Test
onNextButtonClicked_shouldRunPrepareActivity_runsPrepareActivity()76     public void onNextButtonClicked_shouldRunPrepareActivity_runsPrepareActivity() {
77         ProvisioningParams params = generateProvisioningParamsThatShouldRunPrepareActivity();
78         launchLandingActivityWithParams(params);
79 
80         onView(withText(R.string.next)).perform(click());
81 
82         // Has launched the Prepare Activity
83         intended(hasComponent(AdminIntegratedFlowPrepareActivity.class.getName()));
84     }
85 
86     @Ignore("b/181323689")
87     @Test
onNextButtonClicked_shouldNotRunPrepareActivity_doesNotRunPrepareActivity()88     public void onNextButtonClicked_shouldNotRunPrepareActivity_doesNotRunPrepareActivity() {
89         ProvisioningParams params = generateProvisioningParamsThatShouldNotRunPrepareActivity();
90         launchLandingActivityWithParams(params);
91 
92         onView(withText(R.string.next)).perform(click());
93 
94         // Has not launched the Prepare Activity
95         intended(hasComponent(AdminIntegratedFlowPrepareActivity.class.getName()), times(0));
96     }
97 
98     @Ignore("b/181323689")
99     @Test
onCreate_personallyOwned_showsAccountManagementDisclaimer()100     public void onCreate_personallyOwned_showsAccountManagementDisclaimer() {
101         ProvisioningParams params = generateProvisioningParamsWithRequestedProvisioningModes(
102                 FLAG_SUPPORTED_MODES_PERSONALLY_OWNED);
103         launchLandingActivityWithParams(params);
104 
105         assertAccountManagementDisclaimerShown();
106     }
107 
108     @Ignore("b/181323689")
109     @Test
onCreate_organizationAndPersonallyOwned_showsAccountManagementDisclaimer()110     public void onCreate_organizationAndPersonallyOwned_showsAccountManagementDisclaimer() {
111         ProvisioningParams params = generateProvisioningParamsWithRequestedProvisioningModes(
112                 FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED | FLAG_SUPPORTED_MODES_PERSONALLY_OWNED);
113         launchLandingActivityWithParams(params);
114 
115         assertAccountManagementDisclaimerShown();
116     }
117 
118     @Ignore("b/181323689")
119     @Test
onCreate_organizationOwned_showsOwnershipDisclaimer()120     public void onCreate_organizationOwned_showsOwnershipDisclaimer() {
121         ProvisioningParams params = generateProvisioningParamsWithRequestedProvisioningModes(
122                 FLAG_SUPPORTED_MODES_ORGANIZATION_OWNED);
123         launchLandingActivityWithParams(params);
124 
125         assertOwnershipDisclaimerShown();
126     }
127 
128     @Ignore("b/181323689")
129     @Test
onCreate_deviceOwner_showsOwnershipDisclaimer()130     public void onCreate_deviceOwner_showsOwnershipDisclaimer() {
131         ProvisioningParams params = generateProvisioningParamsWithRequestedProvisioningModes(
132                 FLAG_SUPPORTED_MODES_DEVICE_OWNER);
133         launchLandingActivityWithParams(params);
134 
135         assertOwnershipDisclaimerShown();
136     }
137 
assertAccountManagementDisclaimerShown()138     private void assertAccountManagementDisclaimerShown() {
139         onView(withText(R.string.account_management_disclaimer_header))
140                 .check(matches(isDisplayed()));
141         onView(withText(R.string.account_management_disclaimer_subheader))
142                 .check(matches(isDisplayed()));
143     }
144 
assertOwnershipDisclaimerShown()145     private void assertOwnershipDisclaimerShown() {
146         onView(withText(R.string.brand_screen_header))
147                 .check(matches(isDisplayed()));
148     }
149 
launchLandingActivityWithParams(ProvisioningParams params)150     private void launchLandingActivityWithParams(ProvisioningParams params) {
151         Intent intent = new Intent();
152         intent.putExtra(EXTRA_PROVISIONING_PARAMS, params);
153         mActivityRule.launchActivity(intent);
154     }
155 
generateProvisioningParamsWithRequestedProvisioningModes( int requestedProvisioningModes)156     private ProvisioningParams generateProvisioningParamsWithRequestedProvisioningModes(
157             int requestedProvisioningModes) {
158         return createDefaultProvisioningParamsBuilder()
159                 .setInitiatorRequestedProvisioningModes(requestedProvisioningModes)
160                 .build();
161     }
162 
generateProvisioningParamsThatShouldRunPrepareActivity()163     private ProvisioningParams generateProvisioningParamsThatShouldRunPrepareActivity() {
164         // If we need to connect to wifi then prepare activity should run.
165         // For further testing of shouldRunPrepareActivity
166         // see {@link AdminIntegratedFlowPrepareActivityTest}
167         return createDefaultProvisioningParamsBuilder()
168                 .setWifiInfo(new WifiInfo.Builder()
169                         .setSsid("\"ssid\"")
170                         .build())
171                 .build();
172     }
173 
generateProvisioningParamsThatShouldNotRunPrepareActivity()174     private ProvisioningParams generateProvisioningParamsThatShouldNotRunPrepareActivity() {
175         return createDefaultProvisioningParamsBuilder().build();
176     }
177 
createDefaultProvisioningParamsBuilder()178     private static ProvisioningParams.Builder createDefaultProvisioningParamsBuilder() {
179         return new ProvisioningParams.Builder()
180                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
181                 .setProvisioningAction("");
182     }
183 }
184