Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/media/media_internals.h" | 5 #include "content/browser/media/media_internals.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/test/histogram_tester.h" | |
| 16 #include "base/test/test_message_loop.h" | |
| 17 #include "base/threading/thread_task_runner_handle.h" | |
| 15 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "media/base/audio_parameters.h" | 20 #include "media/base/audio_parameters.h" |
| 18 #include "media/base/channel_layout.h" | 21 #include "media/base/channel_layout.h" |
| 22 #include "media/base/media_log.h" | |
| 23 #include "media/blink/watch_time_reporter.h" | |
| 24 #include "testing/gmock/include/gmock/gmock.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "ui/gfx/geometry/size.h" | 26 #include "ui/gfx/geometry/size.h" |
| 21 | 27 |
| 22 namespace { | 28 namespace { |
| 23 const int kTestComponentID = 0; | 29 const int kTestComponentID = 0; |
| 24 const char kTestDeviceID[] = "test-device-id"; | 30 const char kTestDeviceID[] = "test-device-id"; |
| 25 | 31 |
| 26 // This class encapsulates a MediaInternals reference. It also has some useful | 32 // This class encapsulates a MediaInternals reference. It also has some useful |
| 27 // methods to receive a callback, deserialize its associated data and expect | 33 // methods to receive a callback, deserialize its associated data and expect |
| 28 // integer/string values. | 34 // integer/string values. |
| 29 class MediaInternalsTestBase { | 35 class MediaInternalsTestBase { |
| 30 public: | 36 public: |
| 31 MediaInternalsTestBase() | 37 MediaInternalsTestBase() |
| 32 : media_internals_(content::MediaInternals::GetInstance()) { | 38 : media_internals_(content::MediaInternals::GetInstance()) {} |
| 33 } | |
| 34 virtual ~MediaInternalsTestBase() {} | 39 virtual ~MediaInternalsTestBase() {} |
| 35 | 40 |
| 36 protected: | 41 protected: |
| 37 // Extracts and deserializes the JSON update data; merges into |update_data_|. | 42 // Extracts and deserializes the JSON update data; merges into |update_data_|. |
| 38 void UpdateCallbackImpl(const base::string16& update) { | 43 void UpdateCallbackImpl(const base::string16& update) { |
| 39 // Each update string looks like "<JavaScript Function Name>({<JSON>});" | 44 // Each update string looks like "<JavaScript Function Name>({<JSON>});" |
| 40 // or for video capabilities: "<JavaScript Function Name>([{<JSON>}]);". | 45 // or for video capabilities: "<JavaScript Function Name>([{<JSON>}]);". |
| 41 // In the second case we will be able to extract the dictionary if it is the | 46 // In the second case we will be able to extract the dictionary if it is the |
| 42 // only member of the list. | 47 // only member of the list. |
| 43 // To use the JSON reader we need to strip out the JS function name and (). | 48 // To use the JSON reader we need to strip out the JS function name and (). |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 audio_log_->OnCreated(kTestComponentID, test_params_, kTestDeviceID); | 303 audio_log_->OnCreated(kTestComponentID, test_params_, kTestDeviceID); |
| 299 base::RunLoop().RunUntilIdle(); | 304 base::RunLoop().RunUntilIdle(); |
| 300 ExpectStatus("created"); | 305 ExpectStatus("created"); |
| 301 | 306 |
| 302 audio_log_->OnClosed(kTestComponentID); | 307 audio_log_->OnClosed(kTestComponentID); |
| 303 base::RunLoop().RunUntilIdle(); | 308 base::RunLoop().RunUntilIdle(); |
| 304 ExpectStatus("closed"); | 309 ExpectStatus("closed"); |
| 305 } | 310 } |
| 306 | 311 |
| 307 INSTANTIATE_TEST_CASE_P( | 312 INSTANTIATE_TEST_CASE_P( |
| 308 MediaInternalsAudioLogTest, MediaInternalsAudioLogTest, testing::Values( | 313 MediaInternalsAudioLogTest, |
| 309 media::AudioLogFactory::AUDIO_INPUT_CONTROLLER, | 314 MediaInternalsAudioLogTest, |
| 310 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER, | 315 testing::Values(media::AudioLogFactory::AUDIO_INPUT_CONTROLLER, |
| 311 media::AudioLogFactory::AUDIO_OUTPUT_STREAM)); | 316 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER, |
| 317 media::AudioLogFactory::AUDIO_OUTPUT_STREAM)); | |
| 318 | |
| 319 class DirectMediaLog : public media::MediaLog { | |
| 320 public: | |
| 321 explicit DirectMediaLog(int render_process_id) | |
| 322 : render_process_id_(render_process_id), | |
| 323 internals_(content::MediaInternals::GetInstance()) {} | |
| 324 | |
| 325 void AddEvent(std::unique_ptr<media::MediaLogEvent> event) override { | |
| 326 std::vector<media::MediaLogEvent> events(1, *event); | |
| 327 internals_->OnMediaEvents(render_process_id_, events); | |
| 328 } | |
| 329 | |
| 330 private: | |
| 331 ~DirectMediaLog() override {} | |
| 332 | |
| 333 const int render_process_id_; | |
| 334 MediaInternals* const internals_; | |
| 335 | |
| 336 DISALLOW_COPY_AND_ASSIGN(DirectMediaLog); | |
| 337 }; | |
| 338 | |
| 339 class MediaInternalsWatchTimeTest : public testing::Test, | |
| 340 public MediaInternalsTestBase { | |
| 341 public: | |
| 342 MediaInternalsWatchTimeTest() | |
| 343 : media_log_(new DirectMediaLog(0)), | |
| 344 watch_time_keys_(media::MediaLog::GetWatchTimeKeys()), | |
| 345 watch_time_power_keys_(media::MediaLog::GetWatchTimePowerKeys()) {} | |
| 346 | |
| 347 void Initialize(bool has_audio, | |
| 348 bool has_video, | |
| 349 bool is_mse, | |
| 350 bool is_encrypted) { | |
| 351 wtr_.reset(new media::WatchTimeReporter( | |
| 352 has_audio, has_video, is_mse, is_encrypted, true, media_log_, | |
| 353 gfx::Size(800, 600), | |
| 354 base::Bind(&MediaInternalsWatchTimeTest::GetCurrentMediaTime, | |
| 355 base::Unretained(this)))); | |
| 356 wtr_->set_reporting_interval_for_testing(); | |
| 357 } | |
| 358 | |
| 359 void CycleReportingTimer() { | |
|
sandersd (OOO until July 31)
2017/03/31 20:51:06
Seems like a misleading name since no timer is inv
DaleCurtis
2017/04/01 00:51:35
Renamed to CycleWatchTimeReporter().
| |
| 360 base::RunLoop run_loop; | |
| 361 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 362 run_loop.QuitClosure()); | |
| 363 run_loop.Run(); | |
| 364 } | |
| 365 | |
| 366 void ExpectWatchTime(const std::vector<base::StringPiece>& keys, | |
| 367 base::TimeDelta value) { | |
| 368 for (auto key : watch_time_keys_) { | |
| 369 auto it = std::find(keys.begin(), keys.end(), key); | |
| 370 if (it == keys.end()) { | |
| 371 histogram_tester_.ExpectTotalCount(key.as_string(), 0); | |
| 372 } else { | |
| 373 histogram_tester_.ExpectUniqueSample(key.as_string(), | |
| 374 value.InMilliseconds(), 1); | |
| 375 } | |
| 376 } | |
| 377 } | |
| 378 | |
| 379 MOCK_METHOD0(GetCurrentMediaTime, base::TimeDelta()); | |
| 380 | |
| 381 protected: | |
| 382 scoped_refptr<DirectMediaLog> media_log_; | |
| 383 base::HistogramTester histogram_tester_; | |
| 384 std::unique_ptr<media::WatchTimeReporter> wtr_; | |
| 385 const base::flat_set<base::StringPiece> watch_time_keys_; | |
| 386 const base::flat_set<base::StringPiece> watch_time_power_keys_; | |
| 387 | |
| 388 DISALLOW_COPY_AND_ASSIGN(MediaInternalsWatchTimeTest); | |
| 389 }; | |
| 390 | |
| 391 TEST_F(MediaInternalsWatchTimeTest, BasicAudio) { | |
| 392 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(5); | |
| 393 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10); | |
| 394 EXPECT_CALL(*this, GetCurrentMediaTime()) | |
| 395 .WillOnce(testing::Return(base::TimeDelta())) | |
| 396 .WillOnce(testing::Return(kWatchTimeEarly)) | |
| 397 .WillRepeatedly(testing::Return(kWatchTimeLate)); | |
| 398 Initialize(true, false, true, true); | |
| 399 wtr_->OnPlaying(); | |
| 400 | |
| 401 // No log should have been generated yet since the message loop has not had | |
| 402 // any chance to pump. | |
| 403 CycleReportingTimer(); | |
| 404 ExpectWatchTime({}, base::TimeDelta()); | |
| 405 | |
| 406 CycleReportingTimer(); | |
| 407 wtr_.reset(); | |
| 408 | |
| 409 ExpectWatchTime( | |
| 410 {media::MediaLog::kWatchTimeAudioAll, media::MediaLog::kWatchTimeAudioMse, | |
| 411 media::MediaLog::kWatchTimeAudioEme, media::MediaLog::kWatchTimeAudioAc, | |
| 412 media::MediaLog::kWatchTimeAudioEmbeddedExperience}, | |
| 413 kWatchTimeLate); | |
| 414 } | |
| 415 | |
| 416 TEST_F(MediaInternalsWatchTimeTest, BasicVideo) { | |
| 417 constexpr base::TimeDelta kWatchTimeEarly = base::TimeDelta::FromSeconds(5); | |
| 418 constexpr base::TimeDelta kWatchTimeLate = base::TimeDelta::FromSeconds(10); | |
| 419 EXPECT_CALL(*this, GetCurrentMediaTime()) | |
| 420 .WillOnce(testing::Return(base::TimeDelta())) | |
| 421 .WillOnce(testing::Return(kWatchTimeEarly)) | |
| 422 .WillRepeatedly(testing::Return(kWatchTimeLate)); | |
| 423 Initialize(true, true, false, true); | |
| 424 wtr_->OnPlaying(); | |
| 425 | |
| 426 // No log should have been generated yet since the message loop has not had | |
| 427 // any chance to pump. | |
| 428 CycleReportingTimer(); | |
| 429 ExpectWatchTime({}, base::TimeDelta()); | |
| 430 | |
| 431 CycleReportingTimer(); | |
| 432 wtr_.reset(); | |
| 433 | |
| 434 ExpectWatchTime({media::MediaLog::kWatchTimeAudioVideoAll, | |
| 435 media::MediaLog::kWatchTimeAudioVideoSrc, | |
| 436 media::MediaLog::kWatchTimeAudioVideoEme, | |
| 437 media::MediaLog::kWatchTimeAudioVideoAc, | |
| 438 media::MediaLog::kWatchTimeAudioVideoEmbeddedExperience}, | |
| 439 kWatchTimeLate); | |
| 440 } | |
| 312 | 441 |
| 313 } // namespace content | 442 } // namespace content |
| OLD | NEW |