• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from __future__ import annotations
6
7import datetime as dt
8from typing import TYPE_CHECKING, Type
9
10from crossbench.action_runner.action.action import ACTION_TIMEOUT, ActionT
11from crossbench.action_runner.action.action_type import ActionType
12from crossbench.action_runner.action.base_duration import DurationAction
13from crossbench.parse import NumberParser
14
15if TYPE_CHECKING:
16  from crossbench.action_runner.base import ActionRunner
17  from crossbench.config import ConfigParser
18  from crossbench.runner.run import Run
19  from crossbench.types import JsonDict
20
21
22class SwipeAction(DurationAction):
23  TYPE: ActionType = ActionType.SWIPE
24
25  @classmethod
26  def config_parser(cls: Type[ActionT]) -> ConfigParser[ActionT]:
27    parser = super().config_parser()
28    parser.add_argument(
29        "start_x",
30        aliases=("startx",),
31        type=NumberParser.any_int,
32        required=True)
33    parser.add_argument(
34        "start_y",
35        aliases=("starty",),
36        type=NumberParser.any_int,
37        required=True)
38    parser.add_argument(
39        "end_x", aliases=("endx",), type=NumberParser.any_int, required=True)
40    parser.add_argument(
41        "end_y", aliases=("endy",), type=NumberParser.any_int, required=True)
42    return parser
43
44  def __init__(self,
45               start_x: int,
46               start_y: int,
47               end_x: int,
48               end_y: int,
49               duration: dt.timedelta = dt.timedelta(seconds=1),
50               timeout: dt.timedelta = ACTION_TIMEOUT,
51               index: int = 0) -> None:
52    self._start_x: int = start_x
53    self._start_y: int = start_y
54    self._end_x: int = end_x
55    self._end_y: int = end_y
56    super().__init__(duration, timeout, index)
57
58  @property
59  def start_x(self) -> int:
60    return self._start_x
61
62  @property
63  def start_y(self) -> int:
64    return self._start_y
65
66  @property
67  def end_x(self) -> int:
68    return self._end_x
69
70  @property
71  def end_y(self) -> int:
72    return self._end_y
73
74  def run_with(self, run: Run, action_runner: ActionRunner) -> None:
75    action_runner.swipe(run, self)
76
77  def to_json(self) -> JsonDict:
78    details = super().to_json()
79    details["start_x"] = self._start_x
80    details["start_y"] = self._start_y
81    details["end_x"] = self._end_x
82    details["end_y"] = self._end_y
83    return details
84