1 /*
2  * Copyright (C) 2024 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 android.accessibilityservice.cts.utils;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 import android.view.accessibility.AccessibilityNodeInfo;
23 import android.view.accessibility.AccessibilityNodeProvider;
24 
25 import androidx.annotation.Nullable;
26 
27 /**
28  * Class that returns a non-null AccessibilityNodeProvider. Used in AccessibilityEndToEndTest.
29  */
30 public class ProviderCustomView extends View  {
31     private boolean mReturnProvider;
32 
ProviderCustomView(Context context)33     public ProviderCustomView(Context context) {
34         super(context);
35     }
36 
ProviderCustomView(Context context, @Nullable AttributeSet attrs)37     public ProviderCustomView(Context context,
38             @Nullable AttributeSet attrs) {
39         super(context, attrs);
40     }
41 
ProviderCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)42     public ProviderCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
43         super(context, attrs, defStyleAttr);
44     }
45 
ProviderCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)46     public ProviderCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
47             int defStyleRes) {
48         super(context, attrs, defStyleAttr, defStyleRes);
49     }
50 
setReturnProvider(boolean returnProvider)51     public void setReturnProvider(boolean returnProvider) {
52         this.mReturnProvider = returnProvider;
53     }
54 
55     @Override
getAccessibilityNodeProvider()56     public AccessibilityNodeProvider getAccessibilityNodeProvider() {
57         if (mReturnProvider) {
58             return new AccessibilityNodeProvider() {
59                 @Nullable
60                 @Override
61                 public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) {
62                     AccessibilityNodeInfo hostNode =
63                             new AccessibilityNodeInfo(ProviderCustomView.this);
64                     ProviderCustomView.this.onInitializeAccessibilityNodeInfo(hostNode);
65                     return hostNode;
66                 }
67             };
68         }
69         return null;
70     }
71 }
72