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
3 // found in the LICENSE file.
5 #include "ui/views/event_monitor_mac.h"
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"
11 #import "ui/gfx/test/ui_cocoa_test_helper.h"
17 class EventMonitorMacTest : public ui::CocoaTest {
19 EventMonitorMacTest() {}
22 DISALLOW_COPY_AND_ASSIGN(EventMonitorMacTest);
25 class EventCounter : public ui::EventHandler {
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_; }
31 // ui::EventHandler implementation:
32 virtual void OnEvent(ui::Event* event) override {
34 last_event_type_ = event->type();
39 ui::EventType last_event_type_;
44 TEST_F(EventMonitorMacTest, CountEvents) {
47 cocoa_test_event_utils::EnterExitEventWithType(NSMouseExited);
49 // No monitor installed yet, should not receive events.
50 [NSApp sendEvent:event];
51 EXPECT_EQ(0, counter.event_count());
52 EXPECT_EQ(ui::ET_UNKNOWN, counter.last_event_type());
54 // Install monitor, should start receiving event.
55 scoped_ptr<EventMonitor> event_monitor(EventMonitor::Create(&counter));
56 [NSApp sendEvent:event];
57 EXPECT_EQ(1, counter.event_count());
58 EXPECT_EQ(ui::ET_MOUSE_EXITED, counter.last_event_type());
60 // Uninstall monitor, should stop receiving events.
61 event_monitor.reset();
62 [NSApp sendEvent:event];
63 EXPECT_EQ(1, counter.event_count());
64 EXPECT_EQ(ui::ET_MOUSE_EXITED, counter.last_event_type());