Index: components/mus/ws/window_tree_unittest.cc |
diff --git a/components/mus/ws/window_tree_unittest.cc b/components/mus/ws/window_tree_unittest.cc |
index 351154dd4098ff78dfd0fde465d021beafc4aaf5..8eea4521d5a231ac4c1a5a58828a10707c275897 100644 |
--- a/components/mus/ws/window_tree_unittest.cc |
+++ b/components/mus/ws/window_tree_unittest.cc |
@@ -133,6 +133,7 @@ class TestWindowTreeClient : public mus::mojom::WindowTreeClient { |
tracker_.OnEmbeddedAppDisconnected(window); |
} |
void OnUnembed(Id window_id) override { tracker_.OnUnembed(window_id); } |
+ void OnLostCapture(Id window_id) override {} |
void OnTopLevelCreated(uint32_t change_id, |
mojom::WindowDataPtr data) override { |
tracker_.OnTopLevelCreated(change_id, std::move(data)); |
@@ -323,6 +324,8 @@ class TestDisplayManager : public DisplayManager { |
const gfx::Rect& bounds) override {} |
void SetViewportSize(const gfx::Size& size) override {} |
void SetTitle(const base::string16& title) override {} |
+ void SetCapture() override {} |
+ void ReleaseCapture() override {} |
void SetCursorById(int32_t cursor) override { *cursor_id_storage_ = cursor; } |
mojom::Rotation GetRotation() override { return mojom::Rotation::VALUE_0; } |
const mojom::ViewportMetrics& GetViewportMetrics() override { |
@@ -898,5 +901,50 @@ TEST_F(WindowTreeTest, NewTopLevelWindow) { |
EXPECT_FALSE(embed_window->visible()); |
} |
+// Tests that setting capture only works while an input event is being |
+// processed, and the only the capture window can release capture. |
+TEST_F(WindowTreeTest, ExplicitSetCapture) { |
+ TestWindowTreeClient* embed_connection = nullptr; |
+ WindowTreeImpl* window_tree_connection = nullptr; |
+ ServerWindow* window = nullptr; |
+ EXPECT_NO_FATAL_FAILURE( |
+ SetupEventTargeting(&embed_connection, &window_tree_connection, &window)); |
+ const ServerWindow* root_window = *window_tree_connection->roots().begin(); |
+ window_tree_connection->AddWindow( |
+ FirstRootId(window_tree_connection), |
+ ClientWindowIdForWindow(window_tree_connection, window)); |
+ window->SetBounds(gfx::Rect(0, 0, 100, 100)); |
+ ASSERT_TRUE(window_tree_connection->GetHost(window)); |
+ |
+ // Setting capture should fail when there are no active events |
+ mojom::WindowTree* mojom_window_tree = |
+ static_cast<mojom::WindowTree*>(window_tree_connection); |
+ uint32_t change_id = 42; |
+ mojom_window_tree->SetCapture(change_id, WindowIdToTransportId(window->id())); |
+ WindowTreeHostImpl* host = window_tree_connection->GetHost(window); |
+ EXPECT_NE(window, host->GetCaptureWindow()); |
+ |
+ // Setting capture after the event is acknowledged should fail |
+ DispatchEventAndAckImmediately(CreatePointerDownEvent(10, 10)); |
+ mojom_window_tree->SetCapture(++change_id, |
+ WindowIdToTransportId(window->id())); |
+ EXPECT_NE(window, host->GetCaptureWindow()); |
+ |
+ // Settings while the event is being process should pass |
+ DispatchEventWithoutAck(CreatePointerDownEvent(10, 10)); |
+ mojom_window_tree->SetCapture(++change_id, |
+ WindowIdToTransportId(window->id())); |
+ EXPECT_EQ(window, host->GetCaptureWindow()); |
+ AckPreviousEvent(); |
+ |
+ // Only the capture window should be able to release capture |
+ mojom_window_tree->ReleaseCapture(++change_id, |
+ WindowIdToTransportId(root_window->id())); |
+ EXPECT_EQ(window, host->GetCaptureWindow()); |
+ mojom_window_tree->ReleaseCapture(++change_id, |
+ WindowIdToTransportId(window->id())); |
+ EXPECT_EQ(nullptr, host->GetCaptureWindow()); |
+} |
+ |
} // namespace ws |
} // namespace mus |