OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 <algorithm> | |
6 #include <memory> | |
7 | |
8 #include "base/memory/ptr_util.h" | |
9 #include "blimp/common/blob_cache/id_util.h" | |
10 #include "blimp/common/blob_cache/mock_blob_cache.h" | |
11 #include "blimp/net/blob_channel/blob_channel_sender_impl.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 using testing::_; | |
16 using testing::Pointee; | |
17 using testing::Return; | |
18 using testing::UnorderedElementsAre; | |
19 | |
20 namespace blimp { | |
21 namespace { | |
22 | |
23 const char kBlobId[] = | |
24 "\x20\x1e\x33\xb2\x2a\xa4\xf5\x5a\x98\xfc\x6b\x5b\x14\xc6\xab\x2b" | |
25 "\x99\xbc\xcc\x1b\x1c\xa0\xa1\x8a\xf0\x45\x4a\x04\x7f\x6b\x06\x72"; | |
26 const char kBlobPayload[] = "blob-1-payload"; | |
27 const char kBlobId2[] = | |
28 "\x90\x11\x3f\xcf\x1f\xc0\xef\x0a\x72\x11\xe0\x8d\xe4\x74\xd6\xdf" | |
29 "\x04\x04\x8d\x61\xf0\xa1\xdf\xc2\xc6\xd3\x86\x9f\xfd\x92\x97\xf1"; | |
30 | |
31 // Helper function for creating a cache payload vector from a string. | |
32 BlobDataPtr CreatePayload(const std::string& input) { | |
33 BlobDataPtr output(new BlobData(input)); | |
34 return output; | |
35 } | |
36 | |
37 MATCHER_P(BlobDataEqual, expected, "") { | |
38 return expected->data == arg->data; | |
39 } | |
40 | |
41 class MockSenderDelegate : public BlobChannelSenderImpl::Delegate { | |
42 public: | |
43 MockSenderDelegate() {} | |
44 ~MockSenderDelegate() override {} | |
45 | |
46 MOCK_METHOD2(DeliverBlob, void(const BlobId&, BlobDataPtr)); | |
47 }; | |
48 | |
49 class BlobChannelSenderTest : public testing::Test { | |
50 public: | |
51 BlobChannelSenderTest() | |
52 : mock_delegate_(new testing::StrictMock<MockSenderDelegate>), | |
53 mock_cache_(new testing::StrictMock<MockBlobCache>), | |
54 blob_sender_( | |
55 new BlobChannelSenderImpl(base::WrapUnique(mock_cache_), | |
56 base::WrapUnique(mock_delegate_))) {} | |
57 ~BlobChannelSenderTest() override {} | |
58 | |
59 testing::StrictMock<MockSenderDelegate>* mock_delegate_; | |
60 testing::StrictMock<MockBlobCache>* mock_cache_; | |
61 std::unique_ptr<BlobChannelSender> blob_sender_; | |
62 }; | |
63 | |
64 TEST_F(BlobChannelSenderTest, PutBlob) { | |
65 EXPECT_CALL(*mock_cache_, | |
66 Put(kBlobId, BlobDataEqual(CreatePayload(kBlobPayload)))); | |
67 EXPECT_CALL(*mock_cache_, Contains(kBlobId)).WillOnce(Return(false)); | |
68 blob_sender_->PutBlob(kBlobId, CreatePayload(kBlobPayload)); | |
69 } | |
70 | |
71 TEST_F(BlobChannelSenderTest, PutBlobDuplicate) { | |
72 EXPECT_CALL(*mock_cache_, Contains(kBlobId)).WillOnce(Return(true)); | |
73 blob_sender_->PutBlob(kBlobId, CreatePayload(kBlobPayload)); | |
74 } | |
75 | |
76 TEST_F(BlobChannelSenderTest, Push) { | |
77 auto payload = CreatePayload(kBlobPayload); | |
78 EXPECT_CALL(*mock_delegate_, DeliverBlob(kBlobId, BlobDataEqual(payload))); | |
79 EXPECT_CALL(*mock_cache_, Contains(kBlobId)) | |
80 .WillOnce(Return(false)) | |
81 .WillOnce(Return(true)); | |
82 EXPECT_CALL(*mock_cache_, Put(kBlobId, BlobDataEqual(payload))); | |
83 EXPECT_CALL(*mock_cache_, Get(kBlobId)).WillOnce(Return(payload)); | |
84 blob_sender_->PutBlob(kBlobId, payload); | |
85 blob_sender_->DeliverBlob(kBlobId); | |
86 } | |
87 | |
88 TEST_F(BlobChannelSenderTest, GetCachedBlobIds) { | |
89 std::string blob_id1 = CalculateBlobId(kBlobId); | |
90 std::string blob_id2 = CalculateBlobId(kBlobId2); | |
91 BlobDataPtr blob_payload1 = CreatePayload(kBlobPayload); | |
92 | |
93 EXPECT_CALL(*mock_cache_, Contains(blob_id1)).WillOnce(Return(true)); | |
94 EXPECT_CALL(*mock_cache_, Get(blob_id1)).WillOnce(Return(blob_payload1)); | |
95 | |
96 std::vector<BlobId> cache_state; | |
97 cache_state.push_back(blob_id1); | |
98 cache_state.push_back(blob_id2); | |
99 | |
100 EXPECT_CALL(*mock_cache_, GetCachedBlobIds()).WillOnce(Return(cache_state)); | |
101 EXPECT_CALL(*mock_delegate_, | |
102 DeliverBlob(blob_id1, BlobDataEqual(blob_payload1))); | |
103 | |
104 // Mark one of the blobs as delivered. | |
105 blob_sender_->DeliverBlob(blob_id1); | |
106 | |
107 std::vector<BlobChannelSender::CacheStateEntry> actual = | |
108 blob_sender_->GetCachedBlobIds(); | |
109 EXPECT_EQ(2u, actual.size()); | |
110 EXPECT_EQ(blob_id1, actual[0].id); | |
111 EXPECT_TRUE(actual[0].was_delivered); | |
112 EXPECT_EQ(blob_id2, actual[1].id); | |
113 EXPECT_FALSE(actual[1].was_delivered); | |
114 } | |
115 | |
116 } // namespace | |
117 } // namespace blimp | |
OLD | NEW |