| 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 #include "chrome/browser/ui/webui/web_ui_browsertest.h" | 4 #include "chrome/browser/ui/webui/web_ui_browsertest.h" |
| 5 | 5 |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/webui/chrome_web_ui.h" | 14 #include "chrome/browser/ui/webui/chrome_web_ui.h" |
| 15 #include "chrome/browser/ui/webui/test_chrome_web_ui_factory.h" | 15 #include "chrome/browser/ui/browser_navigator.h" |
| 16 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/test/test_tab_strip_model_observer.h" |
| 18 #include "chrome/test/ui_test_utils.h" | 19 #include "chrome/test/ui_test_utils.h" |
| 19 #include "content/browser/tab_contents/tab_contents.h" | 20 #include "content/browser/tab_contents/tab_contents.h" |
| 20 #include "testing/gtest/include/gtest/gtest-spi.h" | 21 #include "testing/gtest/include/gtest/gtest-spi.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 26 const FilePath::CharType kMockJS[] = FILE_PATH_LITERAL("mock4js.js"); |
| 25 const FilePath::CharType kWebUILibraryJS[] = FILE_PATH_LITERAL("test_api.js"); | 27 const FilePath::CharType kWebUILibraryJS[] = FILE_PATH_LITERAL("test_api.js"); |
| 26 const FilePath::CharType kWebUITestFolder[] = FILE_PATH_LITERAL("webui"); | 28 const FilePath::CharType kWebUITestFolder[] = FILE_PATH_LITERAL("webui"); |
| 27 base::LazyInstance<std::vector<std::string> > error_messages_( | 29 base::LazyInstance<std::vector<std::string> > error_messages_( |
| 28 base::LINKER_INITIALIZED); | 30 base::LINKER_INITIALIZED); |
| 29 | 31 |
| 30 // Intercepts all log messages. | 32 // Intercepts all log messages. |
| 31 bool LogHandler(int severity, | 33 bool LogHandler(int severity, |
| 32 const char* file, | 34 const char* file, |
| 33 int line, | 35 int line, |
| 34 size_t message_start, | 36 size_t message_start, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 62 args.push_back(&arg2); | 64 args.push_back(&arg2); |
| 63 return RunJavascriptFunction(function_name, args); | 65 return RunJavascriptFunction(function_name, args); |
| 64 } | 66 } |
| 65 | 67 |
| 66 bool WebUIBrowserTest::RunJavascriptFunction( | 68 bool WebUIBrowserTest::RunJavascriptFunction( |
| 67 const std::string& function_name, | 69 const std::string& function_name, |
| 68 const ConstValueVector& function_arguments) { | 70 const ConstValueVector& function_arguments) { |
| 69 return RunJavascriptUsingHandler(function_name, function_arguments, false); | 71 return RunJavascriptUsingHandler(function_name, function_arguments, false); |
| 70 } | 72 } |
| 71 | 73 |
| 74 bool WebUIBrowserTest::RunJavascriptTestF(const std::string& test_fixture, |
| 75 const std::string& test_name) { |
| 76 ConstValueVector args; |
| 77 args.push_back(Value::CreateStringValue(test_fixture)); |
| 78 args.push_back(Value::CreateStringValue(test_name)); |
| 79 return RunJavascriptTest("RUN_TEST_F", args); |
| 80 } |
| 81 |
| 72 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name) { | 82 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name) { |
| 73 return RunJavascriptTest(test_name, ConstValueVector()); | 83 return RunJavascriptTest(test_name, ConstValueVector()); |
| 74 } | 84 } |
| 75 | 85 |
| 76 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name, | 86 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name, |
| 77 const Value& arg) { | 87 const Value& arg) { |
| 78 ConstValueVector args; | 88 ConstValueVector args; |
| 79 args.push_back(&arg); | 89 args.push_back(&arg); |
| 80 return RunJavascriptTest(test_name, args); | 90 return RunJavascriptTest(test_name, args); |
| 81 } | 91 } |
| 82 | 92 |
| 83 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name, | 93 bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name, |
| 84 const Value& arg1, | 94 const Value& arg1, |
| 85 const Value& arg2) { | 95 const Value& arg2) { |
| 86 ConstValueVector args; | 96 ConstValueVector args; |
| 87 args.push_back(&arg1); | 97 args.push_back(&arg1); |
| 88 args.push_back(&arg2); | 98 args.push_back(&arg2); |
| 89 return RunJavascriptTest(test_name, args); | 99 return RunJavascriptTest(test_name, args); |
| 90 } | 100 } |
| 91 | 101 |
| 92 bool WebUIBrowserTest::RunJavascriptTest( | 102 bool WebUIBrowserTest::RunJavascriptTest( |
| 93 const std::string& test_name, | 103 const std::string& test_name, |
| 94 const ConstValueVector& test_arguments) { | 104 const ConstValueVector& test_arguments) { |
| 95 return RunJavascriptUsingHandler(test_name, test_arguments, true); | 105 return RunJavascriptUsingHandler(test_name, test_arguments, true); |
| 96 } | 106 } |
| 97 | 107 |
| 98 void WebUIBrowserTest::PreLoadJavascriptLibraries(bool override_chrome_send) { | 108 void WebUIBrowserTest::PreLoadJavascriptLibraries( |
| 109 const std::string& preload_test_fixture, |
| 110 const std::string& preload_test_name) { |
| 99 ASSERT_FALSE(libraries_preloaded_); | 111 ASSERT_FALSE(libraries_preloaded_); |
| 100 scoped_ptr<Value> override_chrome_send_arg( | 112 ConstValueVector args; |
| 101 Value::CreateBooleanValue(override_chrome_send)); | 113 args.push_back(Value::CreateStringValue(preload_test_fixture)); |
| 102 RunJavascriptFunction("preloadJavascriptLibraries", | 114 args.push_back(Value::CreateStringValue(preload_test_name)); |
| 103 *override_chrome_send_arg); | 115 RunJavascriptFunction("preloadJavascriptLibraries", args); |
| 104 libraries_preloaded_ = true; | 116 libraries_preloaded_ = true; |
| 105 } | 117 } |
| 106 | 118 |
| 107 WebUIBrowserTest::WebUIBrowserTest() | 119 WebUIBrowserTest::WebUIBrowserTest() |
| 108 : test_handler_(new WebUITestHandler()), | 120 : test_handler_(new WebUITestHandler()), |
| 109 libraries_preloaded_(false), | 121 libraries_preloaded_(false), |
| 110 skip_test_(false) {} | 122 skip_test_(false) {} |
| 111 | 123 |
| 124 void WebUIBrowserTest::BrowsePreload(const GURL& browse_to, |
| 125 const std::string& preload_test_fixture, |
| 126 const std::string& preload_test_name) { |
| 127 // Remember for callback OnLoadStart(). |
| 128 preload_test_fixture_ = preload_test_fixture; |
| 129 preload_test_name_ = preload_test_name; |
| 130 |
| 131 TestNavigationObserver navigation_observer( |
| 132 browser()->GetSelectedTabContentsWrapper(), this); |
| 133 browser::NavigateParams params( |
| 134 browser(), GURL(browse_to), PageTransition::TYPED); |
| 135 params.disposition = CURRENT_TAB; |
| 136 browser::Navigate(¶ms); |
| 137 navigation_observer.WaitForObservation(); |
| 138 } |
| 139 |
| 140 void WebUIBrowserTest::BrowsePrintPreload( |
| 141 const GURL& browse_to, |
| 142 const std::string& preload_test_fixture, |
| 143 const std::string& preload_test_name) { |
| 144 // Remember for callback OnLoadStart(). |
| 145 preload_test_fixture_ = preload_test_fixture; |
| 146 preload_test_name_ = preload_test_name; |
| 147 |
| 148 ui_test_utils::NavigateToURL(browser(), browse_to); |
| 149 |
| 150 TestTabStripModelObserver tabstrip_observer( |
| 151 browser()->tabstrip_model(), this); |
| 152 browser()->Print(); |
| 153 tabstrip_observer.WaitForObservation(); |
| 154 } |
| 155 |
| 112 void WebUIBrowserTest::SetUpInProcessBrowserTestFixture() { | 156 void WebUIBrowserTest::SetUpInProcessBrowserTestFixture() { |
| 113 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); | 157 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); |
| 114 test_data_directory_ = test_data_directory_.Append(kWebUITestFolder); | 158 test_data_directory_ = test_data_directory_.Append(kWebUITestFolder); |
| 115 | 159 |
| 116 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. | 160 // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. |
| 117 FilePath resources_pack_path; | 161 FilePath resources_pack_path; |
| 118 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); | 162 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); |
| 119 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); | 163 ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); |
| 120 | 164 |
| 165 FilePath mockPath; |
| 166 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath)); |
| 167 mockPath = mockPath.AppendASCII("chrome"); |
| 168 mockPath = mockPath.AppendASCII("third_party"); |
| 169 mockPath = mockPath.AppendASCII("mock4js"); |
| 170 mockPath = mockPath.Append(kMockJS); |
| 171 AddLibrary(mockPath); |
| 121 AddLibrary(FilePath(kWebUILibraryJS)); | 172 AddLibrary(FilePath(kWebUILibraryJS)); |
| 122 } | 173 } |
| 123 | 174 |
| 124 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { | 175 WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { |
| 125 return NULL; | 176 return NULL; |
| 126 } | 177 } |
| 127 | 178 |
| 128 void WebUIBrowserTest::skipTest(const std::string& skip_test_message) { | 179 void WebUIBrowserTest::skipTest(const std::string& skip_test_message) { |
| 129 skip_test_ = true; | 180 skip_test_ = true; |
| 130 skip_test_message_.assign(skip_test_message); | 181 skip_test_message_.assign(skip_test_message); |
| 131 } | 182 } |
| 132 | 183 |
| 133 GURL WebUIBrowserTest::WebUITestDataPathToURL( | 184 GURL WebUIBrowserTest::WebUITestDataPathToURL( |
| 134 const FilePath::StringType& path) { | 185 const FilePath::StringType& path) { |
| 135 FilePath dir_test_data; | 186 FilePath dir_test_data; |
| 136 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); | 187 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &dir_test_data)); |
| 137 FilePath test_path(dir_test_data.AppendASCII("webui")); | 188 FilePath test_path(dir_test_data.AppendASCII("webui")); |
| 138 test_path = test_path.Append(path); | 189 test_path = test_path.Append(path); |
| 139 EXPECT_TRUE(file_util::PathExists(test_path)); | 190 EXPECT_TRUE(file_util::PathExists(test_path)); |
| 140 return net::FilePathToFileURL(test_path); | 191 return net::FilePathToFileURL(test_path); |
| 141 } | 192 } |
| 142 | 193 |
| 194 // TestTabStripModelObserver::LoadStartObserver: |
| 195 void WebUIBrowserTest::OnLoadStart() OVERRIDE { |
| 196 PreLoadJavascriptLibraries(preload_test_fixture_, preload_test_name_); |
| 197 } |
| 198 |
| 143 void WebUIBrowserTest::BuildJavascriptLibraries(std::string* content) { | 199 void WebUIBrowserTest::BuildJavascriptLibraries(std::string* content) { |
| 144 ASSERT_TRUE(content != NULL); | 200 ASSERT_TRUE(content != NULL); |
| 145 std::string library_content, src_content; | |
| 146 | |
| 147 std::vector<FilePath>::iterator user_libraries_iterator; | 201 std::vector<FilePath>::iterator user_libraries_iterator; |
| 148 for (user_libraries_iterator = user_libraries_.begin(); | 202 for (user_libraries_iterator = user_libraries_.begin(); |
| 149 user_libraries_iterator != user_libraries_.end(); | 203 user_libraries_iterator != user_libraries_.end(); |
| 150 ++user_libraries_iterator) { | 204 ++user_libraries_iterator) { |
| 205 std::string library_content; |
| 151 if (user_libraries_iterator->IsAbsolute()) { | 206 if (user_libraries_iterator->IsAbsolute()) { |
| 152 ASSERT_TRUE(file_util::ReadFileToString(*user_libraries_iterator, | 207 ASSERT_TRUE(file_util::ReadFileToString(*user_libraries_iterator, |
| 153 &library_content)); | 208 &library_content)); |
| 154 } else { | 209 } else { |
| 155 ASSERT_TRUE(file_util::ReadFileToString( | 210 ASSERT_TRUE(file_util::ReadFileToString( |
| 156 test_data_directory_.Append(*user_libraries_iterator), | 211 test_data_directory_.Append(*user_libraries_iterator), |
| 157 &library_content)); | 212 &library_content)); |
| 158 } | 213 } |
| 159 content->append(library_content); | 214 content->append(library_content); |
| 160 content->append(";\n"); | 215 content->append(";\n"); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 test_handler_->Attach(web_ui_instance); | 279 test_handler_->Attach(web_ui_instance); |
| 225 | 280 |
| 226 if (GetMockMessageHandler()) | 281 if (GetMockMessageHandler()) |
| 227 GetMockMessageHandler()->Attach(web_ui_instance); | 282 GetMockMessageHandler()->Attach(web_ui_instance); |
| 228 } | 283 } |
| 229 | 284 |
| 230 void WebUIBrowserTest::AddLibrary(const FilePath& library_path) { | 285 void WebUIBrowserTest::AddLibrary(const FilePath& library_path) { |
| 231 user_libraries_.push_back(library_path); | 286 user_libraries_.push_back(library_path); |
| 232 } | 287 } |
| 233 | 288 |
| 234 IN_PROC_BROWSER_TEST_F(WebUIBrowserTest, TestSamplePass) { | |
| 235 AddLibrary(FilePath(FILE_PATH_LITERAL("sample_downloads.js"))); | |
| 236 | |
| 237 // Navigate to UI. | |
| 238 // TODO(dtseng): make accessor for subclasses to return? | |
| 239 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL)); | |
| 240 | |
| 241 ASSERT_TRUE(RunJavascriptTest("testAssertFalse")); | |
| 242 ASSERT_FALSE(RunJavascriptTest("FAILS_testAssertFalse")); | |
| 243 ASSERT_TRUE(RunJavascriptTest("testInitialFocus")); | |
| 244 ASSERT_FALSE(RunJavascriptTest("testConsoleError")); | |
| 245 } | |
| 246 | |
| 247 // According to the interface for EXPECT_FATAL_FAILURE | 289 // According to the interface for EXPECT_FATAL_FAILURE |
| 248 // (http://code.google.com/p/googletest/wiki/AdvancedGuide#Catching_Failures) | 290 // (http://code.google.com/p/googletest/wiki/AdvancedGuide#Catching_Failures) |
| 249 // the statement must be statically available. Therefore, we make a static | 291 // the statement must be statically available. Therefore, we make a static |
| 250 // global s_test_ which should point to |this| for the duration of the test run | 292 // global s_test_ which should point to |this| for the duration of the test run |
| 251 // and be cleared afterward. | 293 // and be cleared afterward. |
| 252 class WebUIBrowserExpectFailTest : public WebUIBrowserTest { | 294 class WebUIBrowserExpectFailTest : public WebUIBrowserTest { |
| 253 public: | 295 public: |
| 254 WebUIBrowserExpectFailTest() { | 296 WebUIBrowserExpectFailTest() { |
| 255 EXPECT_FALSE(s_test_); | 297 EXPECT_FALSE(s_test_); |
| 256 s_test_ = this; | 298 s_test_ = this; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 271 static WebUIBrowserTest* s_test_; | 313 static WebUIBrowserTest* s_test_; |
| 272 }; | 314 }; |
| 273 WebUIBrowserTest* WebUIBrowserExpectFailTest::s_test_ = NULL; | 315 WebUIBrowserTest* WebUIBrowserExpectFailTest::s_test_ = NULL; |
| 274 | 316 |
| 275 IN_PROC_BROWSER_TEST_F(WebUIBrowserExpectFailTest, TestFailsFast) { | 317 IN_PROC_BROWSER_TEST_F(WebUIBrowserExpectFailTest, TestFailsFast) { |
| 276 AddLibrary(FilePath(FILE_PATH_LITERAL("sample_downloads.js"))); | 318 AddLibrary(FilePath(FILE_PATH_LITERAL("sample_downloads.js"))); |
| 277 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL)); | 319 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL)); |
| 278 EXPECT_FATAL_FAILURE(RunJavascriptTestNoReturn("FAILS_BogusFunctionName"), | 320 EXPECT_FATAL_FAILURE(RunJavascriptTestNoReturn("FAILS_BogusFunctionName"), |
| 279 "WebUITestHandler::Observe"); | 321 "WebUITestHandler::Observe"); |
| 280 } | 322 } |
| 281 | |
| 282 // crbug.com/88104 - v8_shell#host doesn't build when host=="arm". | |
| 283 #if !defined(ARCH_CPU_ARM_FAMILY) | |
| 284 // This test framework is used in the generated tests, which are included | |
| 285 // below. WebUIBrowserTest requires being on a page which is a WebUI page. Using | |
| 286 // the TestChromeWebUIFactory, we use a dummy URL |kChromeTestBrowserTestPass|, | |
| 287 // which we force to be a WebUI page. | |
| 288 class WebUIBrowserTestPass | |
| 289 : public WebUIBrowserTest, | |
| 290 public TestChromeWebUIFactory::WebUIProvider { | |
| 291 private: | |
| 292 // TestChromeWebUIFactory::WebUIProvider: | |
| 293 virtual WebUI* NewWebUI(TabContents* tab_contents, | |
| 294 const GURL& url) OVERRIDE { | |
| 295 return new ChromeWebUI(tab_contents); | |
| 296 } | |
| 297 | |
| 298 // InProcessBrowserTest: | |
| 299 virtual void SetUpOnMainThread() OVERRIDE { | |
| 300 WebUIBrowserTest::SetUpOnMainThread(); | |
| 301 ui_test_utils::NavigateToURL(browser(), | |
| 302 GURL(kChromeTestBrowserTestPass)); | |
| 303 } | |
| 304 | |
| 305 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
| 306 WebUIBrowserTest::SetUpInProcessBrowserTestFixture(); | |
| 307 TestChromeWebUIFactory::AddFactoryOverride( | |
| 308 GURL(kChromeTestBrowserTestPass).host(), this); | |
| 309 } | |
| 310 | |
| 311 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { | |
| 312 WebUIBrowserTest::TearDownInProcessBrowserTestFixture(); | |
| 313 TestChromeWebUIFactory::RemoveFactoryOverride( | |
| 314 GURL(kChromeTestBrowserTestPass).host()); | |
| 315 } | |
| 316 | |
| 317 static const char kChromeTestBrowserTestPass[]; | |
| 318 }; | |
| 319 | |
| 320 const char WebUIBrowserTestPass::kChromeTestBrowserTestPass[] = | |
| 321 "chrome://WebUIBrowserTestPass"; | |
| 322 | |
| 323 #include "js2webui/chrome/test/data/webui/sample_pass-inl.h" | |
| 324 | |
| 325 #endif // !defined(ARCH_CPU_ARM_FAMILY) | |
| OLD | NEW |