Make castv2 performance test work.
[chromium-blink-merge.git] / ui / views / widget / native_widget_aura_unittest.cc
blob910718b2863fcd30cfdbaadd60a377f2a249258f
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"
26 namespace views {
27 namespace {
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;
33 widget->Init(params);
34 return static_cast<NativeWidgetAura*>(widget->native_widget());
37 class NativeWidgetAuraTest : public aura::test::AuraTestBase {
38 public:
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));
49 private:
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,
64 (480 - 100) / 2,
65 100, 100),
66 window->GetNativeWindow()->bounds());
67 widget->CloseNow();
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,
81 (320 - 100) / 2,
82 100, 100),
83 window->GetNativeWindow()->bounds());
84 widget->CloseNow();
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());
100 widget->CloseNow();
103 class TestLayoutManagerBase : public aura::LayoutManager {
104 public:
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 {}
118 private:
119 DISALLOW_COPY_AND_ASSIGN(TestLayoutManagerBase);
122 // Used by ShowMaximizedDoesntBounceAround. See it for details.
123 class MaximizeLayoutManager : public TestLayoutManagerBase {
124 public:
125 MaximizeLayoutManager() {}
126 ~MaximizeLayoutManager() override {}
128 private:
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 {
141 public:
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
146 // size.
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);
159 private:
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());
182 widget->CloseNow();
185 class PropertyTestLayoutManager : public TestLayoutManagerBase {
186 public:
187 PropertyTestLayoutManager() : added_(false) {}
188 ~PropertyTestLayoutManager() override {}
190 bool added() const { return added_; }
192 private:
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));
198 added_ = true;
201 bool added_;
203 DISALLOW_COPY_AND_ASSIGN(PropertyTestLayoutManager);
206 class PropertyTestWidgetDelegate : public views::WidgetDelegate {
207 public:
208 explicit PropertyTestWidgetDelegate(Widget* widget) : widget_(widget) {}
209 ~PropertyTestWidgetDelegate() override {}
211 private:
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_; }
220 Widget* 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());
238 widget->CloseNow();
241 TEST_F(NativeWidgetAuraTest, GetClientAreaScreenBounds) {
242 // Create a widget.
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 {
260 public:
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_;
276 // View overrides:
277 void OnGestureEvent(ui::GestureEvent* event) override {
278 got_gesture_event_ = true;
279 if (consume_gesture_event_)
280 event->StopPropagation();
283 private:
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);
310 widget->Show();
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
326 // the press.
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.
337 widget->Close();
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));
352 parent->Show();
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));
360 child->Show();
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.
400 parent->Close();
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));
419 widget->Activate();
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());
429 widget->Show();
430 widget->Close();
431 base::MessageLoop::current()->RunUntilIdle();
432 widget->GetNativeTheme(); // Shouldn't crash.
435 // Used to track calls to WidgetDelegate::OnWidgetMove().
436 class MoveTestWidgetDelegate : public WidgetDelegateView {
437 public:
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; }
447 private:
448 bool got_move_;
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;
460 Widget* widget =
461 Widget::CreateWindowWithContextAndBounds(delegate,
462 root_window(),
463 gfx::Rect(10, 10, 100, 200));
464 widget->Show();
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());
470 widget->CloseNow();
473 } // namespace
474 } // namespace views