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 com.google.android.droiddriver.actions.ScrollDirection;
19import com.google.android.droiddriver.exceptions.ActionException;
20
21/**
22 * Interfaces and constants for scroll directions.
23 */
24public interface Direction {
25  /** Logical directions */
26  public enum LogicalDirection {
27    FORWARD, BACKWARD;
28  }
29
30  public enum Axis {
31    HORIZONTAL {
32      private final ScrollDirection[] directions = {ScrollDirection.LEFT, ScrollDirection.RIGHT};
33
34      @Override
35      public ScrollDirection[] getDirections() {
36        return directions;
37      }
38    },
39    VERTICAL {
40      private final ScrollDirection[] directions = {ScrollDirection.UP, ScrollDirection.DOWN};
41
42      @Override
43      public ScrollDirection[] getDirections() {
44        return directions;
45      }
46    };
47    public abstract ScrollDirection[] getDirections();
48  }
49
50  /**
51   * Converts ScrollDirection to LogicalDirection.
52   */
53  public interface PhysicalToLogicalConverter {
54    /**
55     * Converts ScrollDirection to LogicalDirection. It's possible to override
56     * this for RTL (right-to-left) views, for example.
57     */
58    LogicalDirection toLogicalDirection(ScrollDirection direction);
59
60    /** Follows standard directions: up-to-down, left-to-right */
61    PhysicalToLogicalConverter STANDARD_CONVERTER = new PhysicalToLogicalConverter() {
62      @Override
63      public LogicalDirection toLogicalDirection(ScrollDirection direction) {
64        switch (direction) {
65          case UP:
66            return LogicalDirection.BACKWARD;
67          case DOWN:
68            return LogicalDirection.FORWARD;
69          case LEFT:
70            return LogicalDirection.BACKWARD;
71          case RIGHT:
72            return LogicalDirection.FORWARD;
73        }
74        throw new ActionException("Unknown scroll direction: " + direction);
75      }
76    };
77  }
78}
79