OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "ui/views/widget/native_widget_aura.h" |
| 6 |
| 7 #include "base/path_service.h" |
| 8 #include "ui/aura/window.h" |
| 9 #include "ui/base/resource/resource_bundle.h" |
| 10 #include "ui/base/ui_base_paths.h" |
| 11 #include "ui/gl/test/gl_surface_test_support.h" |
| 12 #include "ui/views/controls/textfield/textfield.h" |
| 13 #include "ui/views/test/native_widget_factory.h" |
| 14 #include "ui/views/test/views_test_base.h" |
| 15 #include "ui/views/test/widget_test.h" |
| 16 #include "ui/views/widget/widget_delegate.h" |
| 17 #include "ui/wm/core/base_focus_rules.h" |
| 18 #include "ui/wm/core/focus_controller.h" |
| 19 |
| 20 namespace views { |
| 21 namespace test { |
| 22 |
| 23 class TestFocusRules : public wm::BaseFocusRules { |
| 24 public: |
| 25 TestFocusRules() {} |
| 26 ~TestFocusRules() override {} |
| 27 |
| 28 void set_can_activate(bool can_activate) { can_activate_ = can_activate; } |
| 29 |
| 30 // wm::BaseFocusRules overrides: |
| 31 bool SupportsChildActivation(aura::Window* window) const override { |
| 32 return true; |
| 33 } |
| 34 |
| 35 bool CanActivateWindow(aura::Window* window) const override { |
| 36 return can_activate_; |
| 37 } |
| 38 |
| 39 private: |
| 40 bool can_activate_ = true; |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(TestFocusRules); |
| 43 }; |
| 44 |
| 45 class NativeWidgetAuraTest : public ViewsTestBase { |
| 46 public: |
| 47 NativeWidgetAuraTest() {} |
| 48 ~NativeWidgetAuraTest() override {} |
| 49 |
| 50 void SetUp() override { |
| 51 gl::GLSurfaceTestSupport::InitializeOneOff(); |
| 52 ui::RegisterPathProvider(); |
| 53 base::FilePath ui_test_pak_path; |
| 54 ASSERT_TRUE(PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path)); |
| 55 ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path); |
| 56 |
| 57 ViewsTestBase::SetUp(); |
| 58 } |
| 59 |
| 60 NativeWidget* CreateNativeWidget(const Widget::InitParams& params, |
| 61 Widget* widget) { |
| 62 return CreatePlatformNativeWidgetImpl(params, widget, kDefault, nullptr); |
| 63 } |
| 64 |
| 65 private: |
| 66 DISALLOW_COPY_AND_ASSIGN(NativeWidgetAuraTest); |
| 67 }; |
| 68 |
| 69 // When requesting view focus from a non-active top level widget, focus is not |
| 70 // instantly given. Instead, the view is firstly stored and then it is attempted |
| 71 // to activate the widget. If widget is currently not activatable, focus should |
| 72 // not be grabbed. And focus will be given/restored the next time the widget is |
| 73 // made active. (crbug.com/621791) |
| 74 TEST_F(NativeWidgetAuraTest, NonActiveWindowRequestImeFocus) { |
| 75 TestFocusRules* test_focus_rules = new TestFocusRules; |
| 76 std::unique_ptr<wm::FocusController> focus_controller = |
| 77 base::MakeUnique<wm::FocusController>(test_focus_rules); |
| 78 aura::client::SetActivationClient(GetContext(), focus_controller.get()); |
| 79 |
| 80 Widget* widget1 = new Widget; |
| 81 Widget::InitParams params1(Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 82 params1.context = GetContext(); |
| 83 params1.native_widget = CreateNativeWidget(params1, widget1); |
| 84 params1.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 85 widget1->Init(params1); |
| 86 Textfield* textfield1 = new Textfield; |
| 87 widget1->GetRootView()->AddChildView(textfield1); |
| 88 |
| 89 Widget* widget2 = new Widget; |
| 90 Widget::InitParams params2(Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 91 params2.context = GetContext(); |
| 92 params2.native_widget = CreateNativeWidget(params2, widget2); |
| 93 params2.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 94 widget2->Init(params2); |
| 95 Textfield* textfield2a = new Textfield; |
| 96 widget2->GetRootView()->AddChildView(textfield2a); |
| 97 Textfield* textfield2b = new Textfield; |
| 98 widget2->GetRootView()->AddChildView(textfield2b); |
| 99 |
| 100 views::test::WidgetActivationWaiter waiter1(widget1, true); |
| 101 widget1->Show(); |
| 102 waiter1.Wait(); |
| 103 textfield1->RequestFocus(); |
| 104 EXPECT_TRUE(textfield1->HasFocus()); |
| 105 EXPECT_FALSE(textfield2a->HasFocus()); |
| 106 EXPECT_FALSE(textfield2b->HasFocus()); |
| 107 |
| 108 // Don't allow window activation at this step. |
| 109 test_focus_rules->set_can_activate(false); |
| 110 textfield2a->RequestFocus(); |
| 111 EXPECT_TRUE(textfield1->HasFocus()); |
| 112 EXPECT_FALSE(textfield2a->HasFocus()); |
| 113 EXPECT_FALSE(textfield2b->HasFocus()); |
| 114 |
| 115 // Allow window activation and |widget2| gets activated at this step, focus |
| 116 // should be properly restored. |
| 117 test_focus_rules->set_can_activate(true); |
| 118 views::test::WidgetActivationWaiter waiter2(widget2, true); |
| 119 widget2->Activate(); |
| 120 waiter2.Wait(); |
| 121 EXPECT_TRUE(textfield2a->HasFocus()); |
| 122 EXPECT_FALSE(textfield2b->HasFocus()); |
| 123 EXPECT_FALSE(textfield1->HasFocus()); |
| 124 |
| 125 widget1->CloseNow(); |
| 126 widget2->CloseNow(); |
| 127 } |
| 128 |
| 129 } // namespace test |
| 130 } // namespace views |
OLD | NEW |