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\" : \"svn://svn.chromium.org/chrome/trunk/deps/"
36 "third_party/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 bool HasReferenceFilesInCheckout() {
49 if (!base::PathExists(GetReferenceFilesDir())) {
51 << "Cannot find the working directory for the reference video "
52 << "files, expected at " << GetReferenceFilesDir().value() << ". " <<
53 kAdviseOnGclientSolution;
56 return HasYuvAndY4mFile(test::kReferenceFileName360p) &&
57 HasYuvAndY4mFile(test::kReferenceFileName720p);
60 bool HasYuvAndY4mFile(const base::FilePath::CharType* reference_file) {
61 base::FilePath webrtc_reference_video_yuv = GetReferenceFilesDir()
62 .Append(reference_file).AddExtension(kYuvFileExtension);
63 if (!base::PathExists(webrtc_reference_video_yuv)) {
65 << "Missing YUV reference video to be used for quality"
66 << " comparison, expected at " << webrtc_reference_video_yuv.value()
67 << ". " << kAdviseOnGclientSolution;
71 base::FilePath webrtc_reference_video_y4m = GetReferenceFilesDir()
72 .Append(reference_file).AddExtension(kY4mFileExtension);
73 if (!base::PathExists(webrtc_reference_video_y4m)) {
75 << "Missing Y4M reference video to be used for quality"
76 << " comparison, expected at "<< webrtc_reference_video_y4m.value()
77 << ". " << kAdviseOnGclientSolution;
83 bool SleepInJavascript(content::WebContents* tab_contents, int timeout_msec) {
84 const std::string javascript = base::StringPrintf(
85 "setTimeout(function() {"
86 " window.domAutomationController.send('sleep-ok');"
87 "}, %d)", timeout_msec);
90 bool ok = content::ExecuteScriptAndExtractString(
91 tab_contents, javascript, &result);
92 return ok && result == "sleep-ok";
95 bool PollingWaitUntil(const std::string& javascript,
96 const std::string& evaluates_to,
97 content::WebContents* tab_contents) {
98 return PollingWaitUntil(javascript, evaluates_to, tab_contents,
99 kDefaultPollIntervalMsec);
102 bool PollingWaitUntil(const std::string& javascript,
103 const std::string& evaluates_to,
104 content::WebContents* tab_contents,
105 int poll_interval_msec) {
106 base::Time start_time = base::Time::Now();
107 base::TimeDelta timeout = TestTimeouts::action_max_timeout();
110 while (base::Time::Now() - start_time < timeout) {
112 if (!content::ExecuteScriptAndExtractString(tab_contents, javascript,
114 LOG(ERROR) << "Failed to execute javascript " << javascript;
118 if (evaluates_to == result)
121 // Sleep a bit here to keep this loop from spinlocking too badly.
122 if (!SleepInJavascript(tab_contents, poll_interval_msec)) {
123 // TODO(phoglund): Figure out why this fails every now and then.
124 // It's not a huge deal if it does though.
125 LOG(ERROR) << "Failed to sleep.";
129 << "Timed out while waiting for " << javascript
130 << " to evaluate to " << evaluates_to << "; last result was '" << result