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/stringprintf.h"
10 #include "base/test/test_timeouts.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser_tabstrip.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "content/public/test/browser_test_utils.h"
19 // Relative to the chrome/test/data directory.
20 const base::FilePath::CharType kReferenceFilesDirName
[] =
21 FILE_PATH_LITERAL("webrtc/resources");
22 const base::FilePath::CharType kReferenceFileName360p
[] =
23 FILE_PATH_LITERAL("reference_video_640x360_30fps");
24 const base::FilePath::CharType kReferenceFileName720p
[] =
25 FILE_PATH_LITERAL("reference_video_1280x720_30fps");
26 const base::FilePath::CharType kYuvFileExtension
[] = FILE_PATH_LITERAL("yuv");
27 const base::FilePath::CharType kY4mFileExtension
[] = FILE_PATH_LITERAL("y4m");
29 // This message describes how to modify your .gclient to get the reference
30 // video files downloaded for you.
31 static const char kAdviseOnGclientSolution
[] =
32 "You need to add this solution to your .gclient to run this test:\n"
34 " \"name\" : \"webrtc.DEPS\",\n"
35 " \"url\" : \"https://chromium.googlesource.com/chromium/deps/"
36 "webrtc/webrtc.DEPS\",\n"
39 const int kDefaultPollIntervalMsec
= 250;
41 base::FilePath
GetReferenceFilesDir() {
42 base::FilePath test_data_dir
;
43 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
45 return test_data_dir
.Append(kReferenceFilesDirName
);
48 base::FilePath
GetToolForPlatform(const std::string
& tool_name
) {
49 base::FilePath tools_dir
=
50 GetReferenceFilesDir().Append(FILE_PATH_LITERAL("tools"));
53 .Append(FILE_PATH_LITERAL("win"))
54 .AppendASCII(tool_name
)
55 .AddExtension(FILE_PATH_LITERAL("exe"));
56 #elif defined(OS_MACOSX)
57 return tools_dir
.Append(FILE_PATH_LITERAL("mac")).AppendASCII(tool_name
);
58 #elif defined(OS_LINUX)
59 return tools_dir
.Append(FILE_PATH_LITERAL("linux")).AppendASCII(tool_name
);
61 CHECK(false) << "Can't retrieve tool " << tool_name
<< " on this platform.";
62 return base::FilePath();
66 bool HasReferenceFilesInCheckout() {
67 if (!base::PathExists(GetReferenceFilesDir())) {
69 << "Cannot find the working directory for the reference video "
70 << "files, expected at " << GetReferenceFilesDir().value() << ". " <<
71 kAdviseOnGclientSolution
;
74 return HasYuvAndY4mFile(test::kReferenceFileName360p
) &&
75 HasYuvAndY4mFile(test::kReferenceFileName720p
);
78 bool HasYuvAndY4mFile(const base::FilePath::CharType
* reference_file
) {
79 base::FilePath webrtc_reference_video_yuv
= GetReferenceFilesDir()
80 .Append(reference_file
).AddExtension(kYuvFileExtension
);
81 if (!base::PathExists(webrtc_reference_video_yuv
)) {
83 << "Missing YUV reference video to be used for quality"
84 << " comparison, expected at " << webrtc_reference_video_yuv
.value()
85 << ". " << kAdviseOnGclientSolution
;
89 base::FilePath webrtc_reference_video_y4m
= GetReferenceFilesDir()
90 .Append(reference_file
).AddExtension(kY4mFileExtension
);
91 if (!base::PathExists(webrtc_reference_video_y4m
)) {
93 << "Missing Y4M reference video to be used for quality"
94 << " comparison, expected at "<< webrtc_reference_video_y4m
.value()
95 << ". " << kAdviseOnGclientSolution
;
101 bool SleepInJavascript(content::WebContents
* tab_contents
, int timeout_msec
) {
102 const std::string javascript
= base::StringPrintf(
103 "setTimeout(function() {"
104 " window.domAutomationController.send('sleep-ok');"
105 "}, %d)", timeout_msec
);
108 bool ok
= content::ExecuteScriptAndExtractString(
109 tab_contents
, javascript
, &result
);
110 return ok
&& result
== "sleep-ok";
113 bool PollingWaitUntil(const std::string
& javascript
,
114 const std::string
& evaluates_to
,
115 content::WebContents
* tab_contents
) {
116 return PollingWaitUntil(javascript
, evaluates_to
, tab_contents
,
117 kDefaultPollIntervalMsec
);
120 bool PollingWaitUntil(const std::string
& javascript
,
121 const std::string
& evaluates_to
,
122 content::WebContents
* tab_contents
,
123 int poll_interval_msec
) {
124 base::Time start_time
= base::Time::Now();
125 base::TimeDelta timeout
= TestTimeouts::action_max_timeout();
128 while (base::Time::Now() - start_time
< timeout
) {
130 if (!content::ExecuteScriptAndExtractString(tab_contents
, javascript
,
132 LOG(ERROR
) << "Failed to execute javascript " << javascript
;
136 if (evaluates_to
== result
)
139 // Sleep a bit here to keep this loop from spinlocking too badly.
140 if (!SleepInJavascript(tab_contents
, poll_interval_msec
)) {
141 // TODO(phoglund): Figure out why this fails every now and then.
142 // It's not a huge deal if it does though.
143 LOG(ERROR
) << "Failed to sleep.";
147 << "Timed out while waiting for " << javascript
148 << " to evaluate to " << evaluates_to
<< "; last result was '" << result