1 /**
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  * <p>http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * <p>Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11  * express or implied. See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 package android.accessibilityservice.cts;
15 
16 import static android.view.accessibility.AccessibilityEvent.TYPE_GESTURE_DETECTION_END;
17 import static android.view.accessibility.AccessibilityEvent.TYPE_GESTURE_DETECTION_START;
18 import static android.view.accessibility.AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED;
19 
20 import android.view.accessibility.AccessibilityEvent;
21 
22 import com.android.compatibility.common.util.TestUtils;
23 
24 /**
25  * This accessibility service stub collects all events relating to touch exploration rather than
26  * just the few collected by GestureDetectionStubAccessibilityService
27  */
28 public class TouchExplorationStubAccessibilityService
29         extends GestureDetectionStubAccessibilityService {
30 
31     @Override
onAccessibilityEvent(AccessibilityEvent event)32     public void onAccessibilityEvent(AccessibilityEvent event) {
33         synchronized (mLock) {
34             switch (event.getEventType()) {
35                 case TYPE_VIEW_ACCESSIBILITY_FOCUSED:
36                     mCollectedEvents.add(event.getEventType());
37                     mLock.notifyAll();
38                     break;
39                 case TYPE_GESTURE_DETECTION_START:
40                 case TYPE_GESTURE_DETECTION_END:
41                     mCollectedEvents.add(event.getEventType());
42             }
43         }
44         super.onAccessibilityEvent(event);
45     }
46 
47     /** Wait for accessibility focus from onAccessibilityEvent(). */
waitForAccessibilityFocus()48     public void waitForAccessibilityFocus() {
49         TestUtils.waitOn(mLock, () -> mCollectedEvents.contains(TYPE_VIEW_ACCESSIBILITY_FOCUSED),
50                 EVENT_RECOGNIZE_TIMEOUT_MS, "waitForAccessibilityFocus");
51     }
52 }
53