• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 DroidDriver committers
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 io.appium.droiddriver.actions.accessibility;
18 
19 import android.annotation.TargetApi;
20 import android.view.accessibility.AccessibilityNodeInfo;
21 
22 import io.appium.droiddriver.UiElement;
23 import io.appium.droiddriver.exceptions.ActionException;
24 
25 /**
26  * An {@link AccessibilityAction} that clicks on a UiElement.
27  */
28 @TargetApi(18)
29 public abstract class AccessibilityClickAction extends AccessibilityAction {
30 
31   public static final AccessibilityClickAction SINGLE = new SingleClick(1000L);
32   public static final AccessibilityClickAction LONG = new LongClick(1000L);
33   public static final AccessibilityClickAction DOUBLE = new DoubleClick(1000L);
34 
AccessibilityClickAction(long timeoutMillis)35   protected AccessibilityClickAction(long timeoutMillis) {
36     super(timeoutMillis);
37   }
38 
39   public static class DoubleClick extends AccessibilityClickAction {
DoubleClick(long timeoutMillis)40     public DoubleClick(long timeoutMillis) {
41       super(timeoutMillis);
42     }
43 
44     @SuppressWarnings("IdentityBinaryExpression")
45     @Override
perform(AccessibilityNodeInfo node, UiElement element)46     protected boolean perform(AccessibilityNodeInfo node, UiElement element) {
47       return SINGLE.perform(element) && SINGLE.perform(element);
48     }
49   }
50 
51   public static class LongClick extends AccessibilityClickAction {
LongClick(long timeoutMillis)52     public LongClick(long timeoutMillis) {
53       super(timeoutMillis);
54     }
55 
56     @Override
perform(AccessibilityNodeInfo node, UiElement element)57     protected boolean perform(AccessibilityNodeInfo node, UiElement element) {
58       if (!element.isLongClickable()) {
59         throw new ActionException(element
60             + " is not long-clickable; maybe there is a clickable element in the same location?");
61       }
62       return node.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
63     }
64   }
65 
66   public static class SingleClick extends AccessibilityClickAction {
SingleClick(long timeoutMillis)67     public SingleClick(long timeoutMillis) {
68       super(timeoutMillis);
69     }
70 
71     @Override
perform(AccessibilityNodeInfo node, UiElement element)72     protected boolean perform(AccessibilityNodeInfo node, UiElement element) {
73       if (!element.isClickable()) {
74         throw new ActionException(element
75             + " is not clickable; maybe there is a clickable element in the same location?");
76       }
77       return node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
78     }
79   }
80 
81   @Override
toString()82   public String toString() {
83     return getClass().getSimpleName();
84   }
85 }
86