Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / media / webrtc_browsertest_common.cc
blob34d35a7a94a34a47dbe4e6cf2f30b358f20d57af
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"
17 namespace test {
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"
33 "{\n"
34 " \"name\" : \"webrtc.DEPS\",\n"
35 " \"url\" : \"svn://svn.chromium.org/chrome/trunk/deps/"
36 "third_party/webrtc/webrtc.DEPS\",\n"
37 "}";
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())) {
50 LOG(ERROR)
51 << "Cannot find the working directory for the reference video "
52 << "files, expected at " << GetReferenceFilesDir().value() << ". " <<
53 kAdviseOnGclientSolution;
54 return false;
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)) {
64 LOG(ERROR)
65 << "Missing YUV reference video to be used for quality"
66 << " comparison, expected at " << webrtc_reference_video_yuv.value()
67 << ". " << kAdviseOnGclientSolution;
68 return false;
71 base::FilePath webrtc_reference_video_y4m = GetReferenceFilesDir()
72 .Append(reference_file).AddExtension(kY4mFileExtension);
73 if (!base::PathExists(webrtc_reference_video_y4m)) {
74 LOG(ERROR)
75 << "Missing Y4M reference video to be used for quality"
76 << " comparison, expected at "<< webrtc_reference_video_y4m.value()
77 << ". " << kAdviseOnGclientSolution;
78 return false;
80 return true;
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);
89 std::string result;
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();
108 std::string result;
110 while (base::Time::Now() - start_time < timeout) {
111 std::string result;
112 if (!content::ExecuteScriptAndExtractString(tab_contents, javascript,
113 &result)) {
114 LOG(ERROR) << "Failed to execute javascript " << javascript;
115 return false;
118 if (evaluates_to == result)
119 return true;
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.";
128 LOG(ERROR)
129 << "Timed out while waiting for " << javascript
130 << " to evaluate to " << evaluates_to << "; last result was '" << result
131 << "'";
132 return false;
135 } // namespace test