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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/event_monitor_mac_unittest.mm
diff --git a/ui/views/event_monitor_mac_unittest.mm b/ui/views/event_monitor_mac_unittest.mm
index c6879a7967ad0670c03eb8e5f350c1addfc42689..13dde4d3aec21f52119765ffc239babd616108bd 100644
--- a/ui/views/event_monitor_mac_unittest.mm
+++ b/ui/views/event_monitor_mac_unittest.mm
@@ -2,66 +2,89 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "ui/views/event_monitor_mac.h"
+#import <Cocoa/Cocoa.h>
-#include "ui/events/event.h"
-#include "ui/events/event_handler.h"
-#include "ui/events/event_constants.h"
#import "ui/events/test/cocoa_test_event_utils.h"
-#import "ui/gfx/test/ui_cocoa_test_helper.h"
-
-namespace views {
+#include "ui/events/test/test_event_handler.h"
+#include "ui/views/event_monitor.h"
+#include "ui/views/test/widget_test.h"
namespace {
-class EventMonitorMacTest : public ui::CocoaTest {
+class TestEventGenerator {
public:
- EventMonitorMacTest() {}
+ 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.
+
+ void ClickLeftButton() {
tapted 2014/12/10 02:58:27 Could this just be a member function of EventMonit
+ auto events =
+ cocoa_test_event_utils::MouseClickInView([window_ contentView], 1);
+ [NSApp sendEvent:events.first];
+ [NSApp sendEvent:events.second];
tapted 2014/12/10 02:58:27 I'm not totally confident that this is non-flaky.
+ }
private:
- DISALLOW_COPY_AND_ASSIGN(EventMonitorMacTest);
+ gfx::NativeWindow window_;
};
tapted 2014/12/10 02:58:27 nit: DISALLOW_COPY_AND_ASSIGN
-class EventCounter : public ui::EventHandler {
+} // namespace
+
+namespace views {
+namespace test {
+
+class EventMonitorMacTest : public WidgetTest {
public:
- EventCounter() : event_count_(0), last_event_type_(ui::ET_UNKNOWN) {}
- int event_count() const { return event_count_; }
- ui::EventType last_event_type() { return last_event_type_; }
-
- // ui::EventHandler implementation:
- virtual void OnEvent(ui::Event* event) override {
- ++event_count_;
- last_event_type_ = event->type();
+ void SetUp() override {
tapted 2014/12/10 02:58:27 nit: // testing::Test:
+ WidgetTest::SetUp();
+ widget_ = CreateTopLevelNativeWidget();
+ widget_->SetSize(gfx::Size(100, 100));
+ widget_->Show();
+ generator_.reset(new TestEventGenerator(widget_->GetNativeWindow()));
+ }
+ void TearDown() override {
+ widget_->CloseNow();
+ WidgetTest::TearDown();
}
- private:
- int event_count_;
- ui::EventType last_event_type_;
+ protected:
+ Widget* widget_;
tapted 2014/12/10 02:58:27 nit: initialize widget_(nullptr)
+ scoped_ptr<TestEventGenerator> generator_;
+ ui::test::TestEventHandler handler_;
};
tapted 2014/12/10 02:58:27 nit: private: DISALLOW.. + constructor
-} // namespace
+TEST_F(EventMonitorMacTest, ShouldReceiveAppEventsWhileInstalled) {
+ scoped_ptr<EventMonitor> monitor(
+ EventMonitor::CreateApplicationMonitor(&handler_));
+
+ generator_->ClickLeftButton();
+ EXPECT_EQ(2, handler_.num_mouse_events());
+
+ monitor.reset();
+ generator_->ClickLeftButton();
+ EXPECT_EQ(2, handler_.num_mouse_events());
+}
+
+TEST_F(EventMonitorMacTest, ShouldReceiveWindowEventsWhileInstalled) {
+ scoped_ptr<EventMonitor> monitor(
+ EventMonitor::CreateWindowMonitor(&handler_, widget_->GetNativeWindow()));
+
+ generator_->ClickLeftButton();
+ EXPECT_EQ(2, handler_.num_mouse_events());
+
+ monitor.reset();
+ generator_->ClickLeftButton();
+ EXPECT_EQ(2, handler_.num_mouse_events());
+}
+
+TEST_F(EventMonitorMacTest, ShouldNotReceiveEventsFromOtherWindow) {
+ Widget* widget2 = CreateTopLevelNativeWidget();
+ scoped_ptr<EventMonitor> monitor(
+ EventMonitor::CreateWindowMonitor(&handler_, widget2->GetNativeWindow()));
+
+ generator_->ClickLeftButton();
+ EXPECT_EQ(0, handler_.num_mouse_events());
-TEST_F(EventMonitorMacTest, CountEvents) {
- EventCounter counter;
- NSEvent* event =
- cocoa_test_event_utils::EnterExitEventWithType(NSMouseExited);
-
- // No monitor installed yet, should not receive events.
- [NSApp sendEvent:event];
- EXPECT_EQ(0, counter.event_count());
- EXPECT_EQ(ui::ET_UNKNOWN, counter.last_event_type());
-
- // Install monitor, should start receiving event.
- scoped_ptr<EventMonitor> event_monitor(EventMonitor::Create(&counter));
- [NSApp sendEvent:event];
- EXPECT_EQ(1, counter.event_count());
- EXPECT_EQ(ui::ET_MOUSE_EXITED, counter.last_event_type());
-
- // Uninstall monitor, should stop receiving events.
- event_monitor.reset();
- [NSApp sendEvent:event];
- EXPECT_EQ(1, counter.event_count());
- EXPECT_EQ(ui::ET_MOUSE_EXITED, counter.last_event_type());
+ widget2->CloseNow();
}
+} // namespace test
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698