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

Side by Side Diff: ui/views/event_monitor_mac_unittest.mm

Issue 730833006: MacViews: Implement event monitoring for a specific window (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "ui/views/event_monitor_mac.h" 5 #import <Cocoa/Cocoa.h>
6 6
7 #include "ui/events/event.h"
8 #include "ui/events/event_handler.h"
9 #include "ui/events/event_constants.h"
10 #import "ui/events/test/cocoa_test_event_utils.h" 7 #import "ui/events/test/cocoa_test_event_utils.h"
11 #import "ui/gfx/test/ui_cocoa_test_helper.h" 8 #include "ui/events/test/test_event_handler.h"
12 9 #include "ui/views/event_monitor.h"
13 namespace views { 10 #include "ui/views/test/widget_test.h"
14 11
15 namespace { 12 namespace {
16 13
17 class EventMonitorMacTest : public ui::CocoaTest { 14 class TestEventGenerator {
18 public: 15 public:
19 EventMonitorMacTest() {} 16 TestEventGenerator(gfx::NativeWindow window) : window_(window) {}
tapted 2014/12/10 02:58:27 nit: explicit
Andre 2014/12/10 22:58:05 Deleted, no longer needed.
20 17
21 private: 18 void ClickLeftButton() {
tapted 2014/12/10 02:58:27 Could this just be a member function of EventMonit
22 DISALLOW_COPY_AND_ASSIGN(EventMonitorMacTest); 19 auto events =
23 }; 20 cocoa_test_event_utils::MouseClickInView([window_ contentView], 1);
24 21 [NSApp sendEvent:events.first];
25 class EventCounter : public ui::EventHandler { 22 [NSApp sendEvent:events.second];
tapted 2014/12/10 02:58:27 I'm not totally confident that this is non-flaky.
26 public:
27 EventCounter() : event_count_(0), last_event_type_(ui::ET_UNKNOWN) {}
28 int event_count() const { return event_count_; }
29 ui::EventType last_event_type() { return last_event_type_; }
30
31 // ui::EventHandler implementation:
32 virtual void OnEvent(ui::Event* event) override {
33 ++event_count_;
34 last_event_type_ = event->type();
35 } 23 }
36 24
37 private: 25 private:
38 int event_count_; 26 gfx::NativeWindow window_;
39 ui::EventType last_event_type_;
40 }; 27 };
tapted 2014/12/10 02:58:27 nit: DISALLOW_COPY_AND_ASSIGN
41 28
42 } // namespace 29 } // namespace
43 30
44 TEST_F(EventMonitorMacTest, CountEvents) { 31 namespace views {
45 EventCounter counter; 32 namespace test {
46 NSEvent* event =
47 cocoa_test_event_utils::EnterExitEventWithType(NSMouseExited);
48 33
49 // No monitor installed yet, should not receive events. 34 class EventMonitorMacTest : public WidgetTest {
50 [NSApp sendEvent:event]; 35 public:
51 EXPECT_EQ(0, counter.event_count()); 36 void SetUp() override {
tapted 2014/12/10 02:58:27 nit: // testing::Test:
52 EXPECT_EQ(ui::ET_UNKNOWN, counter.last_event_type()); 37 WidgetTest::SetUp();
38 widget_ = CreateTopLevelNativeWidget();
39 widget_->SetSize(gfx::Size(100, 100));
40 widget_->Show();
41 generator_.reset(new TestEventGenerator(widget_->GetNativeWindow()));
42 }
43 void TearDown() override {
44 widget_->CloseNow();
45 WidgetTest::TearDown();
46 }
53 47
54 // Install monitor, should start receiving event. 48 protected:
55 scoped_ptr<EventMonitor> event_monitor(EventMonitor::Create(&counter)); 49 Widget* widget_;
tapted 2014/12/10 02:58:27 nit: initialize widget_(nullptr)
56 [NSApp sendEvent:event]; 50 scoped_ptr<TestEventGenerator> generator_;
57 EXPECT_EQ(1, counter.event_count()); 51 ui::test::TestEventHandler handler_;
58 EXPECT_EQ(ui::ET_MOUSE_EXITED, counter.last_event_type()); 52 };
tapted 2014/12/10 02:58:27 nit: private: DISALLOW.. + constructor
59 53
60 // Uninstall monitor, should stop receiving events. 54 TEST_F(EventMonitorMacTest, ShouldReceiveAppEventsWhileInstalled) {
61 event_monitor.reset(); 55 scoped_ptr<EventMonitor> monitor(
62 [NSApp sendEvent:event]; 56 EventMonitor::CreateApplicationMonitor(&handler_));
63 EXPECT_EQ(1, counter.event_count()); 57
64 EXPECT_EQ(ui::ET_MOUSE_EXITED, counter.last_event_type()); 58 generator_->ClickLeftButton();
59 EXPECT_EQ(2, handler_.num_mouse_events());
60
61 monitor.reset();
62 generator_->ClickLeftButton();
63 EXPECT_EQ(2, handler_.num_mouse_events());
65 } 64 }
66 65
66 TEST_F(EventMonitorMacTest, ShouldReceiveWindowEventsWhileInstalled) {
67 scoped_ptr<EventMonitor> monitor(
68 EventMonitor::CreateWindowMonitor(&handler_, widget_->GetNativeWindow()));
69
70 generator_->ClickLeftButton();
71 EXPECT_EQ(2, handler_.num_mouse_events());
72
73 monitor.reset();
74 generator_->ClickLeftButton();
75 EXPECT_EQ(2, handler_.num_mouse_events());
76 }
77
78 TEST_F(EventMonitorMacTest, ShouldNotReceiveEventsFromOtherWindow) {
79 Widget* widget2 = CreateTopLevelNativeWidget();
80 scoped_ptr<EventMonitor> monitor(
81 EventMonitor::CreateWindowMonitor(&handler_, widget2->GetNativeWindow()));
82
83 generator_->ClickLeftButton();
84 EXPECT_EQ(0, handler_.num_mouse_events());
85
86 widget2->CloseNow();
87 }
88
89 } // namespace test
67 } // namespace views 90 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698