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.settings.testutils;
18 
19 import static android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU;
20 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON;
21 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
22 
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 
26 import android.accessibilityservice.AccessibilityServiceInfo;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.pm.ApplicationInfo;
30 import android.content.pm.ResolveInfo;
31 import android.content.pm.ServiceInfo;
32 import android.os.Build;
33 import android.provider.Settings;
34 import android.view.accessibility.AccessibilityManager;
35 
36 import com.android.settings.testutils.shadow.SettingsShadowResources;
37 
38 import org.xmlpull.v1.XmlPullParserException;
39 
40 import java.io.IOException;
41 
42 /**
43  * Utility class for common methods used in the accessibility feature related tests
44  */
45 public class AccessibilityTestUtils {
46 
setSoftwareShortcutMode( Context context, boolean gestureNavEnabled, boolean floatingButtonEnabled)47     public static void setSoftwareShortcutMode(
48             Context context, boolean gestureNavEnabled, boolean floatingButtonEnabled) {
49         int mode = floatingButtonEnabled ? ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU : -1;
50 
51         Settings.Secure.putInt(context.getContentResolver(),
52                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, mode);
53 
54         if (gestureNavEnabled) {
55             SettingsShadowResources.overrideResource(
56                     com.android.internal.R.integer.config_navBarInteractionMode,
57                     NAV_BAR_MODE_GESTURAL);
58         } else {
59             SettingsShadowResources.overrideResource(
60                     com.android.internal.R.integer.config_navBarInteractionMode,
61                     NAV_BAR_MODE_3BUTTON);
62         }
63     }
64 
65     /**
66      * Returns a mock {@link AccessibilityManager}
67      */
setupMockAccessibilityManager(Context mockContext)68     public static AccessibilityManager setupMockAccessibilityManager(Context mockContext) {
69         AccessibilityManager am = mock(AccessibilityManager.class);
70         when(mockContext.getSystemService(AccessibilityManager.class)).thenReturn(am);
71         return am;
72     }
73 
createAccessibilityServiceInfo( Context context, ComponentName componentName, boolean isAlwaysOnService)74     public static AccessibilityServiceInfo createAccessibilityServiceInfo(
75             Context context, ComponentName componentName, boolean isAlwaysOnService) {
76         final ApplicationInfo applicationInfo = new ApplicationInfo();
77         applicationInfo.targetSdkVersion = Build.VERSION_CODES.R;
78         final ServiceInfo serviceInfo = new ServiceInfo();
79         applicationInfo.packageName = componentName.getPackageName();
80         serviceInfo.packageName = componentName.getPackageName();
81         serviceInfo.name = componentName.getClassName();
82         serviceInfo.applicationInfo = applicationInfo;
83 
84         final ResolveInfo resolveInfo = new ResolveInfo();
85         resolveInfo.serviceInfo = serviceInfo;
86         try {
87             final AccessibilityServiceInfo info = new AccessibilityServiceInfo(resolveInfo,
88                     context);
89             info.setComponentName(componentName);
90             if (isAlwaysOnService) {
91                 info.flags |= AccessibilityServiceInfo.FLAG_REQUEST_ACCESSIBILITY_BUTTON;
92             }
93             return info;
94         } catch (XmlPullParserException | IOException e) {
95             // Do nothing
96         }
97         return null;
98     }
99 }
100