Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / sync / internal_api / protocol_event_buffer_unittest.cc
blobb197ec0bb160d132413f1ecf64b7453809920e58
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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/scoped_vector.h"
7 #include "base/time/time.h"
8 #include "sync/internal_api/protocol_event_buffer.h"
9 #include "sync/internal_api/public/events/poll_get_updates_request_event.h"
10 #include "sync/internal_api/public/events/protocol_event.h"
11 #include "sync/protocol/sync.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace syncer {
16 class ProtocolEventBufferTest : public ::testing::Test {
17 public:
18 ProtocolEventBufferTest();
19 ~ProtocolEventBufferTest() override;
21 static scoped_ptr<ProtocolEvent> MakeTestEvent(int64 id);
22 static bool HasId(const ProtocolEvent& event, int64 id);
24 protected:
25 ProtocolEventBuffer buffer_;
28 ProtocolEventBufferTest::ProtocolEventBufferTest() {}
30 ProtocolEventBufferTest::~ProtocolEventBufferTest() {}
32 scoped_ptr<ProtocolEvent> ProtocolEventBufferTest::MakeTestEvent(int64 id) {
33 sync_pb::ClientToServerMessage message;
34 return scoped_ptr<ProtocolEvent>(
35 new PollGetUpdatesRequestEvent(
36 base::Time::FromInternalValue(id),
37 message));
40 bool ProtocolEventBufferTest::HasId(const ProtocolEvent& event, int64 id) {
41 return event.GetTimestamp() == base::Time::FromInternalValue(id);
44 TEST_F(ProtocolEventBufferTest, AddThenReturnEvents) {
45 scoped_ptr<ProtocolEvent> e1(MakeTestEvent(1));
46 scoped_ptr<ProtocolEvent> e2(MakeTestEvent(2));
48 buffer_.RecordProtocolEvent(*e1);
49 buffer_.RecordProtocolEvent(*e2);
51 ScopedVector<ProtocolEvent> buffered_events(
52 buffer_.GetBufferedProtocolEvents());
54 ASSERT_EQ(2U, buffered_events.size());
55 EXPECT_TRUE(HasId(*(buffered_events[0]), 1));
56 EXPECT_TRUE(HasId(*(buffered_events[1]), 2));
59 TEST_F(ProtocolEventBufferTest, AddThenOverflowThenReturnEvents) {
60 for (size_t i = 0; i < ProtocolEventBuffer::kBufferSize+1; ++i) {
61 scoped_ptr<ProtocolEvent> e(MakeTestEvent(static_cast<int64>(i)));
62 buffer_.RecordProtocolEvent(*e);
65 ScopedVector<ProtocolEvent> buffered_events(
66 buffer_.GetBufferedProtocolEvents());
67 ASSERT_EQ(ProtocolEventBuffer::kBufferSize, buffered_events.size());
69 for (size_t i = 1; i < ProtocolEventBuffer::kBufferSize+1; ++i) {
70 EXPECT_TRUE(
71 HasId(*(buffered_events[i-1]), static_cast<int64>(i)));
77 } // namespace syncer