| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/base/web_ui_browser_test.h" | 5 #include "chrome/test/base/web_ui_browser_test.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "base/values.h" | 14 #include "base/values.h" |
| 16 #include "chrome/browser/chrome_content_browser_client.h" | 15 #include "chrome/browser/chrome_content_browser_client.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_commands.h" | 18 #include "chrome/browser/ui/browser_commands.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/browser/ui/webui/web_ui_test_handler.h" | 20 #include "chrome/browser/ui/webui/web_ui_test_handler.h" |
| 22 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
| 23 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
| 24 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h" | 23 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h" |
| 25 #include "chrome/test/base/ui_test_utils.h" | 24 #include "chrome/test/base/ui_test_utils.h" |
| 26 #include "content/public/browser/url_data_source.h" | 25 #include "content/public/browser/url_data_source.h" |
| 27 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/browser/web_contents_observer.h" | 27 #include "content/public/browser/web_contents_observer.h" |
| 29 #include "content/public/browser/web_ui_controller.h" | 28 #include "content/public/browser/web_ui_controller.h" |
| 30 #include "content/public/browser/web_ui_message_handler.h" | 29 #include "content/public/browser/web_ui_message_handler.h" |
| 31 #include "content/public/test/browser_test_utils.h" | 30 #include "content/public/test/browser_test_utils.h" |
| 32 #include "content/public/test/test_navigation_observer.h" | 31 #include "content/public/test/test_navigation_observer.h" |
| 33 #include "net/base/filename_util.h" | 32 #include "net/base/filename_util.h" |
| 34 #include "ui/base/resource/resource_bundle.h" | |
| 35 #include "ui/base/resource/resource_handle.h" | 33 #include "ui/base/resource/resource_handle.h" |
| 36 | 34 |
| 37 #if defined(ENABLE_FULL_PRINTING) | 35 #if defined(ENABLE_FULL_PRINTING) |
| 38 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 36 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 39 #endif | 37 #endif |
| 40 | 38 |
| 41 using content::RenderViewHost; | 39 using content::RenderViewHost; |
| 42 using content::WebContents; | 40 using content::WebContents; |
| 43 using content::WebUIController; | 41 using content::WebUIController; |
| 44 using content::WebUIMessageHandler; | 42 using content::WebUIMessageHandler; |
| 45 | 43 |
| 46 namespace { | 44 namespace { |
| 47 | 45 |
| 48 const base::FilePath::CharType kA11yAuditLibraryJSPath[] = FILE_PATH_LITERAL( | |
| 49 "third_party/accessibility-audit/axs_testing.js"); | |
| 50 const base::FilePath::CharType kMockJSPath[] = | |
| 51 FILE_PATH_LITERAL("chrome/third_party/mock4js/mock4js.js"); | |
| 52 const base::FilePath::CharType kWebUILibraryJS[] = | |
| 53 FILE_PATH_LITERAL("test_api.js"); | |
| 54 const base::FilePath::CharType kWebUITestFolder[] = FILE_PATH_LITERAL("webui"); | |
| 55 base::LazyInstance<std::vector<std::string> > error_messages_ = | 46 base::LazyInstance<std::vector<std::string> > error_messages_ = |
| 56 LAZY_INSTANCE_INITIALIZER; | 47 LAZY_INSTANCE_INITIALIZER; |
| 57 | 48 |
| 58 // Intercepts all log messages. | 49 // Intercepts all log messages. |
| 59 bool LogHandler(int severity, | 50 bool LogHandler(int severity, |
| 60 const char* file, | 51 const char* file, |
| 61 int line, | 52 int line, |
| 62 size_t message_start, | 53 size_t message_start, |
| 63 const std::string& str) { | 54 const std::string& str) { |
| 64 if (severity == logging::LOG_ERROR && | 55 if (severity == logging::LOG_ERROR && file && |
| 65 file && | |
| 66 std::string("CONSOLE") == file) { | 56 std::string("CONSOLE") == file) { |
| 67 error_messages_.Get().push_back(str); | 57 error_messages_.Get().push_back(str); |
| 68 } | 58 } |
| 69 | 59 |
| 70 return false; | 60 return false; |
| 71 } | 61 } |
| 72 | 62 |
| 73 class WebUIJsInjectionReadyObserver : public content::WebContentsObserver { | 63 class WebUIJsInjectionReadyObserver : public content::WebContentsObserver { |
| 74 public: | 64 public: |
| 75 WebUIJsInjectionReadyObserver(content::WebContents* web_contents, | 65 WebUIJsInjectionReadyObserver(content::WebContents* web_contents, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 87 } | 77 } |
| 88 | 78 |
| 89 private: | 79 private: |
| 90 WebUIBrowserTest* browser_test_; | 80 WebUIBrowserTest* browser_test_; |
| 91 std::string preload_test_fixture_; | 81 std::string preload_test_fixture_; |
| 92 std::string preload_test_name_; | 82 std::string preload_test_name_; |
| 93 }; | 83 }; |
| 94 | 84 |
| 95 } // namespace | 85 } // namespace |
| 96 | 86 |
| 97 WebUIBrowserTest::~WebUIBrowserTest() {} | 87 WebUIBrowserTest::~WebUIBrowserTest() { |
| 98 | |
| 99 void WebUIBrowserTest::AddLibrary(const base::FilePath& library_path) { | |
| 100 user_libraries_.push_back(library_path); | |
| 101 } | 88 } |
| 102 | 89 |
| 103 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { | 90 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { |
| 104 ConstValueVector empty_args; | 91 ConstValueVector empty_args; |
| 105 return RunJavascriptFunction(function_name, empty_args); | 92 return RunJavascriptFunction(function_name, empty_args); |
| 106 } | 93 } |
| 107 | 94 |
| 108 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, | 95 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, |
| 109 base::Value* arg) { | 96 base::Value* arg) { |
| 110 ConstValueVector args; | 97 ConstValueVector args; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 "preloadJavascriptLibraries", args, false, false, preload_host); | 206 "preloadJavascriptLibraries", args, false, false, preload_host); |
| 220 libraries_preloaded_ = true; | 207 libraries_preloaded_ = true; |
| 221 } | 208 } |
| 222 | 209 |
| 223 void WebUIBrowserTest::BrowsePreload(const GURL& browse_to) { | 210 void WebUIBrowserTest::BrowsePreload(const GURL& browse_to) { |
| 224 content::WebContents* web_contents = | 211 content::WebContents* web_contents = |
| 225 browser()->tab_strip_model()->GetActiveWebContents(); | 212 browser()->tab_strip_model()->GetActiveWebContents(); |
| 226 WebUIJsInjectionReadyObserver injection_observer( | 213 WebUIJsInjectionReadyObserver injection_observer( |
| 227 web_contents, this, preload_test_fixture_, preload_test_name_); | 214 web_contents, this, preload_test_fixture_, preload_test_name_); |
| 228 content::TestNavigationObserver navigation_observer(web_contents); | 215 content::TestNavigationObserver navigation_observer(web_contents); |
| 229 chrome::NavigateParams params(browser(), GURL(browse_to), | 216 chrome::NavigateParams params( |
| 230 content::PAGE_TRANSITION_TYPED); | 217 browser(), GURL(browse_to), content::PAGE_TRANSITION_TYPED); |
| 231 params.disposition = CURRENT_TAB; | 218 params.disposition = CURRENT_TAB; |
| 232 chrome::Navigate(¶ms); | 219 chrome::Navigate(¶ms); |
| 233 navigation_observer.Wait(); | 220 navigation_observer.Wait(); |
| 234 } | 221 } |
| 235 | 222 |
| 236 #if defined(ENABLE_FULL_PRINTING) | 223 #if defined(ENABLE_FULL_PRINTING) |
| 237 | 224 |
| 238 // This custom ContentBrowserClient is used to get notified when a WebContents | 225 // This custom ContentBrowserClient is used to get notified when a WebContents |
| 239 // for the print preview dialog gets created. | 226 // for the print preview dialog gets created. |
| 240 class PrintContentBrowserClient : public chrome::ChromeContentBrowserClient { | 227 class PrintContentBrowserClient : public chrome::ChromeContentBrowserClient { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 251 void Wait() { | 238 void Wait() { |
| 252 message_loop_runner_->Run(); | 239 message_loop_runner_->Run(); |
| 253 content::WaitForLoadStop(preview_dialog_); | 240 content::WaitForLoadStop(preview_dialog_); |
| 254 } | 241 } |
| 255 | 242 |
| 256 private: | 243 private: |
| 257 // ChromeContentBrowserClient implementation: | 244 // ChromeContentBrowserClient implementation: |
| 258 virtual content::WebContentsViewDelegate* GetWebContentsViewDelegate( | 245 virtual content::WebContentsViewDelegate* GetWebContentsViewDelegate( |
| 259 content::WebContents* web_contents) OVERRIDE { | 246 content::WebContents* web_contents) OVERRIDE { |
| 260 preview_dialog_ = web_contents; | 247 preview_dialog_ = web_contents; |
| 261 observer_.reset(new WebUIJsInjectionReadyObserver( | 248 observer_.reset(new WebUIJsInjectionReadyObserver(preview_dialog_, |
| 262 preview_dialog_, browser_test_, preload_test_fixture_, | 249 browser_test_, |
| 263 preload_test_name_)); | 250 preload_test_fixture_, |
| 251 preload_test_name_)); |
| 264 message_loop_runner_->Quit(); | 252 message_loop_runner_->Quit(); |
| 265 return NULL; | 253 return NULL; |
| 266 } | 254 } |
| 267 | 255 |
| 268 WebUIBrowserTest* browser_test_; | 256 WebUIBrowserTest* browser_test_; |
| 269 scoped_ptr<WebUIJsInjectionReadyObserver> observer_; | 257 scoped_ptr<WebUIJsInjectionReadyObserver> observer_; |
| 270 std::string preload_test_fixture_; | 258 std::string preload_test_fixture_; |
| 271 std::string preload_test_name_; | 259 std::string preload_test_name_; |
| 272 content::WebContents* preview_dialog_; | 260 content::WebContents* preview_dialog_; |
| 273 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 261 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 298 #else | 286 #else |
| 299 NOTREACHED(); | 287 NOTREACHED(); |
| 300 #endif | 288 #endif |
| 301 } | 289 } |
| 302 | 290 |
| 303 const char WebUIBrowserTest::kDummyURL[] = "chrome://DummyURL"; | 291 const char WebUIBrowserTest::kDummyURL[] = "chrome://DummyURL"; |
| 304 | 292 |
| 305 WebUIBrowserTest::WebUIBrowserTest() | 293 WebUIBrowserTest::WebUIBrowserTest() |
| 306 : test_handler_(new WebUITestHandler()), | 294 : test_handler_(new WebUITestHandler()), |
| 307 libraries_preloaded_(false), | 295 libraries_preloaded_(false), |
| 308 override_selected_web_ui_(NULL) {} | 296 override_selected_web_ui_(NULL) { |
| 297 } |
| 309 | 298 |
| 310 void WebUIBrowserTest::set_preload_test_fixture( | 299 void WebUIBrowserTest::set_preload_test_fixture( |
| 311 const std::string& preload_test_fixture) { | 300 const std::string& preload_test_fixture) { |
| 312 preload_test_fixture_ = preload_test_fixture; | 301 preload_test_fixture_ = preload_test_fixture; |
| 313 } | 302 } |
| 314 | 303 |
| 315 void WebUIBrowserTest::set_preload_test_name( | 304 void WebUIBrowserTest::set_preload_test_name( |
| 316 const std::string& preload_test_name) { | 305 const std::string& preload_test_name) { |
| 317 preload_test_name_ = preload_test_name; | 306 preload_test_name_ = preload_test_name; |
| 318 } | 307 } |
| 319 | 308 |
| 320 namespace { | 309 namespace { |
| 321 | 310 |
| 322 // DataSource for the dummy URL. If no data source is provided then an error | 311 // DataSource for the dummy URL. If no data source is provided then an error |
| 323 // page is shown. While this doesn't matter for most tests, without it, | 312 // page is shown. While this doesn't matter for most tests, without it, |
| 324 // navigation to different anchors cannot be listened to (via the hashchange | 313 // navigation to different anchors cannot be listened to (via the hashchange |
| 325 // event). | 314 // event). |
| 326 class MockWebUIDataSource : public content::URLDataSource { | 315 class MockWebUIDataSource : public content::URLDataSource { |
| 327 public: | 316 public: |
| 328 MockWebUIDataSource() {} | 317 MockWebUIDataSource() {} |
| 329 | 318 |
| 330 private: | 319 private: |
| 331 virtual ~MockWebUIDataSource() {} | 320 virtual ~MockWebUIDataSource() {} |
| 332 | 321 |
| 333 virtual std::string GetSource() const OVERRIDE { | 322 virtual std::string GetSource() const OVERRIDE { return "dummyurl"; } |
| 334 return "dummyurl"; | |
| 335 } | |
| 336 | 323 |
| 337 virtual void StartDataRequest( | 324 virtual void StartDataRequest( |
| 338 const std::string& path, | 325 const std::string& path, |
| 339 int render_process_id, | 326 int render_process_id, |
| 340 int render_frame_id, | 327 int render_frame_id, |
| 341 const content::URLDataSource::GotDataCallback& callback) OVERRIDE { | 328 const content::URLDataSource::GotDataCallback& callback) OVERRIDE { |
| 342 std::string dummy_html = "<html><body>Dummy</body></html>"; | 329 std::string dummy_html = "<html><body>Dummy</body></html>"; |
| 343 scoped_refptr<base::RefCountedString> response = | 330 scoped_refptr<base::RefCountedString> response = |
| 344 base::RefCountedString::TakeString(&dummy_html); | 331 base::RefCountedString::TakeString(&dummy_html); |
| 345 callback.Run(response.get()); | 332 callback.Run(response.get()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 371 private: | 358 private: |
| 372 DISALLOW_COPY_AND_ASSIGN(MockWebUIProvider); | 359 DISALLOW_COPY_AND_ASSIGN(MockWebUIProvider); |
| 373 }; | 360 }; |
| 374 | 361 |
| 375 base::LazyInstance<MockWebUIProvider> mock_provider_ = | 362 base::LazyInstance<MockWebUIProvider> mock_provider_ = |
| 376 LAZY_INSTANCE_INITIALIZER; | 363 LAZY_INSTANCE_INITIALIZER; |
| 377 | 364 |
| 378 } // namespace | 365 } // namespace |
| 379 | 366 |
| 380 void WebUIBrowserTest::SetUpOnMainThread() { | 367 void WebUIBrowserTest::SetUpOnMainThread() { |
| 368 JavaScriptBrowserTest::SetUpOnMainThread(); |
| 369 |
| 381 logging::SetLogMessageHandler(&LogHandler); | 370 logging::SetLogMessageHandler(&LogHandler); |
| 382 | 371 |
| 372 AddLibrary(base::FilePath(kA11yAuditLibraryJSPath)); |
| 373 |
| 383 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 374 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
| 384 ChromeWebUIControllerFactory::GetInstance()); | 375 ChromeWebUIControllerFactory::GetInstance()); |
| 385 | 376 |
| 386 test_factory_.reset(new TestChromeWebUIControllerFactory); | 377 test_factory_.reset(new TestChromeWebUIControllerFactory); |
| 387 | 378 |
| 388 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); | 379 content::WebUIControllerFactory::RegisterFactory(test_factory_.get()); |
| 389 | 380 |
| 390 test_factory_->AddFactoryOverride( | 381 test_factory_->AddFactoryOverride(GURL(kDummyURL).host(), |
| 391 GURL(kDummyURL).host(), mock_provider_.Pointer()); | 382 mock_provider_.Pointer()); |
| 392 | |
| 393 base::FilePath test_data_directory; | |
| 394 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory)); | |
| 395 test_data_directory = test_data_directory.Append(kWebUITestFolder); | |
| 396 library_search_paths_.push_back(test_data_directory); | |
| 397 | |
| 398 base::FilePath gen_test_data_directory; | |
| 399 ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, | |
| 400 &gen_test_data_directory)); | |
| 401 library_search_paths_.push_back(gen_test_data_directory); | |
| 402 | |
| 403 base::FilePath source_root_directory; | |
| 404 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source_root_directory)); | |
| 405 library_search_paths_.push_back(source_root_directory); | |
| 406 | |
| 407 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. | |
| 408 base::FilePath resources_pack_path; | |
| 409 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); | |
| 410 ResourceBundle::GetSharedInstance().AddDataPackFromPath( | |
| 411 resources_pack_path, ui::SCALE_FACTOR_NONE); | |
| 412 | |
| 413 AddLibrary(base::FilePath(kA11yAuditLibraryJSPath)); | |
| 414 AddLibrary(base::FilePath(kMockJSPath)); | |
| 415 AddLibrary(base::FilePath(kWebUILibraryJS)); | |
| 416 } | 383 } |
| 417 | 384 |
| 418 void WebUIBrowserTest::CleanUpOnMainThread() { | 385 void WebUIBrowserTest::CleanUpOnMainThread() { |
| 419 logging::SetLogMessageHandler(NULL); | 386 logging::SetLogMessageHandler(NULL); |
| 420 | 387 |
| 421 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); | 388 test_factory_->RemoveFactoryOverride(GURL(kDummyURL).host()); |
| 422 content::WebUIControllerFactory::UnregisterFactoryForTesting( | 389 content::WebUIControllerFactory::UnregisterFactoryForTesting( |
| 423 test_factory_.get()); | 390 test_factory_.get()); |
| 424 | 391 |
| 425 // This is needed to avoid a debug assert after the test completes, see stack | 392 // This is needed to avoid a debug assert after the test completes, see stack |
| 426 // trace in http://crrev.com/179347 | 393 // trace in http://crrev.com/179347 |
| 427 content::WebUIControllerFactory::RegisterFactory( | 394 content::WebUIControllerFactory::RegisterFactory( |
| 428 ChromeWebUIControllerFactory::GetInstance()); | 395 ChromeWebUIControllerFactory::GetInstance()); |
| 429 | 396 |
| 430 test_factory_.reset(); | 397 test_factory_.reset(); |
| 431 } | 398 } |
| 432 | 399 |
| 433 void WebUIBrowserTest::SetWebUIInstance(content::WebUI* web_ui) { | 400 void WebUIBrowserTest::SetWebUIInstance(content::WebUI* web_ui) { |
| 434 override_selected_web_ui_ = web_ui; | 401 override_selected_web_ui_ = web_ui; |
| 435 } | 402 } |
| 436 | 403 |
| 437 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { | 404 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { |
| 438 return NULL; | 405 return NULL; |
| 439 } | 406 } |
| 440 | 407 |
| 441 GURL WebUIBrowserTest::WebUITestDataPathToURL( | |
| 442 const base::FilePath::StringType& path) { | |
| 443 base::FilePath dir_test_data; | |
| 444 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | |
| 445 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); | |
| 446 EXPECT_TRUE(base::PathExists(test_path)); | |
| 447 return net::FilePathToFileURL(test_path); | |
| 448 } | |
| 449 | |
| 450 void WebUIBrowserTest::BuildJavascriptLibraries( | |
| 451 std::vector<base::string16>* libraries) { | |
| 452 ASSERT_TRUE(libraries != NULL); | |
| 453 std::vector<base::FilePath>::iterator user_libraries_iterator; | |
| 454 for (user_libraries_iterator = user_libraries_.begin(); | |
| 455 user_libraries_iterator != user_libraries_.end(); | |
| 456 ++user_libraries_iterator) { | |
| 457 std::string library_content; | |
| 458 if (user_libraries_iterator->IsAbsolute()) { | |
| 459 ASSERT_TRUE(base::ReadFileToString(*user_libraries_iterator, | |
| 460 &library_content)) | |
| 461 << user_libraries_iterator->value(); | |
| 462 } else { | |
| 463 bool ok = false; | |
| 464 std::vector<base::FilePath>::iterator library_search_path_iterator; | |
| 465 for (library_search_path_iterator = library_search_paths_.begin(); | |
| 466 library_search_path_iterator != library_search_paths_.end(); | |
| 467 ++library_search_path_iterator) { | |
| 468 ok = base::ReadFileToString( | |
| 469 base::MakeAbsoluteFilePath( | |
| 470 library_search_path_iterator->Append(*user_libraries_iterator)), | |
| 471 &library_content); | |
| 472 if (ok) | |
| 473 break; | |
| 474 } | |
| 475 ASSERT_TRUE(ok) << "User library not found: " | |
| 476 << user_libraries_iterator->value(); | |
| 477 } | |
| 478 library_content.append(";\n"); | |
| 479 | |
| 480 // This magic code puts filenames in stack traces. | |
| 481 library_content.append("//# sourceURL="); | |
| 482 library_content.append(user_libraries_iterator->BaseName().AsUTF8Unsafe()); | |
| 483 library_content.append("\n"); | |
| 484 libraries->push_back(base::UTF8ToUTF16(library_content)); | |
| 485 } | |
| 486 } | |
| 487 | |
| 488 base::string16 WebUIBrowserTest::BuildRunTestJSCall( | |
| 489 bool is_async, | |
| 490 const std::string& function_name, | |
| 491 const WebUIBrowserTest::ConstValueVector& test_func_args) { | |
| 492 ConstValueVector arguments; | |
| 493 base::FundamentalValue* is_async_arg = new base::FundamentalValue(is_async); | |
| 494 arguments.push_back(is_async_arg); | |
| 495 base::StringValue* function_name_arg = new base::StringValue(function_name); | |
| 496 arguments.push_back(function_name_arg); | |
| 497 base::ListValue* baked_argument_list = new base::ListValue(); | |
| 498 ConstValueVector::const_iterator arguments_iterator; | |
| 499 for (arguments_iterator = test_func_args.begin(); | |
| 500 arguments_iterator != test_func_args.end(); | |
| 501 ++arguments_iterator) { | |
| 502 baked_argument_list->Append((*arguments_iterator)->DeepCopy()); | |
| 503 } | |
| 504 arguments.push_back(baked_argument_list); | |
| 505 return content::WebUI::GetJavascriptCall(std::string("runTest"), | |
| 506 arguments.get()); | |
| 507 } | |
| 508 | |
| 509 bool WebUIBrowserTest::RunJavascriptUsingHandler( | 408 bool WebUIBrowserTest::RunJavascriptUsingHandler( |
| 510 const std::string& function_name, | 409 const std::string& function_name, |
| 511 const ConstValueVector& function_arguments, | 410 const ConstValueVector& function_arguments, |
| 512 bool is_test, | 411 bool is_test, |
| 513 bool is_async, | 412 bool is_async, |
| 514 RenderViewHost* preload_host) { | 413 RenderViewHost* preload_host) { |
| 515 // Get the user libraries. Preloading them individually is best, then | 414 // Get the user libraries. Preloading them individually is best, then |
| 516 // we can assign each one a filename for better stack traces. Otherwise | 415 // we can assign each one a filename for better stack traces. Otherwise |
| 517 // append them all to |content|. | 416 // append them all to |content|. |
| 518 base::string16 content; | 417 base::string16 content; |
| 519 std::vector<base::string16> libraries; | 418 std::vector<base::string16> libraries; |
| 520 if (!libraries_preloaded_) { | 419 if (!libraries_preloaded_) { |
| 521 BuildJavascriptLibraries(&libraries); | 420 BuildJavascriptLibraries(&libraries); |
| 522 if (!preload_host) { | 421 if (!preload_host) { |
| 523 content = JoinString(libraries, '\n'); | 422 content = JoinString(libraries, '\n'); |
| 524 libraries.clear(); | 423 libraries.clear(); |
| 525 } | 424 } |
| 526 } | 425 } |
| 527 | 426 |
| 528 if (!function_name.empty()) { | 427 if (!function_name.empty()) { |
| 529 base::string16 called_function; | 428 base::string16 called_function; |
| 530 if (is_test) { | 429 if (is_test) { |
| 531 called_function = | 430 called_function = |
| 532 BuildRunTestJSCall(is_async, function_name, function_arguments); | 431 BuildRunTestJSCall(is_async, function_name, function_arguments); |
| 533 } else { | 432 } else { |
| 534 called_function = | 433 called_function = content::WebUI::GetJavascriptCall( |
| 535 content::WebUI::GetJavascriptCall(function_name, | 434 function_name, function_arguments.get()); |
| 536 function_arguments.get()); | |
| 537 } | 435 } |
| 538 content.append(called_function); | 436 content.append(called_function); |
| 539 } | 437 } |
| 540 | 438 |
| 541 if (!preload_host) | 439 if (!preload_host) |
| 542 SetupHandlers(); | 440 SetupHandlers(); |
| 543 | 441 |
| 544 bool result = true; | 442 bool result = true; |
| 545 | 443 |
| 546 for (size_t i = 0; i < libraries.size(); ++i) | 444 for (size_t i = 0; i < libraries.size(); ++i) |
| 547 test_handler_->PreloadJavaScript(libraries[i], preload_host); | 445 test_handler_->PreloadJavaScript(libraries[i], preload_host); |
| 548 | 446 |
| 549 if (is_test) | 447 if (is_test) |
| 550 result = test_handler_->RunJavaScriptTestWithResult(content); | 448 result = test_handler_->RunJavaScriptTestWithResult(content); |
| 551 else if (preload_host) | 449 else if (preload_host) |
| 552 test_handler_->PreloadJavaScript(content, preload_host); | 450 test_handler_->PreloadJavaScript(content, preload_host); |
| 553 else | 451 else |
| 554 test_handler_->RunJavaScript(content); | 452 test_handler_->RunJavaScript(content); |
| 555 | 453 |
| 556 if (error_messages_.Get().size() > 0) { | 454 if (error_messages_.Get().size() > 0) { |
| 557 LOG(ERROR) << "Encountered javascript console error(s)"; | 455 LOG(ERROR) << "Encountered javascript console error(s)"; |
| 558 result = false; | 456 result = false; |
| 559 error_messages_.Get().clear(); | 457 error_messages_.Get().clear(); |
| 560 } | 458 } |
| 561 return result; | 459 return result; |
| 562 } | 460 } |
| 563 | 461 |
| 564 void WebUIBrowserTest::SetupHandlers() { | 462 void WebUIBrowserTest::SetupHandlers() { |
| 565 content::WebUI* web_ui_instance = override_selected_web_ui_ ? | 463 content::WebUI* web_ui_instance = |
| 566 override_selected_web_ui_ : | 464 override_selected_web_ui_ |
| 567 browser()->tab_strip_model()->GetActiveWebContents()->GetWebUI(); | 465 ? override_selected_web_ui_ |
| 466 : browser()->tab_strip_model()->GetActiveWebContents()->GetWebUI(); |
| 568 ASSERT_TRUE(web_ui_instance != NULL); | 467 ASSERT_TRUE(web_ui_instance != NULL); |
| 569 | 468 |
| 570 test_handler_->set_web_ui(web_ui_instance); | 469 test_handler_->set_web_ui(web_ui_instance); |
| 571 test_handler_->RegisterMessages(); | 470 test_handler_->RegisterMessages(); |
| 572 | 471 |
| 573 if (GetMockMessageHandler()) { | 472 if (GetMockMessageHandler()) { |
| 574 GetMockMessageHandler()->set_web_ui(web_ui_instance); | 473 GetMockMessageHandler()->set_web_ui(web_ui_instance); |
| 575 GetMockMessageHandler()->RegisterMessages(); | 474 GetMockMessageHandler()->RegisterMessages(); |
| 576 } | 475 } |
| 577 } | 476 } |
| 477 |
| 478 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
| 479 const base::FilePath::StringType& path) { |
| 480 base::FilePath dir_test_data; |
| 481 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
| 482 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); |
| 483 EXPECT_TRUE(base::PathExists(test_path)); |
| 484 return net::FilePathToFileURL(test_path); |
| 485 } |
| OLD | NEW |