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/events/test/event_generator.h"
6 #include "ui/events/test/test_event_handler.h"
7 #include "ui/views/event_monitor.h"
8 #include "ui/views/test/widget_test.h"
13 class EventMonitorTest
: public WidgetTest
{
15 EventMonitorTest() : widget_(nullptr) {}
18 void SetUp() override
{
20 widget_
= CreateTopLevelNativeWidget();
21 widget_
->SetSize(gfx::Size(100, 100));
24 new ui::test::EventGenerator(GetContext(), widget_
->GetNativeWindow()));
25 generator_
->set_targeting_application(true);
27 void TearDown() override
{
29 WidgetTest::TearDown();
34 scoped_ptr
<ui::test::EventGenerator
> generator_
;
35 ui::test::TestEventHandler handler_
;
38 DISALLOW_COPY_AND_ASSIGN(EventMonitorTest
);
41 TEST_F(EventMonitorTest
, ShouldReceiveAppEventsWhileInstalled
) {
42 scoped_ptr
<EventMonitor
> monitor(
43 EventMonitor::CreateApplicationMonitor(&handler_
));
45 generator_
->ClickLeftButton();
46 EXPECT_EQ(2, handler_
.num_mouse_events());
49 generator_
->ClickLeftButton();
50 EXPECT_EQ(2, handler_
.num_mouse_events());
53 TEST_F(EventMonitorTest
, ShouldReceiveWindowEventsWhileInstalled
) {
54 scoped_ptr
<EventMonitor
> monitor(
55 EventMonitor::CreateWindowMonitor(&handler_
, widget_
->GetNativeWindow()));
57 generator_
->ClickLeftButton();
58 EXPECT_EQ(2, handler_
.num_mouse_events());
61 generator_
->ClickLeftButton();
62 EXPECT_EQ(2, handler_
.num_mouse_events());
65 TEST_F(EventMonitorTest
, ShouldNotReceiveEventsFromOtherWindow
) {
66 Widget
* widget2
= CreateTopLevelNativeWidget();
67 scoped_ptr
<EventMonitor
> monitor(
68 EventMonitor::CreateWindowMonitor(&handler_
, widget2
->GetNativeWindow()));
70 generator_
->ClickLeftButton();
71 EXPECT_EQ(0, handler_
.num_mouse_events());