还未发布过话题
  • 不需要自己实现 onAccessibilityEvent 方法,直接使用 UiAutomator 的 UiDevice.performActionAndWait() 方法就可以了处理 AccessibilityEvent 事件。
    http://android.xsoftlab.net/reference/android/support/test/uiautomator/UiDevice.html
    核心是实现一个检查 Toast 的 EventCondition 类。可以参考 android.support.test.uiautomator.Until.scrollFinished 实现。
    可以通过命令行工具查看具体的 event 信息。

    adb shell uiautomator events > d:/events.log 
    

    由于 EventCondition 的两个抽象方法是包访问权限,因此必须新建一个 android.support.test.uiautomator 同名的包。
    具体代码如下:

    package android.support.test.uiautomator;
    import android.view.accessibility.AccessibilityEvent;
    import java.util.ArrayList;
    import java.util.List;
    public class MyUntil {
        //检查给定Toast文案是否展示
        public static EventCondition<Boolean> toastShown(final CharSequence toastText) {
            return new EventCondition<Boolean>() {
                private int mMask = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
                private boolean mIsClassMatch = false;
                private static final String FILTER_CLASS_NAME = "android.widget.Toast$TN";
                private boolean mResult = false;
                private List<CharSequence> mText;
    
                @Override
                Boolean apply(AccessibilityEvent event) {
                    mMask &= ~event.getEventType();
                    mIsClassMatch = FILTER_CLASS_NAME.equals(event.getClassName());
                    mText = event.getText();
                    mResult = mMask == 0 && mIsClassMatch && mText.contains(toastText);
                    return mResult;
                }
    
                @Override
                Boolean getResult() {
                    return mResult;
                }
            };
        }
        //获取Toast的文案
        public static EventCondition<String> getToastText() {
            return new EventCondition<String>() {
                private int mMask = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
                private boolean mIsClassMatch = false;
                private static final String FILTER_CLASS_NAME = "android.widget.Toast$TN";
                private List<CharSequence> mText = new ArrayList<>();
    
                @Override
                Boolean apply(AccessibilityEvent event) {
                    mMask &= ~event.getEventType();
                    mIsClassMatch = FILTER_CLASS_NAME.equals(event.getClassName());
                    mText = new ArrayList<>(event.getText());//必须要复制getText否则,在getResult中获取不到结果。
                    return mMask == 0 && mIsClassMatch;
                }
    
                @Override
                String getResult() {
                    String result = null;
                    if (mText.size() > 0) {
                        result = mText.get(0).toString();
                    }
                    return result;
                }
            };
        }
    }
    

    使用方法:

    import android.support.test.uiautomator.MyUntil;//导入类
    
    Runnable action = new Runnable() {
        @Override
        public void run() {
    
        }
    };
    EventCondition<Boolean> condition = MyUntil.toastShown("已经同步到最新");
    assertTrue(mDevice.performActionAndWait(action,condition,3000));
    //EventCondition<Boolean> condition = MyUntil.getToastText();
    //assertThat(mDevice.performActionAndWait(action,condition,3000),is("已经同步到最新"));