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.helpers;
18
19import io.appium.droiddriver.DroidDriver;
20import io.appium.droiddriver.UiElement;
21import io.appium.droiddriver.exceptions.ElementNotFoundException;
22import io.appium.droiddriver.finders.Finder;
23import io.appium.droiddriver.scroll.Direction.PhysicalDirection;
24import io.appium.droiddriver.scroll.Scroller;
25
26/**
27 * Helper for Scroller.
28 */
29public class ScrollerHelper {
30  private final DroidDriver driver;
31  private final Finder containerFinder;
32  private final Scroller scroller;
33
34  public ScrollerHelper(Scroller scroller, DroidDriver driver, Finder containerFinder) {
35    this.scroller = scroller;
36    this.driver = driver;
37    this.containerFinder = containerFinder;
38  }
39
40  /**
41   * Scrolls {@code containerFinder} in both directions if necessary to find
42   * {@code itemFinder}, which is a descendant of {@code containerFinder}.
43   *
44   * @param itemFinder Finder for the desired item; relative to
45   *        {@code containerFinder}
46   * @return the UiElement matching {@code itemFinder}
47   * @throws ElementNotFoundException If no match is found
48   */
49  public UiElement scrollTo(Finder itemFinder) {
50    return scroller.scrollTo(driver, containerFinder, itemFinder);
51  }
52
53  /**
54   * Scrolls {@code containerFinder} in {@code direction} if necessary to find
55   * {@code itemFinder}, which is a descendant of {@code containerFinder}.
56   *
57   * @param itemFinder Finder for the desired item; relative to
58   *        {@code containerFinder}
59   * @param direction specifies where the view port will move instead of the finger
60   * @return the UiElement matching {@code itemFinder}
61   * @throws ElementNotFoundException If no match is found
62   */
63  public UiElement scrollTo(Finder itemFinder, PhysicalDirection direction) {
64    return scroller.scrollTo(driver, containerFinder, itemFinder, direction);
65  }
66
67  /**
68   * Scrolls to {@code itemFinder} and returns true, otherwise returns false.
69   *
70   * @param itemFinder Finder for the desired item
71   * @return true if successful, otherwise false
72   */
73  public boolean canScrollTo(Finder itemFinder) {
74    try {
75      scrollTo(itemFinder);
76      return true;
77    } catch (ElementNotFoundException e) {
78      return false;
79    }
80  }
81}
82