OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_browsertest.h" | 5 #include "chrome/test/base/web_ui_browsertest.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 17 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/browser/ui/browser_commands.h" | 20 #include "chrome/browser/ui/browser_commands.h" |
21 #include "chrome/browser/ui/browser_navigator.h" | 21 #include "chrome/browser/ui/browser_navigator.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
23 #include "chrome/browser/ui/webui/web_ui_test_handler.h" | 23 #include "chrome/browser/ui/webui/web_ui_test_handler.h" |
24 #include "chrome/common/chrome_paths.h" | 24 #include "chrome/common/chrome_paths.h" |
25 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
26 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h" | 26 #include "chrome/test/base/test_chrome_web_ui_controller_factory.h" |
27 #include "chrome/test/base/test_tab_strip_model_observer.h" | 27 #include "chrome/test/base/test_tab_strip_model_observer.h" |
28 #include "chrome/test/base/ui_test_utils.h" | 28 #include "chrome/test/base/ui_test_utils.h" |
29 #include "content/public/browser/navigation_controller.h" | 29 #include "content/public/browser/navigation_controller.h" |
| 30 #include "content/public/browser/notification_observer.h" |
| 31 #include "content/public/browser/notification_registrar.h" |
| 32 #include "content/public/browser/notification_service.h" |
| 33 #include "content/public/browser/notification_types.h" |
| 34 #include "content/public/browser/render_view_host_observer.h" |
30 #include "content/public/browser/url_data_source.h" | 35 #include "content/public/browser/url_data_source.h" |
31 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
32 #include "content/public/browser/web_ui_controller.h" | 37 #include "content/public/browser/web_ui_controller.h" |
33 #include "content/public/browser/web_ui_message_handler.h" | 38 #include "content/public/browser/web_ui_message_handler.h" |
34 #include "net/base/net_util.h" | 39 #include "net/base/net_util.h" |
35 #include "testing/gmock/include/gmock/gmock.h" | 40 #include "testing/gmock/include/gmock/gmock.h" |
36 #include "testing/gtest/include/gtest/gtest-spi.h" | 41 #include "testing/gtest/include/gtest/gtest-spi.h" |
37 #include "ui/base/resource/resource_bundle.h" | 42 #include "ui/base/resource/resource_bundle.h" |
38 #include "ui/base/resource/resource_handle.h" | 43 #include "ui/base/resource/resource_handle.h" |
39 | 44 |
(...skipping 23 matching lines...) Expand all Loading... |
63 const std::string& str) { | 68 const std::string& str) { |
64 if (severity == logging::LOG_ERROR && | 69 if (severity == logging::LOG_ERROR && |
65 file && | 70 file && |
66 std::string("CONSOLE") == file) { | 71 std::string("CONSOLE") == file) { |
67 error_messages_.Get().push_back(str); | 72 error_messages_.Get().push_back(str); |
68 } | 73 } |
69 | 74 |
70 return false; | 75 return false; |
71 } | 76 } |
72 | 77 |
| 78 class RenderViewHostInitializedObserver |
| 79 : public content::RenderViewHostObserver { |
| 80 public: |
| 81 RenderViewHostInitializedObserver(content::RenderViewHost* render_view_host, |
| 82 content::JsInjectionReadyObserver* observer) |
| 83 : content::RenderViewHostObserver(render_view_host), |
| 84 injection_observer_(observer) { |
| 85 } |
| 86 |
| 87 // content::RenderViewHostObserver: |
| 88 virtual void RenderViewHostInitialized() OVERRIDE { |
| 89 injection_observer_->OnJsInjectionReady(render_view_host()); |
| 90 } |
| 91 |
| 92 private: |
| 93 content::JsInjectionReadyObserver* injection_observer_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(RenderViewHostInitializedObserver); |
| 96 }; |
| 97 |
| 98 class WebUIJsInjectionReadyObserver : public content::NotificationObserver { |
| 99 public: |
| 100 explicit WebUIJsInjectionReadyObserver( |
| 101 content::JsInjectionReadyObserver* observer) |
| 102 : injection_observer_(observer) { |
| 103 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_CREATED, |
| 104 content::NotificationService::AllSources()); |
| 105 } |
| 106 |
| 107 // content::NotificationObserver: |
| 108 virtual void Observe(int type, |
| 109 const content::NotificationSource& source, |
| 110 const content::NotificationDetails& details) OVERRIDE { |
| 111 DCHECK_EQ(content::NOTIFICATION_RENDER_VIEW_HOST_CREATED, type); |
| 112 |
| 113 rvh_observer_.reset(new RenderViewHostInitializedObserver( |
| 114 content::Source<content::RenderViewHost>(source).ptr(), |
| 115 injection_observer_)); |
| 116 } |
| 117 |
| 118 private: |
| 119 content::JsInjectionReadyObserver* injection_observer_; |
| 120 |
| 121 scoped_ptr<RenderViewHostInitializedObserver> rvh_observer_; |
| 122 |
| 123 content::NotificationRegistrar registrar_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(WebUIJsInjectionReadyObserver); |
| 126 }; |
| 127 |
73 } // namespace | 128 } // namespace |
74 | 129 |
75 WebUIBrowserTest::~WebUIBrowserTest() {} | 130 WebUIBrowserTest::~WebUIBrowserTest() {} |
76 | 131 |
77 void WebUIBrowserTest::AddLibrary(const base::FilePath& library_path) { | 132 void WebUIBrowserTest::AddLibrary(const base::FilePath& library_path) { |
78 user_libraries_.push_back(library_path); | 133 user_libraries_.push_back(library_path); |
79 } | 134 } |
80 | 135 |
81 // Add a helper JS library to the given WebUIBrowserTest from a path relative to | 136 // Add a helper JS library to the given WebUIBrowserTest from a path relative to |
82 // base::DIR_SOURCE_ROOT. | 137 // base::DIR_SOURCE_ROOT. |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 ASSERT_FALSE(libraries_preloaded_); | 258 ASSERT_FALSE(libraries_preloaded_); |
204 ConstValueVector args; | 259 ConstValueVector args; |
205 args.push_back(new base::StringValue(preload_test_fixture)); | 260 args.push_back(new base::StringValue(preload_test_fixture)); |
206 args.push_back(new base::StringValue(preload_test_name)); | 261 args.push_back(new base::StringValue(preload_test_name)); |
207 RunJavascriptUsingHandler( | 262 RunJavascriptUsingHandler( |
208 "preloadJavascriptLibraries", args, false, false, preload_host); | 263 "preloadJavascriptLibraries", args, false, false, preload_host); |
209 libraries_preloaded_ = true; | 264 libraries_preloaded_ = true; |
210 } | 265 } |
211 | 266 |
212 void WebUIBrowserTest::BrowsePreload(const GURL& browse_to) { | 267 void WebUIBrowserTest::BrowsePreload(const GURL& browse_to) { |
| 268 WebUIJsInjectionReadyObserver injection_observer(this); |
213 content::TestNavigationObserver navigation_observer( | 269 content::TestNavigationObserver navigation_observer( |
214 content::Source<NavigationController>( | 270 content::Source<NavigationController>( |
215 &browser()->tab_strip_model()-> | 271 &browser()->tab_strip_model()-> |
216 GetActiveWebContents()->GetController()), | 272 GetActiveWebContents()->GetController()), |
217 this, 1); | 273 1); |
218 chrome::NavigateParams params(browser(), GURL(browse_to), | 274 chrome::NavigateParams params(browser(), GURL(browse_to), |
219 content::PAGE_TRANSITION_TYPED); | 275 content::PAGE_TRANSITION_TYPED); |
220 params.disposition = CURRENT_TAB; | 276 params.disposition = CURRENT_TAB; |
221 chrome::Navigate(¶ms); | 277 chrome::Navigate(¶ms); |
222 navigation_observer.WaitForObservation( | 278 navigation_observer.WaitForObservation( |
223 base::Bind(&content::RunMessageLoop), | 279 base::Bind(&content::RunMessageLoop), |
224 base::Bind(&MessageLoop::Quit, | 280 base::Bind(&MessageLoop::Quit, |
225 base::Unretained(MessageLoopForUI::current()))); | 281 base::Unretained(MessageLoopForUI::current()))); |
226 } | 282 } |
227 | 283 |
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 // testDone directly and expect pass result. | 771 // testDone directly and expect pass result. |
716 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { | 772 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPassesAsync) { |
717 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); | 773 ASSERT_TRUE(RunJavascriptAsyncTest("testDone")); |
718 } | 774 } |
719 | 775 |
720 // Test that calling testDone during RunJavascriptTest still completes when | 776 // Test that calling testDone during RunJavascriptTest still completes when |
721 // waiting for async result. | 777 // waiting for async result. |
722 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { | 778 IN_PROC_BROWSER_TEST_F(WebUIBrowserAsyncTest, TestTestDoneEarlyPasses) { |
723 ASSERT_TRUE(RunJavascriptTest("testDone")); | 779 ASSERT_TRUE(RunJavascriptTest("testDone")); |
724 } | 780 } |
OLD | NEW |