146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// found in the LICENSE file.
446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/bind.h"
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/files/file_util.h"
746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/files/scoped_temp_dir.h"
846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/macros.h"
946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
1046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/run_loop.h"
1146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/media/webrtc_rtp_dump_handler.h"
1246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/media/webrtc_rtp_dump_writer.h"
1346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "content/public/test/test_browser_thread_bundle.h"
1446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class FakeDumpWriter : public WebRtcRtpDumpWriter {
1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) public:
1946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  FakeDumpWriter(size_t max_dump_size,
2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                 const base::Closure& max_size_reached_callback,
2146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                 bool end_dump_success)
2246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      : WebRtcRtpDumpWriter(base::FilePath(),
2346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                            base::FilePath(),
2446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                            max_dump_size,
2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                            base::Closure()),
2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        max_dump_size_(max_dump_size),
2746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        current_dump_size_(0),
2846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        max_size_reached_callback_(max_size_reached_callback),
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        end_dump_success_(end_dump_success) {}
3046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void WriteRtpPacket(const uint8* packet_header,
3246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                              size_t header_length,
3346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                              size_t packet_length,
3446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                              bool incoming) OVERRIDE {
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    current_dump_size_ += header_length;
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (current_dump_size_ > max_dump_size_)
3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      max_size_reached_callback_.Run();
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void EndDump(RtpDumpType type,
4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                       const EndDumpCallback& finished_callback) OVERRIDE {
4246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    bool incoming_sucess = end_dump_success_;
4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    bool outgoing_success = end_dump_success_;
4446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
4546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (type == RTP_DUMP_INCOMING)
4646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      outgoing_success = false;
4746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    else if (type == RTP_DUMP_OUTGOING)
4846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      incoming_sucess = false;
4946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    base::MessageLoop::current()->PostTask(
5146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        FROM_HERE,
5246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        base::Bind(finished_callback, incoming_sucess, outgoing_success));
5346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
5446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) private:
5646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  size_t max_dump_size_;
5746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  size_t current_dump_size_;
5846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::Closure max_size_reached_callback_;
5946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool end_dump_success_;
6046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)};
6146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
6246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class WebRtcRtpDumpHandlerTest : public testing::Test {
6346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) public:
6446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  WebRtcRtpDumpHandlerTest()
6546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
6646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ResetDumpHandler(base::FilePath(), true);
6746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
6846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
6946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void ResetDumpHandler(const base::FilePath& dir, bool end_dump_success) {
7046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    handler_.reset(new WebRtcRtpDumpHandler(
7146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        dir.empty() ? base::FilePath(FILE_PATH_LITERAL("dummy")) : dir));
7246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
7346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    scoped_ptr<WebRtcRtpDumpWriter> writer(new FakeDumpWriter(
7446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        10,
7546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached,
7646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                   base::Unretained(handler_.get())),
7746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        end_dump_success));
7846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
7946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    handler_->SetDumpWriterForTesting(writer.Pass());
8046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
8146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
82f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void DeleteDumpHandler() { handler_.reset(); }
83f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
8446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void WriteFakeDumpFiles(const base::FilePath& dir,
8546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                          base::FilePath* incoming_dump,
8646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                          base::FilePath* outgoing_dump) {
8746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    *incoming_dump = dir.AppendASCII("recv");
8846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    *outgoing_dump = dir.AppendASCII("send");
8946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    const char dummy[] = "dummy";
9046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_GT(base::WriteFile(*incoming_dump, dummy, arraysize(dummy)), 0);
9146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_GT(base::WriteFile(*outgoing_dump, dummy, arraysize(dummy)), 0);
9246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
9346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
9446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MOCK_METHOD2(OnStopDumpFinished,
9546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)               void(bool success, const std::string& error));
9646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
9746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MOCK_METHOD0(OnStopOngoingDumpsFinished, void(void));
9846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) protected:
10046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  content::TestBrowserThreadBundle thread_bundle_;
10146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<WebRtcRtpDumpHandler> handler_;
10246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)};
10346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
10446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, StateTransition) {
10546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
10646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
10746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  RtpDumpType types[3];
10846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  types[0] = RTP_DUMP_INCOMING;
10946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  types[1] = RTP_DUMP_OUTGOING;
11046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  types[2] = RTP_DUMP_BOTH;
11146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
11246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  for (size_t i = 0; i < arraysize(types); ++i) {
11346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    DVLOG(2) << "Verifying state transition: type = " << types[i];
11446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
11546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // Only StartDump is allowed in STATE_NONE.
11646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_));
11746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    handler_->StopDump(types[i],
11846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                       base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
11946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                  base::Unretained(this)));
12046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
12146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    WebRtcRtpDumpHandler::ReleasedDumps empty_dumps(handler_->ReleaseDumps());
12246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_TRUE(empty_dumps.incoming_dump_path.empty());
12346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_TRUE(empty_dumps.outgoing_dump_path.empty());
12446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_TRUE(handler_->StartDump(types[i], &error));
12546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    base::RunLoop().RunUntilIdle();
12646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
12746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // Only StopDump is allowed in STATE_STARTED.
12846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_FALSE(handler_->StartDump(types[i], &error));
12946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_FALSE(handler_->ReadyToRelease());
13046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
13146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
13246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    handler_->StopDump(types[i],
13346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                       base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
13446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                  base::Unretained(this)));
13546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    base::RunLoop().RunUntilIdle();
13646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
13746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // Only ReleaseDump is allowed in STATE_STOPPED.
13846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_FALSE(handler_->StartDump(types[i], &error));
13946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
14046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_CALL(*this, OnStopDumpFinished(false, testing::_));
14146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    handler_->StopDump(types[i],
14246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                       base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
14346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                  base::Unretained(this)));
14446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_TRUE(handler_->ReadyToRelease());
14546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
14646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    WebRtcRtpDumpHandler::ReleasedDumps dumps(handler_->ReleaseDumps());
14746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (types[i] == RTP_DUMP_INCOMING || types[i] == RTP_DUMP_BOTH)
14846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      EXPECT_FALSE(dumps.incoming_dump_path.empty());
14946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
15046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (types[i] == RTP_DUMP_OUTGOING || types[i] == RTP_DUMP_BOTH)
15146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      EXPECT_FALSE(dumps.outgoing_dump_path.empty());
15246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
15346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    base::RunLoop().RunUntilIdle();
15446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ResetDumpHandler(base::FilePath(), true);
15546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
15646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
15746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
15846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, StoppedWhenMaxSizeReached) {
15946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
16046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
16146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_INCOMING, &error));
16246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
16346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::vector<uint8> buffer(100, 0);
16446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->OnRtpPacket(&buffer[0], buffer.size(), buffer.size(), true);
16546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
16646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
16746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Dumping should have been stopped, so ready to release.
16846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  WebRtcRtpDumpHandler::ReleasedDumps dumps = handler_->ReleaseDumps();
16946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(dumps.incoming_dump_path.empty());
17046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
17146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
17246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingNotStarted) {
17346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::vector<uint8> buffer(100, 0);
17446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->OnRtpPacket(&buffer[0], buffer.size(), buffer.size(), true);
17546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->OnRtpPacket(&buffer[0], buffer.size(), buffer.size(), false);
17646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
17746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
17846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
17946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, PacketIgnoredIfDumpingStopped) {
18046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
18146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
18246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_INCOMING, &error));
18346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
18446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
18546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_INCOMING,
18646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
18746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
18846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
18946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::vector<uint8> buffer(100, 0);
19046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->OnRtpPacket(&buffer[0], buffer.size(), buffer.size(), true);
19146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
19246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
19346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
19446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, CannotStartMoreThanFiveDumps) {
19546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
19646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
19746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_.reset();
19846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
19946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<WebRtcRtpDumpHandler> handlers[6];
20046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
20146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  for (size_t i = 0; i < arraysize(handlers); ++i) {
20246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    handlers[i].reset(new WebRtcRtpDumpHandler(base::FilePath()));
20346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
20446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (i < arraysize(handlers) - 1) {
20546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      EXPECT_TRUE(handlers[i]->StartDump(RTP_DUMP_INCOMING, &error));
20646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    } else {
20746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      EXPECT_FALSE(handlers[i]->StartDump(RTP_DUMP_INCOMING, &error));
20846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    }
20946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
21046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
21146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
21246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, StartStopIncomingThenStartStopOutgoing) {
21346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
21446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
21546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2);
21646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
21746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_INCOMING, &error));
21846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_INCOMING,
21946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
22046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
22146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
22246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_OUTGOING, &error));
22346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_OUTGOING,
22446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
22546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
22646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
22746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
22846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
22946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
23046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, StartIncomingStartOutgoingThenStopBoth) {
23146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
23246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
23346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
23446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
23546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_INCOMING, &error));
23646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_OUTGOING, &error));
23746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
23846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_INCOMING,
23946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
24046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
24146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
24246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
24346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
24446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
24546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, StartBothThenStopIncomingStopOutgoing) {
24646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
24746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
24846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2);
24946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
25046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error));
25146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
25246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_INCOMING,
25346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
25446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
25546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_OUTGOING,
25646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
25746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
25846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
25946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
26046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
26146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
26246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, DumpsCleanedUpIfNotReleased) {
26346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::ScopedTempDir temp_dir;
26446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
26546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ResetDumpHandler(temp_dir.path(), true);
26646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
26746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::FilePath incoming_dump, outgoing_dump;
26846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  WriteFakeDumpFiles(temp_dir.path(), &incoming_dump, &outgoing_dump);
26946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
27046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
27146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error));
27246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
27346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
27446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_BOTH,
27546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
27646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
27746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
27846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
27946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_.reset();
28046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
28146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
28246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(base::PathExists(incoming_dump));
28346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(base::PathExists(outgoing_dump));
28446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
28546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
28646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, DumpDeletedIfEndDumpFailed) {
28746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::ScopedTempDir temp_dir;
28846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
28946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
29046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Make the writer return failure on EndStream.
29146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ResetDumpHandler(temp_dir.path(), false);
29246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
29346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::FilePath incoming_dump, outgoing_dump;
29446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  WriteFakeDumpFiles(temp_dir.path(), &incoming_dump, &outgoing_dump);
29546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
29646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
29746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error));
29846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_)).Times(2);
29946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
30046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_INCOMING,
30146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
30246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
30346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
30446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
30546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(base::PathExists(incoming_dump));
30646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(base::PathExists(outgoing_dump));
30746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
30846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_OUTGOING,
30946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
31046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
31146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
31246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(base::PathExists(outgoing_dump));
31346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
31446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
31546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, StopOngoingDumpsWhileStoppingDumps) {
31646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
31746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error));
31846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
31946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  testing::InSequence s;
32046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
32146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopOngoingDumpsFinished());
32246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
32346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_BOTH,
32446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
32546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
32646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
32746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopOngoingDumps(
32846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      base::Bind(&WebRtcRtpDumpHandlerTest::OnStopOngoingDumpsFinished,
32946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                 base::Unretained(this)));
33046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
33146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
33246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
33346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  WebRtcRtpDumpHandler::ReleasedDumps dumps(handler_->ReleaseDumps());
33446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(dumps.incoming_dump_path.empty());
33546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(dumps.outgoing_dump_path.empty());
33646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
33746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
33846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, StopOngoingDumpsWhileDumping) {
33946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
34046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error));
34146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
34246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopOngoingDumpsFinished());
34346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
34446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopOngoingDumps(
34546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      base::Bind(&WebRtcRtpDumpHandlerTest::OnStopOngoingDumpsFinished,
34646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                 base::Unretained(this)));
34746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
34846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
34946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
35046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  WebRtcRtpDumpHandler::ReleasedDumps dumps(handler_->ReleaseDumps());
35146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(dumps.incoming_dump_path.empty());
35246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(dumps.outgoing_dump_path.empty());
35346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
35446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
35546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, StopOngoingDumpsWhenAlreadyStopped) {
35646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
35746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error));
35846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
35946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  {
36046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
36146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
36246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    handler_->StopDump(RTP_DUMP_BOTH,
36346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                       base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
36446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                  base::Unretained(this)));
36546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    base::RunLoop().RunUntilIdle();
36646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
36746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
36846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopOngoingDumpsFinished());
36946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopOngoingDumps(
37046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      base::Bind(&WebRtcRtpDumpHandlerTest::OnStopOngoingDumpsFinished,
37146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                 base::Unretained(this)));
37246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
37346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
37446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, StopOngoingDumpsWhileStoppingOneDump) {
37546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string error;
37646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error));
37746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
37846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  testing::InSequence s;
37946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopDumpFinished(true, testing::_));
38046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_CALL(*this, OnStopOngoingDumpsFinished());
38146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
38246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopDump(RTP_DUMP_INCOMING,
38346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                     base::Bind(&WebRtcRtpDumpHandlerTest::OnStopDumpFinished,
38446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                base::Unretained(this)));
38546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
38646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  handler_->StopOngoingDumps(
38746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      base::Bind(&WebRtcRtpDumpHandlerTest::OnStopOngoingDumpsFinished,
38846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                 base::Unretained(this)));
38946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
39046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
39146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
39246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  WebRtcRtpDumpHandler::ReleasedDumps dumps(handler_->ReleaseDumps());
39346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(dumps.incoming_dump_path.empty());
39446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_FALSE(dumps.outgoing_dump_path.empty());
39546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
396f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
397f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)TEST_F(WebRtcRtpDumpHandlerTest, DeleteHandlerBeforeStopCallback) {
398f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string error;
399f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
400f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_CALL(*this, OnStopOngoingDumpsFinished())
401f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      .WillOnce(testing::InvokeWithoutArgs(
402f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          this, &WebRtcRtpDumpHandlerTest::DeleteDumpHandler));
403f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
404f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(handler_->StartDump(RTP_DUMP_BOTH, &error));
405f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
406f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  handler_->StopOngoingDumps(
407f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      base::Bind(&WebRtcRtpDumpHandlerTest::OnStopOngoingDumpsFinished,
408f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                 base::Unretained(this)));
409f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
410f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
411f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
412