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;
18
19import android.graphics.Rect;
20import android.os.SystemClock;
21import android.view.ViewConfiguration;
22
23import io.appium.droiddriver.UiElement;
24import io.appium.droiddriver.util.Events;
25
26/**
27 * An action that does clicks on an UiElement.
28 */
29public abstract class ClickAction extends EventAction {
30
31  public static final ClickAction SINGLE = new SingleClick(1000L);
32  public static final ClickAction LONG = new LongClick(1000L);
33  public static final ClickAction DOUBLE = new DoubleClick(1000L);
34
35  private static final long CLICK_DURATION_MILLIS = 100L;
36
37  public static class DoubleClick extends ClickAction {
38    public DoubleClick(long timeoutMillis) {
39      super(timeoutMillis);
40    }
41
42    @Override
43    public boolean perform(InputInjector injector, UiElement element) {
44      SINGLE.perform(element);
45      SINGLE.perform(element);
46      return true;
47    }
48  }
49
50  public static class LongClick extends ClickAction {
51    public LongClick(long timeoutMillis) {
52      super(timeoutMillis);
53    }
54
55    @Override
56    public boolean perform(InputInjector injector, UiElement element) {
57      Rect elementRect = element.getVisibleBounds();
58      long downTime = Events.touchDown(injector, elementRect.centerX(), elementRect.centerY());
59      // see android.test.TouchUtils - *1.5 to make sure it's long press
60      SystemClock.sleep((long) (ViewConfiguration.getLongPressTimeout() * 1.5));
61      Events.touchUp(injector, downTime, elementRect.centerX(), elementRect.centerY());
62      return true;
63    }
64  }
65
66  public static class SingleClick extends ClickAction {
67    public SingleClick(long timeoutMillis) {
68      super(timeoutMillis);
69    }
70
71    @Override
72    public boolean perform(InputInjector injector, UiElement element) {
73      Rect elementRect = element.getVisibleBounds();
74      long downTime = Events.touchDown(injector, elementRect.centerX(), elementRect.centerY());
75      // UiAutomator clickAndSync does this, while
76      // android.test.TouchUtils#clickView sleep 1000
77      SystemClock.sleep(CLICK_DURATION_MILLIS);
78      Events.touchUp(injector, downTime, elementRect.centerX(), elementRect.centerY());
79      return true;
80    }
81  }
82
83  protected ClickAction(long timeoutMillis) {
84    super(timeoutMillis);
85  }
86
87  @Override
88  public String toString() {
89    return getClass().getSimpleName();
90  }
91}
92