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.task;
18 
19 import static android.provider.Settings.Secure.MANAGED_PROVISIONING_DPC_DOWNLOADED;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.mock;
24 import static org.robolectric.Shadows.shadowOf;
25 
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.content.pm.PackageInfo;
29 import android.content.pm.PackageManager;
30 import android.net.ConnectivityManager;
31 import android.provider.Settings;
32 
33 import com.android.managedprovisioning.model.PackageDownloadInfo;
34 import com.android.managedprovisioning.model.ProvisioningParams;
35 
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 /** Test class for {@link DownloadPackaageTask}. */
42 @RunWith(RobolectricTestRunner.class)
43 public class DownloadPackageTaskRoboTest {
44 
45     private static final int USER_ID = 0;
46     private static final ComponentName TEST_COMPONENT_NAME =
47             new ComponentName("test", "test");
48     private static final String TEST_PACKAGE_LOCATION = "http://test.location/test.apk";
49     private static final byte[] TEST_PACKAGE_CHECKSUM = new byte[]{1};
50 
51     private final Context mContext = RuntimeEnvironment.application;
52     private final PackageManager mPackageManager = mContext.getPackageManager();
53     private final ConnectivityManager mConnectivityManager =
54             mContext.getSystemService(ConnectivityManager.class);
55     private final AbstractProvisioningTask.Callback mCallback =
56             mock(AbstractProvisioningTask.Callback.class);
57 
58     @Test
run_doesSetManagedProvisioningDpcDownloaded()59     public void run_doesSetManagedProvisioningDpcDownloaded() {
60         final ProvisioningParams params = createDefaultProvisioningParamsBuilder().build();
61         final DownloadPackageTask mDownloadPackageTask =
62                 new DownloadPackageTask(
63                         mContext, params, params.deviceAdminDownloadInfo, mCallback);
64 
65         mDownloadPackageTask.run(USER_ID);
66 
67         assertManagedProvisioningDpcDownloadedSetTo(1);
68     }
69 
70     @Test
run_alreadyInstalled_doesNotSetManagedProvisioningDpcDownloaded()71     public void run_alreadyInstalled_doesNotSetManagedProvisioningDpcDownloaded() {
72         final ProvisioningParams params = createDefaultProvisioningParamsBuilder()
73                 .setDeviceAdminDownloadInfo(
74                         new PackageDownloadInfo.Builder()
75                                 .setLocation(TEST_PACKAGE_LOCATION)
76                                 .setMinVersion(1) // installed package is version 2
77                                 .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
78                                 .build())
79                 .build();
80         installTestPackage();
81         final DownloadPackageTask mDownloadPackageTask =
82                 new DownloadPackageTask(
83                         mContext, params, params.deviceAdminDownloadInfo, mCallback);
84 
85         mDownloadPackageTask.run(USER_ID);
86 
87         assertManagedProvisioningDpcDownloadedSetTo(0);
88     }
89 
90     @Test
run_installsUpdate_doesSetManagedProvisioningDpcDownloaded()91     public void run_installsUpdate_doesSetManagedProvisioningDpcDownloaded() {
92         final ProvisioningParams params = createDefaultProvisioningParamsBuilder()
93                 .setDeviceAdminDownloadInfo(
94                         new PackageDownloadInfo.Builder()
95                                 .setLocation(TEST_PACKAGE_LOCATION)
96                                 .setMinVersion(3) // installed package is version 2
97                                 .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
98                                 .build())
99                 .build();
100         installTestPackage();
101         final DownloadPackageTask mDownloadPackageTask =
102                 new DownloadPackageTask(
103                         mContext, params, params.deviceAdminDownloadInfo, mCallback);
104 
105         mDownloadPackageTask.run(USER_ID);
106 
107         assertManagedProvisioningDpcDownloadedSetTo(1);
108     }
109 
110     @Test
run_notConnected_doesNotSetManagedProvisioningDpcDownloaded()111     public void run_notConnected_doesNotSetManagedProvisioningDpcDownloaded() {
112         setNotConnected();
113         final ProvisioningParams params = createDefaultProvisioningParamsBuilder().build();
114         final DownloadPackageTask mDownloadPackageTask =
115                 new DownloadPackageTask(
116                         mContext, params, params.deviceAdminDownloadInfo, mCallback);
117 
118         mDownloadPackageTask.run(USER_ID);
119 
120         assertManagedProvisioningDpcDownloadedSetTo(0);
121     }
122 
assertManagedProvisioningDpcDownloadedSetTo(int value)123     private void assertManagedProvisioningDpcDownloadedSetTo(int value) {
124         int dpcInstalledSetting =
125                 Settings.Secure.getInt(
126                         mContext.getContentResolver(), MANAGED_PROVISIONING_DPC_DOWNLOADED, 0);
127         assertThat(dpcInstalledSetting).isEqualTo(value);
128     }
129 
installTestPackage()130     private void installTestPackage() {
131         final PackageInfo packageInfo = new PackageInfo();
132         packageInfo.packageName = TEST_COMPONENT_NAME.getPackageName();
133         packageInfo.versionCode = 2;
134         shadowOf(mPackageManager).installPackage(packageInfo);
135     }
136 
setNotConnected()137     private void setNotConnected() {
138         shadowOf(mConnectivityManager).setActiveNetworkInfo(null);
139     }
140 
createDefaultProvisioningParamsBuilder()141     private static ProvisioningParams.Builder createDefaultProvisioningParamsBuilder() {
142         return new ProvisioningParams.Builder()
143                 .setDeviceAdminComponentName(TEST_COMPONENT_NAME)
144                 .setProvisioningAction("")
145                 .setDeviceAdminDownloadInfo(
146                         new PackageDownloadInfo.Builder()
147                                 .setLocation(TEST_PACKAGE_LOCATION)
148                                 .setPackageChecksum(TEST_PACKAGE_CHECKSUM)
149                                 .build());
150     }
151 }
152