Chromium Code Reviews| 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/.com.google.Chrome.Z6UC3P.12345.event_log.1". | |
| 40 base::FilePath GetExpectedEventLogFileName(const base::FilePath& base_file, | |
| 41 int render_process_id) { | |
| 42 const int kExpectedConsumerId = 1; | |
| 43 return base_file.AddExtension(IntToStringType(render_process_id)) | |
| 44 .AddExtension(FILE_PATH_LITERAL("event_log")) | |
| 45 .AddExtension(IntToStringType(kExpectedConsumerId)); | |
| 46 } | |
| 47 | |
| 48 static const char kMainWebrtcTestHtmlPage[] = "/webrtc/webrtc_jsep01_test.html"; | |
| 49 | |
| 50 std::string ParamsToString(const base::ListValue& parameters) { | |
| 51 std::string parameter_string; | |
| 52 EXPECT_TRUE(base::JSONWriter::Write(parameters, ¶meter_string)); | |
| 53 return parameter_string; | |
| 54 } | |
| 55 | |
| 56 class WebrtcEventLogApiTest : public WebRtcTestBase { | |
| 57 protected: | |
| 58 void SetUp() override { | |
| 59 WebRtcTestBase::SetUp(); | |
| 60 extension_ = extensions::test_util::CreateEmptyExtension(); | |
| 61 } | |
| 62 | |
| 63 void SetUpInProcessBrowserTestFixture() override { | |
| 64 DetectErrorsInJavaScript(); // Look for errors in our rather complex js. | |
| 65 } | |
| 66 | |
| 67 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 68 // Ensure the infobar is enabled, since we expect that in this test. | |
| 69 EXPECT_FALSE(command_line->HasSwitch(switches::kUseFakeUIForMediaStream)); | |
| 70 | |
| 71 // Always use fake devices. | |
| 72 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); | |
| 73 | |
| 74 // Flag used by TestWebAudioMediaStream to force garbage collection. | |
| 75 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc"); | |
| 76 | |
| 77 // Enable the the event log in the extension API. | |
| 78 command_line->AppendSwitch( | |
| 79 switches::kEnableWebRtcEventLoggingFromExtension); | |
| 80 } | |
| 81 | |
| 82 template <typename T> | |
| 83 scoped_refptr<T> CreateFunction() { | |
| 84 scoped_refptr<T> function(new T()); | |
| 85 function->set_extension(extension_.get()); | |
| 86 function->set_has_callback(true); | |
| 87 return function; | |
| 88 } | |
| 89 | |
| 90 void AppendTabIdAndUrl(base::ListValue* parameters, | |
| 91 content::WebContents* tab) { | |
| 92 base::DictionaryValue* request_info = new base::DictionaryValue(); | |
| 93 request_info->SetInteger("tabId", | |
| 94 extensions::ExtensionTabUtil::GetTabId(tab)); | |
| 95 parameters->Append(request_info); | |
| 96 parameters->AppendString(tab->GetURL().GetOrigin().spec()); | |
| 97 } | |
| 98 | |
| 99 private: | |
| 100 scoped_refptr<extensions::Extension> extension_; | |
| 101 }; | |
| 102 | |
| 103 } // namespace | |
| 104 | |
| 105 IN_PROC_BROWSER_TEST_F(WebrtcEventLogApiTest, TestStartStopWebRtcEventLogging) { | |
| 106 if (OnWinXp()) | |
| 107 return; | |
| 108 | |
| 109 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 110 | |
| 111 content::WebContents* left_tab = | |
| 112 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage); | |
| 113 content::WebContents* right_tab = | |
| 114 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage); | |
| 115 | |
| 116 SetupPeerconnectionWithLocalStream(left_tab); | |
| 117 SetupPeerconnectionWithLocalStream(right_tab); | |
| 118 | |
| 119 NegotiateCall(left_tab, right_tab, "VP8"); | |
| 120 | |
| 121 StartDetectingVideo(left_tab, "remote-view"); | |
| 122 StartDetectingVideo(right_tab, "remote-view"); | |
| 123 | |
| 124 // Start the event log. | |
| 125 int seconds = 0; | |
|
Henrik Grunell
2016/03/24 09:36:22
const?
terelius-chromium
2016/03/24 16:21:23
Done.
| |
| 126 base::ListValue start_params; | |
| 127 AppendTabIdAndUrl(&start_params, left_tab); | |
| 128 start_params.AppendInteger(seconds); | |
| 129 scoped_refptr<WebrtcLoggingPrivateStartWebRtcEventLoggingFunction> | |
| 130 start_function(CreateFunction< | |
| 131 WebrtcLoggingPrivateStartWebRtcEventLoggingFunction>()); | |
| 132 scoped_ptr<base::Value> start_result(utils::RunFunctionAndReturnSingleResult( | |
| 133 start_function.get(), ParamsToString(start_params), browser())); | |
| 134 ASSERT_TRUE(start_result.get()); | |
| 135 | |
| 136 // Get the file name. | |
| 137 scoped_ptr<extensions::api::webrtc_logging_private::RecordingInfo> | |
| 138 recordings_info_start( | |
| 139 extensions::api::webrtc_logging_private::RecordingInfo::FromValue( | |
| 140 *start_result.get())); | |
| 141 ASSERT_TRUE(recordings_info_start.get()); | |
| 142 base::FilePath file_name_start( | |
| 143 base::FilePath::FromUTF8Unsafe(recordings_info_start->prefix_path)); | |
| 144 | |
| 145 #if !defined(OS_MACOSX) | |
| 146 // Video is choppy on Mac OS X. http://crbug.com/443542. | |
| 147 WaitForVideoToPlay(left_tab); | |
| 148 WaitForVideoToPlay(right_tab); | |
| 149 #endif | |
| 150 | |
| 151 // Stop the event log. | |
| 152 base::ListValue stop_params; | |
| 153 AppendTabIdAndUrl(&stop_params, left_tab); | |
| 154 scoped_refptr<WebrtcLoggingPrivateStopWebRtcEventLoggingFunction> | |
| 155 stop_function( | |
| 156 CreateFunction<WebrtcLoggingPrivateStopWebRtcEventLoggingFunction>()); | |
| 157 scoped_ptr<base::Value> stop_result(utils::RunFunctionAndReturnSingleResult( | |
| 158 stop_function.get(), ParamsToString(stop_params), browser())); | |
| 159 | |
| 160 // Get the file name. | |
| 161 scoped_ptr<extensions::api::webrtc_logging_private::RecordingInfo> | |
| 162 recordings_info_stop( | |
| 163 extensions::api::webrtc_logging_private::RecordingInfo::FromValue( | |
| 164 *stop_result.get())); | |
| 165 ASSERT_TRUE(recordings_info_stop.get()); | |
| 166 base::FilePath file_name_stop( | |
| 167 base::FilePath::FromUTF8Unsafe(recordings_info_stop->prefix_path)); | |
| 168 | |
| 169 HangUp(left_tab); | |
| 170 HangUp(right_tab); | |
| 171 | |
| 172 // Check that the Start and Stop functions return the same file name. | |
|
Henrik Grunell
2016/03/24 09:36:22
Remove this comment - it's clear from the code wha
terelius-chromium
2016/03/24 16:21:23
Done.
| |
| 173 EXPECT_EQ(file_name_start, file_name_stop); | |
| 174 | |
| 175 // Check that the file exists and is non-empty. | |
| 176 base::ProcessId render_process_id = | |
| 177 base::GetProcId(left_tab->GetRenderProcessHost()->GetHandle()); | |
| 178 EXPECT_NE(render_process_id, base::kNullProcessId); | |
| 179 base::FilePath full_file_name = | |
| 180 GetExpectedEventLogFileName(file_name_stop, render_process_id); | |
| 181 while (!base::PathExists(full_file_name)) { | |
|
Henrik Grunell
2016/03/24 09:36:22
This is dangerous. If the file don't exist the tes
terelius-chromium
2016/03/24 16:21:23
As discussed offline, we rely on the test timeout.
Henrik Grunell
2016/03/31 10:58:47
Acknowledged.
| |
| 182 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | |
| 183 } | |
| 184 EXPECT_TRUE(base::PathExists(full_file_name)); | |
| 185 int64_t file_size = 0; | |
| 186 EXPECT_TRUE(base::GetFileSize(full_file_name, &file_size)); | |
| 187 EXPECT_GT(file_size, 0); | |
| 188 | |
| 189 // Clean up. | |
| 190 base::DeleteFile(full_file_name, false); | |
| 191 } | |
| 192 | |
| 193 IN_PROC_BROWSER_TEST_F(WebrtcEventLogApiTest, | |
| 194 TestStartTimedWebRtcEventLogging) { | |
| 195 if (OnWinXp()) | |
| 196 return; | |
| 197 | |
| 198 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 199 | |
| 200 content::WebContents* left_tab = | |
| 201 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage); | |
| 202 content::WebContents* right_tab = | |
| 203 OpenTestPageAndGetUserMediaInNewTab(kMainWebrtcTestHtmlPage); | |
| 204 | |
| 205 SetupPeerconnectionWithLocalStream(left_tab); | |
| 206 SetupPeerconnectionWithLocalStream(right_tab); | |
| 207 | |
| 208 NegotiateCall(left_tab, right_tab, "VP8"); | |
| 209 | |
| 210 StartDetectingVideo(left_tab, "remote-view"); | |
| 211 StartDetectingVideo(right_tab, "remote-view"); | |
| 212 | |
| 213 // Start the event log. RunFunctionAndReturnSingleResult will block until a | |
| 214 // result is available, which happens when the logging stops after 1 second. | |
| 215 int seconds = 1; | |
| 216 base::ListValue start_params; | |
| 217 AppendTabIdAndUrl(&start_params, left_tab); | |
| 218 start_params.AppendInteger(seconds); | |
| 219 scoped_refptr<WebrtcLoggingPrivateStartWebRtcEventLoggingFunction> | |
| 220 start_function(CreateFunction< | |
| 221 WebrtcLoggingPrivateStartWebRtcEventLoggingFunction>()); | |
| 222 scoped_ptr<base::Value> start_result(utils::RunFunctionAndReturnSingleResult( | |
| 223 start_function.get(), ParamsToString(start_params), browser())); | |
| 224 ASSERT_TRUE(start_result.get()); | |
| 225 | |
| 226 // Get the file name. | |
| 227 scoped_ptr<extensions::api::webrtc_logging_private::RecordingInfo> | |
| 228 recordings_info_start( | |
| 229 extensions::api::webrtc_logging_private::RecordingInfo::FromValue( | |
| 230 *start_result.get())); | |
| 231 ASSERT_TRUE(recordings_info_start.get()); | |
| 232 base::FilePath file_name_start( | |
| 233 base::FilePath::FromUTF8Unsafe(recordings_info_start->prefix_path)); | |
| 234 | |
| 235 #if !defined(OS_MACOSX) | |
| 236 // Video is choppy on Mac OS X. http://crbug.com/443542. | |
| 237 WaitForVideoToPlay(left_tab); | |
| 238 WaitForVideoToPlay(right_tab); | |
| 239 #endif | |
| 240 | |
| 241 HangUp(left_tab); | |
| 242 HangUp(right_tab); | |
| 243 | |
| 244 // The log stops on it's own. Check that the file exists and is non-empty. | |
|
Henrik Grunell
2016/03/24 09:36:22
Nit: "The log has stopped automatically. (...)".
terelius-chromium
2016/03/24 16:21:23
Done.
| |
| 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 while (!base::PathExists(full_file_name)) { | |
| 251 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | |
| 252 } | |
| 253 int64_t file_size = 0; | |
| 254 EXPECT_TRUE(base::GetFileSize(full_file_name, &file_size)); | |
| 255 EXPECT_GT(file_size, 0); | |
| 256 | |
| 257 // Clean up. | |
| 258 base::DeleteFile(full_file_name, false); | |
| 259 } | |
| OLD | NEW |