diff options
author | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-26 01:39:49 +0000 |
---|---|---|
committer | oshima@chromium.org <oshima@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-26 01:39:49 +0000 |
commit | 68a8e06e889030ab860eb3c8040d9812d4b2b8fa (patch) | |
tree | 60717f024907613aeb469f16900c2e371d8ca69a /ui | |
parent | fb0f86daed8fed754c67e8fb4828c4c89061ef1d (diff) | |
download | chromium_src-68a8e06e889030ab860eb3c8040d9812d4b2b8fa.zip chromium_src-68a8e06e889030ab860eb3c8040d9812d4b2b8fa.tar.gz chromium_src-68a8e06e889030ab860eb3c8040d9812d4b2b8fa.tar.bz2 |
Use translated root location in WebInputEvent
This adds root_location to LocatedEvent
Converted a couple of tests to use EventGenerator.
BUG=none
TEST=added TranslatedEvent test to window_unittests.
Review URL: http://codereview.chromium.org/9117018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119169 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/aura/event.cc | 26 | ||||
-rw-r--r-- | ui/aura/event.h | 25 | ||||
-rw-r--r-- | ui/aura/event_unittest.cc | 8 | ||||
-rw-r--r-- | ui/aura/root_window.cc | 9 | ||||
-rw-r--r-- | ui/aura/root_window_unittest.cc | 28 | ||||
-rw-r--r-- | ui/aura/test/event_generator.cc | 38 | ||||
-rw-r--r-- | ui/aura/test/event_generator.h | 13 | ||||
-rw-r--r-- | ui/aura/window_unittest.cc | 17 |
8 files changed, 110 insertions, 54 deletions
diff --git a/ui/aura/event.cc b/ui/aura/event.cc index c62ff40..6723e38 100644 --- a/ui/aura/event.cc +++ b/ui/aura/event.cc @@ -95,29 +95,36 @@ LocatedEvent::LocatedEvent(const base::NativeEvent& native_event) : Event(native_event, ui::EventTypeFromNative(native_event), ui::EventFlagsFromNative(native_event)), - location_(ui::EventLocationFromNative(native_event)) { + location_(ui::EventLocationFromNative(native_event)), + root_location_(location_) { } LocatedEvent::LocatedEvent(const LocatedEvent& model, Window* source, Window* target) : Event(model), - location_(model.location_) { + location_(model.location_), + root_location_(model.root_location_) { if (target && target != source) Window::ConvertPointToWindow(source, target, &location_); } LocatedEvent::LocatedEvent(ui::EventType type, const gfx::Point& location, + const gfx::Point& root_location, int flags) : Event(type, flags), - location_(location) { + location_(location), + root_location_(location) { } -void LocatedEvent::UpdateForTransform(const ui::Transform& transform) { +void LocatedEvent::UpdateForRootTransform(const ui::Transform& root_transform) { + // Transform has to be done at root level. + DCHECK_EQ(root_location_.x(), location_.x()); + DCHECK_EQ(root_location_.y(), location_.y()); gfx::Point3f p(location_); - transform.TransformPointReverse(p); - location_ = p.AsPoint(); + root_transform.TransformPointReverse(p); + root_location_ = location_ = p.AsPoint(); } MouseEvent::MouseEvent(const base::NativeEvent& native_event) @@ -142,8 +149,9 @@ MouseEvent::MouseEvent(const MouseEvent& model, MouseEvent::MouseEvent(ui::EventType type, const gfx::Point& location, + const gfx::Point& root_location, int flags) - : LocatedEvent(type, location, flags) { + : LocatedEvent(type, location, root_location, flags) { } // static @@ -256,7 +264,7 @@ TouchEvent::TouchEvent(const TouchEvent& model, TouchEvent::TouchEvent(ui::EventType type, const gfx::Point& location, int touch_id) - : LocatedEvent(type, location, 0), + : LocatedEvent(type, location, location, 0), touch_id_(touch_id), radius_x_(1.0f), radius_y_(1.0f), @@ -359,7 +367,7 @@ GestureEvent::GestureEvent(ui::EventType type, base::Time time_stamp, float delta_x, float delta_y) - : LocatedEvent(type, gfx::Point(x, y), flags), + : LocatedEvent(type, gfx::Point(x, y), gfx::Point(x, y), flags), delta_x_(delta_x), delta_y_(delta_y) { // XXX: Why is aura::Event::time_stamp_ a TimeDelta instead of a Time? diff --git a/ui/aura/event.h b/ui/aura/event.h index 9f97661..04fe46d 100644 --- a/ui/aura/event.h +++ b/ui/aura/event.h @@ -103,8 +103,10 @@ class AURA_EXPORT LocatedEvent : public Event { int x() const { return location_.x(); } int y() const { return location_.y(); } gfx::Point location() const { return location_; } + gfx::Point root_location() const { return root_location_; } - void UpdateForTransform(const ui::Transform& transform); + // Applies the |root_transform| to both |location_| and |root_location_|. + void UpdateForRootTransform(const ui::Transform& root_transform); protected: explicit LocatedEvent(const base::NativeEvent& native_event); @@ -115,10 +117,15 @@ class AURA_EXPORT LocatedEvent : public Event { LocatedEvent(const LocatedEvent& model, Window* source, Window* target); // Used for synthetic events in testing. - LocatedEvent(ui::EventType type, const gfx::Point& location, int flags); + LocatedEvent(ui::EventType type, + const gfx::Point& location, + const gfx::Point& root_location, + int flags); gfx::Point location_; + gfx::Point root_location_; + private: DISALLOW_COPY_AND_ASSIGN(LocatedEvent); }; @@ -139,7 +146,10 @@ class AURA_EXPORT MouseEvent : public LocatedEvent { int flags); // Used for synthetic events in testing and by the gesture recognizer. - MouseEvent(ui::EventType type, const gfx::Point& location, int flags); + MouseEvent(ui::EventType type, + const gfx::Point& location, + const gfx::Point& root_location, + int flags); // Compares two mouse down events and returns true if the second one should // be considered a repeat of the first. @@ -154,6 +164,8 @@ class AURA_EXPORT MouseEvent : public LocatedEvent { void SetClickCount(int click_count); private: + gfx::Point root_location_; + static MouseEvent* last_click_event_; // Returns the repeat count based on the previous mouse click, if it is // recent enough and within a small enough distance. @@ -172,7 +184,9 @@ class AURA_EXPORT TouchEvent : public LocatedEvent { TouchEvent(const TouchEvent& model, Window* source, Window* target); // Used for synthetic events in testing. - TouchEvent(ui::EventType type, const gfx::Point& location, int touch_id); + TouchEvent(ui::EventType type, + const gfx::Point& root_location, + int touch_id); int touch_id() const { return touch_id_; } float radius_x() const { return radius_x_; } @@ -246,8 +260,9 @@ class AURA_EXPORT DropTargetEvent : public LocatedEvent { public: DropTargetEvent(const ui::OSExchangeData& data, const gfx::Point& location, + const gfx::Point& root_location, int source_operations) - : LocatedEvent(ui::ET_DROP_TARGET_EVENT, location, 0), + : LocatedEvent(ui::ET_DROP_TARGET_EVENT, location, root_location, 0), data_(data), source_operations_(source_operations) { } diff --git a/ui/aura/event_unittest.cc b/ui/aura/event_unittest.cc index 8b6b07c..5579031 100644 --- a/ui/aura/event_unittest.cc +++ b/ui/aura/event_unittest.cc @@ -71,7 +71,8 @@ TEST(EventTest, GetCharacter) { } TEST(EventTest, ClickCount) { - MouseEvent mouseev(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), 0); + const gfx::Point origin(0, 0); + MouseEvent mouseev(ui::ET_MOUSE_PRESSED, origin, origin, 0); for (int i = 1; i <=3 ; ++i) { mouseev.SetClickCount(i); EXPECT_EQ(i, mouseev.GetClickCount()); @@ -79,8 +80,9 @@ TEST(EventTest, ClickCount) { } TEST(EventTest, Repeated) { - MouseEvent mouse_ev1(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), 0); - MouseEvent mouse_ev2(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), 0); + const gfx::Point origin(0, 0); + MouseEvent mouse_ev1(ui::ET_MOUSE_PRESSED, origin, origin, 0); + MouseEvent mouse_ev2(ui::ET_MOUSE_PRESSED, origin, origin, 0); MouseEvent::TestApi test_ev1(&mouse_ev1); MouseEvent::TestApi test_ev2(&mouse_ev2); diff --git a/ui/aura/root_window.cc b/ui/aura/root_window.cc index 6dc9b4c..573e8e93 100644 --- a/ui/aura/root_window.cc +++ b/ui/aura/root_window.cc @@ -143,7 +143,7 @@ bool RootWindow::DispatchMouseEvent(MouseEvent* event) { ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON; - event->UpdateForTransform(layer()->transform()); + event->UpdateForRootTransform(layer()->transform()); last_mouse_location_ = event->location(); @@ -188,7 +188,7 @@ bool RootWindow::DispatchKeyEvent(KeyEvent* event) { } bool RootWindow::DispatchScrollEvent(ScrollEvent* event) { - event->UpdateForTransform(layer()->transform()); + event->UpdateForRootTransform(layer()->transform()); last_mouse_location_ = event->location(); @@ -210,7 +210,7 @@ bool RootWindow::DispatchScrollEvent(ScrollEvent* event) { } bool RootWindow::DispatchTouchEvent(TouchEvent* event) { - event->UpdateForTransform(layer()->transform()); + event->UpdateForRootTransform(layer()->transform()); bool handled = false; Window* target = touch_event_handler_ ? touch_event_handler_ : capture_window_; @@ -528,7 +528,8 @@ ui::GestureStatus RootWindow::ProcessGestureEvent(Window* target, }; gesture_handler_ = target; for (ui::EventType* type = types; *type != ui::ET_UNKNOWN; ++type) { - MouseEvent synth(*type, event->location(), event->flags()); + MouseEvent synth( + *type, event->location(), event->root_location(), event->flags()); if (gesture_handler_->delegate()->OnMouseEvent(&synth)) status = ui::GESTURE_STATUS_SYNTH_MOUSE; // The window that was receiving the gestures may have closed/hidden diff --git a/ui/aura/root_window_unittest.cc b/ui/aura/root_window_unittest.cc index f20d50e..0dbfe3e 100644 --- a/ui/aura/root_window_unittest.cc +++ b/ui/aura/root_window_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -75,7 +75,8 @@ TEST_F(RootWindowTest, DispatchMouseEvent) { // Send a mouse event to window1. gfx::Point point(101, 201); - MouseEvent event1(ui::ET_MOUSE_PRESSED, point, ui::EF_LEFT_MOUSE_BUTTON); + MouseEvent event1( + ui::ET_MOUSE_PRESSED, point, point, ui::EF_LEFT_MOUSE_BUTTON); RootWindow::GetInstance()->DispatchMouseEvent(&event1); // Event was tested for non-client area for the target window. @@ -105,6 +106,7 @@ TEST_F(RootWindowTest, MouseButtonState) { event.reset(new MouseEvent( ui::ET_MOUSE_PRESSED, location, + location, ui::EF_LEFT_MOUSE_BUTTON)); root_window->DispatchMouseEvent(event.get()); EXPECT_TRUE(root_window->IsMouseButtonDown()); @@ -113,6 +115,7 @@ TEST_F(RootWindowTest, MouseButtonState) { event.reset(new MouseEvent( ui::ET_MOUSE_PRESSED, location, + location, ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)); root_window->DispatchMouseEvent(event.get()); EXPECT_TRUE(root_window->IsMouseButtonDown()); @@ -121,6 +124,7 @@ TEST_F(RootWindowTest, MouseButtonState) { event.reset(new MouseEvent( ui::ET_MOUSE_RELEASED, location, + location, ui::EF_RIGHT_MOUSE_BUTTON)); root_window->DispatchMouseEvent(event.get()); EXPECT_TRUE(root_window->IsMouseButtonDown()); @@ -129,6 +133,7 @@ TEST_F(RootWindowTest, MouseButtonState) { event.reset(new MouseEvent( ui::ET_MOUSE_RELEASED, location, + location, ui::EF_SHIFT_DOWN)); root_window->DispatchMouseEvent(event.get()); EXPECT_FALSE(root_window->IsMouseButtonDown()); @@ -137,10 +142,29 @@ TEST_F(RootWindowTest, MouseButtonState) { event.reset(new MouseEvent( ui::ET_MOUSE_PRESSED, location, + location, ui::EF_MIDDLE_MOUSE_BUTTON)); root_window->DispatchMouseEvent(event.get()); EXPECT_TRUE(root_window->IsMouseButtonDown()); } +TEST_F(RootWindowTest, TranslatedEvent) { + RootWindow* root_window = RootWindow::GetInstance(); + scoped_ptr<Window> w1(CreateTestWindowWithDelegate(NULL, 1, + gfx::Rect(50, 50, 100, 100), NULL)); + + gfx::Point origin(100, 100); + MouseEvent root(ui::ET_MOUSE_PRESSED, origin, origin, 0); + + EXPECT_EQ("100,100", root.location().ToString()); + EXPECT_EQ("100,100", root.root_location().ToString()); + + MouseEvent translated_event( + root, root_window, w1.get(), + ui::ET_MOUSE_ENTERED, root.flags()); + EXPECT_EQ("50,50", translated_event.location().ToString()); + EXPECT_EQ("100,100", translated_event.root_location().ToString()); +} + } // namespace test } // namespace aura diff --git a/ui/aura/test/event_generator.cc b/ui/aura/test/event_generator.cc index 8366915..2a581d3 100644 --- a/ui/aura/test/event_generator.cc +++ b/ui/aura/test/event_generator.cc @@ -54,7 +54,8 @@ EventGenerator::~EventGenerator() { void EventGenerator::PressLeftButton() { if ((flags_ & ui::EF_LEFT_MOUSE_BUTTON) == 0) { flags_ |= ui::EF_LEFT_MOUSE_BUTTON; - MouseEvent mouseev(ui::ET_MOUSE_PRESSED, current_location_, flags_); + MouseEvent mouseev( + ui::ET_MOUSE_PRESSED, current_location_, current_location_, flags_); Dispatch(mouseev); } } @@ -62,7 +63,8 @@ void EventGenerator::PressLeftButton() { void EventGenerator::ReleaseLeftButton() { if (flags_ & ui::EF_LEFT_MOUSE_BUTTON) { flags_ ^= ui::EF_LEFT_MOUSE_BUTTON; - MouseEvent mouseev(ui::ET_MOUSE_RELEASED, current_location_, 0); + MouseEvent mouseev( + ui::ET_MOUSE_RELEASED, current_location_, current_location_, 0); Dispatch(mouseev); } } @@ -79,25 +81,29 @@ void EventGenerator::DoubleClickLeftButton() { ReleaseLeftButton(); } -void EventGenerator::MoveMouseTo(const gfx::Point& point) { - if (flags_ & ui::EF_LEFT_MOUSE_BUTTON ) { - MouseEvent middle( - ui::ET_MOUSE_DRAGGED, current_location_.Middle(point), flags_); - Dispatch(middle); - - MouseEvent mouseev(ui::ET_MOUSE_DRAGGED, point, flags_); - Dispatch(mouseev); - } else { - MouseEvent middle( - ui::ET_MOUSE_MOVED, current_location_.Middle(point), flags_); - Dispatch(middle); - - MouseEvent mouseev(ui::ET_MOUSE_MOVED, point, flags_); +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; + const gfx::Point diff = point.Subtract(current_location_); + for (int i = 1; i <= count; i++) { + const gfx::Point move_point = current_location_.Add( + gfx::Point(diff.x() / count * i, diff.y() / count * i)); + MouseEvent mouseev(event_type, move_point, move_point, flags_); Dispatch(mouseev); } current_location_ = point; } +void EventGenerator::MoveMouseRelativeTo(const Window* window, + const gfx::Point& point) { + gfx::Point root_point(point); + aura::RootWindow* root_window = aura::RootWindow::GetInstance(); + aura::Window::ConvertPointToWindow(window, root_window, &root_point); + + MoveMouseTo(root_point); +} + void EventGenerator::DragMouseTo(const gfx::Point& point) { PressLeftButton(); MoveMouseTo(point); diff --git a/ui/aura/test/event_generator.h b/ui/aura/test/event_generator.h index 7859010..94bdfdaf 100644 --- a/ui/aura/test/event_generator.h +++ b/ui/aura/test/event_generator.h @@ -47,12 +47,21 @@ class EventGenerator { void DoubleClickLeftButton(); // Generates events to move mouse to be the given |point|. - void MoveMouseTo(const gfx::Point& point); - + void MoveMouseTo(const gfx::Point& point, int count); + void MoveMouseTo(const gfx::Point& point) { + MoveMouseTo(point, 1); + } void MoveMouseTo(int x, int y) { MoveMouseTo(gfx::Point(x, y)); } + // Generates events to move mouse to be the given |point| in |window|'s + // coordinates. + void MoveMouseRelativeTo(const Window* window, const gfx::Point& point); + void MoveMouseRelativeTo(const Window* window, int x, int y) { + MoveMouseRelativeTo(window, gfx::Point(x, y)); + } + void MoveMouseBy(int x, int y) { MoveMouseTo(current_location_.Add(gfx::Point(x, y))); } diff --git a/ui/aura/window_unittest.cc b/ui/aura/window_unittest.cc index a3a781d..a5bd458 100644 --- a/ui/aura/window_unittest.cc +++ b/ui/aura/window_unittest.cc @@ -492,7 +492,7 @@ TEST_F(WindowTest, ChangeCaptureWhileMouseDown) { window->ReleaseCapture(); w2->SetCapture(); delegate2.set_mouse_event_count(0); - generator.MoveMouseTo(gfx::Point(40, 40)); + generator.MoveMouseTo(gfx::Point(40, 40), 2); EXPECT_EQ(0, delegate.mouse_event_count()); EXPECT_EQ(2, delegate2.mouse_event_count()); } @@ -567,8 +567,6 @@ class MouseEnterExitWindowDelegate : public TestWindowDelegate { // Verifies that the WindowDelegate receives MouseExit and MouseEnter events for // mouse transitions from window to window. TEST_F(WindowTest, MouseEnterExit) { - RootWindow* root_window = RootWindow::GetInstance(); - MouseEnterExitWindowDelegate d1; scoped_ptr<Window> w1( CreateTestWindowWithDelegate(&d1, 1, gfx::Rect(10, 10, 50, 50), NULL)); @@ -576,21 +574,14 @@ TEST_F(WindowTest, MouseEnterExit) { scoped_ptr<Window> w2( CreateTestWindowWithDelegate(&d2, 2, gfx::Rect(70, 70, 50, 50), NULL)); - gfx::Point move_point = w1->bounds().CenterPoint(); - Window::ConvertPointToWindow(w1->parent(), root_window, &move_point); - MouseEvent mouseev1(ui::ET_MOUSE_MOVED, move_point, 0); - root_window->DispatchMouseEvent(&mouseev1); - + test::EventGenerator generator; + generator.MoveMouseToCenterOf(w1.get()); EXPECT_TRUE(d1.entered()); EXPECT_FALSE(d1.exited()); EXPECT_FALSE(d2.entered()); EXPECT_FALSE(d2.exited()); - move_point = w2->bounds().CenterPoint(); - Window::ConvertPointToWindow(w2->parent(), root_window, &move_point); - MouseEvent mouseev2(ui::ET_MOUSE_MOVED, move_point, 0); - root_window->DispatchMouseEvent(&mouseev2); - + generator.MoveMouseToCenterOf(w2.get()); EXPECT_TRUE(d1.entered()); EXPECT_TRUE(d1.exited()); EXPECT_TRUE(d2.entered()); |