[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / wm / core / capture_controller_unittest.cc
blobff072b7fe40d0bf0134286203d03950e63076d9e
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"
19 namespace wm {
21 namespace {
23 // aura::client::CaptureDelegate which allows querying whether native capture
24 // has been acquired.
25 class TestCaptureDelegate : public aura::client::CaptureDelegate {
26 public:
27 TestCaptureDelegate() : has_capture_(false) {}
28 ~TestCaptureDelegate() override {}
30 bool HasNativeCapture() const {
31 return has_capture_;
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; }
41 private:
42 bool has_capture_;
44 DISALLOW_COPY_AND_ASSIGN(TestCaptureDelegate);
47 } // namespace
49 class CaptureControllerTest : public aura::test::AuraTestBase {
50 public:
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.
71 second_host_.reset();
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), ui::EventTimeForNow(), 0,
104 DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
105 EXPECT_EQ(w1.get(), host()->dispatcher()->mouse_pressed_handler());
107 // Build a window in the second WindowEventDispatcher.
108 scoped_ptr<aura::Window> w2(
109 CreateNormalWindow(2, second_host_->window(), NULL));
111 // The act of having the second window take capture should clear out mouse
112 // pressed handler in the first WindowEventDispatcher.
113 w2->SetCapture();
114 EXPECT_EQ(NULL, host()->dispatcher()->mouse_pressed_handler());
117 // Makes sure that when one window gets capture, it forces the release on the
118 // other. This is needed has to be handled explicitly on Linux, and is a sanity
119 // check on Windows.
120 TEST_F(CaptureControllerTest, ResetOtherWindowCaptureOnCapture) {
121 // Create a window inside the WindowEventDispatcher.
122 scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
123 w1->SetCapture();
124 // Both capture clients should return the same capture window.
125 EXPECT_EQ(w1.get(), GetCaptureWindow());
126 EXPECT_EQ(w1.get(), GetSecondCaptureWindow());
128 // Build a window in the second WindowEventDispatcher and give it capture.
129 // Both capture clients should return the same capture window.
130 scoped_ptr<aura::Window> w2(
131 CreateNormalWindow(2, second_host_->window(), NULL));
132 w2->SetCapture();
133 EXPECT_EQ(w2.get(), GetCaptureWindow());
134 EXPECT_EQ(w2.get(), GetSecondCaptureWindow());
137 // Verifies the touch target for the WindowEventDispatcher gets reset on
138 // releasing capture.
139 TEST_F(CaptureControllerTest, TouchTargetResetOnCaptureChange) {
140 // Create a window inside the WindowEventDispatcher.
141 scoped_ptr<aura::Window> w1(CreateNormalWindow(1, root_window(), NULL));
142 ui::test::EventGenerator event_generator1(root_window());
143 event_generator1.PressTouch();
144 w1->SetCapture();
145 // Both capture clients should return the same capture window.
146 EXPECT_EQ(w1.get(), GetCaptureWindow());
147 EXPECT_EQ(w1.get(), GetSecondCaptureWindow());
149 // Build a window in the second WindowEventDispatcher and give it capture.
150 // Both capture clients should return the same capture window.
151 scoped_ptr<aura::Window> w2(
152 CreateNormalWindow(2, second_host_->window(), NULL));
153 w2->SetCapture();
154 EXPECT_EQ(w2.get(), GetCaptureWindow());
155 EXPECT_EQ(w2.get(), GetSecondCaptureWindow());
157 // Release capture on the window. Releasing capture should reset the touch
158 // target of the first WindowEventDispatcher (as it no longer contains the
159 // capture target).
160 w2->ReleaseCapture();
161 EXPECT_EQ(static_cast<aura::Window*>(NULL), GetCaptureWindow());
162 EXPECT_EQ(static_cast<aura::Window*>(NULL), GetSecondCaptureWindow());
165 // Test that native capture is released properly when the window with capture
166 // is reparented to a different root window while it has capture.
167 TEST_F(CaptureControllerTest, ReparentedWhileCaptured) {
168 scoped_ptr<TestCaptureDelegate> delegate(new TestCaptureDelegate);
169 ScopedCaptureClient::TestApi(capture_controller_.get())
170 .SetDelegate(delegate.get());
171 scoped_ptr<TestCaptureDelegate> delegate2(new TestCaptureDelegate);
172 ScopedCaptureClient::TestApi(second_capture_controller_.get())
173 .SetDelegate(delegate2.get());
175 scoped_ptr<aura::Window> w(CreateNormalWindow(1, root_window(), NULL));
176 w->SetCapture();
177 EXPECT_EQ(w.get(), GetCaptureWindow());
178 EXPECT_EQ(w.get(), GetSecondCaptureWindow());
179 EXPECT_TRUE(delegate->HasNativeCapture());
180 EXPECT_FALSE(delegate2->HasNativeCapture());
182 second_host_->window()->AddChild(w.get());
183 EXPECT_EQ(w.get(), GetCaptureWindow());
184 EXPECT_EQ(w.get(), GetSecondCaptureWindow());
185 EXPECT_TRUE(delegate->HasNativeCapture());
186 EXPECT_FALSE(delegate2->HasNativeCapture());
188 w->ReleaseCapture();
189 EXPECT_EQ(nullptr, GetCaptureWindow());
190 EXPECT_EQ(nullptr, GetSecondCaptureWindow());
191 EXPECT_FALSE(delegate->HasNativeCapture());
192 EXPECT_FALSE(delegate2->HasNativeCapture());
195 } // namespace wm