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 <gtest/gtest.h>
6#include <stdint.h>
7
8#include "base/bind.h"
9#include "base/bind_helpers.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/message_loop/message_loop.h"
12#include "base/run_loop.h"
13#include "base/test/simple_test_tick_clock.h"
14#include "media/cast/cast_config.h"
15#include "media/cast/rtcp/rtcp.h"
16#include "media/cast/test/fake_single_thread_task_runner.h"
17#include "media/cast/transport/cast_transport_config.h"
18#include "media/cast/transport/cast_transport_sender_impl.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
21namespace media {
22namespace cast {
23namespace transport {
24
25static const int64 kStartMillisecond = INT64_C(12345678900000);
26
27class FakePacketSender : public transport::PacketSender {
28 public:
29  FakePacketSender() {}
30
31  virtual bool SendPacket(PacketRef packet, const base::Closure& cb) OVERRIDE {
32    return true;
33  }
34};
35
36class CastTransportSenderImplTest : public ::testing::Test {
37 protected:
38  CastTransportSenderImplTest()
39      : num_times_callback_called_(0) {
40    testing_clock_.Advance(
41        base::TimeDelta::FromMilliseconds(kStartMillisecond));
42    task_runner_ = new test::FakeSingleThreadTaskRunner(&testing_clock_);
43  }
44
45  virtual ~CastTransportSenderImplTest() {}
46
47  void InitWithoutLogging() {
48    transport_sender_.reset(
49        new CastTransportSenderImpl(NULL,
50                                    &testing_clock_,
51                                    net::IPEndPoint(),
52                                    base::Bind(&UpdateCastTransportStatus),
53                                    BulkRawEventsCallback(),
54                                    base::TimeDelta(),
55                                    task_runner_,
56                                    &transport_));
57    task_runner_->RunTasks();
58  }
59
60  void InitWithLogging() {
61    transport_sender_.reset(new CastTransportSenderImpl(
62        NULL,
63        &testing_clock_,
64        net::IPEndPoint(),
65        base::Bind(&UpdateCastTransportStatus),
66        base::Bind(&CastTransportSenderImplTest::LogRawEvents,
67                   base::Unretained(this)),
68        base::TimeDelta::FromMilliseconds(10),
69        task_runner_,
70        &transport_));
71    task_runner_->RunTasks();
72  }
73
74  void LogRawEvents(const std::vector<PacketEvent>& packet_events) {
75    num_times_callback_called_++;
76    if (num_times_callback_called_ == 3) {
77      run_loop_.Quit();
78    }
79  }
80
81  static void UpdateCastTransportStatus(transport::CastTransportStatus status) {
82  }
83
84  base::SimpleTestTickClock testing_clock_;
85  scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
86  scoped_ptr<CastTransportSenderImpl> transport_sender_;
87  FakePacketSender transport_;
88  base::MessageLoopForIO message_loop_;
89  base::RunLoop run_loop_;
90  int num_times_callback_called_;
91};
92
93TEST_F(CastTransportSenderImplTest, InitWithoutLogging) {
94  InitWithoutLogging();
95  message_loop_.PostDelayedTask(FROM_HERE,
96                                run_loop_.QuitClosure(),
97                                base::TimeDelta::FromMilliseconds(50));
98  run_loop_.Run();
99  EXPECT_EQ(0, num_times_callback_called_);
100}
101
102TEST_F(CastTransportSenderImplTest, InitWithLogging) {
103  InitWithLogging();
104  message_loop_.PostDelayedTask(FROM_HERE,
105                                run_loop_.QuitClosure(),
106                                base::TimeDelta::FromMilliseconds(50));
107  run_loop_.Run();
108  EXPECT_GT(num_times_callback_called_, 1);
109}
110
111}  // namespace transport
112}  // namespace cast
113}  // namespace media
114