1 /** 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * 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 15 package android.accessibilityservice.cts.utils; 16 17 import static org.hamcrest.CoreMatchers.both; 18 19 import android.app.UiAutomation.AccessibilityEventFilter; 20 import android.view.accessibility.AccessibilityEvent; 21 22 import org.hamcrest.Description; 23 import org.hamcrest.Matcher; 24 import org.hamcrest.Matchers; 25 import org.hamcrest.TypeSafeMatcher; 26 27 /** 28 * Utility class for creating AccessibilityEventFilters 29 */ 30 public class AccessibilityEventFilterUtils { 31 public static AccessibilityEventFilter filterForEventType(int eventType) { 32 return (new AccessibilityEventTypeMatcher(eventType))::matches; 33 } 34 35 public static AccessibilityEventFilter filterWindowsChangedWithChangeTypes(int changes) { 36 return (both(new AccessibilityEventTypeMatcher(AccessibilityEvent.TYPE_WINDOWS_CHANGED)) 37 .and(new WindowChangesMatcher(changes)))::matches; 38 } 39 public static class AccessibilityEventTypeMatcher extends TypeSafeMatcher<AccessibilityEvent> { 40 private int mType; 41 42 public AccessibilityEventTypeMatcher(int type) { 43 super(); 44 mType = type; 45 } 46 47 @Override 48 protected boolean matchesSafely(AccessibilityEvent event) { 49 return event.getEventType() == mType; 50 } 51 52 @Override 53 public void describeTo(Description description) { 54 description.appendText("Matching to type " + mType); 55 } 56 } 57 58 public static class WindowChangesMatcher extends TypeSafeMatcher<AccessibilityEvent> { 59 private int mWindowChanges; 60 61 public WindowChangesMatcher(int windowChanges) { 62 super(); 63 mWindowChanges = windowChanges; 64 } 65 66 @Override 67 protected boolean matchesSafely(AccessibilityEvent event) { 68 return (event.getWindowChanges() & mWindowChanges) == mWindowChanges; 69 } 70 71 @Override 72 public void describeTo(Description description) { 73 description.appendText("With window change type " + mWindowChanges); 74 } 75 } 76 77 public static class ContentChangesMatcher extends TypeSafeMatcher<AccessibilityEvent> { 78 private int mContentChanges; 79 80 public ContentChangesMatcher(int contentChanges) { 81 super(); 82 mContentChanges = contentChanges; 83 } 84 85 @Override 86 protected boolean matchesSafely(AccessibilityEvent event) { 87 return (event.getContentChangeTypes() & mContentChanges) == mContentChanges; 88 } 89 90 @Override 91 public void describeTo(Description description) { 92 description.appendText("With window change type " + mContentChanges); 93 } 94 } 95 } 96