summaryrefslogtreecommitdiffstats
path: root/ui
diff options
context:
space:
mode:
authoroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-11 18:35:41 +0000
committeroshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-11 18:35:41 +0000
commit334e618ffaf56bd97d26f51761a0f59598968cdd (patch)
tree7be2b2ef141cbb18b3ca892bbc680f4f9b08a82a /ui
parent37e4b0c19651785137be8f18657a34a10fec2027 (diff)
downloadchromium_src-334e618ffaf56bd97d26f51761a0f59598968cdd.zip
chromium_src-334e618ffaf56bd97d26f51761a0f59598968cdd.tar.gz
chromium_src-334e618ffaf56bd97d26f51761a0f59598968cdd.tar.bz2
Support multiple displays in EventGenerator
- Introduces EventGeneratorDelegates interface that abstracts coordinates - Added AshEventGeneratorDelegates that uses screen coordinates in ash environment. - Updates several ash tests to use new EventGenerator I'll convert other tests in separate CL. BUG=139024 TEST=covered by tests. Review URL: https://chromiumcodereview.appspot.com/11691010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@176392 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r--ui/aura/test/event_generator.cc180
-rw-r--r--ui/aura/test/event_generator.h60
2 files changed, 181 insertions, 59 deletions
diff --git a/ui/aura/test/event_generator.cc b/ui/aura/test/event_generator.cc
index 6c55ff4c..42deba8 100644
--- a/ui/aura/test/event_generator.cc
+++ b/ui/aura/test/event_generator.cc
@@ -5,6 +5,7 @@
#include "ui/aura/test/event_generator.h"
#include "base/memory/scoped_ptr.h"
+#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/root_window.h"
#include "ui/base/events/event.h"
#include "ui/base/events/event_utils.h"
@@ -19,8 +20,32 @@
#include "ui/base/keycodes/keyboard_code_conversion.h"
#endif
+namespace aura {
+namespace test {
namespace {
+class DefaultEventGeneratorDelegate : public EventGeneratorDelegate {
+ public:
+ explicit DefaultEventGeneratorDelegate(RootWindow* root_window)
+ : root_window_(root_window) {}
+ virtual ~DefaultEventGeneratorDelegate() {}
+
+ // EventGeneratorDelegate overrides:
+ RootWindow* GetRootWindowAt(const gfx::Point& point) const OVERRIDE {
+ return root_window_;
+ }
+
+ client::ScreenPositionClient* GetScreenPositionClient(
+ const aura::Window* window) const OVERRIDE {
+ return NULL;
+ }
+
+ private:
+ RootWindow* root_window_;
+
+ DISALLOW_COPY_AND_ASSIGN(DefaultEventGeneratorDelegate);
+};
+
class TestKeyEvent : public ui::KeyEvent {
public:
TestKeyEvent(const base::NativeEvent& native_event, int flags, bool is_char)
@@ -42,55 +67,49 @@ class TestTouchEvent : public ui::TouchEvent {
DISALLOW_COPY_AND_ASSIGN(TestTouchEvent);
};
-gfx::Point CenterOfWindowInRootWindowCoordinate(aura::RootWindow* root_window,
- aura::Window* window) {
- gfx::Point center = window->bounds().CenterPoint();
- aura::Window::ConvertPointToTarget(window->parent(), root_window, &center);
- return center;
-}
+const int kAllButtonMask = ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON;
} // namespace
-namespace aura {
-namespace test {
-
EventGenerator::EventGenerator(RootWindow* root_window)
- : root_window_(root_window),
- flags_(0) {
+ : delegate_(new DefaultEventGeneratorDelegate(root_window)),
+ current_root_window_(delegate_->GetRootWindowAt(current_location_)),
+ flags_(0),
+ grab_(false) {
}
EventGenerator::EventGenerator(RootWindow* root_window, const gfx::Point& point)
- : root_window_(root_window),
+ : delegate_(new DefaultEventGeneratorDelegate(root_window)),
+ current_location_(point),
+ current_root_window_(delegate_->GetRootWindowAt(current_location_)),
flags_(0),
- current_location_(point) {
+ grab_(false) {
}
EventGenerator::EventGenerator(RootWindow* root_window, Window* window)
- : root_window_(root_window),
+ : delegate_(new DefaultEventGeneratorDelegate(root_window)),
+ current_location_(CenterOfWindow(window)),
+ current_root_window_(delegate_->GetRootWindowAt(current_location_)),
flags_(0),
- current_location_(CenterOfWindowInRootWindowCoordinate(root_window,
- window)) {
+ grab_(false) {
+}
+
+EventGenerator::EventGenerator(EventGeneratorDelegate* delegate)
+ : delegate_(delegate),
+ current_root_window_(delegate_->GetRootWindowAt(current_location_)),
+ flags_(0),
+ grab_(false) {
}
EventGenerator::~EventGenerator() {
}
void EventGenerator::PressLeftButton() {
- if ((flags_ & ui::EF_LEFT_MOUSE_BUTTON) == 0) {
- flags_ |= ui::EF_LEFT_MOUSE_BUTTON;
- ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, current_location_,
- current_location_, flags_);
- Dispatch(mouseev);
- }
+ PressButton(ui::EF_LEFT_MOUSE_BUTTON);
}
void EventGenerator::ReleaseLeftButton() {
- if (flags_ & ui::EF_LEFT_MOUSE_BUTTON) {
- ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, current_location_,
- current_location_, flags_);
- Dispatch(mouseev);
- flags_ ^= ui::EF_LEFT_MOUSE_BUTTON;
- }
+ ReleaseButton(ui::EF_LEFT_MOUSE_BUTTON);
}
void EventGenerator::ClickLeftButton() {
@@ -106,32 +125,26 @@ void EventGenerator::DoubleClickLeftButton() {
}
void EventGenerator::PressRightButton() {
- if ((flags_ & ui::EF_RIGHT_MOUSE_BUTTON) == 0) {
- flags_ |= ui::EF_RIGHT_MOUSE_BUTTON;
- ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, current_location_,
- current_location_, flags_);
- Dispatch(mouseev);
- }
+ PressButton(ui::EF_RIGHT_MOUSE_BUTTON);
}
void EventGenerator::ReleaseRightButton() {
- if (flags_ & ui::EF_RIGHT_MOUSE_BUTTON) {
- ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, current_location_,
- current_location_, flags_);
- Dispatch(mouseev);
- flags_ ^= ui::EF_RIGHT_MOUSE_BUTTON;
- }
+ ReleaseButton(ui::EF_RIGHT_MOUSE_BUTTON);
}
void EventGenerator::MoveMouseTo(const gfx::Point& point, int count) {
DCHECK_GT(count, 0);
const ui::EventType event_type = (flags_ & ui::EF_LEFT_MOUSE_BUTTON) ?
ui::ET_MOUSE_DRAGGED : ui::ET_MOUSE_MOVED;
+
gfx::Vector2dF diff(point - current_location_);
for (float i = 1; i <= count; i++) {
gfx::Vector2dF step(diff);
step.Scale(i / count);
gfx::Point move_point = current_location_ + gfx::ToRoundedVector2d(step);
+ if (!grab_)
+ UpdateCurrentRootWindow(move_point);
+ ConvertPointToTarget(current_root_window_, &move_point);
ui::MouseEvent mouseev(event_type, move_point, move_point, flags_);
Dispatch(mouseev);
}
@@ -139,11 +152,10 @@ void EventGenerator::MoveMouseTo(const gfx::Point& point, int count) {
}
void EventGenerator::MoveMouseRelativeTo(const Window* window,
- const gfx::Point& point) {
- gfx::Point root_point(point);
- Window::ConvertPointToTarget(window, root_window_, &root_point);
-
- MoveMouseTo(root_point);
+ const gfx::Point& point_in_parent) {
+ gfx::Point point(point_in_parent);
+ ConvertPointFromTarget(window, &point);
+ MoveMouseTo(point);
}
void EventGenerator::DragMouseTo(const gfx::Point& point) {
@@ -153,11 +165,12 @@ void EventGenerator::DragMouseTo(const gfx::Point& point) {
}
void EventGenerator::MoveMouseToCenterOf(Window* window) {
- MoveMouseTo(CenterOfWindowInRootWindowCoordinate(root_window_, window));
+ MoveMouseTo(CenterOfWindow(window));
}
void EventGenerator::PressTouch() {
- TestTouchEvent touchev(ui::ET_TOUCH_PRESSED, current_location_, flags_);
+ TestTouchEvent touchev(
+ ui::ET_TOUCH_PRESSED, GetLocationInCurrentRoot(), flags_);
Dispatch(touchev);
}
@@ -166,10 +179,13 @@ void EventGenerator::MoveTouch(const gfx::Point& point) {
Dispatch(touchev);
current_location_ = point;
+ if (!grab_)
+ UpdateCurrentRootWindow(point);
}
void EventGenerator::ReleaseTouch() {
- TestTouchEvent touchev(ui::ET_TOUCH_RELEASED, current_location_, flags_);
+ TestTouchEvent touchev(
+ ui::ET_TOUCH_RELEASED, GetLocationInCurrentRoot(), flags_);
Dispatch(touchev);
}
@@ -180,8 +196,7 @@ void EventGenerator::PressMoveAndReleaseTouchTo(const gfx::Point& point) {
}
void EventGenerator::PressMoveAndReleaseTouchToCenterOf(Window* window) {
- PressMoveAndReleaseTouchTo(CenterOfWindowInRootWindowCoordinate(root_window_,
- window));
+ PressMoveAndReleaseTouchTo(CenterOfWindow(window));
}
void EventGenerator::GestureTapAt(const gfx::Point& location) {
@@ -287,7 +302,7 @@ void EventGenerator::Dispatch(ui::Event& event) {
switch (event.type()) {
case ui::ET_KEY_PRESSED:
case ui::ET_KEY_RELEASED:
- root_window_->AsRootWindowHostDelegate()->OnHostKeyEvent(
+ current_root_window_->AsRootWindowHostDelegate()->OnHostKeyEvent(
static_cast<ui::KeyEvent*>(&event));
break;
case ui::ET_MOUSE_PRESSED:
@@ -297,7 +312,7 @@ void EventGenerator::Dispatch(ui::Event& event) {
case ui::ET_MOUSE_ENTERED:
case ui::ET_MOUSE_EXITED:
case ui::ET_MOUSEWHEEL:
- root_window_->AsRootWindowHostDelegate()->OnHostMouseEvent(
+ current_root_window_->AsRootWindowHostDelegate()->OnHostMouseEvent(
static_cast<ui::MouseEvent*>(&event));
break;
case ui::ET_TOUCH_RELEASED:
@@ -305,7 +320,7 @@ void EventGenerator::Dispatch(ui::Event& event) {
case ui::ET_TOUCH_MOVED:
case ui::ET_TOUCH_STATIONARY:
case ui::ET_TOUCH_CANCELLED:
- root_window_->AsRootWindowHostDelegate()->OnHostTouchEvent(
+ current_root_window_->AsRootWindowHostDelegate()->OnHostTouchEvent(
static_cast<ui::TouchEvent*>(&event));
break;
default:
@@ -345,5 +360,64 @@ void EventGenerator::DispatchKeyEvent(bool is_press,
Dispatch(keyev);
}
+void EventGenerator::UpdateCurrentRootWindow(const gfx::Point& point) {
+ current_root_window_ = delegate_->GetRootWindowAt(point);
+}
+
+void EventGenerator::PressButton(int flag) {
+ if (!(flags_ & flag)) {
+ flags_ |= flag;
+ grab_ = flags_ & kAllButtonMask;
+ gfx::Point location = GetLocationInCurrentRoot();
+ ui::MouseEvent mouseev(ui::ET_MOUSE_PRESSED, location, location, flags_);
+ Dispatch(mouseev);
+ }
+}
+
+void EventGenerator::ReleaseButton(int flag) {
+ if (flags_ & flag) {
+ gfx::Point location = GetLocationInCurrentRoot();
+ ui::MouseEvent mouseev(ui::ET_MOUSE_RELEASED, location,
+ location, flags_);
+ Dispatch(mouseev);
+ flags_ ^= flag;
+ }
+ grab_ = flags_ & kAllButtonMask;
+}
+
+void EventGenerator::ConvertPointFromTarget(const aura::Window* target,
+ gfx::Point* point) const {
+ DCHECK(point);
+ aura::client::ScreenPositionClient* client =
+ delegate_->GetScreenPositionClient(target);
+ if (client)
+ client->ConvertPointToScreen(target, point);
+ else
+ aura::Window::ConvertPointToTarget(target, target->GetRootWindow(), point);
+}
+
+void EventGenerator::ConvertPointToTarget(const aura::Window* target,
+ gfx::Point* point) const {
+ DCHECK(point);
+ aura::client::ScreenPositionClient* client =
+ delegate_->GetScreenPositionClient(target);
+ if (client)
+ client->ConvertPointFromScreen(target, point);
+ else
+ aura::Window::ConvertPointToTarget(target->GetRootWindow(), target, point);
+}
+
+gfx::Point EventGenerator::GetLocationInCurrentRoot() const {
+ gfx::Point p(current_location_);
+ ConvertPointToTarget(current_root_window_, &p);
+ return p;
+}
+
+gfx::Point EventGenerator::CenterOfWindow(const Window* window) const {
+ gfx::Point center = gfx::Rect(window->bounds().size()).CenterPoint();
+ ConvertPointFromTarget(window, &center);
+ return center;
+}
+
} // namespace test
} // namespace aura
diff --git a/ui/aura/test/event_generator.h b/ui/aura/test/event_generator.h
index 1e1626c..f57f242 100644
--- a/ui/aura/test/event_generator.h
+++ b/ui/aura/test/event_generator.h
@@ -6,6 +6,7 @@
#define UI_AURA_TEST_EVENT_GENERATOR_H_
#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/gfx/point.h"
@@ -21,20 +22,47 @@ namespace aura {
class RootWindow;
class Window;
+namespace client {
+class ScreenPositionClient;
+}
+
namespace test {
-// EventGenerator is a tool that generates and dispatch events.
+// A delegate interface for EventGenerator that provides a way to
+// locate aura root window for given point.
+class EventGeneratorDelegate {
+ public:
+ virtual ~EventGeneratorDelegate() {}
+
+ // Returns a root window for given point.
+ virtual RootWindow* GetRootWindowAt(const gfx::Point& point) const = 0;
+
+ // Returns the screen position client that determines the
+ // coordinates used in EventGenerator. EventGenerator uses
+ // RootWindow's coordinate if this retruns NULL.
+ virtual client::ScreenPositionClient* GetScreenPositionClient(
+ const aura::Window* window) const = 0;
+};
+
+// EventGenerator is a tool that generates and dispatch events. The
+// coordinates of the points in API is determined by the
+// EventGeneratorDelegate.
class EventGenerator {
public:
- // Creates an EventGenerator with the mouse/touch location (0,0).
+ // Creates an EventGenerator with the mouse/touch location (0,0),
+ // which uses the |root_window|'s coordinates.
explicit EventGenerator(RootWindow* root_window);
+ // Create an EventGenerator with EventGeneratorDelegate,
+ // which uses the coordinates used by |delegate|.
+ explicit EventGenerator(EventGeneratorDelegate* delegate);
+
// Creates an EventGenerator with the mouse/touch location
- // at |initial_location|.
+ // at |initial_location|, which uses the |root_window|'s coordinates.
EventGenerator(RootWindow* root_window, const gfx::Point& initial_location);
// Creates an EventGenerator with the mouse/touch location
- // centered over |window|.
+ // centered over |window|, which uses the |root_window|'s coordinates.
EventGenerator(RootWindow* root_window, Window* window);
virtual ~EventGenerator();
@@ -176,13 +204,33 @@ class EventGenerator {
// Dispatch the |event| to the RootWindow.
void Dispatch(ui::Event& event);
+ void set_current_root_window(RootWindow* root_window) {
+ current_root_window_ = root_window;
+ }
+
private:
// Dispatch a key event to the RootWindow.
void DispatchKeyEvent(bool is_press, ui::KeyboardCode key_code, int flags);
- RootWindow* root_window_;
- int flags_;
+ void UpdateCurrentRootWindow(const gfx::Point& point);
+ void PressButton(int flag);
+ void ReleaseButton(int flag);
+
+ // Convert a point between API's coordinates and
+ // |target|'s coordinates.
+ void ConvertPointFromTarget(const aura::Window* target,
+ gfx::Point* point) const;
+ void ConvertPointToTarget(const aura::Window* target,
+ gfx::Point* point) const;
+
+ gfx::Point GetLocationInCurrentRoot() const;
+ gfx::Point CenterOfWindow(const Window* window) const;
+
+ scoped_ptr<EventGeneratorDelegate> delegate_;
gfx::Point current_location_;
+ RootWindow* current_root_window_;
+ int flags_;
+ bool grab_;
DISALLOW_COPY_AND_ASSIGN(EventGenerator);
};