OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "base/memory/weak_ptr.h" |
| 6 #include "chrome/browser/ui/browser.h" |
| 7 #include "chrome/browser/ui/constrained_window_tab_helper.h" |
| 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 9 #include "chrome/browser/ui/views/constrained_window_views.h" |
| 10 #include "chrome/test/base/in_process_browser_test.h" |
| 11 #include "chrome/test/base/ui_test_utils.h" |
| 12 #include "views/accelerator.h" |
| 13 #include "views/controls/textfield/textfield.h" |
| 14 #include "views/focus/focus_manager.h" |
| 15 #include "views/layout/fill_layout.h" |
| 16 #include "views/window/dialog_delegate.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 class TestConstrainedDialogContentsView |
| 21 : public views::View, |
| 22 public base::SupportsWeakPtr<TestConstrainedDialogContentsView> { |
| 23 public: |
| 24 TestConstrainedDialogContentsView() |
| 25 : text_field_(new views::Textfield) { |
| 26 SetLayoutManager(new views::FillLayout); |
| 27 AddChildView(text_field_); |
| 28 } |
| 29 |
| 30 views::View* GetInitiallyFocusedView() { |
| 31 return text_field_; |
| 32 } |
| 33 |
| 34 private: |
| 35 views::Textfield* text_field_; |
| 36 DISALLOW_COPY_AND_ASSIGN(TestConstrainedDialogContentsView); |
| 37 }; |
| 38 |
| 39 class TestConstrainedDialog : public views::DialogDelegate { |
| 40 public: |
| 41 TestConstrainedDialog() |
| 42 : contents_((new TestConstrainedDialogContentsView())->AsWeakPtr()), |
| 43 done_(false) { |
| 44 } |
| 45 |
| 46 ~TestConstrainedDialog() {} |
| 47 |
| 48 virtual views::View* GetInitiallyFocusedView() OVERRIDE { |
| 49 return contents_ ? contents_->GetInitiallyFocusedView() : NULL; |
| 50 } |
| 51 |
| 52 virtual views::View* GetContentsView() OVERRIDE { |
| 53 return contents_.get(); |
| 54 } |
| 55 |
| 56 virtual views::Widget* GetWidget() OVERRIDE { |
| 57 return contents_ ? contents_->GetWidget() : NULL; |
| 58 } |
| 59 |
| 60 virtual const views::Widget* GetWidget() const OVERRIDE { |
| 61 return contents_ ? contents_->GetWidget() : NULL; |
| 62 } |
| 63 |
| 64 virtual void DeleteDelegate() OVERRIDE { |
| 65 // Don't delete the delegate yet. We need to keep it around for inspection |
| 66 // later. |
| 67 EXPECT_TRUE(done_); |
| 68 } |
| 69 |
| 70 virtual bool Accept() OVERRIDE { |
| 71 done_ = true; |
| 72 return true; |
| 73 } |
| 74 |
| 75 virtual bool Cancel() OVERRIDE { |
| 76 done_ = true; |
| 77 return true; |
| 78 } |
| 79 |
| 80 bool done() { |
| 81 return done_; |
| 82 } |
| 83 |
| 84 private: |
| 85 // contents_ will be freed when the View goes away. |
| 86 base::WeakPtr<TestConstrainedDialogContentsView> contents_; |
| 87 bool done_; |
| 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(TestConstrainedDialog); |
| 90 }; |
| 91 |
| 92 } // namespace |
| 93 |
| 94 class ConstrainedWindowViewTest : public InProcessBrowserTest { |
| 95 public: |
| 96 ConstrainedWindowViewTest() { |
| 97 set_show_window(true); |
| 98 } |
| 99 }; |
| 100 |
| 101 // Tests the following: |
| 102 // |
| 103 // *) Initially focused view in a constrained dialog receives focus reliably. |
| 104 // |
| 105 // *) Constrained windows that are queued don't register themselves as |
| 106 // accelerator targets until they are displayed. |
| 107 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, FocusTest) { |
| 108 TabContentsWrapper* tab_contents = browser()->GetSelectedTabContentsWrapper(); |
| 109 ASSERT_TRUE(tab_contents != NULL); |
| 110 ConstrainedWindowTabHelper* constrained_window_helper = |
| 111 tab_contents->constrained_window_tab_helper(); |
| 112 ASSERT_TRUE(constrained_window_helper != NULL); |
| 113 |
| 114 // Create a constrained dialog. It will attach itself to tab_contents. |
| 115 scoped_ptr<TestConstrainedDialog> test_dialog1(new TestConstrainedDialog); |
| 116 ConstrainedWindowViews* window1 = |
| 117 new ConstrainedWindowViews(tab_contents, test_dialog1.get()); |
| 118 |
| 119 views::FocusManager* focus_manager = window1->GetFocusManager(); |
| 120 ASSERT_TRUE(focus_manager); |
| 121 |
| 122 // old_target should be the OK button for test_dialog1. |
| 123 views::AcceleratorTarget* old_target = |
| 124 focus_manager->GetCurrentTargetForAccelerator( |
| 125 views::Accelerator(ui::VKEY_RETURN, false, false, false)); |
| 126 ASSERT_TRUE(old_target != NULL); |
| 127 // test_dialog1's text field should be focused. |
| 128 EXPECT_EQ(test_dialog1->GetInitiallyFocusedView(), |
| 129 focus_manager->GetFocusedView()); |
| 130 |
| 131 // Now create a second constrained dialog. This will also be attached to |
| 132 // tab_contents, but will remain hidden since the test_dialog1 is still |
| 133 // showing. |
| 134 scoped_ptr<TestConstrainedDialog> test_dialog2(new TestConstrainedDialog); |
| 135 ConstrainedWindowViews* window2 = |
| 136 new ConstrainedWindowViews(tab_contents, test_dialog2.get()); |
| 137 // Should be the same focus_manager. |
| 138 ASSERT_EQ(focus_manager, window2->GetFocusManager()); |
| 139 |
| 140 // new_target should be the same as old_target since test_dialog2 is still |
| 141 // hidden. |
| 142 views::AcceleratorTarget* new_target = |
| 143 focus_manager->GetCurrentTargetForAccelerator( |
| 144 views::Accelerator(ui::VKEY_RETURN, false, false, false)); |
| 145 ASSERT_TRUE(new_target != NULL); |
| 146 EXPECT_EQ(old_target, new_target); |
| 147 |
| 148 // test_dialog1's text field should still be the view that has focus. |
| 149 EXPECT_EQ(test_dialog1->GetInitiallyFocusedView(), |
| 150 focus_manager->GetFocusedView()); |
| 151 ASSERT_EQ(2u, constrained_window_helper->constrained_window_count()); |
| 152 |
| 153 // Now send a VKEY_RETURN to the browser. This should result in closing |
| 154 // test_dialog1. |
| 155 EXPECT_TRUE(focus_manager->ProcessAccelerator( |
| 156 views::Accelerator(ui::VKEY_RETURN, false, false, false))); |
| 157 ui_test_utils::RunAllPendingInMessageLoop(); |
| 158 |
| 159 EXPECT_TRUE(test_dialog1->done()); |
| 160 EXPECT_FALSE(test_dialog2->done()); |
| 161 EXPECT_EQ(1u, constrained_window_helper->constrained_window_count()); |
| 162 |
| 163 // test_dialog2 will be shown. Focus should be on test_dialog2's text field. |
| 164 EXPECT_EQ(test_dialog2->GetInitiallyFocusedView(), |
| 165 focus_manager->GetFocusedView()); |
| 166 |
| 167 // Send another VKEY_RETURN, closing test_dialog2 |
| 168 EXPECT_TRUE(focus_manager->ProcessAccelerator( |
| 169 views::Accelerator(ui::VKEY_RETURN, false, false, false))); |
| 170 ui_test_utils::RunAllPendingInMessageLoop(); |
| 171 EXPECT_TRUE(test_dialog2->done()); |
| 172 EXPECT_EQ(0u, constrained_window_helper->constrained_window_count()); |
| 173 } |
OLD | NEW |