| Index: chrome/browser/media/webrtc_rtp_dump_handler_unittest.cc
|
| diff --git a/chrome/browser/media/webrtc_rtp_dump_handler_unittest.cc b/chrome/browser/media/webrtc_rtp_dump_handler_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c85c52c7fa8913bcd2549674defd336f389aadfe
|
| --- /dev/null
|
| +++ b/chrome/browser/media/webrtc_rtp_dump_handler_unittest.cc
|
| @@ -0,0 +1,275 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/file_util.h"
|
| +#include "base/files/scoped_temp_dir.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/run_loop.h"
|
| +#include "chrome/browser/media/webrtc_rtp_dump_handler.h"
|
| +#include "chrome/browser/media/webrtc_rtp_dump_writer.h"
|
| +#include "content/public/test/test_browser_thread_bundle.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +class FakeDumpWriter : public WebRtcRtpDumpWriter {
|
| + public:
|
| + explicit FakeDumpWriter(size_t max_dump_size,
|
| + const base::Closure& max_size_reached_callback)
|
| + : WebRtcRtpDumpWriter(base::FilePath(),
|
| + base::FilePath(),
|
| + max_dump_size,
|
| + base::Closure()),
|
| + max_dump_size_(max_dump_size),
|
| + current_dump_size_(0),
|
| + max_size_reached_callback_(max_size_reached_callback) {}
|
| +
|
| + virtual void WriteRtpPacket(const uint8* packet_header,
|
| + size_t header_length,
|
| + size_t packet_length,
|
| + bool incoming) OVERRIDE {
|
| + current_dump_size_ += header_length;
|
| + if (current_dump_size_ > max_dump_size_)
|
| + max_size_reached_callback_.Run();
|
| + }
|
| +
|
| + virtual void EndDump(bool incoming,
|
| + const EndDumpCallback& finished_callback) OVERRIDE {
|
| + base::MessageLoop::current()->PostTask(FROM_HERE,
|
| + base::Bind(finished_callback, true));
|
| + }
|
| +
|
| + private:
|
| + size_t max_dump_size_;
|
| + size_t current_dump_size_;
|
| + base::Closure max_size_reached_callback_;
|
| +};
|
| +
|
| +class WebRtcRtpDumpHandlerTest : public testing::Test {
|
| + public:
|
| + WebRtcRtpDumpHandlerTest()
|
| + : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
|
| + ResetDumpHandler(base::FilePath());
|
| + }
|
| +
|
| + void ResetDumpHandler(const base::FilePath& dir) {
|
| + handler_.reset(
|
| + new WebRtcRtpDumpHandler(dir.empty() ? base::FilePath("dummy") : dir));
|
| +
|
| + scoped_ptr<WebRtcRtpDumpWriter> writer(new FakeDumpWriter(
|
| + 10,
|
| + base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached,
|
| + base::Unretained(handler_.get()))));
|
| +
|
| + handler_->SetDumpWriterForTesting(writer.Pass());
|
| + }
|
| +
|
| + MOCK_METHOD2(OnStopDumpFinished,
|
| + void(bool success, const std::string& error));
|
| +
|
| + protected:
|
| + content::TestBrowserThreadBundle thread_bundle_;
|
| + scoped_ptr<WebRtcRtpDumpHandler> handler_;
|
| +};
|
| +
|
| +TEST_F(WebRtcRtpDumpHandlerTest, StateTransition) {
|
| + std::string error;
|
| +
|
| + WebRtcRtpDumpHandler::PacketType types[3];
|
| + types[0] = WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY;
|
| + types[1] = WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY;
|
| + types[2] = WebRtcRtpDumpHandler::PACKET_TYPE_BOTH;
|
| +
|
| + WebRtcRtpDumpHandler::ReleasedDumps dumps;
|
| + for (size_t i = 0; i < arraysize(types); ++i) {
|
| + DVLOG(2) << "Verifying state transition: type = " << types[i];
|
| +
|
| + // Only StartDump is allowed in STATE_NONE.
|
| + EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_));
|
| + handler_->StopDump(types[i],
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| +
|
| + dumps = handler_->ReleaseDumps();
|
| + EXPECT_TRUE(dumps.incoming_dump_path.empty());
|
| + EXPECT_TRUE(dumps.outgoing_dump_path.empty());
|
| +
|
| + EXPECT_TRUE(handler_->StartDump(types[i], &error));
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + // Only StopDump is allowed in STATE_STARTED.
|
| + EXPECT_FALSE(handler_->StartDump(types[i], &error));
|
| +
|
| + dumps = handler_->ReleaseDumps();
|
| + EXPECT_TRUE(dumps.incoming_dump_path.empty());
|
| + EXPECT_TRUE(dumps.outgoing_dump_path.empty());
|
| +
|
| + EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
|
| + handler_->StopDump(types[i],
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + // Only ReleaseDump is allowed in STATE_STOPPED.
|
| + EXPECT_FALSE(handler_->StartDump(types[i], &error));
|
| +
|
| + EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_));
|
| + handler_->StopDump(types[i],
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| +
|
| + dumps = handler_->ReleaseDumps();
|
| + if (WebRtcRtpDumpHandler::TypeContainsIncoming(types[i]))
|
| + EXPECT_FALSE(dumps.incoming_dump_path.empty());
|
| +
|
| + if (WebRtcRtpDumpHandler::TypeContainsOutgoing(types[i]))
|
| + EXPECT_FALSE(dumps.outgoing_dump_path.empty());
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| + ResetDumpHandler(base::FilePath());
|
| + }
|
| +}
|
| +
|
| +TEST_F(WebRtcRtpDumpHandlerTest, StoppedWhenMaxSizeReached) {
|
| + std::string error;
|
| +
|
| + EXPECT_TRUE(handler_->StartDump(
|
| + WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error));
|
| +
|
| + std::vector<uint8> buffer(100, 0);
|
| + handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true);
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + // Dumping should have been stopped, so ready to release.
|
| + WebRtcRtpDumpHandler::ReleasedDumps dumps = handler_->ReleaseDumps();
|
| + EXPECT_FALSE(dumps.incoming_dump_path.empty());
|
| +}
|
| +
|
| +TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingNotStarted) {
|
| + std::vector<uint8> buffer(100, 0);
|
| + handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true);
|
| + handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), false);
|
| + base::RunLoop().RunUntilIdle();
|
| +}
|
| +
|
| +TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingStopped) {
|
| + std::string error;
|
| +
|
| + EXPECT_TRUE(handler_->StartDump(
|
| + WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error));
|
| +
|
| + EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
|
| + handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY,
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| +
|
| + std::vector<uint8> buffer(100, 0);
|
| + handler_->OnRtpPacket(buffer.data(), buffer.size(), buffer.size(), true);
|
| + base::RunLoop().RunUntilIdle();
|
| +}
|
| +
|
| +TEST_F(WebRtcRtpDumpHandlerTest, CannotStartMoreThanFiveDumps) {
|
| + std::string error;
|
| +
|
| + handler_.reset();
|
| +
|
| + scoped_ptr<WebRtcRtpDumpHandler> handlers[6];
|
| +
|
| + for (size_t i = 0; i < arraysize(handlers); ++i) {
|
| + handlers[i].reset(new WebRtcRtpDumpHandler(base::FilePath()));
|
| +
|
| + if (i < arraysize(handlers) - 1) {
|
| + EXPECT_TRUE(handlers[i]->StartDump(
|
| + WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error));
|
| + } else {
|
| + EXPECT_FALSE(handlers[i]->StartDump(
|
| + WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error));
|
| + }
|
| + }
|
| +}
|
| +
|
| +TEST_F(WebRtcRtpDumpHandlerTest, StartStopIncomingThenStartStopOutgoing) {
|
| + std::string error;
|
| +
|
| + EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2);
|
| +
|
| + EXPECT_TRUE(handler_->StartDump(
|
| + WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error));
|
| + handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY,
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| +
|
| + EXPECT_TRUE(handler_->StartDump(
|
| + WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY, &error));
|
| + handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY,
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| +}
|
| +
|
| +TEST_F(WebRtcRtpDumpHandlerTest, StartIncomingStartOutgoingThenStopBoth) {
|
| + std::string error;
|
| +
|
| + EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
|
| +
|
| + EXPECT_TRUE(handler_->StartDump(
|
| + WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY, &error));
|
| + EXPECT_TRUE(handler_->StartDump(
|
| + WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY, &error));
|
| +
|
| + handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY,
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| +}
|
| +
|
| +TEST_F(WebRtcRtpDumpHandlerTest, StartBothThenStopIncomingStopOutgoing) {
|
| + std::string error;
|
| +
|
| + EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2);
|
| +
|
| + EXPECT_TRUE(
|
| + handler_->StartDump(WebRtcRtpDumpHandler::PACKET_TYPE_BOTH, &error));
|
| +
|
| + handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_INCOMING_ONLY,
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| + handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_OUTGOING_ONLY,
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| +
|
| + base::RunLoop().RunUntilIdle();
|
| +}
|
| +
|
| +TEST_F(WebRtcRtpDumpHandlerTest, DumpsCleanedUpIfNotReleased) {
|
| + base::ScopedTempDir temp_dir;
|
| + ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
|
| +
|
| + ResetDumpHandler(temp_dir.path());
|
| +
|
| + base::FilePath incoming_dump = temp_dir.path().AppendASCII("recv");
|
| + base::FilePath outgoing_dump = temp_dir.path().AppendASCII("send");
|
| + const char dummy[] = "dummy";
|
| + EXPECT_GT(base::WriteFile(incoming_dump, dummy, arraysize(dummy)), 0);
|
| + EXPECT_GT(base::WriteFile(outgoing_dump, dummy, arraysize(dummy)), 0);
|
| +
|
| + std::string error;
|
| + EXPECT_TRUE(
|
| + handler_->StartDump(WebRtcRtpDumpHandler::PACKET_TYPE_BOTH, &error));
|
| +
|
| + EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
|
| + handler_->StopDump(WebRtcRtpDumpHandler::PACKET_TYPE_BOTH,
|
| + base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
|
| + base::Unretained(this)));
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + handler_.reset();
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + EXPECT_FALSE(base::PathExists(incoming_dump));
|
| + EXPECT_FALSE(base::PathExists(outgoing_dump));
|
| +}
|
|
|