1 /*
2  * Copyright (C) 2022 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.internal.accessibility;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
22 
23 import android.accessibilityservice.AccessibilityServiceInfo;
24 import android.content.ComponentName;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.ResolveInfo;
27 import android.content.pm.ServiceInfo;
28 import android.os.Build;
29 
30 import com.android.internal.os.RoSystemProperties;
31 
32 import java.lang.reflect.Field;
33 
34 /**
35  * Test utility methods.
36  */
37 public class TestUtils {
38 
39     /**
40      * Sets the {@code enabled} of the given OneHandedMode flags to simulate device behavior.
41      */
setOneHandedModeEnabled(Object obj, boolean enabled)42     public static void setOneHandedModeEnabled(Object obj, boolean enabled) {
43         try {
44             final Field field = RoSystemProperties.class.getDeclaredField(
45                     "SUPPORT_ONE_HANDED_MODE");
46             field.setAccessible(true);
47             field.setBoolean(obj, enabled);
48         } catch (ReflectiveOperationException e) {
49             throw new RuntimeException(e);
50         }
51     }
52 
53     /**
54      * Creates fake accessibility service info.
55      */
createFakeServiceInfo( String packageLabel, String serviceComponent, String serviceSummary, boolean isAlwaysOnService)56     public static AccessibilityServiceInfo createFakeServiceInfo(
57             String packageLabel, String serviceComponent,
58             String serviceSummary, boolean isAlwaysOnService) {
59         ApplicationInfo applicationInfo = mock(ApplicationInfo.class);
60         applicationInfo.targetSdkVersion = Build.VERSION_CODES.R;
61         ServiceInfo serviceInfo = mock(ServiceInfo.class);
62         ResolveInfo resolveInfo = mock(ResolveInfo.class);
63         resolveInfo.serviceInfo = serviceInfo;
64         resolveInfo.serviceInfo.applicationInfo = applicationInfo;
65         when(resolveInfo.loadLabel(any())).thenReturn(packageLabel);
66 
67         AccessibilityServiceInfo a11yServiceInfo = mock(AccessibilityServiceInfo.class);
68         when(a11yServiceInfo.getResolveInfo()).thenReturn(resolveInfo);
69         when(a11yServiceInfo.getComponentName())
70                 .thenReturn(ComponentName.unflattenFromString(serviceComponent));
71         when(a11yServiceInfo.loadSummary(any())).thenReturn(serviceSummary);
72 
73         if (isAlwaysOnService) {
74             a11yServiceInfo.flags |= AccessibilityServiceInfo
75                     .FLAG_REQUEST_ACCESSIBILITY_BUTTON;
76         }
77 
78         return a11yServiceInfo;
79     }
80 }
81