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.
4
5#include "ui/wm/core/capture_controller.h"
6
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"
21
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"
25#endif
26
27namespace views {
28
29class CaptureControllerTest : public aura::test::AuraTestBase {
30 public:
31  CaptureControllerTest() {}
32
33  virtual void SetUp() OVERRIDE {
34    AuraTestBase::SetUp();
35    capture_controller_.reset(new wm::ScopedCaptureClient(root_window()));
36
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()));
43
44#if !defined(OS_CHROMEOS)
45    desktop_position_client_.reset(
46        new DesktopScreenPositionClient(root_window()));
47
48    second_desktop_position_client_.reset(
49        new DesktopScreenPositionClient(second_host_->window()));
50#endif
51  }
52
53  virtual void TearDown() OVERRIDE {
54    RunAllPendingInMessageLoop();
55
56#if !defined(OS_CHROMEOS)
57    second_desktop_position_client_.reset();
58#endif
59    second_capture_controller_.reset();
60
61    // Kill any active compositors before we hit the compositor shutdown paths.
62    second_host_.reset();
63
64#if !defined(OS_CHROMEOS)
65    desktop_position_client_.reset();
66#endif
67    capture_controller_.reset();
68
69    AuraTestBase::TearDown();
70  }
71
72  aura::Window* GetCaptureWindow() {
73    return capture_controller_->capture_client()->GetCaptureWindow();
74  }
75
76  aura::Window* GetSecondCaptureWindow() {
77    return second_capture_controller_->capture_client()->GetCaptureWindow();
78  }
79
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_;
87#endif
88
89  DISALLOW_COPY_AND_ASSIGN(CaptureControllerTest);
90};
91
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.
94TEST_F(CaptureControllerTest, ResetMouseEventHandlerOnCapture) {
95  // Create a window inside the WindowEventDispatcher.
96  scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
97
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());
104
105  // Build a window in the second WindowEventDispatcher.
106  scoped_ptr<aura::Window> w2(
107      CreateNormalWindow(2, second_host_->window(), NULL));
108
109  // The act of having the second window take capture should clear out mouse
110  // pressed handler in the first WindowEventDispatcher.
111  w2->SetCapture();
112  EXPECT_EQ(NULL, host()->dispatcher()->mouse_pressed_handler());
113}
114
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
117// check on Windows.
118TEST_F(CaptureControllerTest, ResetOtherWindowCaptureOnCapture) {
119  // Create a window inside the WindowEventDispatcher.
120  scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
121  w1->SetCapture();
122  // Both capture clients should return the same capture window.
123  EXPECT_EQ(w1.get(), GetCaptureWindow());
124  EXPECT_EQ(w1.get(), GetSecondCaptureWindow());
125
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));
130  w2->SetCapture();
131  EXPECT_EQ(w2.get(), GetCaptureWindow());
132  EXPECT_EQ(w2.get(), GetSecondCaptureWindow());
133}
134
135// Verifies the touch target for the WindowEventDispatcher gets reset on
136// releasing capture.
137TEST_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();
142  w1->SetCapture();
143  // Both capture clients should return the same capture window.
144  EXPECT_EQ(w1.get(), GetCaptureWindow());
145  EXPECT_EQ(w1.get(), GetSecondCaptureWindow());
146
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));
151  w2->SetCapture();
152  EXPECT_EQ(w2.get(), GetCaptureWindow());
153  EXPECT_EQ(w2.get(), GetSecondCaptureWindow());
154
155  // Release capture on the window. Releasing capture should reset the touch
156  // target of the first WindowEventDispatcher (as it no longer contains the
157  // capture target).
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,
163      1.0f, 1.0f, 1.0f);
164  EXPECT_EQ(static_cast<ui::GestureConsumer*>(w2.get()),
165            ui::GestureRecognizer::Get()->GetTouchLockedTarget(touch_event));
166}
167
168}  // namespace views
169