OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/aura/window_event_dispatcher.h" | 5 #include "ui/aura/window_event_dispatcher.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 1604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1615 virtual ~WindowEventDispatcherTestWithMessageLoop() {} | 1615 virtual ~WindowEventDispatcherTestWithMessageLoop() {} |
1616 | 1616 |
1617 void RunTest() { | 1617 void RunTest() { |
1618 // Reset any event the window may have received when bringing up the window | 1618 // Reset any event the window may have received when bringing up the window |
1619 // (e.g. mouse-move events if the mouse cursor is over the window). | 1619 // (e.g. mouse-move events if the mouse cursor is over the window). |
1620 handler_.Reset(); | 1620 handler_.Reset(); |
1621 | 1621 |
1622 // Start a nested message-loop, post an event to be dispatched, and then | 1622 // Start a nested message-loop, post an event to be dispatched, and then |
1623 // terminate the message-loop. When the message-loop unwinds and gets back, | 1623 // terminate the message-loop. When the message-loop unwinds and gets back, |
1624 // the reposted event should not have fired. | 1624 // the reposted event should not have fired. |
1625 ui::MouseEvent mouse(ui::ET_MOUSE_PRESSED, gfx::Point(10, 10), | 1625 scoped_ptr<ui::MouseEvent> mouse(new ui::MouseEvent(ui::ET_MOUSE_PRESSED, |
1626 gfx::Point(10, 10), ui::EF_NONE, ui::EF_NONE); | 1626 gfx::Point(10, 10), |
1627 message_loop()->PostTask(FROM_HERE, | 1627 gfx::Point(10, 10), |
1628 base::Bind(&WindowEventDispatcher::RepostEvent, | 1628 ui::EF_NONE, |
1629 base::Unretained(host()->dispatcher()), | 1629 ui::EF_NONE)); |
1630 mouse)); | 1630 message_loop()->PostTask( |
1631 message_loop()->PostTask(FROM_HERE, | 1631 FROM_HERE, |
1632 message_loop()->QuitClosure()); | 1632 base::Bind(&WindowEventDispatcherTestWithMessageLoop::RepostEventHelper, |
| 1633 host()->dispatcher(), |
| 1634 base::Passed(&mouse))); |
| 1635 message_loop()->PostTask(FROM_HERE, message_loop()->QuitClosure()); |
1633 | 1636 |
1634 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop()); | 1637 base::MessageLoop::ScopedNestableTaskAllower allow(message_loop()); |
1635 base::RunLoop loop; | 1638 base::RunLoop loop; |
1636 loop.Run(); | 1639 loop.Run(); |
1637 EXPECT_EQ(0, handler_.num_mouse_events()); | 1640 EXPECT_EQ(0, handler_.num_mouse_events()); |
1638 | 1641 |
1639 // Let the current message-loop run. The event-handler will terminate the | 1642 // Let the current message-loop run. The event-handler will terminate the |
1640 // message-loop when it receives the reposted event. | 1643 // message-loop when it receives the reposted event. |
1641 } | 1644 } |
1642 | 1645 |
1643 base::MessageLoop* message_loop() { | 1646 base::MessageLoop* message_loop() { |
1644 return base::MessageLoopForUI::current(); | 1647 return base::MessageLoopForUI::current(); |
1645 } | 1648 } |
1646 | 1649 |
1647 protected: | 1650 protected: |
1648 virtual void SetUp() OVERRIDE { | 1651 virtual void SetUp() OVERRIDE { |
1649 WindowEventDispatcherTest::SetUp(); | 1652 WindowEventDispatcherTest::SetUp(); |
1650 window_.reset(CreateNormalWindow(1, root_window(), NULL)); | 1653 window_.reset(CreateNormalWindow(1, root_window(), NULL)); |
1651 window_->AddPreTargetHandler(&handler_); | 1654 window_->AddPreTargetHandler(&handler_); |
1652 } | 1655 } |
1653 | 1656 |
1654 virtual void TearDown() OVERRIDE { | 1657 virtual void TearDown() OVERRIDE { |
1655 window_.reset(); | 1658 window_.reset(); |
1656 WindowEventDispatcherTest::TearDown(); | 1659 WindowEventDispatcherTest::TearDown(); |
1657 } | 1660 } |
1658 | 1661 |
1659 private: | 1662 private: |
| 1663 // Used to avoid a copying |event| when binding to a closure. |
| 1664 static void RepostEventHelper(WindowEventDispatcher* dispatcher, |
| 1665 scoped_ptr<ui::MouseEvent> event) { |
| 1666 dispatcher->RepostEvent(*event); |
| 1667 } |
| 1668 |
1660 scoped_ptr<Window> window_; | 1669 scoped_ptr<Window> window_; |
1661 ExitMessageLoopOnMousePress handler_; | 1670 ExitMessageLoopOnMousePress handler_; |
1662 | 1671 |
1663 DISALLOW_COPY_AND_ASSIGN(WindowEventDispatcherTestWithMessageLoop); | 1672 DISALLOW_COPY_AND_ASSIGN(WindowEventDispatcherTestWithMessageLoop); |
1664 }; | 1673 }; |
1665 | 1674 |
1666 TEST_F(WindowEventDispatcherTestWithMessageLoop, EventRepostedInNonNestedLoop) { | 1675 TEST_F(WindowEventDispatcherTestWithMessageLoop, EventRepostedInNonNestedLoop) { |
1667 CHECK(!message_loop()->is_running()); | 1676 CHECK(!message_loop()->is_running()); |
1668 // Perform the test in a callback, so that it runs after the message-loop | 1677 // Perform the test in a callback, so that it runs after the message-loop |
1669 // starts. | 1678 // starts. |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1937 EXPECT_TRUE(details.target_destroyed); | 1946 EXPECT_TRUE(details.target_destroyed); |
1938 EXPECT_EQ(first.get(), move.target()); | 1947 EXPECT_EQ(first.get(), move.target()); |
1939 EXPECT_TRUE(dispatch_event.dispatched()); | 1948 EXPECT_TRUE(dispatch_event.dispatched()); |
1940 EXPECT_EQ(second_root, first->GetRootWindow()); | 1949 EXPECT_EQ(second_root, first->GetRootWindow()); |
1941 | 1950 |
1942 first->RemovePreTargetHandler(&dispatch_event); | 1951 first->RemovePreTargetHandler(&dispatch_event); |
1943 second->RemovePreTargetHandler(&move_window); | 1952 second->RemovePreTargetHandler(&move_window); |
1944 } | 1953 } |
1945 | 1954 |
1946 } // namespace aura | 1955 } // namespace aura |
OLD | NEW |