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 virtual ~NativeWidgetAuraTest() {}
42 // testing::Test overrides:
43 virtual 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(aura::WINDOW_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(aura::WINDOW_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(aura::WINDOW_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 virtual ~TestLayoutManagerBase() {}
108 // aura::LayoutManager:
109 virtual void OnWindowResized() override
{}
110 virtual void OnWindowAddedToLayout(aura::Window
* child
) override
{}
111 virtual void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
{}
112 virtual void OnWindowRemovedFromLayout(aura::Window
* child
) override
{}
113 virtual void OnChildWindowVisibilityChanged(aura::Window
* child
,
114 bool visible
) override
{}
115 virtual 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 virtual ~MaximizeLayoutManager() {}
129 // aura::LayoutManager:
130 virtual 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 virtual 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 virtual ~PropertyTestLayoutManager() {}
190 bool added() const { return added_
; }
193 // aura::LayoutManager:
194 virtual 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 virtual ~PropertyTestWidgetDelegate() {}
212 // views::WidgetDelegate:
213 virtual bool CanMaximize() const override
{
216 virtual bool CanMinimize() const override
{
219 virtual bool CanResize() const override
{
222 virtual void DeleteDelegate() override
{
225 virtual Widget
* GetWidget() override
{
228 virtual const Widget
* GetWidget() const override
{
233 DISALLOW_COPY_AND_ASSIGN(PropertyTestWidgetDelegate
);
236 // Verifies that the kCanMaximizeKey/kCanMinimizeKey/kCanResizeKey have the
237 // correct value when added to the layout manager.
238 TEST_F(NativeWidgetAuraTest
, TestPropertiesWhenAddedToLayout
) {
239 root_window()->SetBounds(gfx::Rect(0, 0, 640, 480));
240 PropertyTestLayoutManager
* layout_manager
= new PropertyTestLayoutManager();
241 root_window()->SetLayoutManager(layout_manager
);
242 scoped_ptr
<TestWidget
> widget(new TestWidget());
243 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW
);
244 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
245 params
.delegate
= new PropertyTestWidgetDelegate(widget
.get());
246 params
.parent
= NULL
;
247 params
.context
= root_window();
248 widget
->Init(params
);
249 EXPECT_TRUE(layout_manager
->added());
253 TEST_F(NativeWidgetAuraTest
, GetClientAreaScreenBounds
) {
255 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW
);
256 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
257 params
.context
= root_window();
258 params
.bounds
.SetRect(10, 20, 300, 400);
259 scoped_ptr
<Widget
> widget(new Widget());
260 widget
->Init(params
);
262 // For Aura, client area bounds match window bounds.
263 gfx::Rect client_bounds
= widget
->GetClientAreaBoundsInScreen();
264 EXPECT_EQ(10, client_bounds
.x());
265 EXPECT_EQ(20, client_bounds
.y());
266 EXPECT_EQ(300, client_bounds
.width());
267 EXPECT_EQ(400, client_bounds
.height());
270 // View subclass that tracks whether it has gotten a gesture event.
271 class GestureTrackingView
: public views::View
{
273 GestureTrackingView()
274 : got_gesture_event_(false),
275 consume_gesture_event_(true) {}
277 void set_consume_gesture_event(bool value
) {
278 consume_gesture_event_
= value
;
281 void clear_got_gesture_event() {
282 got_gesture_event_
= false;
284 bool got_gesture_event() const {
285 return got_gesture_event_
;
289 virtual void OnGestureEvent(ui::GestureEvent
* event
) override
{
290 got_gesture_event_
= true;
291 if (consume_gesture_event_
)
292 event
->StopPropagation();
296 // Was OnGestureEvent() invoked?
297 bool got_gesture_event_
;
299 // Dictates what OnGestureEvent() returns.
300 bool consume_gesture_event_
;
302 DISALLOW_COPY_AND_ASSIGN(GestureTrackingView
);
305 // Verifies a capture isn't set on touch press and that the view that gets
306 // the press gets the release.
307 TEST_F(NativeWidgetAuraTest
, DontCaptureOnGesture
) {
308 // Create two views (both sized the same). |child| is configured not to
309 // consume the gesture event.
310 GestureTrackingView
* view
= new GestureTrackingView();
311 GestureTrackingView
* child
= new GestureTrackingView();
312 child
->set_consume_gesture_event(false);
313 view
->SetLayoutManager(new FillLayout
);
314 view
->AddChildView(child
);
315 scoped_ptr
<TestWidget
> widget(new TestWidget());
316 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW_FRAMELESS
);
317 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
318 params
.context
= root_window();
319 params
.bounds
= gfx::Rect(0, 0, 100, 200);
320 widget
->Init(params
);
321 widget
->SetContentsView(view
);
324 ui::TouchEvent
press(
325 ui::ET_TOUCH_PRESSED
, gfx::Point(41, 51), 1, ui::EventTimeForNow());
326 ui::EventDispatchDetails details
=
327 event_processor()->OnEventFromSource(&press
);
328 ASSERT_FALSE(details
.dispatcher_destroyed
);
329 // Both views should get the press.
330 EXPECT_TRUE(view
->got_gesture_event());
331 EXPECT_TRUE(child
->got_gesture_event());
332 view
->clear_got_gesture_event();
333 child
->clear_got_gesture_event();
334 // Touch events should not automatically grab capture.
335 EXPECT_FALSE(widget
->HasCapture());
337 // Release touch. Only |view| should get the release since that it consumed
339 ui::TouchEvent
release(
340 ui::ET_TOUCH_RELEASED
, gfx::Point(250, 251), 1, ui::EventTimeForNow());
341 details
= event_processor()->OnEventFromSource(&release
);
342 ASSERT_FALSE(details
.dispatcher_destroyed
);
343 EXPECT_TRUE(view
->got_gesture_event());
344 EXPECT_FALSE(child
->got_gesture_event());
345 view
->clear_got_gesture_event();
347 // Work around for bug in NativeWidgetAura.
348 // TODO: fix bug and remove this.
352 // Verifies views with layers are targeted for events properly.
353 TEST_F(NativeWidgetAuraTest
, PreferViewLayersToChildWindows
) {
354 // Create two widgets: |parent| and |child|. |child| is a child of |parent|.
355 views::View
* parent_root
= new views::View
;
356 scoped_ptr
<Widget
> parent(new Widget());
357 Widget::InitParams
parent_params(Widget::InitParams::TYPE_WINDOW_FRAMELESS
);
358 parent_params
.ownership
=
359 views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
360 parent_params
.context
= root_window();
361 parent
->Init(parent_params
);
362 parent
->SetContentsView(parent_root
);
363 parent
->SetBounds(gfx::Rect(0, 0, 400, 400));
366 scoped_ptr
<Widget
> child(new Widget());
367 Widget::InitParams
child_params(Widget::InitParams::TYPE_CONTROL
);
368 child_params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
369 child_params
.parent
= parent
->GetNativeWindow();
370 child
->Init(child_params
);
371 child
->SetBounds(gfx::Rect(0, 0, 200, 200));
374 // Point is over |child|.
375 EXPECT_EQ(child
->GetNativeWindow(),
376 parent
->GetNativeWindow()->GetEventHandlerForPoint(
377 gfx::Point(50, 50)));
379 // Create a view with a layer and stack it at the bottom (below |child|).
380 views::View
* view_with_layer
= new views::View
;
381 parent_root
->AddChildView(view_with_layer
);
382 view_with_layer
->SetBounds(0, 0, 50, 50);
383 view_with_layer
->SetPaintToLayer(true);
385 // Make sure that |child| still gets the event.
386 EXPECT_EQ(child
->GetNativeWindow(),
387 parent
->GetNativeWindow()->GetEventHandlerForPoint(
388 gfx::Point(20, 20)));
390 // Move |view_with_layer| to the top and make sure it gets the
391 // event when the point is within |view_with_layer|'s bounds.
392 view_with_layer
->layer()->parent()->StackAtTop(
393 view_with_layer
->layer());
394 EXPECT_EQ(parent
->GetNativeWindow(),
395 parent
->GetNativeWindow()->GetEventHandlerForPoint(
396 gfx::Point(20, 20)));
398 // Point is over |child|, it should get the event.
399 EXPECT_EQ(child
->GetNativeWindow(),
400 parent
->GetNativeWindow()->GetEventHandlerForPoint(
401 gfx::Point(70, 70)));
403 delete view_with_layer
;
404 view_with_layer
= NULL
;
406 EXPECT_EQ(child
->GetNativeWindow(),
407 parent
->GetNativeWindow()->GetEventHandlerForPoint(
408 gfx::Point(20, 20)));
410 // Work around for bug in NativeWidgetAura.
411 // TODO: fix bug and remove this.
415 // Verifies that widget->FlashFrame() sets aura::client::kDrawAttentionKey,
416 // and activating the window clears it.
417 TEST_F(NativeWidgetAuraTest
, FlashFrame
) {
418 scoped_ptr
<Widget
> widget(new Widget());
419 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW
);
420 params
.context
= root_window();
421 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
422 widget
->Init(params
);
423 aura::Window
* window
= widget
->GetNativeWindow();
424 EXPECT_FALSE(window
->GetProperty(aura::client::kDrawAttentionKey
));
425 widget
->FlashFrame(true);
426 EXPECT_TRUE(window
->GetProperty(aura::client::kDrawAttentionKey
));
427 widget
->FlashFrame(false);
428 EXPECT_FALSE(window
->GetProperty(aura::client::kDrawAttentionKey
));
429 widget
->FlashFrame(true);
430 EXPECT_TRUE(window
->GetProperty(aura::client::kDrawAttentionKey
));
432 EXPECT_FALSE(window
->GetProperty(aura::client::kDrawAttentionKey
));
435 TEST_F(NativeWidgetAuraTest
, NoCrashOnThemeAfterClose
) {
436 scoped_ptr
<aura::Window
> parent(new aura::Window(NULL
));
437 parent
->Init(aura::WINDOW_LAYER_NOT_DRAWN
);
438 parent
->SetBounds(gfx::Rect(0, 0, 480, 320));
439 scoped_ptr
<Widget
> widget(new Widget());
440 Init(parent
.get(), widget
.get());
443 base::MessageLoop::current()->RunUntilIdle();
444 widget
->GetNativeTheme(); // Shouldn't crash.
447 // Used to track calls to WidgetDelegate::OnWidgetMove().
448 class MoveTestWidgetDelegate
: public WidgetDelegateView
{
450 MoveTestWidgetDelegate() : got_move_(false) {}
451 virtual ~MoveTestWidgetDelegate() {}
453 void ClearGotMove() { got_move_
= false; }
454 bool got_move() const { return got_move_
; }
456 // WidgetDelegate overrides:
457 virtual void OnWidgetMove() override
{ got_move_
= true; }
462 DISALLOW_COPY_AND_ASSIGN(MoveTestWidgetDelegate
);
465 // This test simulates what happens when a window is normally maximized. That
466 // is, it's layer is acquired for animation then the window is maximized.
467 // Acquiring the layer resets the bounds of the window. This test verifies the
468 // Widget is still notified correctly of a move in this case.
469 TEST_F(NativeWidgetAuraTest
, OnWidgetMovedInvokedAfterAcquireLayer
) {
470 // |delegate| deletes itself when the widget is destroyed.
471 MoveTestWidgetDelegate
* delegate
= new MoveTestWidgetDelegate
;
473 Widget::CreateWindowWithContextAndBounds(delegate
,
475 gfx::Rect(10, 10, 100, 200));
477 delegate
->ClearGotMove();
478 // Simulate a maximize with animation.
479 delete widget
->GetNativeView()->RecreateLayer().release();
480 widget
->SetBounds(gfx::Rect(0, 0, 500, 500));
481 EXPECT_TRUE(delegate
->got_move());