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 com.google.android.droiddriver.actions; 18 19import android.graphics.Rect; 20import android.os.SystemClock; 21import android.view.ViewConfiguration; 22 23import com.google.android.droiddriver.InputInjector; 24import com.google.android.droiddriver.UiElement; 25import com.google.android.droiddriver.exceptions.ActionException; 26import com.google.android.droiddriver.util.Events; 27 28/** 29 * A {@link ScrollAction} that swipes the touch screen. 30 */ 31public class SwipeAction extends ScrollAction { 32 33 private final ScrollDirection direction; 34 private final boolean drag; 35 36 /** 37 * Defaults timeoutMillis to 0. 38 */ 39 public SwipeAction(ScrollDirection direction, boolean drag) { 40 this(direction, drag, 0L); 41 } 42 43 public SwipeAction(ScrollDirection direction, boolean drag, long timeoutMillis) { 44 super(timeoutMillis); 45 this.direction = direction; 46 this.drag = drag; 47 } 48 49 @Override 50 public boolean perform(InputInjector injector, UiElement element) { 51 Rect elementRect = element.getVisibleBounds(); 52 53 int swipeAreaHeightAdjust = (int) (elementRect.height() * 0.1); 54 int swipeAreaWidthAdjust = (int) (elementRect.width() * 0.1); 55 int steps = 50; 56 int startX; 57 int startY; 58 int endX; 59 int endY; 60 61 switch (direction) { 62 case DOWN: 63 startX = elementRect.centerX(); 64 startY = elementRect.bottom - swipeAreaHeightAdjust; 65 endX = elementRect.centerX(); 66 endY = elementRect.top + swipeAreaHeightAdjust; 67 break; 68 case UP: 69 startX = elementRect.centerX(); 70 startY = elementRect.top + swipeAreaHeightAdjust; 71 endX = elementRect.centerX(); 72 endY = elementRect.bottom - swipeAreaHeightAdjust; 73 break; 74 case LEFT: 75 startX = elementRect.left + swipeAreaWidthAdjust; 76 startY = elementRect.centerY(); 77 endX = elementRect.right - swipeAreaWidthAdjust; 78 endY = elementRect.centerY(); 79 break; 80 case RIGHT: 81 startX = elementRect.right - swipeAreaWidthAdjust; 82 startY = elementRect.centerY(); 83 endX = elementRect.left + swipeAreaHeightAdjust; 84 endY = elementRect.centerY(); 85 break; 86 default: 87 throw new ActionException("Unknown scroll direction: " + direction); 88 } 89 90 double xStep = ((double) (endX - startX)) / steps; 91 double yStep = ((double) (endY - startY)) / steps; 92 93 // First touch starts exactly at the point requested 94 long downTime = Events.touchDown(injector, startX, startY); 95 if (drag) { 96 SystemClock.sleep((long) (ViewConfiguration.getLongPressTimeout() * 1.5f)); 97 } 98 for (int i = 1; i < steps; i++) { 99 Events.touchMove(injector, downTime, startX + (int) (xStep * i), startY + (int) (yStep * i)); 100 SystemClock.sleep(5); 101 } 102 if (drag) { 103 // Hold final position for a little bit to simulate drag. 104 SystemClock.sleep(100); 105 } 106 Events.touchUp(injector, downTime, endX, endY); 107 return true; 108 } 109} 110