Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/media/webrtc/webrtc_browsertest_common.h" | 5 #include "chrome/browser/media/webrtc/webrtc_browsertest_common.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 const std::string javascript = base::StringPrintf( | 112 const std::string javascript = base::StringPrintf( |
| 113 "setTimeout(function() {" | 113 "setTimeout(function() {" |
| 114 " window.domAutomationController.send('sleep-ok');" | 114 " window.domAutomationController.send('sleep-ok');" |
| 115 "}, %d)", timeout_msec); | 115 "}, %d)", timeout_msec); |
| 116 | 116 |
| 117 std::string result; | 117 std::string result; |
| 118 bool ok = content::ExecuteScriptAndExtractString( | 118 bool ok = content::ExecuteScriptAndExtractString( |
| 119 tab_contents, javascript, &result); | 119 tab_contents, javascript, &result); |
| 120 return ok && result == "sleep-ok"; | 120 return ok && result == "sleep-ok"; |
| 121 } | 121 } |
| 122 | |
| 123 bool PollingWaitUntil(const std::string& javascript, | 122 bool PollingWaitUntil(const std::string& javascript, |
| 124 const std::string& evaluates_to, | 123 const std::string& evaluates_to, |
| 125 content::WebContents* tab_contents) { | 124 content::WebContents* tab_contents) { |
| 126 return PollingWaitUntil(javascript, evaluates_to, tab_contents, | 125 return PollingWaitUntil(javascript, evaluates_to, tab_contents, |
| 127 kDefaultPollIntervalMsec); | 126 kDefaultPollIntervalMsec); |
| 128 } | 127 } |
| 129 | 128 |
| 130 bool PollingWaitUntil(const std::string& javascript, | 129 bool PollingWaitUntil(const std::string& javascript, |
| 131 const std::string& evaluates_to, | 130 const std::string& evaluates_to, |
| 132 content::WebContents* tab_contents, | 131 content::WebContents* tab_contents, |
| 133 int poll_interval_msec) { | 132 int poll_interval_msec) { |
| 134 base::Time start_time = base::Time::Now(); | 133 base::Time start_time = base::Time::Now(); |
| 135 base::TimeDelta timeout = TestTimeouts::action_max_timeout(); | 134 base::TimeDelta timeout = TestTimeouts::action_max_timeout(); |
| 136 std::string result; | 135 std::string result; |
| 137 | 136 |
| 138 while (base::Time::Now() - start_time < timeout) { | 137 while (base::Time::Now() - start_time < timeout) { |
| 139 std::string result; | |
| 140 if (!content::ExecuteScriptAndExtractString(tab_contents, javascript, | 138 if (!content::ExecuteScriptAndExtractString(tab_contents, javascript, |
| 141 &result)) { | 139 &result)) { |
| 142 LOG(ERROR) << "Failed to execute javascript " << javascript; | 140 LOG(ERROR) << "Failed to execute javascript " << javascript; |
| 143 return false; | 141 return false; |
| 144 } | 142 } |
| 145 | 143 |
| 146 if (evaluates_to == result) { | 144 if (evaluates_to == result) { |
| 147 return true; | 145 return true; |
| 148 } else if (IsErrorResult(result)) { | 146 } else if (IsErrorResult(result)) { |
| 149 LOG(ERROR) << "|" << javascript << "| returned an error: " << result; | 147 LOG(ERROR) << "|" << javascript << "| returned an error: " << result; |
| 150 return false; | 148 return false; |
| 151 } | 149 } |
| 152 | 150 |
| 153 // Sleep a bit here to keep this loop from spinlocking too badly. | 151 // Sleep a bit here to keep this loop from spinlocking too badly. |
| 154 if (!SleepInJavascript(tab_contents, poll_interval_msec)) { | 152 if (!SleepInJavascript(tab_contents, poll_interval_msec)) { |
| 155 // TODO(phoglund): Figure out why this fails every now and then. | 153 // TODO(phoglund): Figure out why this fails every now and then. |
| 156 // It's not a huge deal if it does though. | 154 // It's not a huge deal if it does though. |
| 157 LOG(ERROR) << "Failed to sleep."; | 155 LOG(ERROR) << "Failed to sleep."; |
| 158 } | 156 } |
| 159 } | 157 } |
| 160 LOG(ERROR) | 158 LOG(ERROR) |
| 161 << "Timed out while waiting for " << javascript | 159 << "Timed out while waiting for " << javascript |
| 162 << " to evaluate to " << evaluates_to << "; last result was '" << result | 160 << " to evaluate to " << evaluates_to << "; last result was '" << result |
| 163 << "'"; | 161 << "'"; |
| 164 return false; | 162 return false; |
| 165 } | 163 } |
| 166 | 164 |
| 165 bool PollingWaitUntil(const std::string& javascript, | |
| 166 content::WebContents* tab_contents, | |
| 167 int poll_interval_msec) { | |
| 168 base::Time start_time = base::Time::Now(); | |
| 169 base::TimeDelta timeout = TestTimeouts::action_max_timeout(); | |
| 170 bool result; | |
| 171 | |
| 172 while (base::Time::Now() - start_time < timeout) { | |
| 173 if (!content::ExecuteScriptAndExtractBool(tab_contents, javascript, | |
| 174 &result)) { | |
| 175 LOG(ERROR) << "Failed to execute javascript " << javascript; | |
| 176 return false; | |
| 177 } | |
| 178 | |
| 179 if (result) { | |
| 180 return true; | |
| 181 } | |
| 182 | |
| 183 // Sleep a bit here to keep this loop from spinlocking too badly. | |
| 184 if (!SleepInJavascript(tab_contents, poll_interval_msec)) { | |
| 185 // TODO(phoglund): Figure out why this fails every now and then. | |
| 186 // It's not a huge deal if it does though. | |
| 187 LOG(ERROR) << "Failed to sleep."; | |
| 188 } | |
| 189 } | |
| 190 LOG(ERROR) << "Timed out while waiting for " << javascript; | |
| 191 return false; | |
| 192 } | |
|
mcasas
2017/03/22 17:54:14
This code is very similar to l.129-163. Either ref
| |
| 193 | |
| 167 } // namespace test | 194 } // namespace test |
| OLD | NEW |