1 // Copyright (c) 2013 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/wm/core/capture_controller.h"
7 #include "base/logging.h"
8 #include "ui/aura/client/capture_delegate.h"
9 #include "ui/aura/env.h"
10 #include "ui/aura/test/aura_test_base.h"
11 #include "ui/aura/test/test_screen.h"
12 #include "ui/aura/test/test_window_delegate.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_utils.h"
17 #include "ui/events/test/event_generator.h"
23 // aura::client::CaptureDelegate which allows querying whether native capture
25 class TestCaptureDelegate
: public aura::client::CaptureDelegate
{
27 TestCaptureDelegate() : has_capture_(false) {}
28 ~TestCaptureDelegate() override
{}
30 bool HasNativeCapture() const {
34 // aura::client::CaptureDelegate:
35 void UpdateCapture(aura::Window
* old_capture
,
36 aura::Window
* new_capture
) override
{}
37 void OnOtherRootGotCapture() override
{}
38 void SetNativeCapture() override
{ has_capture_
= true; }
39 void ReleaseNativeCapture() override
{ has_capture_
= false; }
44 DISALLOW_COPY_AND_ASSIGN(TestCaptureDelegate
);
49 class CaptureControllerTest
: public aura::test::AuraTestBase
{
51 CaptureControllerTest() {}
53 void SetUp() override
{
54 AuraTestBase::SetUp();
55 capture_controller_
.reset(new ScopedCaptureClient(root_window()));
57 second_host_
.reset(aura::WindowTreeHost::Create(gfx::Rect(0, 0, 800, 600)));
58 second_host_
->InitHost();
59 second_host_
->window()->Show();
60 second_host_
->SetBounds(gfx::Rect(800, 600));
61 second_capture_controller_
.reset(
62 new ScopedCaptureClient(second_host_
->window()));
65 void TearDown() override
{
66 RunAllPendingInMessageLoop();
68 second_capture_controller_
.reset();
70 // Kill any active compositors before we hit the compositor shutdown paths.
73 capture_controller_
.reset();
75 AuraTestBase::TearDown();
78 aura::Window
* GetCaptureWindow() {
79 return capture_controller_
->capture_client()->GetCaptureWindow();
82 aura::Window
* GetSecondCaptureWindow() {
83 return second_capture_controller_
->capture_client()->GetCaptureWindow();
86 scoped_ptr
<ScopedCaptureClient
> capture_controller_
;
87 scoped_ptr
<aura::WindowTreeHost
> second_host_
;
88 scoped_ptr
<ScopedCaptureClient
> second_capture_controller_
;
90 DISALLOW_COPY_AND_ASSIGN(CaptureControllerTest
);
93 // Makes sure that internal details that are set on mouse down (such as
94 // mouse_pressed_handler()) are cleared when another root window takes capture.
95 TEST_F(CaptureControllerTest
, ResetMouseEventHandlerOnCapture
) {
96 // Create a window inside the WindowEventDispatcher.
97 scoped_ptr
<aura::Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
99 // Make a synthesized mouse down event. Ensure that the WindowEventDispatcher
100 // will dispatch further mouse events to |w1|.
101 ui::MouseEvent
mouse_pressed_event(ui::ET_MOUSE_PRESSED
, gfx::Point(5, 5),
102 gfx::Point(5, 5), 0, 0);
103 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
104 EXPECT_EQ(w1
.get(), host()->dispatcher()->mouse_pressed_handler());
106 // Build a window in the second WindowEventDispatcher.
107 scoped_ptr
<aura::Window
> w2(
108 CreateNormalWindow(2, second_host_
->window(), NULL
));
110 // The act of having the second window take capture should clear out mouse
111 // pressed handler in the first WindowEventDispatcher.
113 EXPECT_EQ(NULL
, host()->dispatcher()->mouse_pressed_handler());
116 // Makes sure that when one window gets capture, it forces the release on the
117 // other. This is needed has to be handled explicitly on Linux, and is a sanity
119 TEST_F(CaptureControllerTest
, ResetOtherWindowCaptureOnCapture
) {
120 // Create a window inside the WindowEventDispatcher.
121 scoped_ptr
<aura::Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
123 // Both capture clients should return the same capture window.
124 EXPECT_EQ(w1
.get(), GetCaptureWindow());
125 EXPECT_EQ(w1
.get(), GetSecondCaptureWindow());
127 // Build a window in the second WindowEventDispatcher and give it capture.
128 // Both capture clients should return the same capture window.
129 scoped_ptr
<aura::Window
> w2(
130 CreateNormalWindow(2, second_host_
->window(), NULL
));
132 EXPECT_EQ(w2
.get(), GetCaptureWindow());
133 EXPECT_EQ(w2
.get(), GetSecondCaptureWindow());
136 // Verifies the touch target for the WindowEventDispatcher gets reset on
137 // releasing capture.
138 TEST_F(CaptureControllerTest
, TouchTargetResetOnCaptureChange
) {
139 // Create a window inside the WindowEventDispatcher.
140 scoped_ptr
<aura::Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
141 ui::test::EventGenerator
event_generator1(root_window());
142 event_generator1
.PressTouch();
144 // Both capture clients should return the same capture window.
145 EXPECT_EQ(w1
.get(), GetCaptureWindow());
146 EXPECT_EQ(w1
.get(), GetSecondCaptureWindow());
148 // Build a window in the second WindowEventDispatcher and give it capture.
149 // Both capture clients should return the same capture window.
150 scoped_ptr
<aura::Window
> w2(
151 CreateNormalWindow(2, second_host_
->window(), NULL
));
153 EXPECT_EQ(w2
.get(), GetCaptureWindow());
154 EXPECT_EQ(w2
.get(), GetSecondCaptureWindow());
156 // Release capture on the window. Releasing capture should reset the touch
157 // target of the first WindowEventDispatcher (as it no longer contains the
159 w2
->ReleaseCapture();
160 EXPECT_EQ(static_cast<aura::Window
*>(NULL
), GetCaptureWindow());
161 EXPECT_EQ(static_cast<aura::Window
*>(NULL
), GetSecondCaptureWindow());
162 ui::TouchEvent
touch_event(
163 ui::ET_TOUCH_PRESSED
, gfx::Point(), 0, 0, ui::EventTimeForNow(), 1.0f
,
165 EXPECT_EQ(static_cast<ui::GestureConsumer
*>(w2
.get()),
166 ui::GestureRecognizer::Get()->GetTouchLockedTarget(touch_event
));
169 // Test that native capture is released properly when the window with capture
170 // is reparented to a different root window while it has capture.
171 TEST_F(CaptureControllerTest
, ReparentedWhileCaptured
) {
172 scoped_ptr
<TestCaptureDelegate
> delegate(new TestCaptureDelegate
);
173 ScopedCaptureClient::TestApi(capture_controller_
.get())
174 .SetDelegate(delegate
.get());
175 scoped_ptr
<TestCaptureDelegate
> delegate2(new TestCaptureDelegate
);
176 ScopedCaptureClient::TestApi(second_capture_controller_
.get())
177 .SetDelegate(delegate2
.get());
179 scoped_ptr
<aura::Window
> w(CreateNormalWindow(1, root_window(), NULL
));
181 EXPECT_EQ(w
.get(), GetCaptureWindow());
182 EXPECT_EQ(w
.get(), GetSecondCaptureWindow());
183 EXPECT_TRUE(delegate
->HasNativeCapture());
184 EXPECT_FALSE(delegate2
->HasNativeCapture());
186 second_host_
->window()->AddChild(w
.get());
187 EXPECT_EQ(w
.get(), GetCaptureWindow());
188 EXPECT_EQ(w
.get(), GetSecondCaptureWindow());
189 EXPECT_TRUE(delegate
->HasNativeCapture());
190 EXPECT_FALSE(delegate2
->HasNativeCapture());
193 EXPECT_EQ(nullptr, GetCaptureWindow());
194 EXPECT_EQ(nullptr, GetSecondCaptureWindow());
195 EXPECT_FALSE(delegate
->HasNativeCapture());
196 EXPECT_FALSE(delegate2
->HasNativeCapture());