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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « views/test/test_event_utils_x.cc ('k') | views/views.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Enable testing code in views/event/event.h by turning on TESTING macro.
6 #define TESTING 1
7
5 #include <map> 8 #include <map>
6 9
7 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
8 #include "base/rand_util.h" 11 #include "base/rand_util.h"
9 #include "base/string_util.h" 12 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
11 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
12 #include "ui/base/clipboard/clipboard.h" 15 #include "ui/base/clipboard/clipboard.h"
13 #include "ui/base/keycodes/keyboard_codes.h" 16 #include "ui/base/keycodes/keyboard_codes.h"
14 #include "ui/base/models/simple_menu_model.h" 17 #include "ui/base/models/simple_menu_model.h"
15 #include "ui/gfx/canvas_skia.h" 18 #include "ui/gfx/canvas_skia.h"
16 #include "ui/gfx/compositor/compositor.h" 19 #include "ui/gfx/compositor/compositor.h"
17 #include "ui/gfx/compositor/layer.h" 20 #include "ui/gfx/compositor/layer.h"
18 #include "ui/gfx/compositor/layer_animator.h" 21 #include "ui/gfx/compositor/layer_animator.h"
19 #include "ui/gfx/compositor/test_compositor.h" 22 #include "ui/gfx/compositor/test_compositor.h"
20 #include "ui/gfx/compositor/test_texture.h" 23 #include "ui/gfx/compositor/test_texture.h"
21 #include "ui/gfx/path.h" 24 #include "ui/gfx/path.h"
22 #include "ui/gfx/transform.h" 25 #include "ui/gfx/transform.h"
23 #include "views/background.h" 26 #include "views/background.h"
24 #include "views/controls/button/button_dropdown.h" 27 #include "views/controls/button/button_dropdown.h"
25 #include "views/controls/button/checkbox.h" 28 #include "views/controls/button/checkbox.h"
26 #include "views/controls/native/native_view_host.h" 29 #include "views/controls/native/native_view_host.h"
27 #include "views/controls/scroll_view.h" 30 #include "views/controls/scroll_view.h"
28 #include "views/controls/textfield/textfield.h" 31 #include "views/controls/textfield/textfield.h"
29 #include "views/events/event.h" 32 #include "views/events/event.h"
30 #include "views/focus/accelerator_handler.h" 33 #include "views/focus/accelerator_handler.h"
31 #include "views/focus/view_storage.h" 34 #include "views/focus/view_storage.h"
35 #include "views/test/test_event_utils.h"
32 #include "views/test/views_test_base.h" 36 #include "views/test/views_test_base.h"
33 #include "views/touchui/gesture_manager.h" 37 #include "views/touchui/gesture_manager.h"
34 #include "views/view.h" 38 #include "views/view.h"
35 #include "views/views_delegate.h" 39 #include "views/views_delegate.h"
36 #include "views/widget/native_widget.h" 40 #include "views/widget/native_widget.h"
37 #include "views/widget/root_view.h" 41 #include "views/widget/root_view.h"
38 #include "views/window/dialog_delegate.h" 42 #include "views/window/dialog_delegate.h"
39 43
40 #if defined(OS_WIN) 44 #if defined(OS_WIN)
41 #include "views/test/test_views_delegate.h" 45 #include "views/test/test_views_delegate.h"
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 // A view subclass that ignores all touch events for testing purposes. 271 // A view subclass that ignores all touch events for testing purposes.
268 class TestViewIgnoreTouch : public TestView { 272 class TestViewIgnoreTouch : public TestView {
269 public: 273 public:
270 TestViewIgnoreTouch() : TestView() {} 274 TestViewIgnoreTouch() : TestView() {}
271 virtual ~TestViewIgnoreTouch() {} 275 virtual ~TestViewIgnoreTouch() {}
272 276
273 private: 277 private:
274 virtual ui::TouchStatus OnTouchEvent(const TouchEvent& event) OVERRIDE; 278 virtual ui::TouchStatus OnTouchEvent(const TouchEvent& event) OVERRIDE;
275 }; 279 };
276 280
281 // Test GestureManager for touch to mouse conversion.
282 class TestGestureManager : public GestureManager {
283 public:
284 // Reset all test state.
285 void Reset() {
286 last_touch_event_ = 0;
287 last_view_ = NULL;
288 previously_handled_flag_ = false;
289 dispatched_synthetic_event_ = false;
290 }
291
292 bool ProcessTouchEventForGesture(const TouchEvent& event,
293 View* source,
294 ui::TouchStatus status);
295 TestGestureManager();
296
297 bool previously_handled_flag_;
298 int last_touch_event_;
299 View *last_view_;
300 bool dispatched_synthetic_event_;
301
302 DISALLOW_COPY_AND_ASSIGN(TestGestureManager);
303 };
304
305 // A TouchEvent with filled in native event part to ease touch to
306 // mouse conversion.
307 class MockTouchEvent : public TouchEvent {
308 public:
309 // Create a new touch event.
310 MockTouchEvent(ui::EventType type,
311 int x,
312 int y,
313 int flags,
314 int touch_id,
315 float radius_x,
316 float radius_y,
317 float angle,
318 float force);
319 };
320
277 //////////////////////////////////////////////////////////////////////////////// 321 ////////////////////////////////////////////////////////////////////////////////
278 // OnBoundsChanged 322 // OnBoundsChanged
279 //////////////////////////////////////////////////////////////////////////////// 323 ////////////////////////////////////////////////////////////////////////////////
280 324
281 void TestView::OnBoundsChanged(const gfx::Rect& previous_bounds) { 325 void TestView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
282 did_change_bounds_ = true; 326 did_change_bounds_ = true;
283 new_bounds_ = bounds(); 327 new_bounds_ = bounds();
284 } 328 }
285 329
286 TEST_F(ViewTest, OnBoundsChanged) { 330 TEST_F(ViewTest, OnBoundsChanged) {
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 // Make sure v1 did not receive the event 574 // Make sure v1 did not receive the event
531 EXPECT_EQ(v1->last_touch_event_type_, 0); 575 EXPECT_EQ(v1->last_touch_event_type_, 0);
532 576
533 EXPECT_EQ(gm.last_touch_event_, 0); 577 EXPECT_EQ(gm.last_touch_event_, 0);
534 EXPECT_EQ(NULL, gm.last_view_); 578 EXPECT_EQ(NULL, gm.last_view_);
535 EXPECT_EQ(gm.previously_handled_flag_, false); 579 EXPECT_EQ(gm.previously_handled_flag_, false);
536 580
537 widget->CloseNow(); 581 widget->CloseNow();
538 } 582 }
539 583
584 bool TestGestureManager::ProcessTouchEventForGesture(
585 const TouchEvent& event,
586 View* source,
587 ui::TouchStatus status) {
588 if (status != ui::TOUCH_STATUS_UNKNOWN) {
589 dispatched_synthetic_event_ = false;
590 return false;
591 }
592 last_touch_event_ = event.type();
593 last_view_ = source;
594 previously_handled_flag_ = status != ui::TOUCH_STATUS_UNKNOWN;
595 return dispatched_synthetic_event_ =
596 GestureManager::ProcessTouchEventForGesture(event, source, status);
597 }
598
599 TestGestureManager::TestGestureManager() {
600 }
601
602 MockTouchEvent::MockTouchEvent(ui::EventType type,
603 int x,
604 int y,
605 int flags,
606 int touch_id,
607 float radius_x,
608 float radius_y,
609 float angle,
610 float force)
611 : TouchEvent(type, x, y, flags, touch_id, radius_x, radius_y,
612 angle, force) {
613 #if defined(USE_X11)
614 // TODO(Gajen): Provide support for non-X platforms.
615
616 // Set NativeEvent and its internal fields.
617 base::NativeEvent native_event = views::GetNativeEventForTesting();
618 views::SetNativeEventLocationForTesting(native_event, x, y);
619 #endif
620 set_native_event(native_event);
621 }
622
623 // Tests Real GestureManager instead of the mock one and validates Touch event
624 // to Mouse conversion when view hierarchy doesn't handle touch events.
625 #if defined(USE_X11)
626 // TODO(Gajen): Add support for non-X platforms.
627 TEST_F(ViewTest, TouchToMouse) {
628 TestGestureManager gm;
629
630 TestView* v1 = new TestViewIgnoreTouch();
631 v1->SetBounds(0, 0, 300, 300);
632
633 TestView* v2 = new TestViewIgnoreTouch();
634 v2->SetBounds(100, 100, 100, 100);
635
636 scoped_ptr<Widget> widget(new Widget());
637 Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
638 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
639 params.bounds = gfx::Rect(50, 50, 650, 650);
640 widget->Init(params);
641 View* root = widget->GetRootView();
642 root->AddChildView(v1);
643 static_cast<internal::RootView*>(root)->SetGestureManagerForTesting(&gm);
644 v1->AddChildView(v2);
645
646 // Test press.
647 v1->Reset();
648 v2->Reset();
649 gm.Reset();
650
651 MockTouchEvent pressed(ui::ET_TOUCH_PRESSED,
652 110,
653 120,
654 0, /* no flags */
655 0, /* first finger touch */
656 1.0, 0.0, 1.0, 0.0);
657 root->OnTouchEvent(pressed);
658
659 EXPECT_EQ(v2->last_touch_event_type_, 0);
660 EXPECT_EQ(v2->location_.x(), 10);
661 EXPECT_EQ(v2->location_.y(), 20);
662 // Make sure v1 did not receive the event
663 EXPECT_EQ(v1->last_touch_event_type_, 0);
664
665 // Since v1 and v2 didn't handled the touch-event, the gesture manager should
666 // handle it.
667 EXPECT_EQ(gm.last_touch_event_, ui::ET_TOUCH_PRESSED);
668 EXPECT_EQ(root, gm.last_view_);
669 EXPECT_EQ(gm.previously_handled_flag_, false);
670 EXPECT_EQ(gm.dispatched_synthetic_event_, true);
671
672 // Check GestureManager converted Touch event to Mouse and propogates to v2
673 // but v1.
674 EXPECT_EQ(v2->last_mouse_event_type_, ui::ET_MOUSE_PRESSED);
675 EXPECT_EQ(v2->location_.x(), 10);
676 EXPECT_EQ(v2->location_.y(), 20);
677 EXPECT_EQ(v1->last_mouse_event_type_, 0);
678 EXPECT_EQ(v1->location_.x(), 0);
679 EXPECT_EQ(v1->location_.y(), 0);
680 widget->CloseNow();
681 }
682 #endif
683
540 //////////////////////////////////////////////////////////////////////////////// 684 ////////////////////////////////////////////////////////////////////////////////
541 // Painting 685 // Painting
542 //////////////////////////////////////////////////////////////////////////////// 686 ////////////////////////////////////////////////////////////////////////////////
543 687
544 void TestView::Paint(gfx::Canvas* canvas) { 688 void TestView::Paint(gfx::Canvas* canvas) {
545 canvas->GetSkCanvas()->getClipBounds(&last_clip_); 689 canvas->GetSkCanvas()->getClipBounds(&last_clip_);
546 } 690 }
547 691
548 void TestView::SchedulePaintInRect(const gfx::Rect& rect) { 692 void TestView::SchedulePaintInRect(const gfx::Rect& rect) {
549 scheduled_paint_rects_.push_back(rect); 693 scheduled_paint_rects_.push_back(rect);
(...skipping 2546 matching lines...) Expand 10 before | Expand all | Expand 10 after
3096 ScrambleTree(content); 3240 ScrambleTree(content);
3097 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer())); 3241 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3098 3242
3099 ScrambleTree(content); 3243 ScrambleTree(content);
3100 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer())); 3244 EXPECT_TRUE(ViewAndLayerTreeAreConsistent(content, content->layer()));
3101 } 3245 }
3102 3246
3103 #endif // VIEWS_COMPOSITOR 3247 #endif // VIEWS_COMPOSITOR
3104 3248
3105 } // namespace views 3249 } // namespace views
OLDNEW
« 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