• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/test/simple_test_tick_clock.h"
8 #include "base/time/tick_clock.h"
9 #include "media/cast/cast_environment.h"
10 #include "media/cast/logging/logging_defines.h"
11 #include "media/cast/logging/simple_event_subscriber.h"
12 #include "media/cast/test/fake_single_thread_task_runner.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace media {
16 namespace cast {
17 
18 class SimpleEventSubscriberTest : public ::testing::Test {
19  protected:
SimpleEventSubscriberTest()20   SimpleEventSubscriberTest()
21       : testing_clock_(new base::SimpleTestTickClock()),
22         task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_)),
23         cast_environment_(new CastEnvironment(
24             scoped_ptr<base::TickClock>(testing_clock_).Pass(),
25             task_runner_,
26             task_runner_,
27             task_runner_)) {
28     cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_);
29   }
30 
~SimpleEventSubscriberTest()31   virtual ~SimpleEventSubscriberTest() {
32     cast_environment_->Logging()->RemoveRawEventSubscriber(&event_subscriber_);
33   }
34 
35   base::SimpleTestTickClock* testing_clock_;  // Owned by CastEnvironment.
36   scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
37   scoped_refptr<CastEnvironment> cast_environment_;
38   SimpleEventSubscriber event_subscriber_;
39 };
40 
TEST_F(SimpleEventSubscriberTest,GetAndResetEvents)41 TEST_F(SimpleEventSubscriberTest, GetAndResetEvents) {
42   // Log some frame events.
43   cast_environment_->Logging()->InsertEncodedFrameEvent(
44       testing_clock_->NowTicks(), FRAME_ENCODED, AUDIO_EVENT,
45       /*rtp_timestamp*/ 100u, /*frame_id*/ 0u, /*frame_size*/ 123,
46       /*key_frame*/ false, 0);
47   cast_environment_->Logging()->InsertFrameEventWithDelay(
48       testing_clock_->NowTicks(), FRAME_PLAYOUT, AUDIO_EVENT,
49       /*rtp_timestamp*/ 100u,
50       /*frame_id*/ 0u, /*delay*/ base::TimeDelta::FromMilliseconds(100));
51   cast_environment_->Logging()->InsertFrameEvent(
52       testing_clock_->NowTicks(), FRAME_DECODED, AUDIO_EVENT,
53       /*rtp_timestamp*/ 200u,
54       /*frame_id*/ 0u);
55 
56   // Log some packet events.
57   cast_environment_->Logging()->InsertPacketEvent(
58       testing_clock_->NowTicks(), PACKET_RECEIVED, AUDIO_EVENT,
59       /*rtp_timestamp*/ 200u,
60       /*frame_id*/ 0u, /*packet_id*/ 1u, /*max_packet_id*/ 5u, /*size*/ 100u);
61   cast_environment_->Logging()->InsertPacketEvent(
62       testing_clock_->NowTicks(), FRAME_DECODED, VIDEO_EVENT,
63       /*rtp_timestamp*/ 200u, /*frame_id*/ 0u, /*packet_id*/ 1u,
64       /*max_packet_id*/ 5u, /*size*/ 100u);
65   cast_environment_->Logging()->InsertPacketEvent(
66       testing_clock_->NowTicks(), FRAME_DECODED, VIDEO_EVENT,
67       /*rtp_timestamp*/ 300u, /*frame_id*/ 0u, /*packet_id*/ 1u,
68       /*max_packet_id*/ 5u, /*size*/ 100u);
69 
70   std::vector<FrameEvent> frame_events;
71   event_subscriber_.GetFrameEventsAndReset(&frame_events);
72   EXPECT_EQ(3u, frame_events.size());
73 
74   std::vector<PacketEvent> packet_events;
75   event_subscriber_.GetPacketEventsAndReset(&packet_events);
76   EXPECT_EQ(3u, packet_events.size());
77 
78   // Calling this function again should result in empty vector because no events
79   // were logged since last call.
80   event_subscriber_.GetFrameEventsAndReset(&frame_events);
81   event_subscriber_.GetPacketEventsAndReset(&packet_events);
82   EXPECT_TRUE(frame_events.empty());
83   EXPECT_TRUE(packet_events.empty());
84 }
85 
86 }  // namespace cast
87 }  // namespace media
88