OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "base/command_line.h" |
| 6 #include "base/json/json_writer.h" |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/threading/platform_thread.h" |
| 9 #include "base/time/time.h" |
| 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/extensions/api/webrtc_logging_private/webrtc_logging_pr
ivate_api.h" |
| 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 13 #include "chrome/browser/extensions/extension_tab_util.h" |
| 14 #include "chrome/browser/media/webrtc_browsertest_base.h" |
| 15 #include "chrome/browser/media/webrtc_browsertest_common.h" |
| 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "content/public/common/content_switches.h" |
| 18 #include "content/public/test/browser_test_utils.h" |
| 19 #include "extensions/browser/api_test_utils.h" |
| 20 #include "extensions/common/test_util.h" |
| 21 #include "media/base/media_switches.h" |
| 22 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 23 |
| 24 #if defined(OS_WIN) |
| 25 #define IntToStringType base::IntToString16 |
| 26 #else |
| 27 #define IntToStringType base::IntToString |
| 28 #endif |
| 29 |
| 30 using extensions::WebrtcLoggingPrivateStartWebRtcEventLoggingFunction; |
| 31 using extensions::WebrtcLoggingPrivateStopWebRtcEventLoggingFunction; |
| 32 |
| 33 namespace utils = extension_function_test_utils; |
| 34 |
| 35 namespace { |
| 36 |
| 37 // Get the expected EventLog file name. The name will be |
| 38 // <temporary path>.<render process id>.event_log.<consumer id>, for example |
| 39 // /tmp/.org.chromium.Chromium.vsygNQ/dnFW8ch/Default/WebRTC |
| 40 // Logs/WebRtcEventLog.1.29113.event_log.1 |
| 41 base::FilePath GetExpectedEventLogFileName(const base::FilePath& base_file, |
| 42 int render_process_id) { |
| 43 static const int kExpectedConsumerId = 1; |
| 44 return base_file.AddExtension(IntToStringType(render_process_id)) |
| 45 .AddExtension(FILE_PATH_LITERAL("event_log")) |
| 46 .AddExtension(IntToStringType(kExpectedConsumerId)); |
| 47 } |
| 48 |
| 49 static const char kMainWebrtcTestHtmlPage[] = "/webrtc/webrtc_jsep01_test.html"; |
| 50 |
| 51 std::string ParamsToString(const base::ListValue& parameters) { |
| 52 std::string parameter_string; |
| 53 EXPECT_TRUE(base::JSONWriter::Write(parameters, ¶meter_string)); |
| 54 return parameter_string; |
| 55 } |
| 56 |
| 57 class WebrtcEventLogApiTest : public WebRtcTestBase { |
| 58 protected: |
| 59 void SetUp() override { |
| 60 WebRtcTestBase::SetUp(); |
| 61 extension_ = extensions::test_util::CreateEmptyExtension(); |
| 62 } |
| 63 |
| 64 void SetUpInProcessBrowserTestFixture() override { |
| 65 DetectErrorsInJavaScript(); // Look for errors in our rather complex js. |
| 66 } |
| 67 |
| 68 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 69 // Ensure the infobar is enabled, since we expect that in this test. |
| 70 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream)); |
| 71 |
| 72 // Always use fake devices. |
| 73 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); |
| 74 |
| 75 // Flag used by TestWebAudioMediaStream to force garbage collection. |
| 76 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc"); |
| 77 |
| 78 // Enable the the event log in the extension API. |
| 79 command_line->AppendSwitch( |
| 80 switches::kEnableWebRtcEventLoggingFromExtension); |
| 81 } |
| 82 |
| 83 template <typename T> |
| 84 scoped_refptr<T> CreateExtensionFunction() { |
| 85 scoped_refptr<T> function(new T()); |
| 86 function->set_extension(extension_.get()); |
| 87 function->set_has_callback(true); |
| 88 return function; |
| 89 } |
| 90 |
| 91 void AppendTabIdAndUrl(base::ListValue* parameters, |
| 92 content::WebContents* tab) { |
| 93 base::DictionaryValue* request_info = new base::DictionaryValue(); |
| 94 request_info->SetInteger("tabId", |
| 95 extensions::ExtensionTabUtil::GetTabId(tab)); |
| 96 parameters->Append(request_info); |
| 97 parameters->AppendString(tab->GetURL().GetOrigin().spec()); |
| 98 } |
| 99 |
| 100 private: |
| 101 scoped_refptr<extensions::Extension> extension_; |
| 102 }; |
| 103 |
| 104 } // namespace |
| 105 |
| 106 IN_PROC_BROWSER_TEST_F(WebrtcEventLogApiTest, TestStartStopWebRtcEventLogging) { |
| 107 ASSERT_TRUE(embedded_test_server()->Start()); |
| 108 |
| 109 content::WebContents* left_tab = |
| 110 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage); |
| 111 content::WebContents* right_tab = |
| 112 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage); |
| 113 |
| 114 SetupPeerconnectionWithLocalStream(left_tab); |
| 115 SetupPeerconnectionWithLocalStream(right_tab); |
| 116 |
| 117 NegotiateCall(left_tab, right_tab, "VP8"); |
| 118 |
| 119 StartDetectingVideo(left_tab, "remote-view"); |
| 120 StartDetectingVideo(right_tab, "remote-view"); |
| 121 |
| 122 // Start the event log. |
| 123 const int seconds = 0; |
| 124 base::ListValue start_params; |
| 125 AppendTabIdAndUrl(&start_params, left_tab); |
| 126 start_params.AppendInteger(seconds); |
| 127 scoped_refptr<WebrtcLoggingPrivateStartWebRtcEventLoggingFunction> |
| 128 start_function(CreateExtensionFunction< |
| 129 WebrtcLoggingPrivateStartWebRtcEventLoggingFunction>()); |
| 130 scoped_ptr<base::Value> start_result(utils::RunFunctionAndReturnSingleResult( |
| 131 start_function.get(), ParamsToString(start_params), browser())); |
| 132 ASSERT_TRUE(start_result.get()); |
| 133 |
| 134 // Get the file name. |
| 135 scoped_ptr<extensions::api::webrtc_logging_private::RecordingInfo> |
| 136 recordings_info_start( |
| 137 extensions::api::webrtc_logging_private::RecordingInfo::FromValue( |
| 138 *start_result.get())); |
| 139 ASSERT_TRUE(recordings_info_start.get()); |
| 140 base::FilePath file_name_start( |
| 141 base::FilePath::FromUTF8Unsafe(recordings_info_start->prefix_path)); |
| 142 |
| 143 #if !defined(OS_MACOSX) |
| 144 // Video is choppy on Mac OS X. http://crbug.com/443542. |
| 145 WaitForVideoToPlay(left_tab); |
| 146 WaitForVideoToPlay(right_tab); |
| 147 #endif |
| 148 |
| 149 // Stop the event log. |
| 150 base::ListValue stop_params; |
| 151 AppendTabIdAndUrl(&stop_params, left_tab); |
| 152 scoped_refptr<WebrtcLoggingPrivateStopWebRtcEventLoggingFunction> |
| 153 stop_function(CreateExtensionFunction< |
| 154 WebrtcLoggingPrivateStopWebRtcEventLoggingFunction>()); |
| 155 scoped_ptr<base::Value> stop_result(utils::RunFunctionAndReturnSingleResult( |
| 156 stop_function.get(), ParamsToString(stop_params), browser())); |
| 157 |
| 158 // Get the file name. |
| 159 scoped_ptr<extensions::api::webrtc_logging_private::RecordingInfo> |
| 160 recordings_info_stop( |
| 161 extensions::api::webrtc_logging_private::RecordingInfo::FromValue( |
| 162 *stop_result.get())); |
| 163 ASSERT_TRUE(recordings_info_stop.get()); |
| 164 base::FilePath file_name_stop( |
| 165 base::FilePath::FromUTF8Unsafe(recordings_info_stop->prefix_path)); |
| 166 |
| 167 HangUp(left_tab); |
| 168 HangUp(right_tab); |
| 169 |
| 170 EXPECT_EQ(file_name_start, file_name_stop); |
| 171 |
| 172 // Check that the file exists and is non-empty. |
| 173 base::ProcessId render_process_id = |
| 174 base::GetProcId(left_tab->GetRenderProcessHost()->GetHandle()); |
| 175 EXPECT_NE(render_process_id, base::kNullProcessId); |
| 176 base::FilePath full_file_name = |
| 177 GetExpectedEventLogFileName(file_name_stop, render_process_id); |
| 178 int64_t file_size = 0; |
| 179 while (!(base::PathExists(full_file_name) && |
| 180 base::GetFileSize(full_file_name, &file_size) && file_size > 0)) { |
| 181 // This should normally not happen, but is here to prevent the test |
| 182 // from becoming flaky on devices with weird timings or when the |
| 183 // /webrtc/webrtc_jsep01_test.html changes. |
| 184 VLOG(1) << "Waiting for logfile to become available..."; |
| 185 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
| 186 } |
| 187 ASSERT_TRUE(base::PathExists(full_file_name)); |
| 188 EXPECT_TRUE(base::GetFileSize(full_file_name, &file_size)); |
| 189 EXPECT_GT(file_size, 0); |
| 190 |
| 191 // Clean up. |
| 192 base::DeleteFile(full_file_name, false); |
| 193 } |
| 194 |
| 195 IN_PROC_BROWSER_TEST_F(WebrtcEventLogApiTest, |
| 196 TestStartTimedWebRtcEventLogging) { |
| 197 ASSERT_TRUE(embedded_test_server()->Start()); |
| 198 |
| 199 content::WebContents* left_tab = |
| 200 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage); |
| 201 content::WebContents* right_tab = |
| 202 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage); |
| 203 |
| 204 SetupPeerconnectionWithLocalStream(left_tab); |
| 205 SetupPeerconnectionWithLocalStream(right_tab); |
| 206 |
| 207 NegotiateCall(left_tab, right_tab, "VP8"); |
| 208 |
| 209 StartDetectingVideo(left_tab, "remote-view"); |
| 210 StartDetectingVideo(right_tab, "remote-view"); |
| 211 |
| 212 // Start the event log. RunFunctionAndReturnSingleResult will block until a |
| 213 // result is available, which happens when the logging stops after 1 second. |
| 214 const int seconds = 1; |
| 215 base::ListValue start_params; |
| 216 AppendTabIdAndUrl(&start_params, left_tab); |
| 217 start_params.AppendInteger(seconds); |
| 218 scoped_refptr<WebrtcLoggingPrivateStartWebRtcEventLoggingFunction> |
| 219 start_function(CreateExtensionFunction< |
| 220 WebrtcLoggingPrivateStartWebRtcEventLoggingFunction>()); |
| 221 scoped_ptr<base::Value> start_result(utils::RunFunctionAndReturnSingleResult( |
| 222 start_function.get(), ParamsToString(start_params), browser())); |
| 223 ASSERT_TRUE(start_result.get()); |
| 224 |
| 225 // Get the file name. |
| 226 scoped_ptr<extensions::api::webrtc_logging_private::RecordingInfo> |
| 227 recordings_info_start( |
| 228 extensions::api::webrtc_logging_private::RecordingInfo::FromValue( |
| 229 *start_result.get())); |
| 230 ASSERT_TRUE(recordings_info_start.get()); |
| 231 base::FilePath file_name_start( |
| 232 base::FilePath::FromUTF8Unsafe(recordings_info_start->prefix_path)); |
| 233 |
| 234 #if !defined(OS_MACOSX) |
| 235 // Video is choppy on Mac OS X. http://crbug.com/443542. |
| 236 WaitForVideoToPlay(left_tab); |
| 237 WaitForVideoToPlay(right_tab); |
| 238 #endif |
| 239 |
| 240 HangUp(left_tab); |
| 241 HangUp(right_tab); |
| 242 |
| 243 // The log has stopped automatically. Check that the file exists and is |
| 244 // non-empty. |
| 245 base::ProcessId render_process_id = |
| 246 base::GetProcId(left_tab->GetRenderProcessHost()->GetHandle()); |
| 247 EXPECT_NE(render_process_id, base::kNullProcessId); |
| 248 base::FilePath full_file_name = |
| 249 GetExpectedEventLogFileName(file_name_start, render_process_id); |
| 250 int64_t file_size = 0; |
| 251 while (!(base::PathExists(full_file_name) && |
| 252 base::GetFileSize(full_file_name, &file_size) && file_size > 0)) { |
| 253 // This should normally not happen, but is here to prevent the test |
| 254 // from becoming flaky on devices with weird timings or when the |
| 255 // /webrtc/webrtc_jsep01_test.html changes. |
| 256 VLOG(1) << "Waiting for logfile to become available..."; |
| 257 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
| 258 } |
| 259 ASSERT_TRUE(base::PathExists(full_file_name)); |
| 260 EXPECT_TRUE(base::GetFileSize(full_file_name, &file_size)); |
| 261 EXPECT_GT(file_size, 0); |
| 262 |
| 263 // Clean up. |
| 264 base::DeleteFile(full_file_name, false); |
| 265 } |
OLD | NEW |