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