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 */
16package com.google.android.droiddriver.scroll;
17
18import android.graphics.Rect;
19
20import com.google.android.droiddriver.DroidDriver;
21import com.google.android.droiddriver.UiElement;
22import com.google.android.droiddriver.finders.Finder;
23import com.google.android.droiddriver.instrumentation.InstrumentationDriver;
24import com.google.android.droiddriver.scroll.Direction.DirectionConverter;
25import com.google.android.droiddriver.scroll.Direction.PhysicalDirection;
26
27/**
28 * Determines whether scrolling is possible by checking whether the last child
29 * in the logical scroll direction is fully visible. This assumes the count of
30 * children is static, and {@link UiElement#getChildren} includes all children
31 * no matter if it is visible. Currently {@link InstrumentationDriver} behaves
32 * this way.
33 * <p>
34 * This does not work if a child is larger than the physical size of the
35 * container.
36 */
37public class StaticSentinelStrategy extends SentinelStrategy {
38  /**
39   * Defaults to FIRST_CHILD_GETTER for backward scrolling, LAST_CHILD_GETTER
40   * for forward scrolling, and the standard {@link DirectionConverter}.
41   */
42  public static final StaticSentinelStrategy DEFAULT = new StaticSentinelStrategy(
43      FIRST_CHILD_GETTER, LAST_CHILD_GETTER, DirectionConverter.STANDARD_CONVERTER);
44
45  public StaticSentinelStrategy(Getter backwardGetter, Getter forwardGetter,
46      DirectionConverter directionConverter) {
47    super(backwardGetter, forwardGetter, directionConverter);
48  }
49
50  @Override
51  public boolean scroll(DroidDriver driver, Finder containerFinder, PhysicalDirection direction) {
52    UiElement sentinel = getSentinel(driver, containerFinder, direction);
53    UiElement container = sentinel.getParent();
54    // If the last child in the logical scroll direction is fully visible, no
55    // more scrolling is possible
56    Rect visibleBounds = container.getVisibleBounds();
57    if (visibleBounds.contains(sentinel.getBounds())) {
58      return false;
59    }
60
61    doScroll(container, direction);
62    return true;
63  }
64}
65