summaryrefslogtreecommitdiffstats
path: root/chrome/test/webdriver/commands/mouse_commands.cc
blob: 20eeff33ca1e4497fa749255f27c441572429806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/webdriver/commands/mouse_commands.h"

#include "base/values.h"
#include "chrome/common/automation_constants.h"
#include "chrome/test/webdriver/commands/response.h"
#include "chrome/test/webdriver/error_codes.h"
#include "chrome/test/webdriver/session.h"
#include "chrome/test/webdriver/web_element_id.h"
#include "ui/gfx/point.h"
#include "ui/gfx/size.h"

namespace webdriver {

MouseCommand::~MouseCommand() {}

bool MouseCommand::DoesPost() {
  return true;
}

void MouseCommand::ExecutePost(Response* response) {
  bool is_displayed;
  ErrorCode code = session_->IsElementDisplayed(
      session_->current_target(), element, &is_displayed);
  if (code != kSuccess) {
    SET_WEBDRIVER_ERROR(response, "Failed to determine element visibility",
                        code);
    return;
  }
  if (!is_displayed) {
    SET_WEBDRIVER_ERROR(response, "Element must be displayed",
                        kElementNotVisible);
    return;
  }

  gfx::Point location;
  code = session_->GetElementLocationInView(element, &location);
  if (code != kSuccess) {
    SET_WEBDRIVER_ERROR(response, "Failed to compute element location.",
                        code);
    return;
  }

  gfx::Size size;
  code = session_->GetElementSize(session_->current_target(), element, &size);
  if (code != kSuccess) {
    SET_WEBDRIVER_ERROR(response, "Failed to compute element size.", code);
    return;
  }

  location.Offset(size.width() / 2, size.height() / 2);
  bool success = false;
  switch (cmd_) {
    case kClick:
      VLOG(1) << "Mouse click at: (" << location.x() << ", "
              << location.y() << ")" << std::endl;
      success = session_->MouseClick(location, automation::kLeftButton);
      break;

    case kHover:
      VLOG(1) << "Mouse hover at: (" << location.x() << ", "
              << location.y() << ")" << std::endl;
      success = session_->MouseMove(location);
      break;

    case kDrag: {
      gfx::Point drag_to(location);
      drag_to.Offset(drag_x_, drag_y_);
      if (drag_to.x() < 0 || drag_to.y() < 0) {
        SET_WEBDRIVER_ERROR(response, "Invalid pos to drag to", kBadRequest);
        return;
      }

      // click on the element
      VLOG(1) << "Dragging mouse from: "
              << "(" << location.x() << ", " << location.y() << ") "
              << "to: (" << drag_to.x() << ", " << drag_to.y() << ")"
              << std::endl;
      success = session_->MouseDrag(location, drag_to);
      break;
    }

    default:
      SET_WEBDRIVER_ERROR(response, "Unknown mouse request", kUnknownCommand);
      return;
  }

  if (!success) {
    SET_WEBDRIVER_ERROR(response, "Performing mouse operation failed",
                        kUnknownError);
    return;
  }
  response->SetStatus(kSuccess);
}

DragCommand::DragCommand(const std::vector<std::string>& path_segments,
                         const DictionaryValue* const parameters)
    : MouseCommand(path_segments, parameters, kDrag) {}

DragCommand::~DragCommand() {}

bool DragCommand::Init(Response* const response) {
  if (WebElementCommand::Init(response)) {
    if (!GetIntegerParameter("x", &drag_x_) ||
        !GetIntegerParameter("y", &drag_y_)) {
      SET_WEBDRIVER_ERROR(response,
                          "Request is missing the required positional data",
                          kBadRequest);
      return false;
    }
    return true;
  }

  return false;
}

ClickCommand::ClickCommand(const std::vector<std::string>& path_segments,
                           const DictionaryValue* const parameters)
    : MouseCommand(path_segments, parameters, kClick) {}

ClickCommand::~ClickCommand() {}

HoverCommand::HoverCommand(const std::vector<std::string>& path_segments,
                           const DictionaryValue* const parameters)
    : MouseCommand(path_segments, parameters, kHover) {}

HoverCommand::~HoverCommand() {}

}  // namespace webdriver