1 // Copyright (c) 2012 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/widget/native_widget_aura.h"
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/aura/client/aura_constants.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/layout_manager.h"
15 #include "ui/aura/test/aura_test_base.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_tree_host.h"
18 #include "ui/events/event.h"
19 #include "ui/events/event_utils.h"
20 #include "ui/gfx/screen.h"
21 #include "ui/views/layout/fill_layout.h"
22 #include "ui/views/widget/root_view.h"
23 #include "ui/views/widget/widget_delegate.h"
24 #include "ui/wm/core/default_activation_client.h"
29 NativeWidgetAura
* Init(aura::Window
* parent
, Widget
* widget
) {
30 Widget::InitParams
params(Widget::InitParams::TYPE_POPUP
);
31 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
32 params
.parent
= parent
;
34 return static_cast<NativeWidgetAura
*>(widget
->native_widget());
37 class NativeWidgetAuraTest
: public aura::test::AuraTestBase
{
39 NativeWidgetAuraTest() {}
40 ~NativeWidgetAuraTest() override
{}
42 // testing::Test overrides:
43 void SetUp() override
{
44 AuraTestBase::SetUp();
45 new wm::DefaultActivationClient(root_window());
46 host()->SetBounds(gfx::Rect(640, 480));
50 DISALLOW_COPY_AND_ASSIGN(NativeWidgetAuraTest
);
53 TEST_F(NativeWidgetAuraTest
, CenterWindowLargeParent
) {
54 // Make a parent window larger than the host represented by
55 // WindowEventDispatcher.
56 scoped_ptr
<aura::Window
> parent(new aura::Window(NULL
));
57 parent
->Init(ui::LAYER_NOT_DRAWN
);
58 parent
->SetBounds(gfx::Rect(0, 0, 1024, 800));
59 scoped_ptr
<Widget
> widget(new Widget());
60 NativeWidgetAura
* window
= Init(parent
.get(), widget
.get());
62 window
->CenterWindow(gfx::Size(100, 100));
63 EXPECT_EQ(gfx::Rect( (640 - 100) / 2,
66 window
->GetNativeWindow()->bounds());
70 TEST_F(NativeWidgetAuraTest
, CenterWindowSmallParent
) {
71 // Make a parent window smaller than the host represented by
72 // WindowEventDispatcher.
73 scoped_ptr
<aura::Window
> parent(new aura::Window(NULL
));
74 parent
->Init(ui::LAYER_NOT_DRAWN
);
75 parent
->SetBounds(gfx::Rect(0, 0, 480, 320));
76 scoped_ptr
<Widget
> widget(new Widget());
77 NativeWidgetAura
* window
= Init(parent
.get(), widget
.get());
79 window
->CenterWindow(gfx::Size(100, 100));
80 EXPECT_EQ(gfx::Rect( (480 - 100) / 2,
83 window
->GetNativeWindow()->bounds());
87 // Verifies CenterWindow() constrains to parent size.
88 TEST_F(NativeWidgetAuraTest
, CenterWindowSmallParentNotAtOrigin
) {
89 // Make a parent window smaller than the host represented by
90 // WindowEventDispatcher and offset it slightly from the origin.
91 scoped_ptr
<aura::Window
> parent(new aura::Window(NULL
));
92 parent
->Init(ui::LAYER_NOT_DRAWN
);
93 parent
->SetBounds(gfx::Rect(20, 40, 480, 320));
94 scoped_ptr
<Widget
> widget(new Widget());
95 NativeWidgetAura
* window
= Init(parent
.get(), widget
.get());
96 window
->CenterWindow(gfx::Size(500, 600));
98 // |window| should be no bigger than |parent|.
99 EXPECT_EQ("20,40 480x320", window
->GetNativeWindow()->bounds().ToString());
103 class TestLayoutManagerBase
: public aura::LayoutManager
{
105 TestLayoutManagerBase() {}
106 ~TestLayoutManagerBase() override
{}
108 // aura::LayoutManager:
109 void OnWindowResized() override
{}
110 void OnWindowAddedToLayout(aura::Window
* child
) override
{}
111 void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
{}
112 void OnWindowRemovedFromLayout(aura::Window
* child
) override
{}
113 void OnChildWindowVisibilityChanged(aura::Window
* child
,
114 bool visible
) override
{}
115 void SetChildBounds(aura::Window
* child
,
116 const gfx::Rect
& requested_bounds
) override
{}
119 DISALLOW_COPY_AND_ASSIGN(TestLayoutManagerBase
);
122 // Used by ShowMaximizedDoesntBounceAround. See it for details.
123 class MaximizeLayoutManager
: public TestLayoutManagerBase
{
125 MaximizeLayoutManager() {}
126 ~MaximizeLayoutManager() override
{}
129 // aura::LayoutManager:
130 void OnWindowAddedToLayout(aura::Window
* child
) override
{
131 // This simulates what happens when adding a maximized window.
132 SetChildBoundsDirect(child
, gfx::Rect(0, 0, 300, 300));
135 DISALLOW_COPY_AND_ASSIGN(MaximizeLayoutManager
);
138 // This simulates BrowserView, which creates a custom RootView so that
139 // OnNativeWidgetSizeChanged that is invoked during Init matters.
140 class TestWidget
: public views::Widget
{
142 TestWidget() : did_size_change_more_than_once_(false) {
145 // Returns true if the size changes to a non-empty size, and then to another
147 bool did_size_change_more_than_once() const {
148 return did_size_change_more_than_once_
;
151 void OnNativeWidgetSizeChanged(const gfx::Size
& new_size
) override
{
152 if (last_size_
.IsEmpty())
153 last_size_
= new_size
;
154 else if (!did_size_change_more_than_once_
&& new_size
!= last_size_
)
155 did_size_change_more_than_once_
= true;
156 Widget::OnNativeWidgetSizeChanged(new_size
);
160 bool did_size_change_more_than_once_
;
161 gfx::Size last_size_
;
163 DISALLOW_COPY_AND_ASSIGN(TestWidget
);
166 // Verifies the size of the widget doesn't change more than once during Init if
167 // the window ends up maximized. This is important as otherwise
168 // RenderWidgetHostViewAura ends up getting resized during construction, which
169 // leads to noticable flashes.
170 TEST_F(NativeWidgetAuraTest
, ShowMaximizedDoesntBounceAround
) {
171 root_window()->SetBounds(gfx::Rect(0, 0, 640, 480));
172 root_window()->SetLayoutManager(new MaximizeLayoutManager
);
173 scoped_ptr
<TestWidget
> widget(new TestWidget());
174 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW
);
175 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
176 params
.parent
= NULL
;
177 params
.context
= root_window();
178 params
.show_state
= ui::SHOW_STATE_MAXIMIZED
;
179 params
.bounds
= gfx::Rect(10, 10, 100, 200);
180 widget
->Init(params
);
181 EXPECT_FALSE(widget
->did_size_change_more_than_once());
185 class PropertyTestLayoutManager
: public TestLayoutManagerBase
{
187 PropertyTestLayoutManager() : added_(false) {}
188 ~PropertyTestLayoutManager() override
{}
190 bool added() const { return added_
; }
193 // aura::LayoutManager:
194 void OnWindowAddedToLayout(aura::Window
* child
) override
{
195 EXPECT_TRUE(child
->GetProperty(aura::client::kCanMaximizeKey
));
196 EXPECT_TRUE(child
->GetProperty(aura::client::kCanMinimizeKey
));
197 EXPECT_TRUE(child
->GetProperty(aura::client::kCanResizeKey
));
203 DISALLOW_COPY_AND_ASSIGN(PropertyTestLayoutManager
);
206 class PropertyTestWidgetDelegate
: public views::WidgetDelegate
{
208 explicit PropertyTestWidgetDelegate(Widget
* widget
) : widget_(widget
) {}
209 ~PropertyTestWidgetDelegate() override
{}
212 // views::WidgetDelegate:
213 bool CanMaximize() const override
{ return true; }
214 bool CanMinimize() const override
{ return true; }
215 bool CanResize() const override
{ return true; }
216 void DeleteDelegate() override
{ delete this; }
217 Widget
* GetWidget() override
{ return widget_
; }
218 const Widget
* GetWidget() const override
{ return widget_
; }
221 DISALLOW_COPY_AND_ASSIGN(PropertyTestWidgetDelegate
);
224 // Verifies that the kCanMaximizeKey/kCanMinimizeKey/kCanResizeKey have the
225 // correct value when added to the layout manager.
226 TEST_F(NativeWidgetAuraTest
, TestPropertiesWhenAddedToLayout
) {
227 root_window()->SetBounds(gfx::Rect(0, 0, 640, 480));
228 PropertyTestLayoutManager
* layout_manager
= new PropertyTestLayoutManager();
229 root_window()->SetLayoutManager(layout_manager
);
230 scoped_ptr
<TestWidget
> widget(new TestWidget());
231 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW
);
232 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
233 params
.delegate
= new PropertyTestWidgetDelegate(widget
.get());
234 params
.parent
= NULL
;
235 params
.context
= root_window();
236 widget
->Init(params
);
237 EXPECT_TRUE(layout_manager
->added());
241 TEST_F(NativeWidgetAuraTest
, GetClientAreaScreenBounds
) {
243 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW
);
244 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
245 params
.context
= root_window();
246 params
.bounds
.SetRect(10, 20, 300, 400);
247 scoped_ptr
<Widget
> widget(new Widget());
248 widget
->Init(params
);
250 // For Aura, client area bounds match window bounds.
251 gfx::Rect client_bounds
= widget
->GetClientAreaBoundsInScreen();
252 EXPECT_EQ(10, client_bounds
.x());
253 EXPECT_EQ(20, client_bounds
.y());
254 EXPECT_EQ(300, client_bounds
.width());
255 EXPECT_EQ(400, client_bounds
.height());
258 // View subclass that tracks whether it has gotten a gesture event.
259 class GestureTrackingView
: public views::View
{
261 GestureTrackingView()
262 : got_gesture_event_(false),
263 consume_gesture_event_(true) {}
265 void set_consume_gesture_event(bool value
) {
266 consume_gesture_event_
= value
;
269 void clear_got_gesture_event() {
270 got_gesture_event_
= false;
272 bool got_gesture_event() const {
273 return got_gesture_event_
;
277 void OnGestureEvent(ui::GestureEvent
* event
) override
{
278 got_gesture_event_
= true;
279 if (consume_gesture_event_
)
280 event
->StopPropagation();
284 // Was OnGestureEvent() invoked?
285 bool got_gesture_event_
;
287 // Dictates what OnGestureEvent() returns.
288 bool consume_gesture_event_
;
290 DISALLOW_COPY_AND_ASSIGN(GestureTrackingView
);
293 // Verifies a capture isn't set on touch press and that the view that gets
294 // the press gets the release.
295 TEST_F(NativeWidgetAuraTest
, DontCaptureOnGesture
) {
296 // Create two views (both sized the same). |child| is configured not to
297 // consume the gesture event.
298 GestureTrackingView
* view
= new GestureTrackingView();
299 GestureTrackingView
* child
= new GestureTrackingView();
300 child
->set_consume_gesture_event(false);
301 view
->SetLayoutManager(new FillLayout
);
302 view
->AddChildView(child
);
303 scoped_ptr
<TestWidget
> widget(new TestWidget());
304 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW_FRAMELESS
);
305 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
306 params
.context
= root_window();
307 params
.bounds
= gfx::Rect(0, 0, 100, 200);
308 widget
->Init(params
);
309 widget
->SetContentsView(view
);
312 ui::TouchEvent
press(
313 ui::ET_TOUCH_PRESSED
, gfx::Point(41, 51), 1, ui::EventTimeForNow());
314 ui::EventDispatchDetails details
=
315 event_processor()->OnEventFromSource(&press
);
316 ASSERT_FALSE(details
.dispatcher_destroyed
);
317 // Both views should get the press.
318 EXPECT_TRUE(view
->got_gesture_event());
319 EXPECT_TRUE(child
->got_gesture_event());
320 view
->clear_got_gesture_event();
321 child
->clear_got_gesture_event();
322 // Touch events should not automatically grab capture.
323 EXPECT_FALSE(widget
->HasCapture());
325 // Release touch. Only |view| should get the release since that it consumed
327 ui::TouchEvent
release(
328 ui::ET_TOUCH_RELEASED
, gfx::Point(250, 251), 1, ui::EventTimeForNow());
329 details
= event_processor()->OnEventFromSource(&release
);
330 ASSERT_FALSE(details
.dispatcher_destroyed
);
331 EXPECT_TRUE(view
->got_gesture_event());
332 EXPECT_FALSE(child
->got_gesture_event());
333 view
->clear_got_gesture_event();
335 // Work around for bug in NativeWidgetAura.
336 // TODO: fix bug and remove this.
340 // Verifies views with layers are targeted for events properly.
341 TEST_F(NativeWidgetAuraTest
, PreferViewLayersToChildWindows
) {
342 // Create two widgets: |parent| and |child|. |child| is a child of |parent|.
343 views::View
* parent_root
= new views::View
;
344 scoped_ptr
<Widget
> parent(new Widget());
345 Widget::InitParams
parent_params(Widget::InitParams::TYPE_WINDOW_FRAMELESS
);
346 parent_params
.ownership
=
347 views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
348 parent_params
.context
= root_window();
349 parent
->Init(parent_params
);
350 parent
->SetContentsView(parent_root
);
351 parent
->SetBounds(gfx::Rect(0, 0, 400, 400));
354 scoped_ptr
<Widget
> child(new Widget());
355 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
356 child_params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
357 child_params
.parent
= parent
->GetNativeWindow();
358 child
->Init(child_params
);
359 child
->SetBounds(gfx::Rect(0, 0, 200, 200));
362 // Point is over |child|.
363 EXPECT_EQ(child
->GetNativeWindow(),
364 parent
->GetNativeWindow()->GetEventHandlerForPoint(
365 gfx::Point(50, 50)));
367 // Create a view with a layer and stack it at the bottom (below |child|).
368 views::View
* view_with_layer
= new views::View
;
369 parent_root
->AddChildView(view_with_layer
);
370 view_with_layer
->SetBounds(0, 0, 50, 50);
371 view_with_layer
->SetPaintToLayer(true);
373 // Make sure that |child| still gets the event.
374 EXPECT_EQ(child
->GetNativeWindow(),
375 parent
->GetNativeWindow()->GetEventHandlerForPoint(
376 gfx::Point(20, 20)));
378 // Move |view_with_layer| to the top and make sure it gets the
379 // event when the point is within |view_with_layer|'s bounds.
380 view_with_layer
->layer()->parent()->StackAtTop(
381 view_with_layer
->layer());
382 EXPECT_EQ(parent
->GetNativeWindow(),
383 parent
->GetNativeWindow()->GetEventHandlerForPoint(
384 gfx::Point(20, 20)));
386 // Point is over |child|, it should get the event.
387 EXPECT_EQ(child
->GetNativeWindow(),
388 parent
->GetNativeWindow()->GetEventHandlerForPoint(
389 gfx::Point(70, 70)));
391 delete view_with_layer
;
392 view_with_layer
= NULL
;
394 EXPECT_EQ(child
->GetNativeWindow(),
395 parent
->GetNativeWindow()->GetEventHandlerForPoint(
396 gfx::Point(20, 20)));
398 // Work around for bug in NativeWidgetAura.
399 // TODO: fix bug and remove this.
403 // Verifies that widget->FlashFrame() sets aura::client::kDrawAttentionKey,
404 // and activating the window clears it.
405 TEST_F(NativeWidgetAuraTest
, FlashFrame
) {
406 scoped_ptr
<Widget
> widget(new Widget());
407 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW
);
408 params
.context
= root_window();
409 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
410 widget
->Init(params
);
411 aura::Window
* window
= widget
->GetNativeWindow();
412 EXPECT_FALSE(window
->GetProperty(aura::client::kDrawAttentionKey
));
413 widget
->FlashFrame(true);
414 EXPECT_TRUE(window
->GetProperty(aura::client::kDrawAttentionKey
));
415 widget
->FlashFrame(false);
416 EXPECT_FALSE(window
->GetProperty(aura::client::kDrawAttentionKey
));
417 widget
->FlashFrame(true);
418 EXPECT_TRUE(window
->GetProperty(aura::client::kDrawAttentionKey
));
420 EXPECT_FALSE(window
->GetProperty(aura::client::kDrawAttentionKey
));
423 TEST_F(NativeWidgetAuraTest
, NoCrashOnThemeAfterClose
) {
424 scoped_ptr
<aura::Window
> parent(new aura::Window(NULL
));
425 parent
->Init(ui::LAYER_NOT_DRAWN
);
426 parent
->SetBounds(gfx::Rect(0, 0, 480, 320));
427 scoped_ptr
<Widget
> widget(new Widget());
428 Init(parent
.get(), widget
.get());
431 base::MessageLoop::current()->RunUntilIdle();
432 widget
->GetNativeTheme(); // Shouldn't crash.
435 // Used to track calls to WidgetDelegate::OnWidgetMove().
436 class MoveTestWidgetDelegate
: public WidgetDelegateView
{
438 MoveTestWidgetDelegate() : got_move_(false) {}
439 ~MoveTestWidgetDelegate() override
{}
441 void ClearGotMove() { got_move_
= false; }
442 bool got_move() const { return got_move_
; }
444 // WidgetDelegate overrides:
445 void OnWidgetMove() override
{ got_move_
= true; }
450 DISALLOW_COPY_AND_ASSIGN(MoveTestWidgetDelegate
);
453 // This test simulates what happens when a window is normally maximized. That
454 // is, it's layer is acquired for animation then the window is maximized.
455 // Acquiring the layer resets the bounds of the window. This test verifies the
456 // Widget is still notified correctly of a move in this case.
457 TEST_F(NativeWidgetAuraTest
, OnWidgetMovedInvokedAfterAcquireLayer
) {
458 // |delegate| deletes itself when the widget is destroyed.
459 MoveTestWidgetDelegate
* delegate
= new MoveTestWidgetDelegate
;
461 Widget::CreateWindowWithContextAndBounds(delegate
,
463 gfx::Rect(10, 10, 100, 200));
465 delegate
->ClearGotMove();
466 // Simulate a maximize with animation.
467 delete widget
->GetNativeView()->RecreateLayer().release();
468 widget
->SetBounds(gfx::Rect(0, 0, 500, 500));
469 EXPECT_TRUE(delegate
->got_move());