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 if (contents_) | |
50 return contents_->GetInitiallyFocusedView(); | |
Ben Goodger (Google)
2011/10/31 18:28:38
You can use a ternary operator for these types of
asanka
2011/11/01 14:21:52
Done.
| |
51 return NULL; | |
52 } | |
53 | |
54 virtual views::View* GetContentsView() OVERRIDE { | |
55 return contents_.get(); | |
56 } | |
57 | |
58 virtual views::Widget* GetWidget() OVERRIDE { | |
59 if (contents_) | |
60 return contents_->GetWidget(); | |
61 return NULL; | |
62 } | |
63 | |
64 virtual const views::Widget* GetWidget() const OVERRIDE { | |
65 if (contents_) | |
66 return contents_->GetWidget(); | |
67 return NULL; | |
68 } | |
69 | |
70 virtual void DeleteDelegate() OVERRIDE { | |
71 // Don't delete the delegate yet. We need to keep it around for inspection | |
72 // later. | |
73 EXPECT_TRUE(done_); | |
74 } | |
75 | |
76 virtual bool Accept() OVERRIDE { | |
77 done_ = true; | |
78 return true; | |
79 } | |
80 | |
81 virtual bool Cancel() OVERRIDE { | |
82 done_ = true; | |
83 return true; | |
84 } | |
85 | |
86 bool done() { | |
87 return done_; | |
88 } | |
89 | |
90 private: | |
91 // contents_ will be freed when the View goes away. | |
92 base::WeakPtr<TestConstrainedDialogContentsView> contents_; | |
93 bool done_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(TestConstrainedDialog); | |
96 }; | |
97 | |
98 } // namespace | |
99 | |
100 class ConstrainedWindowViewTest : public InProcessBrowserTest { | |
101 public: | |
102 ConstrainedWindowViewTest() { | |
103 set_show_window(true); | |
104 } | |
105 }; | |
106 | |
107 // Tests the following: | |
108 // | |
109 // *) Initially focused view in a constrained dialog receives focus reliably. | |
110 // | |
111 // *) Constrained windows that are queued don't register themselves as | |
112 // accelerator targets until they are displayed. | |
113 IN_PROC_BROWSER_TEST_F(ConstrainedWindowViewTest, FocusTest) { | |
114 TabContentsWrapper* tab_contents = browser()->GetSelectedTabContentsWrapper(); | |
115 ASSERT_TRUE(tab_contents != NULL); | |
116 ConstrainedWindowTabHelper* constrained_window_helper = | |
117 tab_contents->constrained_window_tab_helper(); | |
118 ASSERT_TRUE(constrained_window_helper != NULL); | |
119 | |
120 // Create a constrained dialog. It will attach itself to tab_contents. | |
121 scoped_ptr<TestConstrainedDialog> test_dialog1(new TestConstrainedDialog); | |
122 ConstrainedWindowViews* window1 = | |
123 new ConstrainedWindowViews(tab_contents, test_dialog1.get()); | |
124 | |
125 views::FocusManager* focus_manager = window1->GetFocusManager(); | |
126 ASSERT_TRUE(focus_manager); | |
127 | |
128 // old_target should be the OK button for test_dialog1. | |
129 views::AcceleratorTarget* old_target = | |
130 focus_manager->GetCurrentTargetForAccelerator( | |
131 views::Accelerator(ui::VKEY_RETURN, false, false, false)); | |
132 ASSERT_TRUE(old_target != NULL); | |
133 // test_dialog1's text field should be focused. | |
134 EXPECT_EQ(test_dialog1->GetInitiallyFocusedView(), | |
135 focus_manager->GetFocusedView()); | |
136 | |
137 // Now create a second constrained dialog. This will also be attached to | |
138 // tab_contents, but will remain hidden since the test_dialog1 is still | |
139 // showing. | |
140 scoped_ptr<TestConstrainedDialog> test_dialog2(new TestConstrainedDialog); | |
141 ConstrainedWindowViews* window2 = | |
142 new ConstrainedWindowViews(tab_contents, test_dialog2.get()); | |
143 // Should be the same focus_manager. | |
144 ASSERT_EQ(focus_manager, window2->GetFocusManager()); | |
145 | |
146 // new_target should be the same as old_target since test_dialog2 is still | |
147 // hidden. | |
148 views::AcceleratorTarget* new_target = | |
149 focus_manager->GetCurrentTargetForAccelerator( | |
150 views::Accelerator(ui::VKEY_RETURN, false, false, false)); | |
151 ASSERT_TRUE(new_target != NULL); | |
152 EXPECT_EQ(old_target, new_target); | |
153 | |
154 // test_dialog1's text field should still be the view that has focus. | |
155 EXPECT_EQ(test_dialog1->GetInitiallyFocusedView(), | |
156 focus_manager->GetFocusedView()); | |
157 ASSERT_EQ(2u, constrained_window_helper->constrained_window_count()); | |
158 | |
159 // Now send a VKEY_RETURN to the browser. This should result in closing | |
160 // test_dialog1. | |
161 EXPECT_TRUE(focus_manager->ProcessAccelerator( | |
162 views::Accelerator(ui::VKEY_RETURN, false, false, false))); | |
163 ui_test_utils::RunAllPendingInMessageLoop(); | |
164 | |
165 EXPECT_TRUE(test_dialog1->done()); | |
166 EXPECT_FALSE(test_dialog2->done()); | |
167 EXPECT_EQ(1u, constrained_window_helper->constrained_window_count()); | |
168 | |
169 // test_dialog2 will be shown. Focus should be on test_dialog2's text field. | |
170 EXPECT_EQ(test_dialog2->GetInitiallyFocusedView(), | |
171 focus_manager->GetFocusedView()); | |
172 | |
173 // Send another VKEY_RETURN, closing test_dialog2 | |
174 EXPECT_TRUE(focus_manager->ProcessAccelerator( | |
175 views::Accelerator(ui::VKEY_RETURN, false, false, false))); | |
176 ui_test_utils::RunAllPendingInMessageLoop(); | |
177 EXPECT_TRUE(test_dialog2->done()); | |
178 EXPECT_EQ(0u, constrained_window_helper->constrained_window_count()); | |
179 } | |
OLD | NEW |