Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Unified Diff: views/view_unittest.cc

Issue 8521001: Touch to Mouse conversion validation of views::GestureManager (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Incorporated gyp changes Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « views/test/test_event_utils_x.cc ('k') | views/views.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: views/view_unittest.cc
diff --git a/views/view_unittest.cc b/views/view_unittest.cc
index d1b032d109c59cdaa47549c31149f2a1cbf4ca7e..9933d9c37e7cd1062c3c29fe6b69e196eeeee8e8 100644
--- a/views/view_unittest.cc
+++ b/views/view_unittest.cc
@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+// Enable testing code in views/event/event.h by turning on TESTING macro.
+#define TESTING 1
+
#include <map>
#include "base/memory/scoped_ptr.h"
@@ -29,6 +32,7 @@
#include "views/events/event.h"
#include "views/focus/accelerator_handler.h"
#include "views/focus/view_storage.h"
+#include "views/test/test_event_utils.h"
#include "views/test/views_test_base.h"
#include "views/touchui/gesture_manager.h"
#include "views/view.h"
@@ -274,6 +278,46 @@ class TestViewIgnoreTouch : public TestView {
virtual ui::TouchStatus OnTouchEvent(const TouchEvent& event) OVERRIDE;
};
+// Test GestureManager for touch to mouse conversion.
+class TestGestureManager : public GestureManager {
+ public:
+ // Reset all test state.
+ void Reset() {
+ last_touch_event_ = 0;
+ last_view_ = NULL;
+ previously_handled_flag_ = false;
+ dispatched_synthetic_event_ = false;
+ }
+
+ bool ProcessTouchEventForGesture(const TouchEvent& event,
+ View* source,
+ ui::TouchStatus status);
+ TestGestureManager();
+
+ bool previously_handled_flag_;
+ int last_touch_event_;
+ View *last_view_;
+ bool dispatched_synthetic_event_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestGestureManager);
+};
+
+// A TouchEvent with filled in native event part to ease touch to
+// mouse conversion.
+class MockTouchEvent : public TouchEvent {
+ public:
+ // Create a new touch event.
+ MockTouchEvent(ui::EventType type,
+ int x,
+ int y,
+ int flags,
+ int touch_id,
+ float radius_x,
+ float radius_y,
+ float angle,
+ float force);
+};
+
////////////////////////////////////////////////////////////////////////////////
// OnBoundsChanged
////////////////////////////////////////////////////////////////////////////////
@@ -537,6 +581,106 @@ TEST_F(ViewTest, TouchEvent) {
widget->CloseNow();
}
+bool TestGestureManager::ProcessTouchEventForGesture(
+ const TouchEvent& event,
+ View* source,
+ ui::TouchStatus status) {
+ if (status != ui::TOUCH_STATUS_UNKNOWN) {
+ dispatched_synthetic_event_ = false;
+ return false;
+ }
+ last_touch_event_ = event.type();
+ last_view_ = source;
+ previously_handled_flag_ = status != ui::TOUCH_STATUS_UNKNOWN;
+ return dispatched_synthetic_event_ =
+ GestureManager::ProcessTouchEventForGesture(event, source, status);
+}
+
+TestGestureManager::TestGestureManager() {
+}
+
+MockTouchEvent::MockTouchEvent(ui::EventType type,
+ int x,
+ int y,
+ int flags,
+ int touch_id,
+ float radius_x,
+ float radius_y,
+ float angle,
+ float force)
+ : TouchEvent(type, x, y, flags, touch_id, radius_x, radius_y,
+ angle, force) {
+#if defined(USE_X11)
+ // TODO(Gajen): Provide support for non-X platforms.
+
+ // Set NativeEvent and its internal fields.
+ base::NativeEvent native_event = views::GetNativeEventForTesting();
+ views::SetNativeEventLocationForTesting(native_event, x, y);
+#endif
+ set_native_event(native_event);
+}
+
+// Tests Real GestureManager instead of the mock one and validates Touch event
+// to Mouse conversion when view hierarchy doesn't handle touch events.
+#if defined(USE_X11)
+// TODO(Gajen): Add support for non-X platforms.
+TEST_F(ViewTest, TouchToMouse) {
+ TestGestureManager gm;
+
+ TestView* v1 = new TestViewIgnoreTouch();
+ v1->SetBounds(0, 0, 300, 300);
+
+ TestView* v2 = new TestViewIgnoreTouch();
+ v2->SetBounds(100, 100, 100, 100);
+
+ scoped_ptr<Widget> widget(new Widget());
+ Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
+ params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
+ params.bounds = gfx::Rect(50, 50, 650, 650);
+ widget->Init(params);
+ View* root = widget->GetRootView();
+ root->AddChildView(v1);
+ static_cast<internal::RootView*>(root)->SetGestureManagerForTesting(&gm);
+ v1->AddChildView(v2);
+
+ // Test press.
+ v1->Reset();
+ v2->Reset();
+ gm.Reset();
+
+ MockTouchEvent pressed(ui::ET_TOUCH_PRESSED,
+ 110,
+ 120,
+ 0, /* no flags */
+ 0, /* first finger touch */
+ 1.0, 0.0, 1.0, 0.0);
+ root->OnTouchEvent(pressed);
+
+ EXPECT_EQ(v2->last_touch_event_type_, 0);
+ EXPECT_EQ(v2->location_.x(), 10);
+ EXPECT_EQ(v2->location_.y(), 20);
+ // Make sure v1 did not receive the event
+ EXPECT_EQ(v1->last_touch_event_type_, 0);
+
+ // Since v1 and v2 didn't handled the touch-event, the gesture manager should
+ // handle it.
+ EXPECT_EQ(gm.last_touch_event_, ui::ET_TOUCH_PRESSED);
+ EXPECT_EQ(root, gm.last_view_);
+ EXPECT_EQ(gm.previously_handled_flag_, false);
+ EXPECT_EQ(gm.dispatched_synthetic_event_, true);
+
+ // Check GestureManager converted Touch event to Mouse and propogates to v2
+ // but v1.
+ EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_PRESSED);
+ EXPECT_EQ(v2->location_.x(), 10);
+ EXPECT_EQ(v2->location_.y(), 20);
+ EXPECT_EQ(v1->last_mouse_event_type_, 0);
+ EXPECT_EQ(v1->location_.x(), 0);
+ EXPECT_EQ(v1->location_.y(), 0);
+ widget->CloseNow();
+}
+#endif
+
////////////////////////////////////////////////////////////////////////////////
// Painting
////////////////////////////////////////////////////////////////////////////////
« no previous file with comments | « views/test/test_event_utils_x.cc ('k') | views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698