Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/webrtc/webrtc_internals.h" | 5 #include "content/browser/webrtc/webrtc_internals.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/browser/webrtc/webrtc_internals_ui_observer.h" | 13 #include "content/browser/webrtc/webrtc_internals_ui_observer.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
| 15 #include "mojo/public/cpp/bindings/binding.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 static const char kContraints[] = "c"; | 22 static const char kContraints[] = "c"; |
| 22 static const char kRtcConfiguration[] = "r"; | 23 static const char kRtcConfiguration[] = "r"; |
| 23 static const char kUrl[] = "u"; | 24 static const char kUrl[] = "u"; |
| 24 | 25 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 37 value_.reset(value ? value->DeepCopy() : nullptr); | 38 value_.reset(value ? value->DeepCopy() : nullptr); |
| 38 if (loop_) | 39 if (loop_) |
| 39 loop_->Quit(); | 40 loop_->Quit(); |
| 40 } | 41 } |
| 41 | 42 |
| 42 std::string command_; | 43 std::string command_; |
| 43 std::unique_ptr<base::Value> value_; | 44 std::unique_ptr<base::Value> value_; |
| 44 base::RunLoop* loop_; | 45 base::RunLoop* loop_; |
| 45 }; | 46 }; |
| 46 | 47 |
| 48 class MockWakeLockService : public device::mojom::WakeLockService { | |
| 49 public: | |
| 50 MockWakeLockService(device::mojom::WakeLockServiceRequest request) | |
| 51 : binding_(this, std::move(request)), has_wakelock_(false) {} | |
| 52 ~MockWakeLockService() override {} | |
| 53 | |
| 54 // Implement device::mojom::WakeLockService: | |
| 55 void RequestWakeLock() override { has_wakelock_ = true; } | |
| 56 void CancelWakeLock() override { has_wakelock_ = false; } | |
| 57 void AddClient(device::mojom::WakeLockServiceRequest request) override {} | |
| 58 void HasWakeLockForTests( | |
|
chfremer
2017/05/18 17:48:31
Probably no longer needed?
ke.he
2017/05/19 03:34:50
They are purl virtual functions in the base class,
| |
| 59 const HasWakeLockForTestsCallback& callback) override {} | |
| 60 | |
| 61 bool HasWakeLock() { | |
| 62 base::RunLoop().RunUntilIdle(); | |
| 63 return has_wakelock_; | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 mojo::Binding<device::mojom::WakeLockService> binding_; | |
| 68 bool has_wakelock_; | |
| 69 }; | |
| 70 | |
| 47 // Derived class for testing only. Allows the tests to have their own instance | 71 // Derived class for testing only. Allows the tests to have their own instance |
| 48 // for testing and control the period for which WebRTCInternals will bulk up | 72 // for testing and control the period for which WebRTCInternals will bulk up |
| 49 // updates (changes down from 500ms to 1ms). | 73 // updates (changes down from 500ms to 1ms). |
| 50 class WebRTCInternalsForTest : public NON_EXPORTED_BASE(WebRTCInternals) { | 74 class WebRTCInternalsForTest : public NON_EXPORTED_BASE(WebRTCInternals) { |
| 51 public: | 75 public: |
| 52 WebRTCInternalsForTest() : WebRTCInternals(1, true) {} | 76 WebRTCInternalsForTest() |
| 77 : WebRTCInternals(1, true), | |
| 78 wake_lock_service_(mojo::MakeRequest(&wake_lock_)) {} | |
| 53 ~WebRTCInternalsForTest() override {} | 79 ~WebRTCInternalsForTest() override {} |
| 80 bool HasWakeLock() { return wake_lock_service_.HasWakeLock(); } | |
| 81 | |
| 82 private: | |
| 83 MockWakeLockService wake_lock_service_; | |
| 54 }; | 84 }; |
| 55 | 85 |
| 56 } // namespace | 86 } // namespace |
| 57 | 87 |
| 58 class WebRtcInternalsTest : public testing::Test { | 88 class WebRtcInternalsTest : public testing::Test { |
| 59 public: | 89 public: |
| 60 WebRtcInternalsTest() : io_thread_(BrowserThread::UI, &io_loop_) {} | 90 WebRtcInternalsTest() : io_thread_(BrowserThread::UI, &io_loop_) {} |
| 61 | 91 |
| 62 protected: | 92 protected: |
| 63 void VerifyString(const base::DictionaryValue* dict, | 93 void VerifyString(const base::DictionaryValue* dict, |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 | 418 |
| 389 webrtc_internals.AddObserver(&observer); | 419 webrtc_internals.AddObserver(&observer); |
| 390 webrtc_internals.FileSelectionCanceled(nullptr); | 420 webrtc_internals.FileSelectionCanceled(nullptr); |
| 391 | 421 |
| 392 loop.Run(); | 422 loop.Run(); |
| 393 | 423 |
| 394 EXPECT_EQ("audioDebugRecordingsFileSelectionCancelled", observer.command()); | 424 EXPECT_EQ("audioDebugRecordingsFileSelectionCancelled", observer.command()); |
| 395 EXPECT_EQ(nullptr, observer.value()); | 425 EXPECT_EQ(nullptr, observer.value()); |
| 396 } | 426 } |
| 397 | 427 |
| 398 TEST_F(WebRtcInternalsTest, PowerSaveBlock) { | 428 TEST_F(WebRtcInternalsTest, WakeLock) { |
| 399 int kRenderProcessId = 1; | 429 int kRenderProcessId = 1; |
| 400 const int pid = 1; | 430 const int pid = 1; |
| 401 const int lid[] = {1, 2, 3}; | 431 const int lid[] = {1, 2, 3}; |
| 402 | 432 |
| 403 WebRTCInternalsForTest webrtc_internals; | 433 WebRTCInternalsForTest webrtc_internals; |
| 404 | 434 |
| 405 // Add a few peer connections. | 435 // Add a few peer connections. |
| 406 EXPECT_EQ(0, webrtc_internals.num_open_connections()); | 436 EXPECT_EQ(0, webrtc_internals.num_open_connections()); |
| 407 EXPECT_FALSE(webrtc_internals.IsPowerSavingBlocked()); | 437 EXPECT_FALSE(webrtc_internals.HasWakeLock()); |
| 408 webrtc_internals.OnAddPeerConnection(kRenderProcessId, pid, lid[0], kUrl, | 438 webrtc_internals.OnAddPeerConnection(kRenderProcessId, pid, lid[0], kUrl, |
| 409 kRtcConfiguration, kContraints); | 439 kRtcConfiguration, kContraints); |
| 410 EXPECT_EQ(1, webrtc_internals.num_open_connections()); | 440 EXPECT_EQ(1, webrtc_internals.num_open_connections()); |
| 411 EXPECT_TRUE(webrtc_internals.IsPowerSavingBlocked()); | 441 EXPECT_TRUE(webrtc_internals.HasWakeLock()); |
| 412 | 442 |
| 413 webrtc_internals.OnAddPeerConnection(kRenderProcessId, pid, lid[1], kUrl, | 443 webrtc_internals.OnAddPeerConnection(kRenderProcessId, pid, lid[1], kUrl, |
| 414 kRtcConfiguration, kContraints); | 444 kRtcConfiguration, kContraints); |
| 415 EXPECT_EQ(2, webrtc_internals.num_open_connections()); | 445 EXPECT_EQ(2, webrtc_internals.num_open_connections()); |
| 416 EXPECT_TRUE(webrtc_internals.IsPowerSavingBlocked()); | 446 EXPECT_TRUE(webrtc_internals.HasWakeLock()); |
| 417 | 447 |
| 418 webrtc_internals.OnAddPeerConnection(kRenderProcessId, pid, lid[2], kUrl, | 448 webrtc_internals.OnAddPeerConnection(kRenderProcessId, pid, lid[2], kUrl, |
| 419 kRtcConfiguration, kContraints); | 449 kRtcConfiguration, kContraints); |
| 420 EXPECT_EQ(3, webrtc_internals.num_open_connections()); | 450 EXPECT_EQ(3, webrtc_internals.num_open_connections()); |
| 421 EXPECT_TRUE(webrtc_internals.IsPowerSavingBlocked()); | 451 EXPECT_TRUE(webrtc_internals.HasWakeLock()); |
| 422 | 452 |
| 423 // Remove a peer connection without closing it first. | 453 // Remove a peer connection without closing it first. |
| 424 webrtc_internals.OnRemovePeerConnection(pid, lid[2]); | 454 webrtc_internals.OnRemovePeerConnection(pid, lid[2]); |
| 425 EXPECT_EQ(2, webrtc_internals.num_open_connections()); | 455 EXPECT_EQ(2, webrtc_internals.num_open_connections()); |
| 426 EXPECT_TRUE(webrtc_internals.IsPowerSavingBlocked()); | 456 EXPECT_TRUE(webrtc_internals.HasWakeLock()); |
| 427 | 457 |
| 428 // Close the remaining peer connections. | 458 // Close the remaining peer connections. |
| 429 webrtc_internals.OnUpdatePeerConnection(pid, lid[1], "stop", std::string()); | 459 webrtc_internals.OnUpdatePeerConnection(pid, lid[1], "stop", std::string()); |
| 430 EXPECT_EQ(1, webrtc_internals.num_open_connections()); | 460 EXPECT_EQ(1, webrtc_internals.num_open_connections()); |
| 431 EXPECT_TRUE(webrtc_internals.IsPowerSavingBlocked()); | 461 EXPECT_TRUE(webrtc_internals.HasWakeLock()); |
| 432 | 462 |
| 433 webrtc_internals.OnUpdatePeerConnection(pid, lid[0], "stop", std::string()); | 463 webrtc_internals.OnUpdatePeerConnection(pid, lid[0], "stop", std::string()); |
| 434 EXPECT_EQ(0, webrtc_internals.num_open_connections()); | 464 EXPECT_EQ(0, webrtc_internals.num_open_connections()); |
| 435 EXPECT_FALSE(webrtc_internals.IsPowerSavingBlocked()); | 465 EXPECT_FALSE(webrtc_internals.HasWakeLock()); |
| 436 | 466 |
| 437 // Remove the remaining peer connections. | 467 // Remove the remaining peer connections. |
| 438 webrtc_internals.OnRemovePeerConnection(pid, lid[1]); | 468 webrtc_internals.OnRemovePeerConnection(pid, lid[1]); |
| 439 EXPECT_EQ(0, webrtc_internals.num_open_connections()); | 469 EXPECT_EQ(0, webrtc_internals.num_open_connections()); |
| 440 EXPECT_FALSE(webrtc_internals.IsPowerSavingBlocked()); | 470 EXPECT_FALSE(webrtc_internals.HasWakeLock()); |
| 441 | 471 |
| 442 webrtc_internals.OnRemovePeerConnection(pid, lid[0]); | 472 webrtc_internals.OnRemovePeerConnection(pid, lid[0]); |
| 443 EXPECT_EQ(0, webrtc_internals.num_open_connections()); | 473 EXPECT_EQ(0, webrtc_internals.num_open_connections()); |
| 444 EXPECT_FALSE(webrtc_internals.IsPowerSavingBlocked()); | 474 EXPECT_FALSE(webrtc_internals.HasWakeLock()); |
| 445 } | 475 } |
| 446 | 476 |
| 447 } // namespace content | 477 } // namespace content |
| OLD | NEW |