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