Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: ui/views/widget/native_widget_aura_unittest.cc

Issue 1905333004: Corrects NativeWidgetAura state model to not minimize on every restore (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Corrects NativeWidgetAura state model to not minimize on every restore (nits) Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ui/views/widget/widget.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/widget/native_widget_aura.h" 5 #include "ui/views/widget/native_widget_aura.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/aura/client/aura_constants.h" 13 #include "ui/aura/client/aura_constants.h"
14 #include "ui/aura/env.h" 14 #include "ui/aura/env.h"
15 #include "ui/aura/layout_manager.h" 15 #include "ui/aura/layout_manager.h"
16 #include "ui/aura/test/aura_test_base.h" 16 #include "ui/aura/test/aura_test_base.h"
17 #include "ui/aura/window.h" 17 #include "ui/aura/window.h"
18 #include "ui/aura/window_observer.h"
18 #include "ui/aura/window_tree_host.h" 19 #include "ui/aura/window_tree_host.h"
19 #include "ui/events/event.h" 20 #include "ui/events/event.h"
20 #include "ui/events/event_utils.h" 21 #include "ui/events/event_utils.h"
21 #include "ui/gfx/screen.h" 22 #include "ui/gfx/screen.h"
22 #include "ui/views/layout/fill_layout.h" 23 #include "ui/views/layout/fill_layout.h"
23 #include "ui/views/test/widget_test.h" 24 #include "ui/views/test/widget_test.h"
24 #include "ui/views/widget/root_view.h" 25 #include "ui/views/widget/root_view.h"
25 #include "ui/views/widget/widget_delegate.h" 26 #include "ui/views/widget/widget_delegate.h"
26 #include "ui/wm/core/base_focus_rules.h" 27 #include "ui/wm/core/base_focus_rules.h"
27 #include "ui/wm/core/default_activation_client.h" 28 #include "ui/wm/core/default_activation_client.h"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 private: 82 private:
82 std::unique_ptr<wm::FocusController> focus_controller_; 83 std::unique_ptr<wm::FocusController> focus_controller_;
83 TestFocusRules* test_focus_rules_; 84 TestFocusRules* test_focus_rules_;
84 85
85 DISALLOW_COPY_AND_ASSIGN(NativeWidgetAuraTest); 86 DISALLOW_COPY_AND_ASSIGN(NativeWidgetAuraTest);
86 }; 87 };
87 88
88 TEST_F(NativeWidgetAuraTest, CenterWindowLargeParent) { 89 TEST_F(NativeWidgetAuraTest, CenterWindowLargeParent) {
89 // Make a parent window larger than the host represented by 90 // Make a parent window larger than the host represented by
90 // WindowEventDispatcher. 91 // WindowEventDispatcher.
91 std::unique_ptr<aura::Window> parent(new aura::Window(NULL)); 92 std::unique_ptr<aura::Window> parent(new aura::Window(nullptr));
92 parent->Init(ui::LAYER_NOT_DRAWN); 93 parent->Init(ui::LAYER_NOT_DRAWN);
93 parent->SetBounds(gfx::Rect(0, 0, 1024, 800)); 94 parent->SetBounds(gfx::Rect(0, 0, 1024, 800));
94 std::unique_ptr<Widget> widget(new Widget()); 95 std::unique_ptr<Widget> widget(new Widget());
95 NativeWidgetAura* window = Init(parent.get(), widget.get()); 96 NativeWidgetAura* window = Init(parent.get(), widget.get());
96 97
97 window->CenterWindow(gfx::Size(100, 100)); 98 window->CenterWindow(gfx::Size(100, 100));
98 EXPECT_EQ(gfx::Rect( (640 - 100) / 2, 99 EXPECT_EQ(gfx::Rect( (640 - 100) / 2,
99 (480 - 100) / 2, 100 (480 - 100) / 2,
100 100, 100), 101 100, 100),
101 window->GetNativeWindow()->bounds()); 102 window->GetNativeWindow()->bounds());
102 widget->CloseNow(); 103 widget->CloseNow();
103 } 104 }
104 105
105 TEST_F(NativeWidgetAuraTest, CenterWindowSmallParent) { 106 TEST_F(NativeWidgetAuraTest, CenterWindowSmallParent) {
106 // Make a parent window smaller than the host represented by 107 // Make a parent window smaller than the host represented by
107 // WindowEventDispatcher. 108 // WindowEventDispatcher.
108 std::unique_ptr<aura::Window> parent(new aura::Window(NULL)); 109 std::unique_ptr<aura::Window> parent(new aura::Window(nullptr));
109 parent->Init(ui::LAYER_NOT_DRAWN); 110 parent->Init(ui::LAYER_NOT_DRAWN);
110 parent->SetBounds(gfx::Rect(0, 0, 480, 320)); 111 parent->SetBounds(gfx::Rect(0, 0, 480, 320));
111 std::unique_ptr<Widget> widget(new Widget()); 112 std::unique_ptr<Widget> widget(new Widget());
112 NativeWidgetAura* window = Init(parent.get(), widget.get()); 113 NativeWidgetAura* window = Init(parent.get(), widget.get());
113 114
114 window->CenterWindow(gfx::Size(100, 100)); 115 window->CenterWindow(gfx::Size(100, 100));
115 EXPECT_EQ(gfx::Rect( (480 - 100) / 2, 116 EXPECT_EQ(gfx::Rect( (480 - 100) / 2,
116 (320 - 100) / 2, 117 (320 - 100) / 2,
117 100, 100), 118 100, 100),
118 window->GetNativeWindow()->bounds()); 119 window->GetNativeWindow()->bounds());
119 widget->CloseNow(); 120 widget->CloseNow();
120 } 121 }
121 122
122 // Verifies CenterWindow() constrains to parent size. 123 // Verifies CenterWindow() constrains to parent size.
123 TEST_F(NativeWidgetAuraTest, CenterWindowSmallParentNotAtOrigin) { 124 TEST_F(NativeWidgetAuraTest, CenterWindowSmallParentNotAtOrigin) {
124 // Make a parent window smaller than the host represented by 125 // Make a parent window smaller than the host represented by
125 // WindowEventDispatcher and offset it slightly from the origin. 126 // WindowEventDispatcher and offset it slightly from the origin.
126 std::unique_ptr<aura::Window> parent(new aura::Window(NULL)); 127 std::unique_ptr<aura::Window> parent(new aura::Window(nullptr));
127 parent->Init(ui::LAYER_NOT_DRAWN); 128 parent->Init(ui::LAYER_NOT_DRAWN);
128 parent->SetBounds(gfx::Rect(20, 40, 480, 320)); 129 parent->SetBounds(gfx::Rect(20, 40, 480, 320));
129 std::unique_ptr<Widget> widget(new Widget()); 130 std::unique_ptr<Widget> widget(new Widget());
130 NativeWidgetAura* window = Init(parent.get(), widget.get()); 131 NativeWidgetAura* window = Init(parent.get(), widget.get());
131 window->CenterWindow(gfx::Size(500, 600)); 132 window->CenterWindow(gfx::Size(500, 600));
132 133
133 // |window| should be no bigger than |parent|. 134 // |window| should be no bigger than |parent|.
134 EXPECT_EQ("20,40 480x320", window->GetNativeWindow()->bounds().ToString()); 135 EXPECT_EQ("20,40 480x320", window->GetNativeWindow()->bounds().ToString());
135 widget->CloseNow(); 136 widget->CloseNow();
136 } 137 }
137 138
138 TEST_F(NativeWidgetAuraTest, CreateMinimized) { 139 TEST_F(NativeWidgetAuraTest, CreateMinimized) {
139 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); 140 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
140 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 141 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
141 params.parent = NULL; 142 params.parent = nullptr;
142 params.context = root_window(); 143 params.context = root_window();
143 params.show_state = ui::SHOW_STATE_MINIMIZED; 144 params.show_state = ui::SHOW_STATE_MINIMIZED;
144 params.bounds.SetRect(0, 0, 1024, 800); 145 params.bounds.SetRect(0, 0, 1024, 800);
145 std::unique_ptr<Widget> widget(new Widget()); 146 std::unique_ptr<Widget> widget(new Widget());
146 widget->Init(params); 147 widget->Init(params);
147 widget->Show(); 148 widget->Show();
148 149
149 EXPECT_TRUE(widget->IsMinimized()); 150 EXPECT_TRUE(widget->IsMinimized());
150 widget->CloseNow(); 151 widget->CloseNow();
151 } 152 }
152 153
154 // A WindowObserver that counts kShowStateKey property changes.
155 class TestWindowObserver : public aura::WindowObserver {
156 public:
157 explicit TestWindowObserver(gfx::NativeWindow window) : window_(window) {
158 window_->AddObserver(this);
159 }
160 ~TestWindowObserver() override {
161 window_->RemoveObserver(this);
162 }
163
164 // aura::WindowObserver:
165 void OnWindowPropertyChanged(aura::Window* window,
166 const void* key,
167 intptr_t old) override {
168 if (key != aura::client::kShowStateKey)
169 return;
170 count_++;
171 state_ = window_->GetProperty(aura::client::kShowStateKey);
172 }
173
174 int count() const { return count_; }
175 ui::WindowShowState state() const { return state_; }
176 void Reset() { count_ = 0; }
177
178 private:
179 gfx::NativeWindow window_;
180 int count_ = 0;
181 ui::WindowShowState state_ = ui::WindowShowState::SHOW_STATE_DEFAULT;
182
183 DISALLOW_COPY_AND_ASSIGN(TestWindowObserver);
184 };
185
186 // Tests that window transitions from normal to minimized and back do not
187 // involve extra show state transitions.
188 TEST_F(NativeWidgetAuraTest, ToggleState) {
189 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
190 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
191 params.parent = nullptr;
192 params.context = root_window();
193 params.show_state = ui::SHOW_STATE_NORMAL;
194 params.bounds.SetRect(0, 0, 1024, 800);
195 Widget widget;
196 widget.Init(params);
197 std::unique_ptr<TestWindowObserver> observer(
198 new TestWindowObserver(widget.GetNativeWindow()));
199 widget.Show();
200 EXPECT_FALSE(widget.IsMinimized());
201 EXPECT_EQ(0, observer->count());
202 EXPECT_EQ(ui::WindowShowState::SHOW_STATE_DEFAULT, observer->state());
203
204 widget.Minimize();
205 EXPECT_TRUE(widget.IsMinimized());
206 EXPECT_EQ(1, observer->count());
207 EXPECT_EQ(ui::WindowShowState::SHOW_STATE_MINIMIZED, observer->state());
208 observer->Reset();
209
210 widget.Show();
211 widget.Restore();
212 EXPECT_EQ(1, observer->count());
213 EXPECT_EQ(ui::WindowShowState::SHOW_STATE_NORMAL, observer->state());
214
215 observer.reset();
216 EXPECT_FALSE(widget.IsMinimized());
217 widget.CloseNow();
218 }
219
153 class TestLayoutManagerBase : public aura::LayoutManager { 220 class TestLayoutManagerBase : public aura::LayoutManager {
154 public: 221 public:
155 TestLayoutManagerBase() {} 222 TestLayoutManagerBase() {}
156 ~TestLayoutManagerBase() override {} 223 ~TestLayoutManagerBase() override {}
157 224
158 // aura::LayoutManager: 225 // aura::LayoutManager:
159 void OnWindowResized() override {} 226 void OnWindowResized() override {}
160 void OnWindowAddedToLayout(aura::Window* child) override {} 227 void OnWindowAddedToLayout(aura::Window* child) override {}
161 void OnWillRemoveWindowFromLayout(aura::Window* child) override {} 228 void OnWillRemoveWindowFromLayout(aura::Window* child) override {}
162 void OnWindowRemovedFromLayout(aura::Window* child) override {} 229 void OnWindowRemovedFromLayout(aura::Window* child) override {}
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 // Verifies the size of the widget doesn't change more than once during Init if 283 // Verifies the size of the widget doesn't change more than once during Init if
217 // the window ends up maximized. This is important as otherwise 284 // the window ends up maximized. This is important as otherwise
218 // RenderWidgetHostViewAura ends up getting resized during construction, which 285 // RenderWidgetHostViewAura ends up getting resized during construction, which
219 // leads to noticable flashes. 286 // leads to noticable flashes.
220 TEST_F(NativeWidgetAuraTest, ShowMaximizedDoesntBounceAround) { 287 TEST_F(NativeWidgetAuraTest, ShowMaximizedDoesntBounceAround) {
221 root_window()->SetBounds(gfx::Rect(0, 0, 640, 480)); 288 root_window()->SetBounds(gfx::Rect(0, 0, 640, 480));
222 root_window()->SetLayoutManager(new MaximizeLayoutManager); 289 root_window()->SetLayoutManager(new MaximizeLayoutManager);
223 std::unique_ptr<TestWidget> widget(new TestWidget()); 290 std::unique_ptr<TestWidget> widget(new TestWidget());
224 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); 291 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
225 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 292 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
226 params.parent = NULL; 293 params.parent = nullptr;
227 params.context = root_window(); 294 params.context = root_window();
228 params.show_state = ui::SHOW_STATE_MAXIMIZED; 295 params.show_state = ui::SHOW_STATE_MAXIMIZED;
229 params.bounds = gfx::Rect(10, 10, 100, 200); 296 params.bounds = gfx::Rect(10, 10, 100, 200);
230 widget->Init(params); 297 widget->Init(params);
231 EXPECT_FALSE(widget->did_size_change_more_than_once()); 298 EXPECT_FALSE(widget->did_size_change_more_than_once());
232 widget->CloseNow(); 299 widget->CloseNow();
233 } 300 }
234 301
235 class PropertyTestLayoutManager : public TestLayoutManagerBase { 302 class PropertyTestLayoutManager : public TestLayoutManagerBase {
236 public: 303 public:
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // Verifies that the kCanMaximizeKey/kCanMinimizeKey/kCanResizeKey have the 341 // Verifies that the kCanMaximizeKey/kCanMinimizeKey/kCanResizeKey have the
275 // correct value when added to the layout manager. 342 // correct value when added to the layout manager.
276 TEST_F(NativeWidgetAuraTest, TestPropertiesWhenAddedToLayout) { 343 TEST_F(NativeWidgetAuraTest, TestPropertiesWhenAddedToLayout) {
277 root_window()->SetBounds(gfx::Rect(0, 0, 640, 480)); 344 root_window()->SetBounds(gfx::Rect(0, 0, 640, 480));
278 PropertyTestLayoutManager* layout_manager = new PropertyTestLayoutManager(); 345 PropertyTestLayoutManager* layout_manager = new PropertyTestLayoutManager();
279 root_window()->SetLayoutManager(layout_manager); 346 root_window()->SetLayoutManager(layout_manager);
280 std::unique_ptr<TestWidget> widget(new TestWidget()); 347 std::unique_ptr<TestWidget> widget(new TestWidget());
281 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); 348 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
282 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 349 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
283 params.delegate = new PropertyTestWidgetDelegate(widget.get()); 350 params.delegate = new PropertyTestWidgetDelegate(widget.get());
284 params.parent = NULL; 351 params.parent = nullptr;
285 params.context = root_window(); 352 params.context = root_window();
286 widget->Init(params); 353 widget->Init(params);
287 EXPECT_TRUE(layout_manager->added()); 354 EXPECT_TRUE(layout_manager->added());
288 widget->CloseNow(); 355 widget->CloseNow();
289 } 356 }
290 357
291 TEST_F(NativeWidgetAuraTest, GetClientAreaScreenBounds) { 358 TEST_F(NativeWidgetAuraTest, GetClientAreaScreenBounds) {
292 // Create a widget. 359 // Create a widget.
293 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); 360 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
294 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 361 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 EXPECT_EQ(parent->GetNativeWindow(), 499 EXPECT_EQ(parent->GetNativeWindow(),
433 parent->GetNativeWindow()->GetEventHandlerForPoint( 500 parent->GetNativeWindow()->GetEventHandlerForPoint(
434 gfx::Point(20, 20))); 501 gfx::Point(20, 20)));
435 502
436 // Point is over |child|, it should get the event. 503 // Point is over |child|, it should get the event.
437 EXPECT_EQ(child->GetNativeWindow(), 504 EXPECT_EQ(child->GetNativeWindow(),
438 parent->GetNativeWindow()->GetEventHandlerForPoint( 505 parent->GetNativeWindow()->GetEventHandlerForPoint(
439 gfx::Point(70, 70))); 506 gfx::Point(70, 70)));
440 507
441 delete view_with_layer; 508 delete view_with_layer;
442 view_with_layer = NULL; 509 view_with_layer = nullptr;
443 510
444 EXPECT_EQ(child->GetNativeWindow(), 511 EXPECT_EQ(child->GetNativeWindow(),
445 parent->GetNativeWindow()->GetEventHandlerForPoint( 512 parent->GetNativeWindow()->GetEventHandlerForPoint(
446 gfx::Point(20, 20))); 513 gfx::Point(20, 20)));
447 514
448 // Work around for bug in NativeWidgetAura. 515 // Work around for bug in NativeWidgetAura.
449 // TODO: fix bug and remove this. 516 // TODO: fix bug and remove this.
450 parent->Close(); 517 parent->Close();
451 } 518 }
452 519
(...skipping 11 matching lines...) Expand all
464 EXPECT_TRUE(window->GetProperty(aura::client::kDrawAttentionKey)); 531 EXPECT_TRUE(window->GetProperty(aura::client::kDrawAttentionKey));
465 widget->FlashFrame(false); 532 widget->FlashFrame(false);
466 EXPECT_FALSE(window->GetProperty(aura::client::kDrawAttentionKey)); 533 EXPECT_FALSE(window->GetProperty(aura::client::kDrawAttentionKey));
467 widget->FlashFrame(true); 534 widget->FlashFrame(true);
468 EXPECT_TRUE(window->GetProperty(aura::client::kDrawAttentionKey)); 535 EXPECT_TRUE(window->GetProperty(aura::client::kDrawAttentionKey));
469 widget->Activate(); 536 widget->Activate();
470 EXPECT_FALSE(window->GetProperty(aura::client::kDrawAttentionKey)); 537 EXPECT_FALSE(window->GetProperty(aura::client::kDrawAttentionKey));
471 } 538 }
472 539
473 TEST_F(NativeWidgetAuraTest, NoCrashOnThemeAfterClose) { 540 TEST_F(NativeWidgetAuraTest, NoCrashOnThemeAfterClose) {
474 std::unique_ptr<aura::Window> parent(new aura::Window(NULL)); 541 std::unique_ptr<aura::Window> parent(new aura::Window(nullptr));
475 parent->Init(ui::LAYER_NOT_DRAWN); 542 parent->Init(ui::LAYER_NOT_DRAWN);
476 parent->SetBounds(gfx::Rect(0, 0, 480, 320)); 543 parent->SetBounds(gfx::Rect(0, 0, 480, 320));
477 std::unique_ptr<Widget> widget(new Widget()); 544 std::unique_ptr<Widget> widget(new Widget());
478 Init(parent.get(), widget.get()); 545 Init(parent.get(), widget.get());
479 widget->Show(); 546 widget->Show();
480 widget->Close(); 547 widget->Close();
481 base::MessageLoop::current()->RunUntilIdle(); 548 base::MessageLoop::current()->RunUntilIdle();
482 widget->GetNativeTheme(); // Shouldn't crash. 549 widget->GetNativeTheme(); // Shouldn't crash.
483 } 550 }
484 551
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 EXPECT_FALSE(delegate.view()->HasFocus()); 597 EXPECT_FALSE(delegate.view()->HasFocus());
531 598
532 test_focus_rules()->set_can_activate(true); 599 test_focus_rules()->set_can_activate(true);
533 views::test::TestInitialFocusWidgetDelegate delegate2(root_window()); 600 views::test::TestInitialFocusWidgetDelegate delegate2(root_window());
534 delegate2.GetWidget()->Show(); 601 delegate2.GetWidget()->Show();
535 EXPECT_TRUE(delegate2.view()->HasFocus()); 602 EXPECT_TRUE(delegate2.view()->HasFocus());
536 } 603 }
537 604
538 } // namespace 605 } // namespace
539 } // namespace views 606 } // namespace views
OLDNEW
« no previous file with comments | « no previous file | ui/views/widget/widget.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698