1 /*
2  * Copyright (C) 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.analytics;
18 
19 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME;
21 
22 import static com.android.managedprovisioning.common.Globals.ACTION_RESUME_PROVISIONING;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 
29 import android.app.admin.DevicePolicyManager;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.content.pm.PackageManager;
33 import android.test.AndroidTestCase;
34 
35 import androidx.test.filters.SmallTest;
36 
37 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
38 
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 
42 import java.util.List;
43 
44 /**
45  * Unit-tests for {@link AnalyticsUtils}.
46  */
47 @SmallTest
48 public class AnalyticsUtilsTest extends AndroidTestCase {
49     private static final String INVALID_PACKAGE_NAME = "invalid-package-name";
50     private static final String VALID_PACKAGE_NAME = "valid-package-name";
51     private static final String VALID_INSTALLER_PACKAGE = "valid-installer-package";
52     private static final String INVALID_PROVISIONING_EXTRA = "invalid-provisioning-extra";
53 
54     @Mock private Context mockContext;
55     @Mock private PackageManager mockPackageManager;
56 
57     @Override
setUp()58     public void setUp() {
59         // this is necessary for mockito to work
60         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
61 
62         MockitoAnnotations.initMocks(this);
63 
64         when(mockContext.getPackageManager()).thenReturn(mockPackageManager);
65     }
66 
testGetInstallerPackageName_invalidPackage()67     public void testGetInstallerPackageName_invalidPackage() {
68         // WHEN getting installer package name for an invalid package.
69         when(mockPackageManager.getInstallerPackageName(INVALID_PACKAGE_NAME))
70                 .thenThrow(new IllegalArgumentException());
71         // THEN null should be returned and exception should be digested.
72         assertNull(AnalyticsUtils.getInstallerPackageName(mockContext, INVALID_PACKAGE_NAME));
73     }
74 
testGetInstallerPackageName_validPackage()75     public void testGetInstallerPackageName_validPackage() {
76         // WHEN getting installer package name for a valid package.
77         when(mockPackageManager.getInstallerPackageName(VALID_PACKAGE_NAME))
78                 .thenReturn(VALID_INSTALLER_PACKAGE);
79         // THEN valid installer package name should be returned.
80         assertEquals(VALID_INSTALLER_PACKAGE,
81                 AnalyticsUtils.getInstallerPackageName(mockContext, VALID_PACKAGE_NAME));
82     }
83 
testGetAllProvisioningExtras_NullIntent()84     public void testGetAllProvisioningExtras_NullIntent() {
85         // WHEN getting provisioning extras using null Intent.
86         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(null);
87         // THEN an empty list of valid provisioning extras should be returned.
88         assertEquals(0, provisioningExtras.size());
89     }
90 
testGetAllProvisioningExtras_ProvisioningResume()91     public void testGetAllProvisioningExtras_ProvisioningResume() {
92         // GIVEN provisioning was resumed
93         Intent intent = new Intent(ACTION_RESUME_PROVISIONING);
94         // WHEN getting provisioning extras using resume provisioning intent.
95         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent);
96         // THEN an empty list of valid provisioning extras should be returned.
97         assertEquals(0, provisioningExtras.size());
98     }
99 
testGetAllProvisioningExtras_NullBundleExtras()100     public void testGetAllProvisioningExtras_NullBundleExtras() {
101         // GIVEN intent has null extras
102         Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE);
103         // WHEN getting provisioning extras with null extras
104         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent);
105         // THEN an empty list of valid provisioning extras should be returned.
106         assertEquals(0, provisioningExtras.size());
107     }
108 
testGetAllProvisioningExtras()109     public void testGetAllProvisioningExtras() {
110         // GIVEN intent with both valid and invalid provisioning extras
111         Intent intent = new Intent(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE);
112         intent.putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, "");
113         intent.putExtra(INVALID_PROVISIONING_EXTRA, "");
114         intent.putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, "");
115         // WHEN getting provisioning extras using the intent
116         List<String> provisioningExtras = AnalyticsUtils.getAllProvisioningExtras(intent);
117         // THEN a list of valid provisioning extras should be returned.
118         assertEquals(2, provisioningExtras.size());
119         assertThat(provisioningExtras.contains(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME)).isTrue();
120         assertThat(provisioningExtras.contains(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE)).isTrue();
121     }
122 
testGetProvisioningTime_shouldReturnDiff()123     public void testGetProvisioningTime_shouldReturnDiff() {
124         final ManagedProvisioningSharedPreferences mockPrefs =
125                 mock(ManagedProvisioningSharedPreferences.class);
126         when(mockPrefs.getProvisioningStartedTimestamp()).thenReturn(5L);
127 
128         assertThat(AnalyticsUtils.getProvisioningTime(mockPrefs, () -> 20L)).isEqualTo(15L);
129     }
130 
testGetProvisioningTime_shouldReturnZero()131     public void testGetProvisioningTime_shouldReturnZero() {
132         final ManagedProvisioningSharedPreferences mockPrefs =
133                 mock(ManagedProvisioningSharedPreferences.class);
134         when(mockPrefs.getProvisioningStartedTimestamp()).thenReturn(0L);
135 
136         assertThat(AnalyticsUtils.getProvisioningTime(mockPrefs, () -> 20L)).isEqualTo(-1);
137     }
138 }
139