Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "ui/aura/client/aura_constants.h" | 12 #include "ui/aura/client/aura_constants.h" |
| 13 #include "ui/aura/env.h" | 13 #include "ui/aura/env.h" |
| 14 #include "ui/aura/layout_manager.h" | 14 #include "ui/aura/layout_manager.h" |
| 15 #include "ui/aura/test/aura_test_base.h" | 15 #include "ui/aura/test/aura_test_base.h" |
| 16 #include "ui/aura/window.h" | 16 #include "ui/aura/window.h" |
| 17 #include "ui/aura/window_tree_host.h" | 17 #include "ui/aura/window_tree_host.h" |
| 18 #include "ui/events/event.h" | 18 #include "ui/events/event.h" |
| 19 #include "ui/events/event_utils.h" | 19 #include "ui/events/event_utils.h" |
| 20 #include "ui/gfx/screen.h" | 20 #include "ui/gfx/screen.h" |
| 21 #include "ui/views/layout/fill_layout.h" | 21 #include "ui/views/layout/fill_layout.h" |
| 22 #include "ui/views/test/widget_test.h" | |
| 22 #include "ui/views/widget/root_view.h" | 23 #include "ui/views/widget/root_view.h" |
| 23 #include "ui/views/widget/widget_delegate.h" | 24 #include "ui/views/widget/widget_delegate.h" |
| 25 #include "ui/wm/core/base_focus_rules.h" | |
| 24 #include "ui/wm/core/default_activation_client.h" | 26 #include "ui/wm/core/default_activation_client.h" |
| 27 #include "ui/wm/core/focus_controller.h" | |
| 25 | 28 |
| 26 namespace views { | 29 namespace views { |
| 27 namespace { | 30 namespace { |
| 28 | 31 |
| 29 NativeWidgetAura* Init(aura::Window* parent, Widget* widget) { | 32 NativeWidgetAura* Init(aura::Window* parent, Widget* widget) { |
| 30 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | 33 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); |
| 31 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 34 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 32 params.parent = parent; | 35 params.parent = parent; |
| 33 widget->Init(params); | 36 widget->Init(params); |
| 34 return static_cast<NativeWidgetAura*>(widget->native_widget()); | 37 return static_cast<NativeWidgetAura*>(widget->native_widget()); |
| 35 } | 38 } |
| 36 | 39 |
| 40 // TestFocusRules is intended to provide a way to manually set a window's | |
| 41 // activatability so that the focus rules can be tested. | |
| 42 class TestFocusRules : public wm::BaseFocusRules { | |
| 43 public: | |
| 44 TestFocusRules() {} | |
| 45 ~TestFocusRules() override {} | |
| 46 | |
| 47 void set_can_activate(bool can_activate) { can_activate_ = can_activate; } | |
| 48 | |
| 49 // wm::BaseFocusRules overrides: | |
| 50 bool SupportsChildActivation(aura::Window* window) const override { | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 bool CanActivateWindow(aura::Window* window) const override { | |
| 55 return can_activate_; | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 bool can_activate_ = true; | |
| 60 | |
| 61 DISALLOW_COPY_AND_ASSIGN(TestFocusRules); | |
| 62 }; | |
| 63 | |
| 37 class NativeWidgetAuraTest : public aura::test::AuraTestBase { | 64 class NativeWidgetAuraTest : public aura::test::AuraTestBase { |
| 38 public: | 65 public: |
| 39 NativeWidgetAuraTest() {} | 66 NativeWidgetAuraTest() {} |
| 40 ~NativeWidgetAuraTest() override {} | 67 ~NativeWidgetAuraTest() override {} |
| 41 | 68 |
| 69 TestFocusRules* test_focus_rules() { return test_focus_rules_; } | |
| 70 | |
| 42 // testing::Test overrides: | 71 // testing::Test overrides: |
| 43 void SetUp() override { | 72 void SetUp() override { |
| 44 AuraTestBase::SetUp(); | 73 AuraTestBase::SetUp(); |
| 45 new wm::DefaultActivationClient(root_window()); | 74 test_focus_rules_ = new TestFocusRules; |
| 75 focus_controller_.reset(new wm::FocusController(test_focus_rules_)); | |
| 76 aura::client::SetActivationClient(root_window(), focus_controller_.get()); | |
| 46 host()->SetBounds(gfx::Rect(640, 480)); | 77 host()->SetBounds(gfx::Rect(640, 480)); |
| 47 } | 78 } |
| 48 | 79 |
| 49 private: | 80 private: |
| 81 scoped_ptr<wm::FocusController> focus_controller_; | |
| 82 TestFocusRules* test_focus_rules_; | |
| 83 | |
| 50 DISALLOW_COPY_AND_ASSIGN(NativeWidgetAuraTest); | 84 DISALLOW_COPY_AND_ASSIGN(NativeWidgetAuraTest); |
| 51 }; | 85 }; |
| 52 | 86 |
| 53 TEST_F(NativeWidgetAuraTest, CenterWindowLargeParent) { | 87 TEST_F(NativeWidgetAuraTest, CenterWindowLargeParent) { |
| 54 // Make a parent window larger than the host represented by | 88 // Make a parent window larger than the host represented by |
| 55 // WindowEventDispatcher. | 89 // WindowEventDispatcher. |
| 56 scoped_ptr<aura::Window> parent(new aura::Window(NULL)); | 90 scoped_ptr<aura::Window> parent(new aura::Window(NULL)); |
| 57 parent->Init(ui::LAYER_NOT_DRAWN); | 91 parent->Init(ui::LAYER_NOT_DRAWN); |
| 58 parent->SetBounds(gfx::Rect(0, 0, 1024, 800)); | 92 parent->SetBounds(gfx::Rect(0, 0, 1024, 800)); |
| 59 scoped_ptr<Widget> widget(new Widget()); | 93 scoped_ptr<Widget> widget(new Widget()); |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 gfx::Rect(10, 10, 100, 200)); | 512 gfx::Rect(10, 10, 100, 200)); |
| 479 widget->Show(); | 513 widget->Show(); |
| 480 delegate->ClearGotMove(); | 514 delegate->ClearGotMove(); |
| 481 // Simulate a maximize with animation. | 515 // Simulate a maximize with animation. |
| 482 delete widget->GetNativeView()->RecreateLayer().release(); | 516 delete widget->GetNativeView()->RecreateLayer().release(); |
| 483 widget->SetBounds(gfx::Rect(0, 0, 500, 500)); | 517 widget->SetBounds(gfx::Rect(0, 0, 500, 500)); |
| 484 EXPECT_TRUE(delegate->got_move()); | 518 EXPECT_TRUE(delegate->got_move()); |
| 485 widget->CloseNow(); | 519 widget->CloseNow(); |
| 486 } | 520 } |
| 487 | 521 |
| 522 // Tests that if a widget has a view which should be initially focused when the | |
| 523 // widget is shown, this view should not get focused if the associated window | |
| 524 // can not be activated. | |
| 525 TEST_F(NativeWidgetAuraTest, PreventFocusOnNonActivableWindow) { | |
| 526 test_focus_rules()->set_can_activate(false); | |
| 527 scoped_ptr<views::Widget> w1(new views::Widget); | |
|
sky
2016/03/07 16:05:15
You don't need a scoped_ptr here. Declare w1 on th
| |
| 528 views::test::TestInitialFocusWidgetDelegate* delegate = | |
| 529 new views::test::TestInitialFocusWidgetDelegate(root_window()); | |
| 530 views::Widget::InitParams params1; | |
| 531 params1.delegate = delegate; | |
| 532 params1.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 533 params1.context = root_window(); | |
| 534 w1->Init(params1); | |
| 535 w1->Show(); | |
| 536 EXPECT_FALSE(delegate->view()->HasFocus()); | |
| 537 | |
| 538 test_focus_rules()->set_can_activate(true); | |
| 539 scoped_ptr<views::Widget> w2(new views::Widget); | |
|
sky
2016/03/07 16:05:15
Same comment about w2.
| |
| 540 views::test::TestInitialFocusWidgetDelegate* delegate2 = | |
| 541 new views::test::TestInitialFocusWidgetDelegate(root_window()); | |
| 542 views::Widget::InitParams params2; | |
| 543 params2.delegate = delegate2; | |
| 544 params2.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 545 params2.context = root_window(); | |
| 546 w2->Init(params2); | |
| 547 w2->Show(); | |
| 548 EXPECT_TRUE(delegate2->view()->HasFocus()); | |
| 549 } | |
| 550 | |
| 488 } // namespace | 551 } // namespace |
| 489 } // namespace views | 552 } // namespace views |
| OLD | NEW |