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