1 /*
2  * Copyright (C) 2023 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.server.healthconnect.migration;
18 
19 import static org.mockito.ArgumentMatchers.argThat;
20 import static org.mockito.Mockito.when;
21 
22 import android.Manifest;
23 import android.content.pm.ActivityInfo;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.health.connect.HealthConnectManager;
27 
28 import com.android.server.healthconnect.HealthConnectDeviceConfigManager;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /** Common methods and variables used by migration unit tests. */
34 public class MigrationTestUtils {
35     public static final String MOCK_CONFIGURED_PACKAGE = "com.configured.app";
36     static final String MOCK_UNCONFIGURED_PACKAGE_ONE = "com.unconfigured.app";
37     static final String MOCK_UNCONFIGURED_PACKAGE_TWO = "com.unconfigured.apptwo";
38     static final String MOCK_QUERIED_BROADCAST_RECEIVER_ONE = ".SampleReceiverOne";
39     static final String MOCK_QUERIED_BROADCAST_RECEIVER_TWO = ".SampleReceiverTwo";
40     static final String MOCK_CERTIFICATE_ONE =
41             "962F386525EE206D5ED146A3433411042E7F91D0C267C9DD08BA4F1F5E354076";
42     static final String MOCK_CERTIFICATE_TWO =
43             "962F386525EE206D5ED146A3433411042E7F91D0C267C9DD08BA4F1F5E354000";
44     static final String MOCK_CERTIFICATE_THREE =
45             "962F386525EE206D5ED146A3433411042E7F91D0C267C9DD08BA4F1F5E350000";
46     static final String[] PERMISSIONS_TO_CHECK =
47             new String[] {Manifest.permission.MIGRATE_HEALTH_CONNECT_DATA};
48 
getTimeoutPeriodBuffer()49     static long getTimeoutPeriodBuffer() {
50         return HealthConnectDeviceConfigManager.getInitialisedInstance().getExecutionTimeBuffer()
51                 * 2;
52     }
53 
createResolveInfoList( boolean nullActivityInfo, String packageName, String... broadcastReceivers)54     static List<ResolveInfo> createResolveInfoList(
55             boolean nullActivityInfo, String packageName, String... broadcastReceivers) {
56         List<ResolveInfo> resolveInfoArray = new ArrayList<ResolveInfo>();
57         for (String broadcastReceiver : broadcastReceivers) {
58             ResolveInfo resolveInfo = new ResolveInfo();
59             if (!nullActivityInfo) {
60                 resolveInfo.activityInfo = new ActivityInfo();
61                 resolveInfo.activityInfo.packageName = packageName;
62                 resolveInfo.activityInfo.name = packageName + broadcastReceiver;
63             }
64             resolveInfoArray.add(resolveInfo);
65         }
66         return resolveInfoArray;
67     }
68 
setResolveActivityResult(ResolveInfo result, PackageManager packageManager)69     public static void setResolveActivityResult(ResolveInfo result, PackageManager packageManager) {
70         setResolveActivityResult(result, packageManager, PackageManager.MATCH_ALL);
71     }
72 
setResolveActivityResult( ResolveInfo result, PackageManager packageManager, int flags)73     static void setResolveActivityResult(
74             ResolveInfo result, PackageManager packageManager, int flags) {
75         when(packageManager.resolveActivity(
76                         argThat(
77                                 intent ->
78                                         (HealthConnectManager.ACTION_SHOW_MIGRATION_INFO.equals(
79                                                 intent.getAction()))),
80                         argThat(flag -> (flag.getValue() == flags))))
81                 .thenReturn(result);
82     }
83 }
84