OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/desktop_aura/desktop_native_widget_aura.h" | 5 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "ui/aura/client/aura_constants.h" |
8 #include "ui/aura/client/cursor_client.h" | 9 #include "ui/aura/client/cursor_client.h" |
| 10 #include "ui/aura/client/window_tree_client.h" |
| 11 #include "ui/aura/test/event_generator.h" |
9 #include "ui/aura/test/test_window_delegate.h" | 12 #include "ui/aura/test/test_window_delegate.h" |
10 #include "ui/aura/window.h" | 13 #include "ui/aura/window.h" |
11 #include "ui/aura/window_tree_host.h" | 14 #include "ui/aura/window_tree_host.h" |
12 #include "ui/views/test/views_test_base.h" | 15 #include "ui/views/test/views_test_base.h" |
| 16 #include "ui/views/test/widget_test.h" |
13 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
14 #include "ui/wm/public/dispatcher_client.h" | 18 #include "ui/wm/public/dispatcher_client.h" |
15 | 19 |
16 namespace views { | 20 namespace views { |
| 21 namespace test { |
17 | 22 |
18 typedef ViewsTestBase DesktopNativeWidgetAuraTest; | 23 typedef ViewsTestBase DesktopNativeWidgetAuraTest; |
19 | 24 |
20 // Verifies creating a Widget with a parent that is not in a RootWindow doesn't | 25 // Verifies creating a Widget with a parent that is not in a RootWindow doesn't |
21 // crash. | 26 // crash. |
22 TEST_F(DesktopNativeWidgetAuraTest, CreateWithParentNotInRootWindow) { | 27 TEST_F(DesktopNativeWidgetAuraTest, CreateWithParentNotInRootWindow) { |
23 scoped_ptr<aura::Window> window(new aura::Window(NULL)); | 28 scoped_ptr<aura::Window> window(new aura::Window(NULL)); |
24 Widget widget; | 29 Widget widget; |
25 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); | 30 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
26 params.bounds = gfx::Rect(0, 0, 200, 200); | 31 params.bounds = gfx::Rect(0, 0, 200, 200); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 // |RunWithDispatcher()| below. | 227 // |RunWithDispatcher()| below. |
223 aura::client::DispatcherRunLoop run_loop(client, NULL); | 228 aura::client::DispatcherRunLoop run_loop(client, NULL); |
224 base::Closure quit_runloop = run_loop.QuitClosure(); | 229 base::Closure quit_runloop = run_loop.QuitClosure(); |
225 message_loop()->PostTask(FROM_HERE, | 230 message_loop()->PostTask(FROM_HERE, |
226 base::Bind(&QuitNestedLoopAndCloseWidget, | 231 base::Bind(&QuitNestedLoopAndCloseWidget, |
227 base::Passed(&widget), | 232 base::Passed(&widget), |
228 base::Unretained(&quit_runloop))); | 233 base::Unretained(&quit_runloop))); |
229 run_loop.Run(); | 234 run_loop.Run(); |
230 } | 235 } |
231 | 236 |
| 237 // This class provides functionality to create fullscreen and top level popup |
| 238 // windows. It additionally tests whether the destruction of these windows |
| 239 // occurs correctly in desktop AURA without crashing. |
| 240 // It provides facilities to test the following cases:- |
| 241 // 1. Child window destroyed which should lead to the destruction of the |
| 242 // parent. |
| 243 // 2. Parent window destroyed which should lead to the child being destroyed. |
| 244 class DesktopAuraTopLevelWindowTest |
| 245 : public views::TestViewsDelegate, |
| 246 public aura::WindowObserver { |
| 247 public: |
| 248 DesktopAuraTopLevelWindowTest() |
| 249 : top_level_widget_(NULL), |
| 250 owned_window_(NULL), |
| 251 owner_destroyed_(false), |
| 252 owned_window_destroyed_(false) {} |
| 253 |
| 254 virtual ~DesktopAuraTopLevelWindowTest() { |
| 255 EXPECT_TRUE(owner_destroyed_); |
| 256 EXPECT_TRUE(owned_window_destroyed_); |
| 257 top_level_widget_ = NULL; |
| 258 owned_window_ = NULL; |
| 259 } |
| 260 |
| 261 // views::TestViewsDelegate overrides. |
| 262 virtual void OnBeforeWidgetInit( |
| 263 Widget::InitParams* params, |
| 264 internal::NativeWidgetDelegate* delegate) OVERRIDE { |
| 265 if (!params->native_widget) |
| 266 params->native_widget = new views::DesktopNativeWidgetAura(delegate); |
| 267 } |
| 268 |
| 269 void CreateTopLevelWindow(const gfx::Rect& bounds, bool fullscreen) { |
| 270 Widget::InitParams init_params; |
| 271 init_params.type = Widget::InitParams::TYPE_WINDOW; |
| 272 init_params.bounds = bounds; |
| 273 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 274 init_params.layer_type = aura::WINDOW_LAYER_NOT_DRAWN; |
| 275 init_params.accept_events = fullscreen; |
| 276 |
| 277 widget_.Init(init_params); |
| 278 |
| 279 owned_window_ = new aura::Window(&child_window_delegate_); |
| 280 owned_window_->SetType(ui::wm::WINDOW_TYPE_NORMAL); |
| 281 owned_window_->SetName("TestTopLevelWindow"); |
| 282 if (fullscreen) { |
| 283 owned_window_->SetProperty(aura::client::kShowStateKey, |
| 284 ui::SHOW_STATE_FULLSCREEN); |
| 285 } else { |
| 286 owned_window_->SetType(ui::wm::WINDOW_TYPE_MENU); |
| 287 } |
| 288 owned_window_->Init(aura::WINDOW_LAYER_TEXTURED); |
| 289 aura::client::ParentWindowWithContext( |
| 290 owned_window_, |
| 291 widget_.GetNativeView()->GetRootWindow(), |
| 292 gfx::Rect(0, 0, 1900, 1600)); |
| 293 owned_window_->Show(); |
| 294 owned_window_->AddObserver(this); |
| 295 |
| 296 ASSERT_TRUE(owned_window_->parent() != NULL); |
| 297 owned_window_->parent()->AddObserver(this); |
| 298 |
| 299 top_level_widget_ = |
| 300 views::Widget::GetWidgetForNativeView(owned_window_->parent()); |
| 301 ASSERT_TRUE(top_level_widget_ != NULL); |
| 302 } |
| 303 |
| 304 void DestroyOwnedWindow() { |
| 305 ASSERT_TRUE(owned_window_ != NULL); |
| 306 delete owned_window_; |
| 307 } |
| 308 |
| 309 void DestroyOwnerWindow() { |
| 310 ASSERT_TRUE(top_level_widget_ != NULL); |
| 311 top_level_widget_->CloseNow(); |
| 312 } |
| 313 |
| 314 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE { |
| 315 window->RemoveObserver(this); |
| 316 if (window == owned_window_) { |
| 317 owned_window_destroyed_ = true; |
| 318 } else if (window == top_level_widget_->GetNativeView()) { |
| 319 owner_destroyed_ = true; |
| 320 } else { |
| 321 ADD_FAILURE() << "Unexpected window destroyed callback: " << window; |
| 322 } |
| 323 } |
| 324 |
| 325 aura::Window* owned_window() { |
| 326 return owned_window_; |
| 327 } |
| 328 |
| 329 views::Widget* top_level_widget() { |
| 330 return top_level_widget_; |
| 331 } |
| 332 |
| 333 private: |
| 334 views::Widget widget_; |
| 335 views::Widget* top_level_widget_; |
| 336 aura::Window* owned_window_; |
| 337 bool owner_destroyed_; |
| 338 bool owned_window_destroyed_; |
| 339 aura::test::TestWindowDelegate child_window_delegate_; |
| 340 |
| 341 DISALLOW_COPY_AND_ASSIGN(DesktopAuraTopLevelWindowTest); |
| 342 }; |
| 343 |
| 344 typedef WidgetTest DesktopAuraWidgetTest; |
| 345 |
| 346 TEST_F(DesktopAuraWidgetTest, FullscreenWindowDestroyedBeforeOwnerTest) { |
| 347 ViewsDelegate::views_delegate = NULL; |
| 348 DesktopAuraTopLevelWindowTest fullscreen_window; |
| 349 ASSERT_NO_FATAL_FAILURE(fullscreen_window.CreateTopLevelWindow( |
| 350 gfx::Rect(0, 0, 200, 200), true)); |
| 351 |
| 352 RunPendingMessages(); |
| 353 ASSERT_NO_FATAL_FAILURE(fullscreen_window.DestroyOwnedWindow()); |
| 354 RunPendingMessages(); |
| 355 } |
| 356 |
| 357 TEST_F(DesktopAuraWidgetTest, FullscreenWindowOwnerDestroyed) { |
| 358 ViewsDelegate::views_delegate = NULL; |
| 359 |
| 360 DesktopAuraTopLevelWindowTest fullscreen_window; |
| 361 ASSERT_NO_FATAL_FAILURE(fullscreen_window.CreateTopLevelWindow( |
| 362 gfx::Rect(0, 0, 200, 200), true)); |
| 363 |
| 364 RunPendingMessages(); |
| 365 ASSERT_NO_FATAL_FAILURE(fullscreen_window.DestroyOwnerWindow()); |
| 366 RunPendingMessages(); |
| 367 } |
| 368 |
| 369 TEST_F(DesktopAuraWidgetTest, TopLevelOwnedPopupTest) { |
| 370 ViewsDelegate::views_delegate = NULL; |
| 371 DesktopAuraTopLevelWindowTest popup_window; |
| 372 ASSERT_NO_FATAL_FAILURE(popup_window.CreateTopLevelWindow( |
| 373 gfx::Rect(0, 0, 200, 200), false)); |
| 374 |
| 375 RunPendingMessages(); |
| 376 ASSERT_NO_FATAL_FAILURE(popup_window.DestroyOwnedWindow()); |
| 377 RunPendingMessages(); |
| 378 } |
| 379 |
| 380 // This test validates that when a top level owned popup Aura window is |
| 381 // resized, the widget is resized as well. |
| 382 TEST_F(DesktopAuraWidgetTest, TopLevelOwnedPopupResizeTest) { |
| 383 ViewsDelegate::views_delegate = NULL; |
| 384 DesktopAuraTopLevelWindowTest popup_window; |
| 385 ASSERT_NO_FATAL_FAILURE(popup_window.CreateTopLevelWindow( |
| 386 gfx::Rect(0, 0, 200, 200), false)); |
| 387 |
| 388 gfx::Rect new_size(0, 0, 400, 400); |
| 389 popup_window.owned_window()->SetBounds(new_size); |
| 390 |
| 391 EXPECT_EQ(popup_window.top_level_widget()->GetNativeView()->bounds().size(), |
| 392 new_size.size()); |
| 393 RunPendingMessages(); |
| 394 ASSERT_NO_FATAL_FAILURE(popup_window.DestroyOwnedWindow()); |
| 395 RunPendingMessages(); |
| 396 } |
| 397 |
| 398 } // namespace test |
232 } // namespace views | 399 } // namespace views |
OLD | NEW |