| 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 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/file_path.h" | 6 #include "base/file_path.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/string_number_conversions.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/test/automation/tab_proxy.h" | 14 #include "chrome/test/automation/tab_proxy.h" |
| 14 #include "chrome/test/ui/ui_test.h" | 15 #include "chrome/test/ui/ui_test.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Provides a UI Test that lets us take the browser to a url, and | 19 // Provides a UI Test that lets us take the browser to a url, and |
| 19 // wait for a cookie value to be set before closing the page. | 20 // wait for a cookie value to be set or a JavaScript expression to evaluate |
| 21 // true before closing the page. It is undefined what happens if you specify |
| 22 // both a cookie and a JS expression. |
| 20 class UrlFetchTest : public UITest { | 23 class UrlFetchTest : public UITest { |
| 21 public: | 24 public: |
| 22 UrlFetchTest() { | 25 UrlFetchTest() { |
| 23 show_window_ = true; | 26 show_window_ = true; |
| 24 dom_automation_enabled_ = true; | 27 dom_automation_enabled_ = true; |
| 25 } | 28 } |
| 26 struct UrlFetchTestResult { | 29 struct UrlFetchTestResult { |
| 27 std::string cookie_value; | 30 std::string cookie_value; |
| 28 std::string javascript_variable; | 31 std::string javascript_variable; |
| 29 }; | 32 }; |
| 30 | 33 |
| 31 void SetUp() { | 34 void SetUp() { |
| 32 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 35 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 33 if (cmd_line->HasSwitch("reference_build")) { | 36 if (cmd_line->HasSwitch("reference_build")) { |
| 34 FilePath dir; | 37 FilePath dir; |
| 35 PathService::Get(chrome::DIR_TEST_TOOLS, &dir); | 38 PathService::Get(chrome::DIR_TEST_TOOLS, &dir); |
| 36 dir = dir.AppendASCII("reference_build"); | 39 dir = dir.AppendASCII("reference_build"); |
| 37 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
| 38 dir = dir.AppendASCII("chrome"); | 41 dir = dir.AppendASCII("chrome"); |
| 39 #elif defined(OS_LINUX) | 42 #elif defined(OS_LINUX) |
| 40 dir = dir.AppendASCII("chrome_linux"); | 43 dir = dir.AppendASCII("chrome_linux"); |
| 41 #elif defined(OS_MACOSX) | 44 #elif defined(OS_MACOSX) |
| 42 dir = dir.AppendASCII("chrome_mac"); | 45 dir = dir.AppendASCII("chrome_mac"); |
| 43 #endif | 46 #endif |
| 44 browser_directory_ = dir; | 47 browser_directory_ = dir; |
| 45 } | 48 } |
| 46 UITest::SetUp(); | 49 UITest::SetUp(); |
| 47 } | 50 } |
| 48 | 51 |
| 49 void RunTest(const GURL& url, const char* wait_cookie_name, | 52 void RunTest(const GURL& url, |
| 50 const char* wait_cookie_value, const char* var_to_fetch, | 53 const char* wait_cookie_name, |
| 54 const char* wait_cookie_value, |
| 55 const char* var_to_fetch, |
| 56 const std::wstring& wait_js_expr, |
| 57 const std::wstring& wait_js_frame_xpath, |
| 58 int wait_js_timeout_ms, |
| 51 UrlFetchTestResult* result) { | 59 UrlFetchTestResult* result) { |
| 52 scoped_refptr<TabProxy> tab(GetActiveTab()); | 60 scoped_refptr<TabProxy> tab(GetActiveTab()); |
| 53 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url)); | 61 ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURL(url)); |
| 54 | 62 |
| 55 if (wait_cookie_name) { | 63 if (wait_cookie_name) { |
| 56 if (wait_cookie_value) { | 64 if (wait_cookie_value) { |
| 57 bool completed = WaitUntilCookieValue( | 65 bool completed = WaitUntilCookieValue( |
| 58 tab.get(), url, wait_cookie_name, | 66 tab.get(), url, wait_cookie_name, |
| 59 TestTimeouts::huge_test_timeout_ms(), | 67 TestTimeouts::huge_test_timeout_ms(), |
| 60 wait_cookie_value); | 68 wait_cookie_value); |
| 61 ASSERT_TRUE(completed); | 69 ASSERT_TRUE(completed); |
| 62 } else { | 70 } else { |
| 63 result->cookie_value = WaitUntilCookieNonEmpty( | 71 result->cookie_value = WaitUntilCookieNonEmpty( |
| 64 tab.get(), url, wait_cookie_name, | 72 tab.get(), url, wait_cookie_name, |
| 65 TestTimeouts::huge_test_timeout_ms()); | 73 TestTimeouts::huge_test_timeout_ms()); |
| 66 ASSERT_TRUE(result->cookie_value.length()); | 74 ASSERT_TRUE(result->cookie_value.length()); |
| 67 } | 75 } |
| 76 } else if (!wait_js_expr.empty()) { |
| 77 bool completed = WaitUntilJavaScriptCondition(tab.get(), |
| 78 wait_js_frame_xpath, |
| 79 wait_js_expr, |
| 80 wait_js_timeout_ms); |
| 81 ASSERT_TRUE(completed); |
| 68 } | 82 } |
| 69 if (var_to_fetch) { | 83 if (var_to_fetch) { |
| 70 std::string script = StringPrintf( | 84 std::string script = StringPrintf( |
| 71 "window.domAutomationController.send(%s);", var_to_fetch); | 85 "window.domAutomationController.send(%s);", var_to_fetch); |
| 72 | 86 |
| 73 std::wstring value; | 87 std::wstring value; |
| 74 bool success = tab->ExecuteAndExtractString(L"", ASCIIToWide(script), | 88 bool success = tab->ExecuteAndExtractString(L"", ASCIIToWide(script), |
| 75 &value); | 89 &value); |
| 76 ASSERT_TRUE(success); | 90 ASSERT_TRUE(success); |
| 77 result->javascript_variable = WideToUTF8(value); | 91 result->javascript_variable = WideToUTF8(value); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 95 // Waits for a cookie named <name> to be set before exiting successfully. | 109 // Waits for a cookie named <name> to be set before exiting successfully. |
| 96 // | 110 // |
| 97 // --wait_cookie_value=<value> | 111 // --wait_cookie_value=<value> |
| 98 // In conjunction with --wait_cookie_name, this waits for a specific value | 112 // In conjunction with --wait_cookie_name, this waits for a specific value |
| 99 // to be set. (Incompatible with --wait_cookie_output) | 113 // to be set. (Incompatible with --wait_cookie_output) |
| 100 // | 114 // |
| 101 // --wait_cookie_output=<filepath> | 115 // --wait_cookie_output=<filepath> |
| 102 // In conjunction with --wait_cookie_name, this saves the cookie value to | 116 // In conjunction with --wait_cookie_name, this saves the cookie value to |
| 103 // a file at the given path. (Incompatible with --wait_cookie_value) | 117 // a file at the given path. (Incompatible with --wait_cookie_value) |
| 104 // | 118 // |
| 119 // --wait_js_expr=<jscript_expr> |
| 120 // Waits for a javascript expression to evaluate true before exiting |
| 121 // successfully. |
| 122 // |
| 123 // --wait_js_timeout=<timeout_ms> |
| 124 // In conjunction with --wait_js_condition, this sets the timeout in ms |
| 125 // that we are prepared to wait. If this timeout is exceeded, we will exit |
| 126 // with failure. Note that a timeout greater than the gtest timeout will not |
| 127 // be honored. |
| 128 // |
| 129 // --wait_js_frame_xpath=<xpath> |
| 130 // In conjuction with --wait_js_condition, the JavaScript expression is |
| 131 // executed in the context of the frame that matches the provided xpath. |
| 132 // If this is not specified (or empty string), then the main frame is used. |
| 133 // |
| 105 // --jsvar=<name> | 134 // --jsvar=<name> |
| 106 // At the end of the test, fetch the named javascript variable from the page. | 135 // At the end of the test, fetch the named javascript variable from the page. |
| 107 // | 136 // |
| 108 // --jsvar_output=<filepath> | 137 // --jsvar_output=<filepath> |
| 109 // Write the value of the variable named by '--jsvar' to a file at the given | 138 // Write the value of the variable named by '--jsvar' to a file at the given |
| 110 // path. | 139 // path. |
| 111 // | 140 // |
| 112 // --reference_build | 141 // --reference_build |
| 113 // Use the reference build of chrome for the test. | 142 // Use the reference build of chrome for the test. |
| 114 TEST_F(UrlFetchTest, UrlFetch) { | 143 TEST_F(UrlFetchTest, UrlFetch) { |
| 115 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 144 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 116 | 145 |
| 117 if (!cmd_line->HasSwitch("url")) | 146 if (!cmd_line->HasSwitch("url")) |
| 118 return; | 147 return; |
| 119 | 148 |
| 120 std::string cookie_name = | 149 std::string cookie_name = |
| 121 cmd_line->GetSwitchValueASCII("wait_cookie_name"); | 150 cmd_line->GetSwitchValueASCII("wait_cookie_name"); |
| 122 std::string cookie_value = | 151 std::string cookie_value = |
| 123 cmd_line->GetSwitchValueASCII("wait_cookie_value"); | 152 cmd_line->GetSwitchValueASCII("wait_cookie_value"); |
| 153 std::wstring js_expr = |
| 154 UTF8ToWide(cmd_line->GetSwitchValueASCII("wait_js_expr")); |
| 155 std::wstring js_frame_xpath = |
| 156 UTF8ToWide(cmd_line->GetSwitchValueASCII("wait_js_frame_xpath")); |
| 157 std::string js_timeout_ms_str = |
| 158 cmd_line->GetSwitchValueASCII("wait_js_timeout"); |
| 124 | 159 |
| 125 std::string jsvar = cmd_line->GetSwitchValueASCII("jsvar"); | 160 std::string jsvar = cmd_line->GetSwitchValueASCII("jsvar"); |
| 161 int js_timeout_ms = -1; // no timeout, wait forever |
| 162 |
| 163 if (!js_timeout_ms_str.empty()) |
| 164 base::StringToInt(js_timeout_ms_str, &js_timeout_ms); |
| 126 | 165 |
| 127 UrlFetchTestResult result; | 166 UrlFetchTestResult result; |
| 128 RunTest(GURL(cmd_line->GetSwitchValueASCII("url")), | 167 RunTest(GURL(cmd_line->GetSwitchValueASCII("url")), |
| 129 cookie_name.length() > 0 ? cookie_name.c_str() : NULL, | 168 cookie_name.length() > 0 ? cookie_name.c_str() : NULL, |
| 130 cookie_value.length() > 0 ? cookie_value.c_str() : NULL, | 169 cookie_value.length() > 0 ? cookie_value.c_str() : NULL, |
| 131 jsvar.length() > 0 ? jsvar.c_str() : NULL, | 170 jsvar.length() > 0 ? jsvar.c_str() : NULL, |
| 171 js_expr, |
| 172 js_frame_xpath, |
| 173 js_timeout_ms, |
| 132 &result); | 174 &result); |
| 133 | 175 |
| 134 // Write out the cookie if requested | 176 // Write out the cookie if requested |
| 135 FilePath cookie_output_path = | 177 FilePath cookie_output_path = |
| 136 cmd_line->GetSwitchValuePath("wait_cookie_output"); | 178 cmd_line->GetSwitchValuePath("wait_cookie_output"); |
| 137 if (cookie_output_path.value().size() > 0) { | 179 if (cookie_output_path.value().size() > 0) { |
| 138 ASSERT_TRUE(WriteValueToFile(result.cookie_value, cookie_output_path)); | 180 ASSERT_TRUE(WriteValueToFile(result.cookie_value, cookie_output_path)); |
| 139 } | 181 } |
| 140 | 182 |
| 141 // Write out the JS Variable if requested | 183 // Write out the JS Variable if requested |
| 142 FilePath jsvar_output_path = cmd_line->GetSwitchValuePath("jsvar_output"); | 184 FilePath jsvar_output_path = cmd_line->GetSwitchValuePath("jsvar_output"); |
| 143 if (jsvar_output_path.value().size() > 0) { | 185 if (jsvar_output_path.value().size() > 0) { |
| 144 ASSERT_TRUE(WriteValueToFile(result.javascript_variable, | 186 ASSERT_TRUE(WriteValueToFile(result.javascript_variable, |
| 145 jsvar_output_path)); | 187 jsvar_output_path)); |
| 146 } | 188 } |
| 147 } | 189 } |
| 148 | 190 |
| 149 } // namespace | 191 } // namespace |
| OLD | NEW |