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/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 "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser_tabstrip.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "content/public/test/browser_test_utils.h"
20 // Relative to the chrome/test/data directory.
21 const base::FilePath::CharType kReferenceFilesDirName
[] =
22 FILE_PATH_LITERAL("webrtc/resources");
23 const base::FilePath::CharType kReferenceFileName360p
[] =
24 FILE_PATH_LITERAL("reference_video_640x360_30fps");
25 const base::FilePath::CharType kReferenceFileName720p
[] =
26 FILE_PATH_LITERAL("reference_video_1280x720_30fps");
27 const base::FilePath::CharType kYuvFileExtension
[] = FILE_PATH_LITERAL("yuv");
28 const base::FilePath::CharType kY4mFileExtension
[] = FILE_PATH_LITERAL("y4m");
30 // This message describes how to modify your .gclient to get the reference
31 // video files downloaded for you.
32 static const char kAdviseOnGclientSolution
[] =
33 "You need to add this solution to your .gclient to run this test:\n"
35 " \"name\" : \"webrtc.DEPS\",\n"
36 " \"url\" : \"https://chromium.googlesource.com/chromium/deps/"
37 "webrtc/webrtc.DEPS\",\n"
40 const int kDefaultPollIntervalMsec
= 250;
42 bool IsErrorResult(const std::string
& result
) {
43 return base::StartsWith(result
, "failed-",
44 base::CompareCase::INSENSITIVE_ASCII
);
47 base::FilePath
GetReferenceFilesDir() {
48 base::FilePath test_data_dir
;
49 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
51 return test_data_dir
.Append(kReferenceFilesDirName
);
54 base::FilePath
GetToolForPlatform(const std::string
& tool_name
) {
55 base::FilePath tools_dir
=
56 GetReferenceFilesDir().Append(FILE_PATH_LITERAL("tools"));
59 .Append(FILE_PATH_LITERAL("win"))
60 .AppendASCII(tool_name
)
61 .AddExtension(FILE_PATH_LITERAL("exe"));
62 #elif defined(OS_MACOSX)
63 return tools_dir
.Append(FILE_PATH_LITERAL("mac")).AppendASCII(tool_name
);
64 #elif defined(OS_LINUX)
65 return tools_dir
.Append(FILE_PATH_LITERAL("linux")).AppendASCII(tool_name
);
67 CHECK(false) << "Can't retrieve tool " << tool_name
<< " on this platform.";
68 return base::FilePath();
72 bool HasReferenceFilesInCheckout() {
73 if (!base::PathExists(GetReferenceFilesDir())) {
75 << "Cannot find the working directory for the reference video "
76 << "files, expected at " << GetReferenceFilesDir().value() << ". " <<
77 kAdviseOnGclientSolution
;
80 return HasYuvAndY4mFile(test::kReferenceFileName360p
) &&
81 HasYuvAndY4mFile(test::kReferenceFileName720p
);
84 bool HasYuvAndY4mFile(const base::FilePath::CharType
* reference_file
) {
85 base::FilePath webrtc_reference_video_yuv
= GetReferenceFilesDir()
86 .Append(reference_file
).AddExtension(kYuvFileExtension
);
87 if (!base::PathExists(webrtc_reference_video_yuv
)) {
89 << "Missing YUV reference video to be used for quality"
90 << " comparison, expected at " << webrtc_reference_video_yuv
.value()
91 << ". " << kAdviseOnGclientSolution
;
95 base::FilePath webrtc_reference_video_y4m
= GetReferenceFilesDir()
96 .Append(reference_file
).AddExtension(kY4mFileExtension
);
97 if (!base::PathExists(webrtc_reference_video_y4m
)) {
99 << "Missing Y4M reference video to be used for quality"
100 << " comparison, expected at "<< webrtc_reference_video_y4m
.value()
101 << ". " << kAdviseOnGclientSolution
;
107 bool SleepInJavascript(content::WebContents
* tab_contents
, int timeout_msec
) {
108 const std::string javascript
= base::StringPrintf(
109 "setTimeout(function() {"
110 " window.domAutomationController.send('sleep-ok');"
111 "}, %d)", timeout_msec
);
114 bool ok
= content::ExecuteScriptAndExtractString(
115 tab_contents
, javascript
, &result
);
116 return ok
&& result
== "sleep-ok";
119 bool PollingWaitUntil(const std::string
& javascript
,
120 const std::string
& evaluates_to
,
121 content::WebContents
* tab_contents
) {
122 return PollingWaitUntil(javascript
, evaluates_to
, tab_contents
,
123 kDefaultPollIntervalMsec
);
126 bool PollingWaitUntil(const std::string
& javascript
,
127 const std::string
& evaluates_to
,
128 content::WebContents
* tab_contents
,
129 int poll_interval_msec
) {
130 base::Time start_time
= base::Time::Now();
131 base::TimeDelta timeout
= TestTimeouts::action_max_timeout();
134 while (base::Time::Now() - start_time
< timeout
) {
136 if (!content::ExecuteScriptAndExtractString(tab_contents
, javascript
,
138 LOG(ERROR
) << "Failed to execute javascript " << javascript
;
142 if (evaluates_to
== result
) {
144 } else if (IsErrorResult(result
)) {
145 LOG(ERROR
) << "|" << javascript
<< "| returned an error: " << result
;
149 // Sleep a bit here to keep this loop from spinlocking too badly.
150 if (!SleepInJavascript(tab_contents
, poll_interval_msec
)) {
151 // TODO(phoglund): Figure out why this fails every now and then.
152 // It's not a huge deal if it does though.
153 LOG(ERROR
) << "Failed to sleep.";
157 << "Timed out while waiting for " << javascript
158 << " to evaluate to " << evaluates_to
<< "; last result was '" << result