| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/mus/ws/event_dispatcher.h" | 5 #include "components/mus/ws/event_dispatcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <queue> | 10 #include <queue> |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 | 201 |
| 202 bool AreAnyPointersDown() const; | 202 bool AreAnyPointersDown() const; |
| 203 // Deletes everything created during SetUp() | 203 // Deletes everything created during SetUp() |
| 204 void ClearSetup(); | 204 void ClearSetup(); |
| 205 std::unique_ptr<ServerWindow> CreateChildWindowWithParent( | 205 std::unique_ptr<ServerWindow> CreateChildWindowWithParent( |
| 206 const WindowId& id, | 206 const WindowId& id, |
| 207 ServerWindow* parent); | 207 ServerWindow* parent); |
| 208 // Creates a window which is a child of |root_window_|. | 208 // Creates a window which is a child of |root_window_|. |
| 209 std::unique_ptr<ServerWindow> CreateChildWindow(const WindowId& id); | 209 std::unique_ptr<ServerWindow> CreateChildWindow(const WindowId& id); |
| 210 bool IsMouseButtonDown() const; | 210 bool IsMouseButtonDown() const; |
| 211 bool IsWindowPointerTarget(const ServerWindow* window) const; | 211 bool IsWindowPointerTarget(ServerWindow* window) const; |
| 212 int NumberPointerTargetsForWindow(ServerWindow* window) const; | 212 int NumberPointerTargetsForWindow(ServerWindow* window) const; |
| 213 ServerWindow* GetActiveSystemModalWindow() const; | |
| 214 | 213 |
| 215 protected: | 214 protected: |
| 216 // testing::Test: | 215 // testing::Test: |
| 217 void SetUp() override; | 216 void SetUp() override; |
| 218 | 217 |
| 219 private: | 218 private: |
| 220 std::unique_ptr<TestServerWindowDelegate> window_delegate_; | 219 std::unique_ptr<TestServerWindowDelegate> window_delegate_; |
| 221 std::unique_ptr<ServerWindow> root_window_; | 220 std::unique_ptr<ServerWindow> root_window_; |
| 222 std::unique_ptr<TestEventDispatcherDelegate> test_event_dispatcher_delegate_; | 221 std::unique_ptr<TestEventDispatcherDelegate> test_event_dispatcher_delegate_; |
| 223 std::unique_ptr<EventDispatcher> event_dispatcher_; | 222 std::unique_ptr<EventDispatcher> event_dispatcher_; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 249 | 248 |
| 250 std::unique_ptr<ServerWindow> EventDispatcherTest::CreateChildWindow( | 249 std::unique_ptr<ServerWindow> EventDispatcherTest::CreateChildWindow( |
| 251 const WindowId& id) { | 250 const WindowId& id) { |
| 252 return CreateChildWindowWithParent(id, root_window_.get()); | 251 return CreateChildWindowWithParent(id, root_window_.get()); |
| 253 } | 252 } |
| 254 | 253 |
| 255 bool EventDispatcherTest::IsMouseButtonDown() const { | 254 bool EventDispatcherTest::IsMouseButtonDown() const { |
| 256 return EventDispatcherTestApi(event_dispatcher_.get()).is_mouse_button_down(); | 255 return EventDispatcherTestApi(event_dispatcher_.get()).is_mouse_button_down(); |
| 257 } | 256 } |
| 258 | 257 |
| 259 bool EventDispatcherTest::IsWindowPointerTarget( | 258 bool EventDispatcherTest::IsWindowPointerTarget(ServerWindow* window) const { |
| 260 const ServerWindow* window) const { | |
| 261 return EventDispatcherTestApi(event_dispatcher_.get()) | 259 return EventDispatcherTestApi(event_dispatcher_.get()) |
| 262 .IsWindowPointerTarget(window); | 260 .IsObservingWindow(window); |
| 263 } | 261 } |
| 264 | 262 |
| 265 int EventDispatcherTest::NumberPointerTargetsForWindow( | 263 int EventDispatcherTest::NumberPointerTargetsForWindow( |
| 266 ServerWindow* window) const { | 264 ServerWindow* window) const { |
| 267 return EventDispatcherTestApi(event_dispatcher_.get()) | 265 return EventDispatcherTestApi(event_dispatcher_.get()) |
| 268 .NumberPointerTargetsForWindow(window); | 266 .NumberPointerTargetsForWindow(window); |
| 269 } | 267 } |
| 270 | 268 |
| 271 ServerWindow* EventDispatcherTest::GetActiveSystemModalWindow() const { | |
| 272 ModalWindowController* mwc = | |
| 273 EventDispatcherTestApi(event_dispatcher_.get()).modal_window_controller(); | |
| 274 return ModalWindowControllerTestApi(mwc).GetActiveSystemModalWindow(); | |
| 275 } | |
| 276 | |
| 277 void EventDispatcherTest::SetUp() { | 269 void EventDispatcherTest::SetUp() { |
| 278 testing::Test::SetUp(); | 270 testing::Test::SetUp(); |
| 279 | 271 |
| 280 window_delegate_.reset(new TestServerWindowDelegate()); | 272 window_delegate_.reset(new TestServerWindowDelegate()); |
| 281 root_window_.reset(new ServerWindow(window_delegate_.get(), WindowId(1, 2))); | 273 root_window_.reset(new ServerWindow(window_delegate_.get(), WindowId(1, 2))); |
| 282 window_delegate_->set_root_window(root_window_.get()); | 274 window_delegate_->set_root_window(root_window_.get()); |
| 283 root_window_->SetVisible(true); | 275 root_window_->SetVisible(true); |
| 284 | 276 |
| 285 test_event_dispatcher_delegate_.reset( | 277 test_event_dispatcher_delegate_.reset( |
| 286 new TestEventDispatcherDelegate(root_window_.get())); | 278 new TestEventDispatcherDelegate(root_window_.get())); |
| (...skipping 1044 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1331 EXPECT_TRUE(details->in_nonclient_area); | 1323 EXPECT_TRUE(details->in_nonclient_area); |
| 1332 | 1324 |
| 1333 ASSERT_TRUE(details->event); | 1325 ASSERT_TRUE(details->event); |
| 1334 ASSERT_TRUE(details->event->IsPointerEvent()); | 1326 ASSERT_TRUE(details->event->IsPointerEvent()); |
| 1335 | 1327 |
| 1336 ui::PointerEvent* dispatched_event = details->event->AsPointerEvent(); | 1328 ui::PointerEvent* dispatched_event = details->event->AsPointerEvent(); |
| 1337 EXPECT_EQ(gfx::Point(25, 25), dispatched_event->root_location()); | 1329 EXPECT_EQ(gfx::Point(25, 25), dispatched_event->root_location()); |
| 1338 EXPECT_EQ(gfx::Point(-25, 15), dispatched_event->location()); | 1330 EXPECT_EQ(gfx::Point(-25, 15), dispatched_event->location()); |
| 1339 } | 1331 } |
| 1340 | 1332 |
| 1341 // Tests that events on a system modal window target the modal window itself. | |
| 1342 TEST_F(EventDispatcherTest, ModalWindowEventOnSystemModal) { | |
| 1343 std::unique_ptr<ServerWindow> w1 = CreateChildWindow(WindowId(1, 3)); | |
| 1344 | |
| 1345 root_window()->SetBounds(gfx::Rect(0, 0, 100, 100)); | |
| 1346 w1->SetBounds(gfx::Rect(10, 10, 30, 30)); | |
| 1347 w1->SetModal(); | |
| 1348 | |
| 1349 // Send event that is over |w1|. | |
| 1350 const ui::PointerEvent mouse_pressed(ui::MouseEvent( | |
| 1351 ui::ET_MOUSE_PRESSED, gfx::Point(15, 15), gfx::Point(15, 15), | |
| 1352 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); | |
| 1353 event_dispatcher()->ProcessEvent(mouse_pressed); | |
| 1354 | |
| 1355 std::unique_ptr<DispatchedEventDetails> details = | |
| 1356 test_event_dispatcher_delegate()->GetAndAdvanceDispatchedEventDetails(); | |
| 1357 ASSERT_TRUE(details); | |
| 1358 EXPECT_EQ(w1.get(), details->window); | |
| 1359 EXPECT_FALSE(details->in_nonclient_area); | |
| 1360 | |
| 1361 ASSERT_TRUE(details->event); | |
| 1362 ASSERT_TRUE(details->event->IsPointerEvent()); | |
| 1363 | |
| 1364 ui::PointerEvent* dispatched_event = details->event->AsPointerEvent(); | |
| 1365 EXPECT_EQ(gfx::Point(15, 15), dispatched_event->root_location()); | |
| 1366 EXPECT_EQ(gfx::Point(5, 5), dispatched_event->location()); | |
| 1367 } | |
| 1368 | |
| 1369 // Tests that events outside of system modal window target the modal window. | |
| 1370 TEST_F(EventDispatcherTest, ModalWindowEventOutsideSystemModal) { | |
| 1371 std::unique_ptr<ServerWindow> w1 = CreateChildWindow(WindowId(1, 3)); | |
| 1372 | |
| 1373 root_window()->SetBounds(gfx::Rect(0, 0, 100, 100)); | |
| 1374 w1->SetBounds(gfx::Rect(10, 10, 30, 30)); | |
| 1375 w1->SetModal(); | |
| 1376 event_dispatcher()->AddSystemModalWindow(w1.get()); | |
| 1377 | |
| 1378 // Send event that is over |w1|. | |
| 1379 const ui::PointerEvent mouse_pressed(ui::MouseEvent( | |
| 1380 ui::ET_MOUSE_PRESSED, gfx::Point(45, 15), gfx::Point(45, 15), | |
| 1381 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON)); | |
| 1382 event_dispatcher()->ProcessEvent(mouse_pressed); | |
| 1383 | |
| 1384 std::unique_ptr<DispatchedEventDetails> details = | |
| 1385 test_event_dispatcher_delegate()->GetAndAdvanceDispatchedEventDetails(); | |
| 1386 ASSERT_TRUE(details); | |
| 1387 EXPECT_EQ(w1.get(), details->window); | |
| 1388 EXPECT_TRUE(details->in_nonclient_area); | |
| 1389 | |
| 1390 ASSERT_TRUE(details->event); | |
| 1391 ASSERT_TRUE(details->event->IsPointerEvent()); | |
| 1392 | |
| 1393 ui::PointerEvent* dispatched_event = details->event->AsPointerEvent(); | |
| 1394 EXPECT_EQ(gfx::Point(45, 15), dispatched_event->root_location()); | |
| 1395 EXPECT_EQ(gfx::Point(35, 5), dispatched_event->location()); | |
| 1396 } | |
| 1397 | 1333 |
| 1398 // Tests that setting capture to a descendant of a modal parent fails. | 1334 // Tests that setting capture to a descendant of a modal parent fails. |
| 1399 TEST_F(EventDispatcherTest, ModalWindowSetCaptureDescendantOfModalParent) { | 1335 TEST_F(EventDispatcherTest, ModalWindowSetCaptureDescendantOfModalParent) { |
| 1400 std::unique_ptr<ServerWindow> w1 = CreateChildWindow(WindowId(1, 3)); | 1336 std::unique_ptr<ServerWindow> w1 = CreateChildWindow(WindowId(1, 3)); |
| 1401 std::unique_ptr<ServerWindow> w11 = | 1337 std::unique_ptr<ServerWindow> w11 = |
| 1402 CreateChildWindowWithParent(WindowId(1, 4), w1.get()); | 1338 CreateChildWindowWithParent(WindowId(1, 4), w1.get()); |
| 1403 std::unique_ptr<ServerWindow> w2 = CreateChildWindow(WindowId(1, 5)); | 1339 std::unique_ptr<ServerWindow> w2 = CreateChildWindow(WindowId(1, 5)); |
| 1404 | 1340 |
| 1405 root_window()->SetBounds(gfx::Rect(0, 0, 100, 100)); | 1341 root_window()->SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 1406 w1->SetBounds(gfx::Rect(10, 10, 30, 30)); | 1342 w1->SetBounds(gfx::Rect(10, 10, 30, 30)); |
| 1407 w11->SetBounds(gfx::Rect(10, 10, 10, 10)); | 1343 w11->SetBounds(gfx::Rect(10, 10, 10, 10)); |
| 1408 w2->SetBounds(gfx::Rect(50, 10, 10, 10)); | 1344 w2->SetBounds(gfx::Rect(50, 10, 10, 10)); |
| 1409 | 1345 |
| 1410 w1->AddTransientWindow(w2.get()); | 1346 w1->AddTransientWindow(w2.get()); |
| 1411 w2->SetModal(); | 1347 w2->SetModal(); |
| 1412 | 1348 |
| 1413 EXPECT_FALSE(event_dispatcher()->SetCaptureWindow(w11.get(), false)); | 1349 EXPECT_FALSE(event_dispatcher()->SetCaptureWindow(w11.get(), false)); |
| 1414 EXPECT_EQ(nullptr, event_dispatcher()->capture_window()); | 1350 EXPECT_EQ(nullptr, event_dispatcher()->capture_window()); |
| 1415 } | 1351 } |
| 1416 | 1352 |
| 1417 // Tests that setting capture to a window unrelated to a modal parent works. | 1353 // Tests that setting capture to a window unrelated to a modal parent works. |
| 1418 TEST_F(EventDispatcherTest, ModalWindowSetCaptureUnrelatedWindow) { | 1354 TEST_F(EventDispatcherTest, ModalWindowSetCaptureUnrelatedWindow) { |
| 1419 std::unique_ptr<ServerWindow> w1 = CreateChildWindow(WindowId(1, 3)); | 1355 std::unique_ptr<ServerWindow> w1 = CreateChildWindow(WindowId(1, 3)); |
| 1420 std::unique_ptr<ServerWindow> w2 = CreateChildWindow(WindowId(1, 4)); | 1356 std::unique_ptr<ServerWindow> w2 = CreateChildWindow(WindowId(1, 5)); |
| 1421 std::unique_ptr<ServerWindow> w3 = CreateChildWindow(WindowId(1, 5)); | 1357 std::unique_ptr<ServerWindow> w3 = CreateChildWindow(WindowId(1, 6)); |
| 1422 | 1358 |
| 1423 root_window()->SetBounds(gfx::Rect(0, 0, 100, 100)); | 1359 root_window()->SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 1424 w1->SetBounds(gfx::Rect(10, 10, 30, 30)); | 1360 w1->SetBounds(gfx::Rect(10, 10, 30, 30)); |
| 1425 w2->SetBounds(gfx::Rect(50, 10, 10, 10)); | 1361 w2->SetBounds(gfx::Rect(50, 10, 10, 10)); |
| 1426 w3->SetBounds(gfx::Rect(70, 10, 10, 10)); | 1362 w3->SetBounds(gfx::Rect(70, 10, 10, 10)); |
| 1427 | 1363 |
| 1428 w1->AddTransientWindow(w2.get()); | 1364 w1->AddTransientWindow(w2.get()); |
| 1429 w2->SetModal(); | 1365 w2->SetModal(); |
| 1430 | 1366 |
| 1431 EXPECT_TRUE(event_dispatcher()->SetCaptureWindow(w3.get(), false)); | 1367 EXPECT_TRUE(event_dispatcher()->SetCaptureWindow(w3.get(), false)); |
| 1432 EXPECT_EQ(w3.get(), event_dispatcher()->capture_window()); | 1368 EXPECT_EQ(w3.get(), event_dispatcher()->capture_window()); |
| 1433 } | 1369 } |
| 1434 | 1370 |
| 1435 // Tests that setting capture fails when there is a system modal window. | |
| 1436 TEST_F(EventDispatcherTest, ModalWindowSystemSetCapture) { | |
| 1437 std::unique_ptr<ServerWindow> w1 = CreateChildWindow(WindowId(1, 3)); | |
| 1438 std::unique_ptr<ServerWindow> w2 = CreateChildWindow(WindowId(1, 4)); | |
| 1439 | |
| 1440 root_window()->SetBounds(gfx::Rect(0, 0, 100, 100)); | |
| 1441 w1->SetBounds(gfx::Rect(10, 10, 30, 30)); | |
| 1442 w2->SetBounds(gfx::Rect(50, 10, 10, 10)); | |
| 1443 | |
| 1444 event_dispatcher()->AddSystemModalWindow(w2.get()); | |
| 1445 | |
| 1446 EXPECT_FALSE(event_dispatcher()->SetCaptureWindow(w1.get(), false)); | |
| 1447 EXPECT_EQ(nullptr, event_dispatcher()->capture_window()); | |
| 1448 } | |
| 1449 | |
| 1450 // Tests having multiple system modal windows. | |
| 1451 TEST_F(EventDispatcherTest, ModalWindowMultipleSystemModals) { | |
| 1452 std::unique_ptr<ServerWindow> w1 = CreateChildWindow(WindowId(1, 3)); | |
| 1453 std::unique_ptr<ServerWindow> w2 = CreateChildWindow(WindowId(1, 4)); | |
| 1454 std::unique_ptr<ServerWindow> w3 = CreateChildWindow(WindowId(1, 5)); | |
| 1455 | |
| 1456 w2->SetVisible(false); | |
| 1457 | |
| 1458 // In the beginning, there should be no active system modal window. | |
| 1459 EXPECT_EQ(nullptr, GetActiveSystemModalWindow()); | |
| 1460 | |
| 1461 // Add a visible system modal window. It should become the active one. | |
| 1462 event_dispatcher()->AddSystemModalWindow(w1.get()); | |
| 1463 EXPECT_EQ(w1.get(), GetActiveSystemModalWindow()); | |
| 1464 | |
| 1465 // Add an invisible system modal window. It should not change the active one. | |
| 1466 event_dispatcher()->AddSystemModalWindow(w2.get()); | |
| 1467 EXPECT_EQ(w1.get(), GetActiveSystemModalWindow()); | |
| 1468 | |
| 1469 // Add another visible system modal window. It should become the active one. | |
| 1470 event_dispatcher()->AddSystemModalWindow(w3.get()); | |
| 1471 EXPECT_EQ(w3.get(), GetActiveSystemModalWindow()); | |
| 1472 | |
| 1473 // Make an existing system modal window visible. It should become the active | |
| 1474 // one. | |
| 1475 w2->SetVisible(true); | |
| 1476 EXPECT_EQ(w2.get(), GetActiveSystemModalWindow()); | |
| 1477 | |
| 1478 // Remove the active system modal window. Next one should become active. | |
| 1479 w2.reset(); | |
| 1480 EXPECT_EQ(w3.get(), GetActiveSystemModalWindow()); | |
| 1481 | |
| 1482 // Remove an inactive system modal window. It should not change the active | |
| 1483 // one. | |
| 1484 w1.reset(); | |
| 1485 EXPECT_EQ(w3.get(), GetActiveSystemModalWindow()); | |
| 1486 | |
| 1487 // Remove the last remaining system modal window. There should be no active | |
| 1488 // one anymore. | |
| 1489 w3.reset(); | |
| 1490 EXPECT_EQ(nullptr, GetActiveSystemModalWindow()); | |
| 1491 } | |
| 1492 | |
| 1493 } // namespace test | 1371 } // namespace test |
| 1494 } // namespace ws | 1372 } // namespace ws |
| 1495 } // namespace mus | 1373 } // namespace mus |
| OLD | NEW |