| OLD | NEW |
| (Empty) |
| 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_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 const int kDefaultPollIntervalMsec = 250; | |
| 45 | |
| 46 bool IsErrorResult(const std::string& result) { | |
| 47 return base::StartsWith(result, "failed-", | |
| 48 base::CompareCase::INSENSITIVE_ASCII); | |
| 49 } | |
| 50 | |
| 51 base::FilePath GetReferenceFilesDir() { | |
| 52 base::FilePath test_data_dir; | |
| 53 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | |
| 54 | |
| 55 return test_data_dir.Append(kReferenceFilesDirName); | |
| 56 } | |
| 57 | |
| 58 base::FilePath GetToolForPlatform(const std::string& tool_name) { | |
| 59 base::FilePath tools_dir = | |
| 60 GetReferenceFilesDir().Append(FILE_PATH_LITERAL("tools")); | |
| 61 #if defined(OS_WIN) | |
| 62 return tools_dir | |
| 63 .Append(FILE_PATH_LITERAL("win")) | |
| 64 .AppendASCII(tool_name) | |
| 65 .AddExtension(FILE_PATH_LITERAL("exe")); | |
| 66 #elif defined(OS_MACOSX) | |
| 67 return tools_dir.Append(FILE_PATH_LITERAL("mac")).AppendASCII(tool_name); | |
| 68 #elif defined(OS_LINUX) | |
| 69 return tools_dir.Append(FILE_PATH_LITERAL("linux")).AppendASCII(tool_name); | |
| 70 #else | |
| 71 CHECK(false) << "Can't retrieve tool " << tool_name << " on this platform."; | |
| 72 return base::FilePath(); | |
| 73 #endif | |
| 74 } | |
| 75 | |
| 76 bool HasReferenceFilesInCheckout() { | |
| 77 if (!base::PathExists(GetReferenceFilesDir())) { | |
| 78 LOG(ERROR) | |
| 79 << "Cannot find the working directory for the reference video " | |
| 80 << "files, expected at " << GetReferenceFilesDir().value() << ". " << | |
| 81 kAdviseOnGclientSolution; | |
| 82 return false; | |
| 83 } | |
| 84 return HasYuvAndY4mFile(test::kReferenceFileName360p) && | |
| 85 HasYuvAndY4mFile(test::kReferenceFileName720p); | |
| 86 } | |
| 87 | |
| 88 bool HasYuvAndY4mFile(const base::FilePath::CharType* reference_file) { | |
| 89 base::FilePath webrtc_reference_video_yuv = GetReferenceFilesDir() | |
| 90 .Append(reference_file).AddExtension(kYuvFileExtension); | |
| 91 if (!base::PathExists(webrtc_reference_video_yuv)) { | |
| 92 LOG(ERROR) | |
| 93 << "Missing YUV reference video to be used for quality" | |
| 94 << " comparison, expected at " << webrtc_reference_video_yuv.value() | |
| 95 << ". " << kAdviseOnGclientSolution; | |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 base::FilePath webrtc_reference_video_y4m = GetReferenceFilesDir() | |
| 100 .Append(reference_file).AddExtension(kY4mFileExtension); | |
| 101 if (!base::PathExists(webrtc_reference_video_y4m)) { | |
| 102 LOG(ERROR) | |
| 103 << "Missing Y4M reference video to be used for quality" | |
| 104 << " comparison, expected at "<< webrtc_reference_video_y4m.value() | |
| 105 << ". " << kAdviseOnGclientSolution; | |
| 106 return false; | |
| 107 } | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 bool SleepInJavascript(content::WebContents* tab_contents, int timeout_msec) { | |
| 112 const std::string javascript = base::StringPrintf( | |
| 113 "setTimeout(function() {" | |
| 114 " window.domAutomationController.send('sleep-ok');" | |
| 115 "}, %d)", timeout_msec); | |
| 116 | |
| 117 std::string result; | |
| 118 bool ok = content::ExecuteScriptAndExtractString( | |
| 119 tab_contents, javascript, &result); | |
| 120 return ok && result == "sleep-ok"; | |
| 121 } | |
| 122 | |
| 123 bool PollingWaitUntil(const std::string& javascript, | |
| 124 const std::string& evaluates_to, | |
| 125 content::WebContents* tab_contents) { | |
| 126 return PollingWaitUntil(javascript, evaluates_to, tab_contents, | |
| 127 kDefaultPollIntervalMsec); | |
| 128 } | |
| 129 | |
| 130 bool PollingWaitUntil(const std::string& javascript, | |
| 131 const std::string& evaluates_to, | |
| 132 content::WebContents* tab_contents, | |
| 133 int poll_interval_msec) { | |
| 134 base::Time start_time = base::Time::Now(); | |
| 135 base::TimeDelta timeout = TestTimeouts::action_max_timeout(); | |
| 136 std::string result; | |
| 137 | |
| 138 while (base::Time::Now() - start_time < timeout) { | |
| 139 std::string result; | |
| 140 if (!content::ExecuteScriptAndExtractString(tab_contents, javascript, | |
| 141 &result)) { | |
| 142 LOG(ERROR) << "Failed to execute javascript " << javascript; | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 if (evaluates_to == result) { | |
| 147 return true; | |
| 148 } else if (IsErrorResult(result)) { | |
| 149 LOG(ERROR) << "|" << javascript << "| returned an error: " << result; | |
| 150 return false; | |
| 151 } | |
| 152 | |
| 153 // Sleep a bit here to keep this loop from spinlocking too badly. | |
| 154 if (!SleepInJavascript(tab_contents, poll_interval_msec)) { | |
| 155 // TODO(phoglund): Figure out why this fails every now and then. | |
| 156 // It's not a huge deal if it does though. | |
| 157 LOG(ERROR) << "Failed to sleep."; | |
| 158 } | |
| 159 } | |
| 160 LOG(ERROR) | |
| 161 << "Timed out while waiting for " << javascript | |
| 162 << " to evaluate to " << evaluates_to << "; last result was '" << result | |
| 163 << "'"; | |
| 164 return false; | |
| 165 } | |
| 166 | |
| 167 } // namespace test | |
| OLD | NEW |