OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/test/ui_test_utils.h" | 5 #include "chrome/test/ui_test_utils.h" |
6 | 6 |
7 #include "base/json_reader.h" | 7 #include "base/json_reader.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 int navigations_completed_; | 68 int navigations_completed_; |
69 | 69 |
70 // The number of navigations to wait for. | 70 // The number of navigations to wait for. |
71 int number_of_navigations_; | 71 int number_of_navigations_; |
72 | 72 |
73 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver); | 73 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver); |
74 }; | 74 }; |
75 | 75 |
76 class DOMOperationObserver : public NotificationObserver { | 76 class DOMOperationObserver : public NotificationObserver { |
77 public: | 77 public: |
78 explicit DOMOperationObserver(TabContents* tab_contents) { | 78 explicit DOMOperationObserver(RenderViewHost* render_view_host) { |
79 registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE, | 79 registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE, |
80 Source<TabContents>(tab_contents)); | 80 Source<RenderViewHost>(render_view_host)); |
81 RunMessageLoop(); | 81 RunMessageLoop(); |
82 } | 82 } |
83 | 83 |
84 virtual void Observe(NotificationType type, | 84 virtual void Observe(NotificationType type, |
85 const NotificationSource& source, | 85 const NotificationSource& source, |
86 const NotificationDetails& details) { | 86 const NotificationDetails& details) { |
87 DCHECK(type == NotificationType::DOM_OPERATION_RESPONSE); | 87 DCHECK(type == NotificationType::DOM_OPERATION_RESPONSE); |
88 Details<DomOperationNotificationDetails> dom_op_details(details); | 88 Details<DomOperationNotificationDetails> dom_op_details(details); |
89 response_ = dom_op_details->json(); | 89 response_ = dom_op_details->json(); |
90 MessageLoopForUI::current()->Quit(); | 90 MessageLoopForUI::current()->Quit(); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 | 223 |
224 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, | 224 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, |
225 const GURL& url, | 225 const GURL& url, |
226 int number_of_navigations) { | 226 int number_of_navigations) { |
227 NavigationController* controller = | 227 NavigationController* controller = |
228 &browser->GetSelectedTabContents()->controller(); | 228 &browser->GetSelectedTabContents()->controller(); |
229 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); | 229 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); |
230 WaitForNavigations(controller, number_of_navigations); | 230 WaitForNavigations(controller, number_of_navigations); |
231 } | 231 } |
232 | 232 |
233 Value* ExecuteJavaScript(TabContents* tab_contents, | 233 Value* ExecuteJavaScript(RenderViewHost* render_view_host, |
234 const std::wstring& frame_xpath, | 234 const std::wstring& frame_xpath, |
235 const std::wstring& original_script) { | 235 const std::wstring& original_script) { |
236 // TODO(jcampan): we should make the domAutomationController not require an | 236 // TODO(jcampan): we should make the domAutomationController not require an |
237 // automation id. | 237 // automation id. |
238 std::wstring script = L"window.domAutomationController.setAutomationId(0);" + | 238 std::wstring script = L"window.domAutomationController.setAutomationId(0);" + |
239 original_script; | 239 original_script; |
240 tab_contents->render_view_host()->ExecuteJavascriptInWebFrame(frame_xpath, | 240 render_view_host->ExecuteJavascriptInWebFrame(frame_xpath, script); |
241 script); | 241 DOMOperationObserver dom_op_observer(render_view_host); |
242 DOMOperationObserver dom_op_observer(tab_contents); | |
243 std::string json = dom_op_observer.response(); | 242 std::string json = dom_op_observer.response(); |
244 // Wrap |json| in an array before deserializing because valid JSON has an | 243 // Wrap |json| in an array before deserializing because valid JSON has an |
245 // array or an object as the root. | 244 // array or an object as the root. |
246 json.insert(0, "["); | 245 json.insert(0, "["); |
247 json.append("]"); | 246 json.append("]"); |
248 | 247 |
249 scoped_ptr<Value> root_val(JSONReader::Read(json, true)); | 248 scoped_ptr<Value> root_val(JSONReader::Read(json, true)); |
250 if (!root_val->IsType(Value::TYPE_LIST)) | 249 if (!root_val->IsType(Value::TYPE_LIST)) |
251 return NULL; | 250 return NULL; |
252 | 251 |
253 ListValue* list = static_cast<ListValue*>(root_val.get()); | 252 ListValue* list = static_cast<ListValue*>(root_val.get()); |
254 Value* result; | 253 Value* result; |
255 if (!list || !list->GetSize() || | 254 if (!list || !list->GetSize() || |
256 !list->Remove(0, &result)) // Remove gives us ownership of the value. | 255 !list->Remove(0, &result)) // Remove gives us ownership of the value. |
257 return NULL; | 256 return NULL; |
258 | 257 |
259 return result; | 258 return result; |
260 } | 259 } |
261 | 260 |
262 bool ExecuteJavaScriptAndExtractInt(TabContents* tab_contents, | 261 bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, |
263 const std::wstring& frame_xpath, | 262 const std::wstring& frame_xpath, |
264 const std::wstring& script, | 263 const std::wstring& script, |
265 int* result) { | 264 int* result) { |
266 DCHECK(result); | 265 DCHECK(result); |
267 scoped_ptr<Value> value(ExecuteJavaScript(tab_contents, frame_xpath, script)); | 266 scoped_ptr<Value> value(ExecuteJavaScript(render_view_host, frame_xpath, |
| 267 script)); |
268 if (!value.get()) | 268 if (!value.get()) |
269 return false; | 269 return false; |
270 | 270 |
271 return value->GetAsInteger(result); | 271 return value->GetAsInteger(result); |
272 } | 272 } |
273 | 273 |
274 bool ExecuteJavaScriptAndExtractBool(TabContents* tab_contents, | 274 bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host, |
275 const std::wstring& frame_xpath, | 275 const std::wstring& frame_xpath, |
276 const std::wstring& script, | 276 const std::wstring& script, |
277 bool* result) { | 277 bool* result) { |
278 DCHECK(result); | 278 DCHECK(result); |
279 scoped_ptr<Value> value(ExecuteJavaScript(tab_contents, frame_xpath, script)); | 279 scoped_ptr<Value> value(ExecuteJavaScript(render_view_host, frame_xpath, |
| 280 script)); |
280 if (!value.get()) | 281 if (!value.get()) |
281 return false; | 282 return false; |
282 | 283 |
283 return value->GetAsBoolean(result); | 284 return value->GetAsBoolean(result); |
284 } | 285 } |
285 | 286 |
286 bool ExecuteJavaScriptAndExtractString(TabContents* tab_contents, | 287 bool ExecuteJavaScriptAndExtractString(RenderViewHost* render_view_host, |
287 const std::wstring& frame_xpath, | 288 const std::wstring& frame_xpath, |
288 const std::wstring& script, | 289 const std::wstring& script, |
289 std::string* result) { | 290 std::string* result) { |
290 DCHECK(result); | 291 DCHECK(result); |
291 scoped_ptr<Value> value(ExecuteJavaScript(tab_contents, frame_xpath, script)); | 292 scoped_ptr<Value> value(ExecuteJavaScript(render_view_host, frame_xpath, |
| 293 script)); |
292 if (!value.get()) | 294 if (!value.get()) |
293 return false; | 295 return false; |
294 | 296 |
295 return value->GetAsString(result); | 297 return value->GetAsString(result); |
296 } | 298 } |
297 | 299 |
298 GURL GetTestUrl(const std::wstring& dir, const std::wstring file) { | 300 GURL GetTestUrl(const std::wstring& dir, const std::wstring file) { |
299 FilePath path; | 301 FilePath path; |
300 PathService::Get(chrome::DIR_TEST_DATA, &path); | 302 PathService::Get(chrome::DIR_TEST_DATA, &path); |
301 path = path.Append(FilePath::FromWStringHack(dir)); | 303 path = path.Append(FilePath::FromWStringHack(dir)); |
302 path = path.Append(FilePath::FromWStringHack(file)); | 304 path = path.Append(FilePath::FromWStringHack(file)); |
303 return net::FilePathToFileURL(path); | 305 return net::FilePathToFileURL(path); |
304 } | 306 } |
305 | 307 |
306 void WaitForDownloadCount(DownloadManager* download_manager, size_t count) { | 308 void WaitForDownloadCount(DownloadManager* download_manager, size_t count) { |
307 DownloadsCompleteObserver download_observer(download_manager, count); | 309 DownloadsCompleteObserver download_observer(download_manager, count); |
308 } | 310 } |
309 | 311 |
310 } // namespace ui_test_utils | 312 } // namespace ui_test_utils |
OLD | NEW |