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/views/corewm/capture_controller.h"
7 #include "base/logging.h"
8 #include "ui/aura/env.h"
9 #include "ui/aura/root_window.h"
10 #include "ui/aura/test/aura_test_base.h"
11 #include "ui/aura/test/event_generator.h"
12 #include "ui/aura/test/test_screen.h"
13 #include "ui/aura/test/test_window_delegate.h"
14 #include "ui/events/event.h"
15 #include "ui/events/event_utils.h"
16 #include "ui/views/test/views_test_base.h"
17 #include "ui/views/view.h"
18 #include "ui/views/widget/root_view.h"
19 #include "ui/views/widget/widget.h"
21 #if !defined(OS_CHROMEOS)
22 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
23 #include "ui/views/widget/desktop_aura/desktop_screen_position_client.h"
28 class CaptureControllerTest : public aura::test::AuraTestBase {
30 CaptureControllerTest() {}
32 virtual void SetUp() OVERRIDE {
33 AuraTestBase::SetUp();
34 capture_controller_.reset(new corewm::ScopedCaptureClient(root_window()));
36 second_root_.reset(new aura::RootWindow(
37 aura::RootWindow::CreateParams(gfx::Rect(0, 0, 800, 600))));
40 second_root_->SetHostSize(gfx::Size(800, 600));
41 second_capture_controller_.reset(
42 new corewm::ScopedCaptureClient(second_root_.get()));
44 #if !defined(OS_CHROMEOS)
45 desktop_position_client_.reset(new DesktopScreenPositionClient());
46 aura::client::SetScreenPositionClient(root_window(),
47 desktop_position_client_.get());
49 second_desktop_position_client_.reset(new DesktopScreenPositionClient());
50 aura::client::SetScreenPositionClient(
52 second_desktop_position_client_.get());
56 virtual void TearDown() OVERRIDE {
57 RunAllPendingInMessageLoop();
59 #if !defined(OS_CHROMEOS)
60 second_desktop_position_client_.reset();
62 second_capture_controller_.reset();
64 // Kill any active compositors before we hit the compositor shutdown paths.
67 #if !defined(OS_CHROMEOS)
68 desktop_position_client_.reset();
70 capture_controller_.reset();
72 AuraTestBase::TearDown();
75 aura::Window* GetCaptureWindow() {
76 return capture_controller_->capture_client()->GetCaptureWindow();
79 aura::Window* GetSecondCaptureWindow() {
80 return second_capture_controller_->capture_client()->GetCaptureWindow();
83 scoped_ptr<corewm::ScopedCaptureClient> capture_controller_;
84 scoped_ptr<aura::RootWindow> second_root_;
85 scoped_ptr<corewm::ScopedCaptureClient> second_capture_controller_;
86 #if !defined(OS_CHROMEOS)
87 scoped_ptr<aura::client::ScreenPositionClient> desktop_position_client_;
88 scoped_ptr<aura::client::ScreenPositionClient>
89 second_desktop_position_client_;
92 DISALLOW_COPY_AND_ASSIGN(CaptureControllerTest);
95 // Makes sure that internal details that are set on mouse down (such as
96 // mouse_pressed_handler()) are cleared when another root window takes capture.
97 TEST_F(CaptureControllerTest, ResetMouseEventHandlerOnCapture) {
98 // Create a window inside the RootWindow.
99 scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
101 // Make a synthesized mouse down event. Ensure that the RootWindow will
102 // dispatch further mouse events to |w1|.
103 ui::MouseEvent mouse_pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(5, 5),
104 gfx::Point(5, 5), 0);
105 root_window()->AsRootWindowHostDelegate()->OnHostMouseEvent(
106 &mouse_pressed_event);
107 EXPECT_EQ(w1.get(), root_window()->mouse_pressed_handler());
109 // Build a window in the second RootWindow.
110 scoped_ptr<aura::Window> w2(CreateNormalWindow(2, second_root_.get(), NULL));
112 // The act of having the second window take capture should clear out mouse
113 // pressed handler in the first RootWindow.
115 EXPECT_EQ(NULL, root_window()->mouse_pressed_handler());
118 // Makes sure that when one window gets capture, it forces the release on the
119 // other. This is needed has to be handled explicitly on Linux, and is a sanity
121 TEST_F(CaptureControllerTest, ResetOtherWindowCaptureOnCapture) {
122 // Create a window inside the RootWindow.
123 scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
125 // Both capture clients should return the same capture window.
126 EXPECT_EQ(w1.get(), GetCaptureWindow());
127 EXPECT_EQ(w1.get(), GetSecondCaptureWindow());
129 // Build a window in the second RootWindow and give it capture. Both capture
130 // clients should return the same capture window.
131 scoped_ptr<aura::Window> w2(CreateNormalWindow(2, second_root_.get(), NULL));
133 EXPECT_EQ(w2.get(), GetCaptureWindow());
134 EXPECT_EQ(w2.get(), GetSecondCaptureWindow());
137 // Verifies the touch target for the RootWindow gets reset on releasing capture.
138 TEST_F(CaptureControllerTest, TouchTargetResetOnCaptureChange) {
139 // Create a window inside the RootWindow.
140 scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
141 aura::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 RootWindow and give it capture. Both capture
149 // clients should return the same capture window.
150 scoped_ptr<aura::Window> w2(CreateNormalWindow(2, second_root_.get(), 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 RootWindow (as it no longer contains the capture
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(