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/env.h"
9 #include "ui/aura/test/aura_test_base.h"
10 #include "ui/aura/test/test_screen.h"
11 #include "ui/aura/test/test_window_delegate.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_event_dispatcher.h"
14 #include "ui/events/event.h"
15 #include "ui/events/event_utils.h"
16 #include "ui/events/test/event_generator.h"
17 #include "ui/views/test/views_test_base.h"
18 #include "ui/views/view.h"
19 #include "ui/views/widget/root_view.h"
20 #include "ui/views/widget/widget.h"
22 #if !defined(OS_CHROMEOS)
23 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
24 #include "ui/views/widget/desktop_aura/desktop_screen_position_client.h"
29 class CaptureControllerTest
: public aura::test::AuraTestBase
{
31 CaptureControllerTest() {}
33 virtual void SetUp() OVERRIDE
{
34 AuraTestBase::SetUp();
35 capture_controller_
.reset(new wm::ScopedCaptureClient(root_window()));
37 second_host_
.reset(aura::WindowTreeHost::Create(gfx::Rect(0, 0, 800, 600)));
38 second_host_
->InitHost();
39 second_host_
->window()->Show();
40 second_host_
->SetBounds(gfx::Rect(800, 600));
41 second_capture_controller_
.reset(
42 new wm::ScopedCaptureClient(second_host_
->window()));
44 #if !defined(OS_CHROMEOS)
45 desktop_position_client_
.reset(
46 new DesktopScreenPositionClient(root_window()));
48 second_desktop_position_client_
.reset(
49 new DesktopScreenPositionClient(second_host_
->window()));
53 virtual void TearDown() OVERRIDE
{
54 RunAllPendingInMessageLoop();
56 #if !defined(OS_CHROMEOS)
57 second_desktop_position_client_
.reset();
59 second_capture_controller_
.reset();
61 // Kill any active compositors before we hit the compositor shutdown paths.
64 #if !defined(OS_CHROMEOS)
65 desktop_position_client_
.reset();
67 capture_controller_
.reset();
69 AuraTestBase::TearDown();
72 aura::Window
* GetCaptureWindow() {
73 return capture_controller_
->capture_client()->GetCaptureWindow();
76 aura::Window
* GetSecondCaptureWindow() {
77 return second_capture_controller_
->capture_client()->GetCaptureWindow();
80 scoped_ptr
<wm::ScopedCaptureClient
> capture_controller_
;
81 scoped_ptr
<aura::WindowTreeHost
> second_host_
;
82 scoped_ptr
<wm::ScopedCaptureClient
> second_capture_controller_
;
83 #if !defined(OS_CHROMEOS)
84 scoped_ptr
<aura::client::ScreenPositionClient
> desktop_position_client_
;
85 scoped_ptr
<aura::client::ScreenPositionClient
>
86 second_desktop_position_client_
;
89 DISALLOW_COPY_AND_ASSIGN(CaptureControllerTest
);
92 // Makes sure that internal details that are set on mouse down (such as
93 // mouse_pressed_handler()) are cleared when another root window takes capture.
94 TEST_F(CaptureControllerTest
, ResetMouseEventHandlerOnCapture
) {
95 // Create a window inside the WindowEventDispatcher.
96 scoped_ptr
<aura::Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
98 // Make a synthesized mouse down event. Ensure that the WindowEventDispatcher
99 // will dispatch further mouse events to |w1|.
100 ui::MouseEvent
mouse_pressed_event(ui::ET_MOUSE_PRESSED
, gfx::Point(5, 5),
101 gfx::Point(5, 5), 0, 0);
102 DispatchEventUsingWindowDispatcher(&mouse_pressed_event
);
103 EXPECT_EQ(w1
.get(), host()->dispatcher()->mouse_pressed_handler());
105 // Build a window in the second WindowEventDispatcher.
106 scoped_ptr
<aura::Window
> w2(
107 CreateNormalWindow(2, second_host_
->window(), NULL
));
109 // The act of having the second window take capture should clear out mouse
110 // pressed handler in the first WindowEventDispatcher.
112 EXPECT_EQ(NULL
, host()->dispatcher()->mouse_pressed_handler());
115 // Makes sure that when one window gets capture, it forces the release on the
116 // other. This is needed has to be handled explicitly on Linux, and is a sanity
118 TEST_F(CaptureControllerTest
, ResetOtherWindowCaptureOnCapture
) {
119 // Create a window inside the WindowEventDispatcher.
120 scoped_ptr
<aura::Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
122 // Both capture clients should return the same capture window.
123 EXPECT_EQ(w1
.get(), GetCaptureWindow());
124 EXPECT_EQ(w1
.get(), GetSecondCaptureWindow());
126 // Build a window in the second WindowEventDispatcher and give it capture.
127 // Both capture clients should return the same capture window.
128 scoped_ptr
<aura::Window
> w2(
129 CreateNormalWindow(2, second_host_
->window(), NULL
));
131 EXPECT_EQ(w2
.get(), GetCaptureWindow());
132 EXPECT_EQ(w2
.get(), GetSecondCaptureWindow());
135 // Verifies the touch target for the WindowEventDispatcher gets reset on
136 // releasing capture.
137 TEST_F(CaptureControllerTest
, TouchTargetResetOnCaptureChange
) {
138 // Create a window inside the WindowEventDispatcher.
139 scoped_ptr
<aura::Window
> w1(CreateNormalWindow(1, root_window(), NULL
));
140 ui::test::EventGenerator
event_generator1(root_window());
141 event_generator1
.PressTouch();
143 // Both capture clients should return the same capture window.
144 EXPECT_EQ(w1
.get(), GetCaptureWindow());
145 EXPECT_EQ(w1
.get(), GetSecondCaptureWindow());
147 // Build a window in the second WindowEventDispatcher and give it capture.
148 // Both capture clients should return the same capture window.
149 scoped_ptr
<aura::Window
> w2(
150 CreateNormalWindow(2, second_host_
->window(), NULL
));
152 EXPECT_EQ(w2
.get(), GetCaptureWindow());
153 EXPECT_EQ(w2
.get(), GetSecondCaptureWindow());
155 // Release capture on the window. Releasing capture should reset the touch
156 // target of the first WindowEventDispatcher (as it no longer contains the
158 w2
->ReleaseCapture();
159 EXPECT_EQ(static_cast<aura::Window
*>(NULL
), GetCaptureWindow());
160 EXPECT_EQ(static_cast<aura::Window
*>(NULL
), GetSecondCaptureWindow());
161 ui::TouchEvent
touch_event(
162 ui::ET_TOUCH_PRESSED
, gfx::Point(), 0, 0, ui::EventTimeForNow(), 1.0f
,
164 EXPECT_EQ(static_cast<ui::GestureConsumer
*>(w2
.get()),
165 ui::GestureRecognizer::Get()->GetTouchLockedTarget(touch_event
));