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 // TODO(xiyuan): Update this when WidgetDelegate has bounds change hook. |
| 50 virtual void SaveWindowPlacement(const gfx::Rect& bounds, |
| 51 ui::WindowShowState show_state) OVERRIDE { |
| 52 if (should_quit_on_size_change_ && last_size_ != bounds.size()) { |
| 53 // Schedule message loop quit because we could be called while |
| 54 // the bounds change call is on the stack and not in the nested message |
| 55 // loop. |
| 56 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 57 &MessageLoop::Quit, base::Unretained(MessageLoop::current()))); |
| 58 } |
| 59 |
| 60 last_size_ = bounds.size(); |
| 61 } |
| 62 |
| 63 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE { |
| 64 should_quit_on_size_change_ = false; // No quit when we are closing. |
| 65 HtmlDialogView::OnDialogClosed(json_retval); |
| 66 } |
| 67 |
48 virtual void OnTabMainFrameFirstRender() OVERRIDE { | 68 virtual void OnTabMainFrameFirstRender() OVERRIDE { |
49 HtmlDialogView::OnTabMainFrameFirstRender(); | 69 HtmlDialogView::OnTabMainFrameFirstRender(); |
50 painted_ = true; | 70 painted_ = true; |
51 MessageLoop::current()->Quit(); | 71 MessageLoop::current()->Quit(); |
52 } | 72 } |
53 | 73 |
54 private: | 74 // Whether first rendered notification is received. |
55 bool painted_; | 75 bool painted_; |
56 | 76 |
| 77 // Whether we should quit message loop when size change is detected. |
| 78 bool should_quit_on_size_change_; |
| 79 gfx::Size last_size_; |
| 80 |
57 DISALLOW_COPY_AND_ASSIGN(TestHtmlDialogView); | 81 DISALLOW_COPY_AND_ASSIGN(TestHtmlDialogView); |
58 }; | 82 }; |
59 | 83 |
60 } // namespace | 84 } // namespace |
61 | 85 |
62 class HtmlDialogBrowserTest : public InProcessBrowserTest { | 86 class HtmlDialogBrowserTest : public InProcessBrowserTest { |
63 public: | 87 public: |
64 HtmlDialogBrowserTest() {} | 88 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 }; | 89 }; |
118 | 90 |
119 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 91 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
120 #define MAYBE_SizeWindow SizeWindow | 92 #define MAYBE_SizeWindow SizeWindow |
121 #else | 93 #else |
122 // http://code.google.com/p/chromium/issues/detail?id=52602 | 94 // http://code.google.com/p/chromium/issues/detail?id=52602 |
123 // Windows has some issues resizing windows- an off by one problem, | 95 // 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 | 96 // 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 | 97 // Mac builds yet. On Chrome OS, this test doesn't apply since ChromeOS |
126 // doesn't allow resizing of windows. | 98 // doesn't allow resizing of windows. |
127 #define MAYBE_SizeWindow DISABLED_SizeWindow | 99 #define MAYBE_SizeWindow DISABLED_SizeWindow |
128 #endif | 100 #endif |
129 | 101 |
130 IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, MAYBE_SizeWindow) { | 102 IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, MAYBE_SizeWindow) { |
131 test::TestHtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( | 103 test::TestHtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( |
132 GURL(chrome::kChromeUIChromeURLsURL)); | 104 GURL(chrome::kChromeUIChromeURLsURL)); |
133 delegate->set_size(kInitialWidth, kInitialHeight); | 105 delegate->set_size(kInitialWidth, kInitialHeight); |
134 | 106 |
135 HtmlDialogView* html_view = | 107 TestHtmlDialogView* html_view = |
136 new HtmlDialogView(browser()->profile(), delegate); | 108 new TestHtmlDialogView(browser()->profile(), delegate); |
137 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 109 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
138 ASSERT_TRUE(tab_contents != NULL); | 110 ASSERT_TRUE(tab_contents != NULL); |
139 views::Widget::CreateWindowWithParent(html_view, | 111 views::Widget::CreateWindowWithParent(html_view, |
140 tab_contents->GetDialogRootWindow()); | 112 tab_contents->GetDialogRootWindow()); |
141 html_view->InitDialog(); | 113 html_view->InitDialog(); |
142 html_view->GetWidget()->Show(); | 114 html_view->GetWidget()->Show(); |
143 | 115 |
144 MessageLoopForUI::current()->AddObserver( | 116 // TestHtmlDialogView should quit current message loop on size change. |
145 WindowChangedObserver::GetInstance()); | 117 html_view->set_should_quit_on_size_change(true); |
146 | 118 |
147 gfx::Rect bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 119 gfx::Rect bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
148 | 120 |
149 gfx::Rect set_bounds = bounds; | 121 gfx::Rect set_bounds = bounds; |
150 gfx::Rect actual_bounds, rwhv_bounds; | 122 gfx::Rect actual_bounds, rwhv_bounds; |
151 | 123 |
152 // Bigger than the default in both dimensions. | 124 // Bigger than the default in both dimensions. |
153 set_bounds.set_width(400); | 125 set_bounds.set_width(400); |
154 set_bounds.set_height(300); | 126 set_bounds.set_height(300); |
155 | 127 |
156 html_view->MoveContents(tab_contents, set_bounds); | 128 html_view->MoveContents(tab_contents, set_bounds); |
157 ui_test_utils::RunMessageLoop(); | 129 ui_test_utils::RunMessageLoop(); // TestHtmlDialogView will quit. |
158 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 130 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
159 EXPECT_EQ(set_bounds, actual_bounds); | 131 EXPECT_EQ(set_bounds, actual_bounds); |
160 | 132 |
161 rwhv_bounds = html_view->dom_contents()->tab_contents()-> | 133 rwhv_bounds = html_view->dom_contents()->tab_contents()-> |
162 GetRenderWidgetHostView()->GetViewBounds(); | 134 GetRenderWidgetHostView()->GetViewBounds(); |
163 EXPECT_LT(0, rwhv_bounds.width()); | 135 EXPECT_LT(0, rwhv_bounds.width()); |
164 EXPECT_LT(0, rwhv_bounds.height()); | 136 EXPECT_LT(0, rwhv_bounds.height()); |
165 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); | 137 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); |
166 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); | 138 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); |
167 | 139 |
168 // Larger in one dimension and smaller in the other. | 140 // Larger in one dimension and smaller in the other. |
169 set_bounds.set_width(550); | 141 set_bounds.set_width(550); |
170 set_bounds.set_height(250); | 142 set_bounds.set_height(250); |
171 | 143 |
172 html_view->MoveContents(tab_contents, set_bounds); | 144 html_view->MoveContents(tab_contents, set_bounds); |
173 ui_test_utils::RunMessageLoop(); | 145 ui_test_utils::RunMessageLoop(); // TestHtmlDialogView will quit. |
174 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 146 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
175 EXPECT_EQ(set_bounds, actual_bounds); | 147 EXPECT_EQ(set_bounds, actual_bounds); |
176 | 148 |
177 rwhv_bounds = html_view->dom_contents()->tab_contents()-> | 149 rwhv_bounds = html_view->dom_contents()->tab_contents()-> |
178 GetRenderWidgetHostView()->GetViewBounds(); | 150 GetRenderWidgetHostView()->GetViewBounds(); |
179 EXPECT_LT(0, rwhv_bounds.width()); | 151 EXPECT_LT(0, rwhv_bounds.width()); |
180 EXPECT_LT(0, rwhv_bounds.height()); | 152 EXPECT_LT(0, rwhv_bounds.height()); |
181 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); | 153 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); |
182 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); | 154 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); |
183 | 155 |
184 // Get very small. | 156 // Get very small. |
185 set_bounds.set_width(kMinimumWidthToTestFor); | 157 gfx::Size min_size = html_view->GetWidget()->GetMinimumSize(); |
186 set_bounds.set_height(kMinimumHeightToTestFor); | 158 set_bounds.set_size(min_size); |
187 | 159 |
188 html_view->MoveContents(tab_contents, set_bounds); | 160 html_view->MoveContents(tab_contents, set_bounds); |
189 ui_test_utils::RunMessageLoop(); | 161 ui_test_utils::RunMessageLoop(); // TestHtmlDialogView will quit. |
190 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 162 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
191 EXPECT_EQ(set_bounds, actual_bounds); | 163 EXPECT_EQ(set_bounds, actual_bounds); |
192 | 164 |
193 rwhv_bounds = html_view->dom_contents()->tab_contents()-> | 165 rwhv_bounds = html_view->dom_contents()->tab_contents()-> |
194 GetRenderWidgetHostView()->GetViewBounds(); | 166 GetRenderWidgetHostView()->GetViewBounds(); |
195 EXPECT_LT(0, rwhv_bounds.width()); | 167 EXPECT_LT(0, rwhv_bounds.width()); |
196 EXPECT_LT(0, rwhv_bounds.height()); | 168 EXPECT_LT(0, rwhv_bounds.height()); |
197 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); | 169 EXPECT_GE(set_bounds.width(), rwhv_bounds.width()); |
198 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); | 170 EXPECT_GE(set_bounds.height(), rwhv_bounds.height()); |
199 | 171 |
200 // Check to make sure we can't get to 0x0 | 172 // Check to make sure we can't get to 0x0 |
201 set_bounds.set_width(0); | 173 set_bounds.set_width(0); |
202 set_bounds.set_height(0); | 174 set_bounds.set_height(0); |
203 | 175 |
204 html_view->MoveContents(tab_contents, set_bounds); | 176 html_view->MoveContents(tab_contents, set_bounds); |
205 ui_test_utils::RunMessageLoop(); | 177 ui_test_utils::RunMessageLoop(); // TestHtmlDialogView will quit. |
206 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); | 178 actual_bounds = html_view->GetWidget()->GetClientAreaScreenBounds(); |
207 EXPECT_LT(0, actual_bounds.width()); | 179 EXPECT_LT(0, actual_bounds.width()); |
208 EXPECT_LT(0, actual_bounds.height()); | 180 EXPECT_LT(0, actual_bounds.height()); |
209 | |
210 MessageLoopForUI::current()->RemoveObserver( | |
211 WindowChangedObserver::GetInstance()); | |
212 } | 181 } |
213 | 182 |
214 // This is timing out about 5~10% of runs. See crbug.com/86059. | 183 // This is timing out about 5~10% of runs. See crbug.com/86059. |
215 IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, DISABLED_WebContentRendered) { | 184 IN_PROC_BROWSER_TEST_F(HtmlDialogBrowserTest, DISABLED_WebContentRendered) { |
216 HtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( | 185 HtmlDialogUIDelegate* delegate = new test::TestHtmlDialogUIDelegate( |
217 GURL(chrome::kChromeUIChromeURLsURL)); | 186 GURL(chrome::kChromeUIChromeURLsURL)); |
218 | 187 |
219 TestHtmlDialogView* html_view = | 188 TestHtmlDialogView* html_view = |
220 new TestHtmlDialogView(browser()->profile(), delegate); | 189 new TestHtmlDialogView(browser()->profile(), delegate); |
221 TabContents* tab_contents = browser()->GetSelectedTabContents(); | 190 TabContents* tab_contents = browser()->GetSelectedTabContents(); |
222 ASSERT_TRUE(tab_contents != NULL); | 191 ASSERT_TRUE(tab_contents != NULL); |
223 views::Widget::CreateWindowWithParent(html_view, | 192 views::Widget::CreateWindowWithParent(html_view, |
224 tab_contents->GetDialogRootWindow()); | 193 tab_contents->GetDialogRootWindow()); |
225 EXPECT_TRUE(html_view->initialized_); | 194 EXPECT_TRUE(html_view->initialized_); |
226 | 195 |
227 html_view->InitDialog(); | 196 html_view->InitDialog(); |
228 html_view->GetWidget()->Show(); | 197 html_view->GetWidget()->Show(); |
229 | 198 |
230 // TestHtmlDialogView::OnTabMainFrameFirstRender() will Quit(). | 199 // TestHtmlDialogView::OnTabMainFrameFirstRender() will Quit(). |
231 MessageLoopForUI::current()->Run(); | 200 MessageLoopForUI::current()->Run(); |
232 | 201 |
233 EXPECT_TRUE(html_view->painted()); | 202 EXPECT_TRUE(html_view->painted()); |
234 | 203 |
235 html_view->GetWidget()->Close(); | 204 html_view->GetWidget()->Close(); |
236 } | 205 } |
OLD | NEW |