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.
4
5 #include "chrome/browser/media/webrtc/webrtc_browsertest_common.h"
6
7 #include "base/files/file_util.h"
8 #include "base/path_service.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/test/test_timeouts.h"
12 #include "base/time/time.h"
13 #include "build/build_config.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"
18
19 namespace test {
20
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");
30
31 // This message describes how to modify your .gclient to get the reference
32 // video files downloaded for you.
33 const char kAdviseOnGclientSolution[] =
34 "To run this test, you must run download_from_google_storage --config\n"
35 "and follow the instructions (use 'browser' for project id)\n"
36 "You also need to add this solution to your .gclient:\n"
37 "{\n"
38 " \"name\" : \"webrtc.DEPS\",\n"
39 " \"url\" : \"https://chromium.googlesource.com/chromium/deps/"
40 "webrtc/webrtc.DEPS\",\n"
41 "}\n"
42 "and run gclient sync. This will download the required ref files.";
43
44 #if defined(THREAD_SANITIZER) || defined(MEMORY_SANITIZER) || \
45 defined(ADDRESS_SANITIZER)
46 #if defined(OS_CHROMEOS)
47 const int kDefaultPollIntervalMsec = 2000;
48 #else
49 const int kDefaultPollIntervalMsec = 1000;
50 #endif // OS_CHROMEOS
51 #else
52 #if defined(OS_CHROMEOS)
53 const int kDefaultPollIntervalMsec = 500;
54 #else
55 const int kDefaultPollIntervalMsec = 250;
56 #endif // OS_CHROMEOS
57 #endif
58
IsErrorResult(const std::string & result)59 bool IsErrorResult(const std::string& result) {
60 return base::StartsWith(result, "failed-",
61 base::CompareCase::INSENSITIVE_ASCII);
62 }
63
GetReferenceFilesDir()64 base::FilePath GetReferenceFilesDir() {
65 base::FilePath test_data_dir;
66 base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
67
68 return test_data_dir.Append(kReferenceFilesDirName);
69 }
70
GetToolForPlatform(const std::string & tool_name)71 base::FilePath GetToolForPlatform(const std::string& tool_name) {
72 base::FilePath tools_dir =
73 GetReferenceFilesDir().Append(FILE_PATH_LITERAL("tools"));
74 #if defined(OS_WIN)
75 return tools_dir
76 .Append(FILE_PATH_LITERAL("win"))
77 .AppendASCII(tool_name)
78 .AddExtension(FILE_PATH_LITERAL("exe"));
79 #elif defined(OS_MAC)
80 return tools_dir.Append(FILE_PATH_LITERAL("mac")).AppendASCII(tool_name);
81 #elif defined(OS_LINUX) || defined(OS_CHROMEOS)
82 return tools_dir.Append(FILE_PATH_LITERAL("linux")).AppendASCII(tool_name);
83 #else
84 CHECK(false) << "Can't retrieve tool " << tool_name << " on this platform.";
85 return base::FilePath();
86 #endif
87 }
88
HasReferenceFilesInCheckout()89 bool HasReferenceFilesInCheckout() {
90 if (!base::PathExists(GetReferenceFilesDir())) {
91 LOG(ERROR)
92 << "Cannot find the working directory for the reference video "
93 << "files, expected at " << GetReferenceFilesDir().value() << ". " <<
94 kAdviseOnGclientSolution;
95 return false;
96 }
97 return HasYuvAndY4mFile(test::kReferenceFileName360p) &&
98 HasYuvAndY4mFile(test::kReferenceFileName720p);
99 }
100
HasYuvAndY4mFile(const base::FilePath::CharType * reference_file)101 bool HasYuvAndY4mFile(const base::FilePath::CharType* reference_file) {
102 base::FilePath webrtc_reference_video_yuv = GetReferenceFilesDir()
103 .Append(reference_file).AddExtension(kYuvFileExtension);
104 if (!base::PathExists(webrtc_reference_video_yuv)) {
105 LOG(ERROR)
106 << "Missing YUV reference video to be used for quality"
107 << " comparison, expected at " << webrtc_reference_video_yuv.value()
108 << ". " << kAdviseOnGclientSolution;
109 return false;
110 }
111
112 base::FilePath webrtc_reference_video_y4m = GetReferenceFilesDir()
113 .Append(reference_file).AddExtension(kY4mFileExtension);
114 if (!base::PathExists(webrtc_reference_video_y4m)) {
115 LOG(ERROR)
116 << "Missing Y4M reference video to be used for quality"
117 << " comparison, expected at "<< webrtc_reference_video_y4m.value()
118 << ". " << kAdviseOnGclientSolution;
119 return false;
120 }
121 return true;
122 }
123
SleepInJavascript(content::WebContents * tab_contents,int timeout_msec)124 bool SleepInJavascript(content::WebContents* tab_contents, int timeout_msec) {
125 const std::string javascript = base::StringPrintf(
126 "setTimeout(function() {"
127 " window.domAutomationController.send('sleep-ok');"
128 "}, %d)", timeout_msec);
129
130 std::string result;
131 bool ok = content::ExecuteScriptAndExtractString(
132 tab_contents, javascript, &result);
133 return ok && result == "sleep-ok";
134 }
135
PollingWaitUntil(const std::string & javascript,const std::string & evaluates_to,content::WebContents * tab_contents)136 bool PollingWaitUntil(const std::string& javascript,
137 const std::string& evaluates_to,
138 content::WebContents* tab_contents) {
139 return PollingWaitUntil(javascript, evaluates_to, tab_contents,
140 kDefaultPollIntervalMsec);
141 }
142
PollingWaitUntil(const std::string & javascript,const std::string & evaluates_to,content::WebContents * tab_contents,int poll_interval_msec)143 bool PollingWaitUntil(const std::string& javascript,
144 const std::string& evaluates_to,
145 content::WebContents* tab_contents,
146 int poll_interval_msec) {
147 base::Time start_time = base::Time::Now();
148 base::TimeDelta timeout = TestTimeouts::action_max_timeout();
149 std::string result;
150
151 while (base::Time::Now() - start_time < timeout) {
152 std::string result;
153 if (!content::ExecuteScriptAndExtractString(tab_contents, javascript,
154 &result)) {
155 LOG(ERROR) << "Failed to execute javascript " << javascript;
156 return false;
157 }
158
159 if (evaluates_to == result) {
160 return true;
161 } else if (IsErrorResult(result)) {
162 LOG(ERROR) << "|" << javascript << "| returned an error: " << result;
163 return false;
164 }
165
166 // Sleep a bit here to keep this loop from spinlocking too badly.
167 if (!SleepInJavascript(tab_contents, poll_interval_msec)) {
168 // TODO(phoglund): Figure out why this fails every now and then.
169 // It's not a huge deal if it does though.
170 LOG(ERROR) << "Failed to sleep.";
171 }
172 }
173 LOG(ERROR)
174 << "Timed out while waiting for " << javascript
175 << " to evaluate to " << evaluates_to << "; last result was '" << result
176 << "'";
177 return false;
178 }
179
180 } // namespace test
181