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