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.actions.ScrollAction;
24import io.appium.droiddriver.scroll.Direction.PhysicalDirection;
25import io.appium.droiddriver.util.Strings;
26
27/**
28 * An {@link AccessibilityAction} that scrolls an UiElement.
29 */
30@TargetApi(18)
31public class AccessibilityScrollAction extends AccessibilityAction implements ScrollAction {
32  private final PhysicalDirection direction;
33
34  public AccessibilityScrollAction(PhysicalDirection direction) {
35    this(direction, 1000L);
36  }
37
38  public AccessibilityScrollAction(PhysicalDirection direction, long timeoutMillis) {
39    super(timeoutMillis);
40    this.direction = direction;
41  }
42
43  @Override
44  protected boolean perform(AccessibilityNodeInfo node, UiElement element) {
45    if (!element.isScrollable()) {
46      return false;
47    }
48
49    switch (direction) {
50      case UP:
51      case LEFT:
52        return node.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
53      case DOWN:
54      case RIGHT:
55        return node.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
56    }
57    return false;
58  }
59
60  @Override
61  public String toString() {
62    return Strings.toStringHelper(this).addValue(direction).toString();
63  }
64}
65