OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | |
6 #include "base/bind_helpers.h" | |
5 #include "base/file_path.h" | 7 #include "base/file_path.h" |
6 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
7 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
8 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/browser/ui/views/html_dialog_view.h" | 12 #include "chrome/browser/ui/views/html_dialog_view.h" |
11 #include "chrome/browser/ui/webui/test_html_dialog_ui_delegate.h" | 13 #include "chrome/browser/ui/webui/test_html_dialog_ui_delegate.h" |
12 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
14 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
15 #include "content/browser/renderer_host/render_widget_host_view.h" | 17 #include "content/browser/renderer_host/render_widget_host_view.h" |
16 #include "content/browser/tab_contents/tab_contents.h" | 18 #include "content/browser/tab_contents/tab_contents.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "views/widget/widget.h" | 21 #include "views/widget/widget.h" |
20 | 22 |
21 using testing::Eq; | 23 using testing::Eq; |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 // Window non-client-area means that the minimum size for the window | 27 // Initial size of HTMLDialog for SizeWindow test case. |
26 // won't be the actual minimum size - our layout and resizing code | |
27 // makes sure the chrome is always visible. | |
28 const int kMinimumWidthToTestFor = 20; | |
29 const int kMinimumHeightToTestFor = 30; | |
30 | |
31 // Initial size of HTMLDialog for SizeWindow test case. They must be different | |
32 // from the above kMinimumWidthToTestFor/kMinimumHeightToTestFor. | |
33 const int kInitialWidth = 40; | 28 const int kInitialWidth = 40; |
34 const int kInitialHeight = 40; | 29 const int kInitialHeight = 40; |
35 | 30 |
36 class TestHtmlDialogView: public HtmlDialogView { | 31 class TestHtmlDialogView: public HtmlDialogView { |
37 public: | 32 public: |
38 TestHtmlDialogView(Profile* profile, HtmlDialogUIDelegate* delegate) | 33 TestHtmlDialogView(Profile* profile, HtmlDialogUIDelegate* delegate) |
39 : HtmlDialogView(profile, delegate), | 34 : HtmlDialogView(profile, delegate), |
40 painted_(false) { | 35 painted_(false), |
36 should_quit_on_size_change_(false) { | |
37 delegate->GetDialogSize(&last_size_); | |
41 } | 38 } |
42 | 39 |
43 bool painted() const { | 40 bool painted() const { |
44 return painted_; | 41 return painted_; |
45 } | 42 } |
46 | 43 |
47 protected: | 44 void set_should_quit_on_size_change(bool should_quit) { |
45 should_quit_on_size_change_ = should_quit; | |
46 } | |
47 | |
48 private: | |
49 virtual void SaveWindowPlacement(const gfx::Rect& bounds, | |
50 ui::WindowShowState show_state) OVERRIDE { | |
51 if (should_quit_on_size_change_ && last_size_ != bounds.size()) { | |
Ben Goodger (Google)
2011/11/17 20:50:15
I guess this is less hacky than it was before... b
xiyuan
2011/11/17 22:05:42
Yeah. I'll add a TODO comment to update this when
| |
52 // Schedule message loop quit because we could be called while | |
53 // the bounds change call is on the stack and not in the nested message | |
54 // loop. | |
55 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
56 &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); | |
57 } | |
58 | |
59 last_size_ = bounds.size(); | |
60 } | |
61 | |
62 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { | |
63 should_quit_on_size_change_ = false; // No quit when we are closing. | |
64 HtmlDialogView::OnDialogClosed(json_retval); | |
65 } | |
66 | |
48 virtual void OnTabMainFrameFirstRender() OVERRIDE { | 67 virtual void OnTabMainFrameFirstRender() OVERRIDE { |
49 HtmlDialogView::OnTabMainFrameFirstRender(); | 68 HtmlDialogView::OnTabMainFrameFirstRender(); |
50 painted_ = true; | 69 painted_ = true; |
51 MessageLoop::current()->Quit(); | 70 MessageLoop::current()->Quit(); |
52 } | 71 } |
53 | 72 |
54 private: | 73 // Whether first rendered notification is received. |
55 bool painted_; | 74 bool painted_; |
56 | 75 |
76 // Whether we should quit message loop when size change is detected. | |
77 bool should_quit_on_size_change_; | |
78 gfx::Size last_size_; | |
79 | |
57 DISALLOW_COPY_AND_ASSIGN(TestHtmlDialogView); | 80 DISALLOW_COPY_AND_ASSIGN(TestHtmlDialogView); |
58 }; | 81 }; |
59 | 82 |
60 } // namespace | 83 } // namespace |
61 | 84 |
62 class HtmlDialogBrowserTest : public InProcessBrowserTest { | 85 class HtmlDialogBrowserTest : public InProcessBrowserTest { |
63 public: | 86 public: |
64 HtmlDialogBrowserTest() {} | 87 HtmlDialogBrowserTest() {} |
65 | |
66 class WindowChangedObserver : public MessageLoopForUI::Observer { | |
67 public: | |
68 WindowChangedObserver() {} | |
69 | |
70 static WindowChangedObserver* GetInstance() { | |
71 return Singleton<WindowChangedObserver>::get(); | |
72 } | |
73 | |
74 #if defined(OS_WIN) | |
75 // This method is called before processing a message. | |
76 virtual base::EventStatus WillProcessEvent( | |
77 const base::NativeEvent& event) OVERRIDE { | |
78 return base::EVENT_CONTINUE; | |
79 } | |
80 | |
81 // This method is called after processing a message. | |
82 virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE { | |
83 // Either WM_PAINT or WM_TIMER indicates the actual work of | |
84 // pushing through the window resizing messages is done since | |
85 // they are lower priority (we don't get to see the | |
86 // WM_WINDOWPOSCHANGED message here). | |
87 if (event.message == WM_PAINT || event.message == WM_TIMER) | |
88 MessageLoop::current()->Quit(); | |
89 } | |
90 #elif defined(TOUCH_UI) || defined(USE_AURA) | |
91 // This method is called before processing a message. | |
92 virtual base::EventStatus WillProcessEvent( | |
93 const base::NativeEvent& event) OVERRIDE { | |
94 return base::EVENT_CONTINUE; | |
95 } | |
96 | |
97 // This method is called after processing a message. | |
98 virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE { | |
99 // TODO(oshima): X11/Xlib.h imports various definitions that | |
100 // caused compilation error. | |
101 NOTIMPLEMENTED(); | |
102 } | |
103 #elif defined(TOOLKIT_USES_GTK) | |
104 // This method is called before processing a message. | |
105 virtual void WillProcessEvent(GdkEvent* event) OVERRIDE {} | |
106 | |
107 // This method is called after processing a message. | |
108 virtual void DidProcessEvent(GdkEvent* event) OVERRIDE { | |
109 // Quit once the GDK_CONFIGURE event has been processed - seeing | |
110 // this means the window sizing request that was made actually | |
111 // happened. | |
112 if (event->type == GDK_CONFIGURE) | |
113 MessageLoop::current()->Quit(); | |
114 } | |
115 #endif | |
116 }; | |
117 }; | 88 }; |
118 | 89 |
119 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 90 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
120 #define MAYBE_SizeWindow SizeWindow | 91 #define MAYBE_SizeWindow SizeWindow |
121 #else | 92 #else |
122 // http://code.google.com/p/chromium/issues/detail?id=52602 | 93 // http://code.google.com/p/chromium/issues/detail?id=52602 |
123 // Windows has some issues resizing windows- an off by one problem, | 94 // Windows has some issues resizing windows- an off by one problem, |
124 // and a minimum size that seems too big. This file isn't included in | 95 // and a minimum size that seems too big. This file isn't included in |
125 // Mac builds yet. On Chrome OS, this test doesn't apply since ChromeOS | 96 // Mac builds yet. On Chrome OS, this test doesn't apply since ChromeOS |
126 // doesn't allow resizing of windows. | 97 // doesn't allow resizing of windows. |
127 #define MAYBE_SizeWindow DISABLED_SizeWindow | 98 #define MAYBE_SizeWindow DISABLED_SizeWindow |
128 #endif | 99 #endif |
129 | 100 |
130 IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, MAYBE_SizeWindow) { | 101 IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, MAYBE_SizeWindow) { |
131 test::TestHtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( | 102 test::TestHtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( |
132 GURL(chrome::kChromeUIChromeURLsURL)); | 103 GURL(chrome::kChromeUIChromeURLsURL)); |
133 delegate->set_size(kInitialWidth, kInitialHeight); | 104 delegate->set_size(kInitialWidth, kInitialHeight); |
134 | 105 |
135 HtmlDialogView* html_view = | 106 TestHtmlDialogView* html_view = |
136 new HtmlDialogView(browser()->profile(), delegate); | 107 new TestHtmlDialogView(browser()->profile(), delegate); |
137 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 108 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
138 ASSERT_TRUE(tab_contents != NULL); | 109 ASSERT_TRUE(tab_contents != NULL); |
139 views::Widget::CreateWindowWithParent(html_view, | 110 views::Widget::CreateWindowWithParent(html_view, |
140 tab_contents->GetDialogRootWindow()); | 111 tab_contents->GetDialogRootWindow()); |
141 html_view->InitDialog(); | 112 html_view->InitDialog(); |
142 html_view->GetWidget()->Show(); | 113 html_view->GetWidget()->Show(); |
143 | 114 |
144 MessageLoopForUI::current()->AddObserver( | 115 // TestHtmlDialogView should quit current message loop on size change. |
145 WindowChangedObserver::GetInstance()); | 116 html_view->set_should_quit_on_size_change(true); |
146 | 117 |
147 gfx::Rect bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 118 gfx::Rect bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
148 | 119 |
149 gfx::Rect set_bounds = bounds; | 120 gfx::Rect set_bounds = bounds; |
150 gfx::Rect actual_bounds, rwhv_bounds; | 121 gfx::Rect actual_bounds, rwhv_bounds; |
151 | 122 |
152 // Bigger than the default in both dimensions. | 123 // Bigger than the default in both dimensions. |
153 set_bounds.set_width(400); | 124 set_bounds.set_width(400); |
154 set_bounds.set_height(300); | 125 set_bounds.set_height(300); |
155 | 126 |
156 html_view->MoveContents(tab_contents, set_bounds); | 127 html_view->MoveContents(tab_contents, set_bounds); |
157 ui_test_utils::RunMessageLoop(); | 128 ui_test_utils::RunMessageLoop(); // TestHtmlDialogView will quit. |
158 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 129 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
159 EXPECT_EQ(set_bounds, actual_bounds); | 130 EXPECT_EQ(set_bounds, actual_bounds); |
160 | 131 |
161 rwhv_bounds = html_view->dom_contents()->tab_contents()-> | 132 rwhv_bounds = html_view->dom_contents()->tab_contents()-> |
162 GetRenderWidgetHostView()->GetViewBounds(); | 133 GetRenderWidgetHostView()->GetViewBounds(); |
163 EXPECT_LT(0, rwhv_bounds.width()); | 134 EXPECT_LT(0, rwhv_bounds.width()); |
164 EXPECT_LT(0, rwhv_bounds.height()); | 135 EXPECT_LT(0, rwhv_bounds.height()); |
165 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); | 136 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); |
166 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); | 137 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); |
167 | 138 |
168 // Larger in one dimension and smaller in the other. | 139 // Larger in one dimension and smaller in the other. |
169 set_bounds.set_width(550); | 140 set_bounds.set_width(550); |
170 set_bounds.set_height(250); | 141 set_bounds.set_height(250); |
171 | 142 |
172 html_view->MoveContents(tab_contents, set_bounds); | 143 html_view->MoveContents(tab_contents, set_bounds); |
173 ui_test_utils::RunMessageLoop(); | 144 ui_test_utils::RunMessageLoop(); // TestHtmlDialogView will quit. |
174 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 145 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
175 EXPECT_EQ(set_bounds, actual_bounds); | 146 EXPECT_EQ(set_bounds, actual_bounds); |
176 | 147 |
177 rwhv_bounds = html_view->dom_contents()->tab_contents()-> | 148 rwhv_bounds = html_view->dom_contents()->tab_contents()-> |
178 GetRenderWidgetHostView()->GetViewBounds(); | 149 GetRenderWidgetHostView()->GetViewBounds(); |
179 EXPECT_LT(0, rwhv_bounds.width()); | 150 EXPECT_LT(0, rwhv_bounds.width()); |
180 EXPECT_LT(0, rwhv_bounds.height()); | 151 EXPECT_LT(0, rwhv_bounds.height()); |
181 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); | 152 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); |
182 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); | 153 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); |
183 | 154 |
184 // Get very small. | 155 // Get very small. |
185 set_bounds.set_width(kMinimumWidthToTestFor); | 156 gfx::Size min_size = html_view->GetWidget()->GetMinimumSize(); |
186 set_bounds.set_height(kMinimumHeightToTestFor); | 157 set_bounds.set_size(min_size); |
187 | 158 |
188 html_view->MoveContents(tab_contents, set_bounds); | 159 html_view->MoveContents(tab_contents, set_bounds); |
189 ui_test_utils::RunMessageLoop(); | 160 ui_test_utils::RunMessageLoop(); // TestHtmlDialogView will quit. |
190 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 161 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
191 EXPECT_EQ(set_bounds, actual_bounds); | 162 EXPECT_EQ(set_bounds, actual_bounds); |
192 | 163 |
193 rwhv_bounds = html_view->dom_contents()->tab_contents()-> | 164 rwhv_bounds = html_view->dom_contents()->tab_contents()-> |
194 GetRenderWidgetHostView()->GetViewBounds(); | 165 GetRenderWidgetHostView()->GetViewBounds(); |
195 EXPECT_LT(0, rwhv_bounds.width()); | 166 EXPECT_LT(0, rwhv_bounds.width()); |
196 EXPECT_LT(0, rwhv_bounds.height()); | 167 EXPECT_LT(0, rwhv_bounds.height()); |
197 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); | 168 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); |
198 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); | 169 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); |
199 | 170 |
200 // Check to make sure we can't get to 0x0 | 171 // Check to make sure we can't get to 0x0 |
201 set_bounds.set_width(0); | 172 set_bounds.set_width(0); |
202 set_bounds.set_height(0); | 173 set_bounds.set_height(0); |
203 | 174 |
204 html_view->MoveContents(tab_contents, set_bounds); | 175 html_view->MoveContents(tab_contents, set_bounds); |
205 ui_test_utils::RunMessageLoop(); | 176 ui_test_utils::RunMessageLoop(); // TestHtmlDialogView will quit. |
206 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 177 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
207 EXPECT_LT(0, actual_bounds.width()); | 178 EXPECT_LT(0, actual_bounds.width()); |
208 EXPECT_LT(0, actual_bounds.height()); | 179 EXPECT_LT(0, actual_bounds.height()); |
209 | |
210 MessageLoopForUI::current()->RemoveObserver( | |
211 WindowChangedObserver::GetInstance()); | |
212 } | 180 } |
213 | 181 |
214 // This is timing out about 5~10% of runs. See crbug.com/86059. | 182 // This is timing out about 5~10% of runs. See crbug.com/86059. |
215 IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, DISABLED_WebContentRendered) { | 183 IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, DISABLED_WebContentRendered) { |
216 HtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( | 184 HtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( |
217 GURL(chrome::kChromeUIChromeURLsURL)); | 185 GURL(chrome::kChromeUIChromeURLsURL)); |
218 | 186 |
219 TestHtmlDialogView* html_view = | 187 TestHtmlDialogView* html_view = |
220 new TestHtmlDialogView(browser()->profile(), delegate); | 188 new TestHtmlDialogView(browser()->profile(), delegate); |
221 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 189 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
222 ASSERT_TRUE(tab_contents != NULL); | 190 ASSERT_TRUE(tab_contents != NULL); |
223 views::Widget::CreateWindowWithParent(html_view, | 191 views::Widget::CreateWindowWithParent(html_view, |
224 tab_contents->GetDialogRootWindow()); | 192 tab_contents->GetDialogRootWindow()); |
225 EXPECT_TRUE(html_view->initialized_); | 193 EXPECT_TRUE(html_view->initialized_); |
226 | 194 |
227 html_view->InitDialog(); | 195 html_view->InitDialog(); |
228 html_view->GetWidget()->Show(); | 196 html_view->GetWidget()->Show(); |
229 | 197 |
230 // TestHtmlDialogView::OnTabMainFrameFirstRender() will Quit(). | 198 // TestHtmlDialogView::OnTabMainFrameFirstRender() will Quit(). |
231 MessageLoopForUI::current()->Run(); | 199 MessageLoopForUI::current()->Run(); |
232 | 200 |
233 EXPECT_TRUE(html_view->painted()); | 201 EXPECT_TRUE(html_view->painted()); |
234 | 202 |
235 html_view->GetWidget()->Close(); | 203 html_view->GetWidget()->Close(); |
236 } | 204 } |
OLD | NEW |