[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / media / webrtc_browsertest_common.cc
blobeb2e0ce87fddcff7f1389078c44d64e365a20261
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
3 // found in the LICENSE file.
5 #include "chrome/browser/media/webrtc_browsertest_common.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "base/process/launch.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/test/test_timeouts.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser_tabstrip.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "content/public/test/browser_test_utils.h"
19 namespace test {
21 // Relative to the chrome/test/data directory.
22 const base::FilePath::CharType kReferenceFilesDirName[] =
23 FILE_PATH_LITERAL("webrtc/resources");
24 const base::FilePath::CharType kReferenceFileName360p[] =
25 FILE_PATH_LITERAL("reference_video_640x360_30fps");
26 const base::FilePath::CharType kReferenceFileName720p[] =
27 FILE_PATH_LITERAL("reference_video_1280x720_30fps");
28 const base::FilePath::CharType kYuvFileExtension[] = FILE_PATH_LITERAL("yuv");
29 const base::FilePath::CharType kY4mFileExtension[] = FILE_PATH_LITERAL("y4m");
31 // This message describes how to modify your .gclient to get the reference
32 // video files downloaded for you.
33 static const char kAdviseOnGclientSolution[] =
34 "You need to add this solution to your .gclient to run this test:\n"
35 "{\n"
36 " \"name\" : \"webrtc.DEPS\",\n"
37 " \"url\" : \"svn://svn.chromium.org/chrome/trunk/deps/"
38 "third_party/webrtc/webrtc.DEPS\",\n"
39 "}";
41 const int kDefaultPollIntervalMsec = 250;
43 base::FilePath GetReferenceFilesDir() {
44 base::FilePath test_data_dir;
45 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
47 return test_data_dir.Append(kReferenceFilesDirName);
50 bool HasReferenceFilesInCheckout() {
51 if (!base::PathExists(GetReferenceFilesDir())) {
52 LOG(ERROR)
53 << "Cannot find the working directory for the reference video "
54 << "files, expected at " << GetReferenceFilesDir().value() << ". " <<
55 kAdviseOnGclientSolution;
56 return false;
58 return HasYuvAndY4mFile(test::kReferenceFileName360p) &&
59 HasYuvAndY4mFile(test::kReferenceFileName720p);
62 bool HasYuvAndY4mFile(const base::FilePath::CharType* reference_file) {
63 base::FilePath webrtc_reference_video_yuv = GetReferenceFilesDir()
64 .Append(reference_file).AddExtension(kYuvFileExtension);
65 if (!base::PathExists(webrtc_reference_video_yuv)) {
66 LOG(ERROR)
67 << "Missing YUV reference video to be used for quality"
68 << " comparison, expected at " << webrtc_reference_video_yuv.value()
69 << ". " << kAdviseOnGclientSolution;
70 return false;
73 base::FilePath webrtc_reference_video_y4m = GetReferenceFilesDir()
74 .Append(reference_file).AddExtension(kY4mFileExtension);
75 if (!base::PathExists(webrtc_reference_video_y4m)) {
76 LOG(ERROR)
77 << "Missing Y4M reference video to be used for quality"
78 << " comparison, expected at "<< webrtc_reference_video_y4m.value()
79 << ". " << kAdviseOnGclientSolution;
80 return false;
82 return true;
85 bool SleepInJavascript(content::WebContents* tab_contents, int timeout_msec) {
86 const std::string javascript = base::StringPrintf(
87 "setTimeout(function() {"
88 " window.domAutomationController.send('sleep-ok');"
89 "}, %d)", timeout_msec);
91 std::string result;
92 bool ok = content::ExecuteScriptAndExtractString(
93 tab_contents, javascript, &result);
94 return ok && result == "sleep-ok";
97 bool PollingWaitUntil(const std::string& javascript,
98 const std::string& evaluates_to,
99 content::WebContents* tab_contents) {
100 return PollingWaitUntil(javascript, evaluates_to, tab_contents,
101 kDefaultPollIntervalMsec);
104 bool PollingWaitUntil(const std::string& javascript,
105 const std::string& evaluates_to,
106 content::WebContents* tab_contents,
107 int poll_interval_msec) {
108 base::Time start_time = base::Time::Now();
109 base::TimeDelta timeout = TestTimeouts::action_max_timeout();
110 std::string result;
112 while (base::Time::Now() - start_time < timeout) {
113 std::string result;
114 if (!content::ExecuteScriptAndExtractString(tab_contents, javascript,
115 &result)) {
116 LOG(ERROR) << "Failed to execute javascript " << javascript;
117 return false;
120 if (evaluates_to == result)
121 return true;
123 // Sleep a bit here to keep this loop from spinlocking too badly.
124 if (!SleepInJavascript(tab_contents, poll_interval_msec)) {
125 // TODO(phoglund): Figure out why this fails every now and then.
126 // It's not a huge deal if it does though.
127 LOG(ERROR) << "Failed to sleep.";
130 LOG(ERROR)
131 << "Timed out while waiting for " << javascript
132 << " to evaluate to " << evaluates_to << "; last result was '" << result
133 << "'";
134 return false;
137 static base::FilePath::CharType kServerExecutable[] =
138 #if defined(OS_WIN)
139 FILE_PATH_LITERAL("peerconnection_server.exe");
140 #else
141 FILE_PATH_LITERAL("peerconnection_server");
142 #endif
144 const char PeerConnectionServerRunner::kDefaultPort[] = "7778";
146 bool PeerConnectionServerRunner::Start() {
147 base::FilePath peerconnection_server;
148 if (!PathService::Get(base::DIR_MODULE, &peerconnection_server)) {
149 LOG(ERROR) << "Failed retrieving base::DIR_MODULE!";
150 return false;
152 peerconnection_server = peerconnection_server.Append(kServerExecutable);
154 if (!base::PathExists(peerconnection_server)) {
155 LOG(ERROR)
156 << "Missing " << kServerExecutable << ". You must build "
157 << "it so it ends up next to the browser test binary.";
158 return false;
161 CommandLine command_line(peerconnection_server);
162 command_line.AppendSwitchASCII("port", kDefaultPort);
163 VLOG(0) << "Running " << command_line.GetCommandLineString();
164 return base::LaunchProcess(command_line,
165 base::LaunchOptions(),
166 &server_pid_);
169 bool PeerConnectionServerRunner::Stop() {
170 return base::KillProcess(server_pid_, 0, false);
173 void PeerConnectionServerRunner::KillAllPeerConnectionServers() {
174 if (!base::KillProcesses(kServerExecutable, -1, NULL)) {
175 LOG(ERROR) << "Failed to kill instances of " << kServerExecutable << ".";
176 return;
178 base::WaitForProcessesToExit(kServerExecutable,
179 base::TimeDelta::FromSeconds(5), NULL);
182 } // namespace test