OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/bind.h" |
| 6 #include "base/macros.h" |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/media/webrtc_rtp_dump_handler.h" |
| 10 #include "chrome/browser/media/webrtc_rtp_dump_writer.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 class FakeDumpWriter : public WebRtcRtpDumpWriter { |
| 16 public: |
| 17 explicit FakeDumpWriter(size_t max_dump_size, |
| 18 const base::Closure& max_size_reached_callback) |
| 19 : WebRtcRtpDumpWriter(base::FilePath(), |
| 20 base::FilePath(), |
| 21 max_dump_size, |
| 22 base::Closure()), |
| 23 max_dump_size_(max_dump_size), |
| 24 current_dump_size_(0), |
| 25 max_size_reached_callback_(max_size_reached_callback) {} |
| 26 |
| 27 virtual void WriteRtpPacket(const uint8* packet_header, |
| 28 size_t header_length, |
| 29 size_t packet_length, |
| 30 bool incoming) OVERRIDE { |
| 31 current_dump_size_ += header_length; |
| 32 if (current_dump_size_ > max_dump_size_) |
| 33 max_size_reached_callback_.Run(); |
| 34 } |
| 35 |
| 36 virtual void EndDump( |
| 37 const base::Callback<void(bool)>& finished_callback) OVERRIDE { |
| 38 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 39 base::Bind(finished_callback, true)); |
| 40 } |
| 41 |
| 42 private: |
| 43 size_t max_dump_size_; |
| 44 size_t current_dump_size_; |
| 45 base::Closure max_size_reached_callback_; |
| 46 }; |
| 47 |
| 48 class WebRtcRtpDumpHandlerTest : public testing::Test { |
| 49 public: |
| 50 WebRtcRtpDumpHandlerTest() |
| 51 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 52 ResetDumpHandler(); |
| 53 } |
| 54 |
| 55 void ResetDumpHandler() { |
| 56 handler_.reset(new WebRtcRtpDumpHandler(base::FilePath())); |
| 57 |
| 58 scoped_ptr<WebRtcRtpDumpWriter> writer(new FakeDumpWriter( |
| 59 10, |
| 60 base::Bind(&WebRtcRtpDumpHandlerTest::OnMaxSizeReached, |
| 61 base::Unretained(this)))); |
| 62 |
| 63 handler_->SetDumpWriterForTesting(writer.Pass()); |
| 64 } |
| 65 |
| 66 MOCK_METHOD1(OnDumpReleased, |
| 67 void(const WebRtcRtpDumpHandler::ReleasedDumps& dumps)); |
| 68 |
| 69 MOCK_METHOD0(OnMaxSizeReached, void(void)); |
| 70 |
| 71 protected: |
| 72 content::TestBrowserThreadBundle thread_bundle_; |
| 73 scoped_ptr<WebRtcRtpDumpHandler> handler_; |
| 74 }; |
| 75 |
| 76 TEST_F(WebRtcRtpDumpHandlerTest, StateTransition) { |
| 77 WebRtcRtpDumpHandler::PacketType type_none(false, false); |
| 78 EXPECT_FALSE(handler_->StartDump(type_none)); |
| 79 |
| 80 WebRtcRtpDumpHandler::PacketType types[3]; |
| 81 types[0] = WebRtcRtpDumpHandler::PacketType(true, false); |
| 82 types[1] = WebRtcRtpDumpHandler::PacketType(false, true); |
| 83 types[2] = WebRtcRtpDumpHandler::PacketType(true, true); |
| 84 for (size_t i = 0; i < arraysize(types); ++i) { |
| 85 // Only StartDump is allowed in STATE_NONE. |
| 86 EXPECT_FALSE(handler_->StopDump(types[i])); |
| 87 EXPECT_FALSE( |
| 88 handler_->ReleaseDump(WebRtcRtpDumpHandler::ReleaseDumpCallback())); |
| 89 EXPECT_TRUE(handler_->StartDump(types[i])); |
| 90 |
| 91 // Only StopDump is allowed in STATE_STARTED. |
| 92 EXPECT_FALSE(handler_->StartDump(types[i])); |
| 93 EXPECT_FALSE( |
| 94 handler_->ReleaseDump(WebRtcRtpDumpHandler::ReleaseDumpCallback())); |
| 95 EXPECT_TRUE(handler_->StopDump(types[i])); |
| 96 |
| 97 // Only ReleaseDump is allowed in STATE_STOPPED. |
| 98 EXPECT_FALSE(handler_->StartDump(types[i])); |
| 99 EXPECT_FALSE(handler_->StopDump(types[i])); |
| 100 |
| 101 EXPECT_CALL(*this, OnDumpReleased(testing::_)); |
| 102 EXPECT_TRUE(handler_->ReleaseDump(base::Bind( |
| 103 &WebRtcRtpDumpHandlerTest::OnDumpReleased, base::Unretained(this)))); |
| 104 |
| 105 base::RunLoop().RunUntilIdle(); |
| 106 ResetDumpHandler(); |
| 107 } |
| 108 } |
| 109 |
| 110 TEST_F(WebRtcRtpDumpHandlerTest, StartOneDirectionDumpingFailsInInvalidState) { |
| 111 // Start incoming and then start outgoing should succeed. |
| 112 WebRtcRtpDumpHandler::PacketType type(true, false); |
| 113 EXPECT_TRUE(handler_->StartDump(type)); |
| 114 type = {false, true}; |
| 115 EXPECT_TRUE(handler_->StartDump(type)); |
| 116 |
| 117 // Start incoming when outgoing has stopped should fail. |
| 118 ResetDumpHandler(); |
| 119 type = {false, true}; |
| 120 EXPECT_TRUE(handler_->StartDump(type)); |
| 121 EXPECT_TRUE(handler_->StopDump(type)); |
| 122 type = {true, false}; |
| 123 EXPECT_FALSE(handler_->StartDump(type)); |
| 124 |
| 125 // Start incoming after outgoing dump is released and before the callback |
| 126 // happens should fail. |
| 127 ResetDumpHandler(); |
| 128 type = {false, true}; |
| 129 EXPECT_TRUE(handler_->StartDump(type)); |
| 130 EXPECT_TRUE(handler_->StopDump(type)); |
| 131 |
| 132 EXPECT_CALL(*this, OnDumpReleased(testing::_)); |
| 133 EXPECT_TRUE(handler_->ReleaseDump(base::Bind( |
| 134 &WebRtcRtpDumpHandlerTest::OnDumpReleased, base::Unretained(this)))); |
| 135 |
| 136 type = {true, false}; |
| 137 EXPECT_FALSE(handler_->StartDump(type)); |
| 138 |
| 139 base::RunLoop().RunUntilIdle(); |
| 140 } |
| 141 |
| 142 TEST_F(WebRtcRtpDumpHandlerTest, MaxDumpSizeReachedCallback) { |
| 143 WebRtcRtpDumpHandler::PacketType type(true, false); |
| 144 EXPECT_TRUE(handler_->StartDump(type)); |
| 145 EXPECT_CALL(*this, OnMaxSizeReached()); |
| 146 |
| 147 std::vector<uint8> buffer(100, 0); |
| 148 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 149 base::RunLoop().RunUntilIdle(); |
| 150 } |
| 151 |
| 152 TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingNotStarted) { |
| 153 std::vector<uint8> buffer(100, 0); |
| 154 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 155 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), false); |
| 156 base::RunLoop().RunUntilIdle(); |
| 157 } |
| 158 |
| 159 TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingStopped) { |
| 160 WebRtcRtpDumpHandler::PacketType type(true, false); |
| 161 EXPECT_TRUE(handler_->StartDump(type)); |
| 162 EXPECT_TRUE(handler_->StopDump(type)); |
| 163 |
| 164 std::vector<uint8> buffer(100, 0); |
| 165 handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true); |
| 166 base::RunLoop().RunUntilIdle(); |
| 167 } |
| 168 |
| 169 TEST_F(WebRtcRtpDumpHandlerTest, CannotStartMoreThanFiveDumps) { |
| 170 handler_.reset(); |
| 171 |
| 172 scoped_ptr<WebRtcRtpDumpHandler> handlers[6]; |
| 173 |
| 174 for (size_t i = 0; i < arraysize(handlers); ++i) { |
| 175 handlers[i].reset(new WebRtcRtpDumpHandler(base::FilePath())); |
| 176 |
| 177 WebRtcRtpDumpHandler::PacketType type(true, false); |
| 178 if (i < arraysize(handlers) - 1) |
| 179 EXPECT_TRUE(handlers[i]->StartDump(type)); |
| 180 else |
| 181 EXPECT_FALSE(handlers[i]->StartDump(type)); |
| 182 } |
| 183 } |
OLD | NEW |