| 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/message_loop/message_loop.h" |
| 12 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 #include "chrome/browser/chrome_content_browser_client.h" | 16 #include "chrome/browser/chrome_content_browser_client.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/browser_commands.h" | 19 #include "chrome/browser/ui/browser_commands.h" |
| 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 20 #include "chrome/browser/ui/webui/web_ui_test_handler.h" | 21 #include "chrome/browser/ui/webui/web_ui_test_handler.h" |
| 21 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 browser_test_->PreLoadJavascriptLibraries( | 76 browser_test_->PreLoadJavascriptLibraries( |
| 76 preload_test_fixture_, preload_test_name_, rvh); | 77 preload_test_fixture_, preload_test_name_, rvh); |
| 77 } | 78 } |
| 78 | 79 |
| 79 private: | 80 private: |
| 80 WebUIBrowserTest* browser_test_; | 81 WebUIBrowserTest* browser_test_; |
| 81 std::string preload_test_fixture_; | 82 std::string preload_test_fixture_; |
| 82 std::string preload_test_name_; | 83 std::string preload_test_name_; |
| 83 }; | 84 }; |
| 84 | 85 |
| 86 class WebUIMainObserver : public content::WebContentsObserver { |
| 87 public: |
| 88 explicit WebUIMainObserver(content::WebContents* web_contents) |
| 89 : content::WebContentsObserver(web_contents), |
| 90 message_loop_runner_(new content::MessageLoopRunner), |
| 91 has_fired_(false) { |
| 92 } |
| 93 |
| 94 void Wait() { |
| 95 if (has_fired_) |
| 96 return; |
| 97 |
| 98 message_loop_runner_->Run(); |
| 99 } |
| 100 |
| 101 virtual void DidRunWebUIMojoMain() OVERRIDE { |
| 102 has_fired_ = true; |
| 103 message_loop_runner_->Quit(); |
| 104 } |
| 105 |
| 106 private: |
| 107 // The MessageLoopRunner used to spin the message loop. |
| 108 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 109 |
| 110 bool has_fired_; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(WebUIMainObserver); |
| 113 }; |
| 114 |
| 85 } // namespace | 115 } // namespace |
| 86 | 116 |
| 87 WebUIBrowserTest::~WebUIBrowserTest() { | 117 WebUIBrowserTest::~WebUIBrowserTest() { |
| 88 } | 118 } |
| 89 | 119 |
| 90 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { | 120 bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { |
| 91 ConstValueVector empty_args; | 121 ConstValueVector empty_args; |
| 92 return RunJavascriptFunction(function_name, empty_args); | 122 return RunJavascriptFunction(function_name, empty_args); |
| 93 } | 123 } |
| 94 | 124 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 WebUIJsInjectionReadyObserver injection_observer( | 243 WebUIJsInjectionReadyObserver injection_observer( |
| 214 web_contents, this, preload_test_fixture_, preload_test_name_); | 244 web_contents, this, preload_test_fixture_, preload_test_name_); |
| 215 content::TestNavigationObserver navigation_observer(web_contents); | 245 content::TestNavigationObserver navigation_observer(web_contents); |
| 216 chrome::NavigateParams params( | 246 chrome::NavigateParams params( |
| 217 browser(), GURL(browse_to), content::PAGE_TRANSITION_TYPED); | 247 browser(), GURL(browse_to), content::PAGE_TRANSITION_TYPED); |
| 218 params.disposition = CURRENT_TAB; | 248 params.disposition = CURRENT_TAB; |
| 219 chrome::Navigate(¶ms); | 249 chrome::Navigate(¶ms); |
| 220 navigation_observer.Wait(); | 250 navigation_observer.Wait(); |
| 221 } | 251 } |
| 222 | 252 |
| 253 void WebUIBrowserTest::BrowsePreloadAndWaitForMain(const GURL& browse_to) { |
| 254 content::WebContents* web_contents = |
| 255 browser()->tab_strip_model()->GetActiveWebContents(); |
| 256 WebUIMainObserver webui_main_observer(web_contents); |
| 257 BrowsePreload(browse_to); |
| 258 webui_main_observer.Wait(); |
| 259 } |
| 260 |
| 223 #if defined(ENABLE_FULL_PRINTING) | 261 #if defined(ENABLE_FULL_PRINTING) |
| 224 | 262 |
| 225 // This custom ContentBrowserClient is used to get notified when a WebContents | 263 // This custom ContentBrowserClient is used to get notified when a WebContents |
| 226 // for the print preview dialog gets created. | 264 // for the print preview dialog gets created. |
| 227 class PrintContentBrowserClient : public chrome::ChromeContentBrowserClient { | 265 class PrintContentBrowserClient : public chrome::ChromeContentBrowserClient { |
| 228 public: | 266 public: |
| 229 PrintContentBrowserClient(WebUIBrowserTest* browser_test, | 267 PrintContentBrowserClient(WebUIBrowserTest* browser_test, |
| 230 const std::string& preload_test_fixture, | 268 const std::string& preload_test_fixture, |
| 231 const std::string& preload_test_name) | 269 const std::string& preload_test_name) |
| 232 : browser_test_(browser_test), | 270 : browser_test_(browser_test), |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 } | 514 } |
| 477 | 515 |
| 478 GURL WebUIBrowserTest::WebUITestDataPathToURL( | 516 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
| 479 const base::FilePath::StringType& path) { | 517 const base::FilePath::StringType& path) { |
| 480 base::FilePath dir_test_data; | 518 base::FilePath dir_test_data; |
| 481 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | 519 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
| 482 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); | 520 base::FilePath test_path(dir_test_data.Append(kWebUITestFolder).Append(path)); |
| 483 EXPECT_TRUE(base::PathExists(test_path)); | 521 EXPECT_TRUE(base::PathExists(test_path)); |
| 484 return net::FilePathToFileURL(test_path); | 522 return net::FilePathToFileURL(test_path); |
| 485 } | 523 } |
| OLD | NEW |