• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.truth.Truth.assertThat;
20 
21 import static org.robolectric.Shadows.shadowOf;
22 
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.pm.PackageInfo;
26 import android.content.pm.PackageManager;
27 
28 import com.android.managedprovisioning.common.Utils;
29 import com.android.managedprovisioning.model.PackageDownloadInfo;
30 import com.android.managedprovisioning.model.ProvisioningParams;
31 import com.android.managedprovisioning.model.WifiInfo;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 
38 /**
39  * Robolectric tests for {@link AdminIntegratedFlowPrepareActivity}.
40  */
41 @RunWith(RobolectricTestRunner.class)
42 public class AdminIntegratedFlowPrepareActivityTest {
43 
44     private static final ComponentName TEST_COMPONENT_NAME =
45             new ComponentName("test", "test");
46     private static final String TEST_PACKAGE_LOCATION = "http://test.location/test.apk";
47     private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[]{1};
48 
49     Utils mUtils = new Utils();
50     Context mContext = RuntimeEnvironment.application;
51     PackageManager mPackageManager = mContext.getPackageManager();
52 
53     @Test
shouldRunPrepareActivity_returnsFalse()54     public void shouldRunPrepareActivity_returnsFalse() {
55         ProvisioningParams params = createDefaultProvisioningParamsBuilder().build();
56 
57         boolean shouldRun =
58                 AdminIntegratedFlowPrepareActivity.shouldRunPrepareActivity(
59                         mUtils, mContext, params);
60 
61         assertThat(shouldRun).isFalse();
62     }
63 
64     @Test
shouldRunPrepareActivity_wifi_returnsTrue()65     public void shouldRunPrepareActivity_wifi_returnsTrue() {
66         ProvisioningParams params = createDefaultProvisioningParamsBuilder()
67                 .setWifiInfo(new WifiInfo.Builder()
68                         .setSsid("ssid")
69                         .build())
70                 .build();
71 
72         boolean shouldRun =
73                 AdminIntegratedFlowPrepareActivity.shouldRunPrepareActivity(
74                         mUtils, mContext, params);
75 
76         assertThat(shouldRun).isTrue();
77     }
78 
79     @Test
shouldRunPrepareActivity_mobileData_returnsTrue()80     public void shouldRunPrepareActivity_mobileData_returnsTrue() {
81         ProvisioningParams params = createDefaultProvisioningParamsBuilder()
82                 .setUseMobileData(true)
83                 .build();
84 
85         boolean shouldRun =
86                 AdminIntegratedFlowPrepareActivity.shouldRunPrepareActivity(
87                         mUtils, mContext, params);
88 
89         assertThat(shouldRun).isTrue();
90     }
91 
92     @Test
shouldRunPrepareActivity_download_notSideloaded_returnsTrue()93     public void shouldRunPrepareActivity_download_notSideloaded_returnsTrue() {
94         ProvisioningParams params = createDefaultProvisioningParamsBuilder()
95                 .setDeviceAdminDownloadInfo(
96                         new PackageDownloadInfo.Builder()
97                                 .setLocation(TEST_PACKAGE_LOCATION)
98                                 .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
99                                 .build())
100                 .build();
101 
102 
103         boolean shouldRun =
104                 AdminIntegratedFlowPrepareActivity.shouldRunPrepareActivity(
105                         mUtils, mContext, params);
106 
107         assertThat(shouldRun).isTrue();
108     }
109 
110     @Test
shouldRunPrepareActivity_download_noMinimum_sideloaded_returnsTrue()111     public void shouldRunPrepareActivity_download_noMinimum_sideloaded_returnsTrue() {
112         ProvisioningParams params = createDefaultProvisioningParamsBuilder()
113                 .setDeviceAdminDownloadInfo(
114                         new PackageDownloadInfo.Builder()
115                                 .setLocation(TEST_PACKAGE_LOCATION)
116                                 .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
117                                 .build())
118                 .build();
119         installTestPackage();
120 
121 
122         boolean shouldRun =
123                 AdminIntegratedFlowPrepareActivity.shouldRunPrepareActivity(
124                         mUtils, mContext, params);
125 
126         assertThat(shouldRun).isTrue();
127     }
128 
129     @Test
shouldRunPrepareActivity_download_minimum_sideloaded_meetsMinimum_returnsFalse()130     public void shouldRunPrepareActivity_download_minimum_sideloaded_meetsMinimum_returnsFalse() {
131         ProvisioningParams params = createDefaultProvisioningParamsBuilder()
132                 .setDeviceAdminDownloadInfo(
133                         new PackageDownloadInfo.Builder()
134                                 .setLocation(TEST_PACKAGE_LOCATION)
135                                 .setMinVersion(1) // installed package is version 2
136                                 .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
137                                 .build())
138                 .build();
139         installTestPackage();
140 
141         boolean shouldRun =
142                 AdminIntegratedFlowPrepareActivity.shouldRunPrepareActivity(
143                         mUtils, mContext, params);
144 
145         assertThat(shouldRun).isFalse();
146     }
147 
148     @Test
shouldRunPrepareActivity_download_minimum_sideloaded_doesNotMeetMinimum_returnsFalse()149     public void shouldRunPrepareActivity_download_minimum_sideloaded_doesNotMeetMinimum_returnsFalse() {
150         ProvisioningParams params = createDefaultProvisioningParamsBuilder()
151                 .setDeviceAdminDownloadInfo(
152                         new PackageDownloadInfo.Builder()
153                                 .setLocation(TEST_PACKAGE_LOCATION)
154                                 .setMinVersion(3) // installed package is version 2
155                                 .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
156                                 .build())
157                 .build();
158         installTestPackage();
159 
160         boolean shouldRun =
161                 AdminIntegratedFlowPrepareActivity.shouldRunPrepareActivity(
162                         mUtils, mContext, params);
163 
164         assertThat(shouldRun).isTrue();
165     }
166 
installTestPackage()167     private void installTestPackage() {
168         PackageInfo packageInfo = new PackageInfo();
169         packageInfo.packageName = TEST_COMPONENT_NAME.getPackageName();
170         packageInfo.versionCode = 2;
171         shadowOf(mPackageManager).installPackage(packageInfo);
172     }
173 
createDefaultProvisioningParamsBuilder()174     private static ProvisioningParams.Builder createDefaultProvisioningParamsBuilder() {
175         return new ProvisioningParams.Builder()
176                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
177                 .setProvisioningAction("");
178     }
179 }
180