| 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/mock_blob_cache.h" |
| 10 #include "blimp/net/blob_channel/blob_channel_sender.h" |
| 11 #include "blimp/net/blob_channel/mock_blob_channel_bindings.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace blimp { |
| 16 namespace { |
| 17 |
| 18 using testing::Pointee; |
| 19 using testing::Return; |
| 20 |
| 21 const char kBlobId[] = "blob-1"; |
| 22 const char kBlobPayload[] = "blob-1-payload"; |
| 23 |
| 24 // Helper function for creating a cache payload vector from a string. |
| 25 scoped_refptr<RefCountedVector> CreatePayload(const std::string& input) { |
| 26 scoped_refptr<RefCountedVector> output(new RefCountedVector); |
| 27 output->data.assign(input.begin(), input.end()); |
| 28 return output; |
| 29 } |
| 30 |
| 31 std::vector<uint8_t> StringToVector(const std::string& str) { |
| 32 std::vector<uint8_t> output; |
| 33 output.assign(str.begin(), str.end()); |
| 34 return output; |
| 35 } |
| 36 |
| 37 // Checks if a RefCountedVector matches |expected|. |
| 38 MATCHER_P(RefCountedEqualsContainer, expected, "") { |
| 39 return (expected.size() == arg.data.size()) && |
| 40 std::equal(arg.data.begin(), arg.data.end(), expected.begin()); |
| 41 } |
| 42 |
| 43 // Verifies that a sequence container's contents match those of another |
| 44 // container. The container types may differ (e.g. string & vector<char>). |
| 45 MATCHER_P(EqualsContainer, expected, "") { |
| 46 return (expected.size() == arg.size()) && |
| 47 std::equal(arg.begin(), arg.end(), expected.begin()); |
| 48 } |
| 49 |
| 50 class BlobChannelSenderTest : public testing::Test { |
| 51 public: |
| 52 BlobChannelSenderTest() : blob_sender_(&mock_cache_, &mock_bindings_) {} |
| 53 ~BlobChannelSenderTest() override {} |
| 54 |
| 55 testing::StrictMock<MockBlobSenderBindings> mock_bindings_; |
| 56 testing::StrictMock<MockBlobCache> mock_cache_; |
| 57 BlobChannelSender blob_sender_; |
| 58 }; |
| 59 |
| 60 TEST_F(BlobChannelSenderTest, TestPut) { |
| 61 EXPECT_CALL(mock_cache_, Put(kBlobId, Pointee(RefCountedEqualsContainer( |
| 62 std::string(kBlobPayload))))); |
| 63 EXPECT_CALL(mock_cache_, Contains(kBlobId)).WillOnce(Return(false)); |
| 64 blob_sender_.Put( |
| 65 kBlobId, |
| 66 base::WrapUnique(new std::vector<uint8_t>(StringToVector(kBlobPayload)))); |
| 67 } |
| 68 |
| 69 TEST_F(BlobChannelSenderTest, TestPutDuplicate) { |
| 70 EXPECT_CALL(mock_cache_, Contains(kBlobId)).WillOnce(Return(true)); |
| 71 blob_sender_.Put( |
| 72 kBlobId, |
| 73 base::WrapUnique(new std::vector<uint8_t>(StringToVector(kBlobPayload)))); |
| 74 } |
| 75 |
| 76 TEST_F(BlobChannelSenderTest, TestPush) { |
| 77 auto payload = CreatePayload(kBlobPayload); |
| 78 EXPECT_CALL(mock_bindings_, |
| 79 Send(kBlobId, EqualsContainer(std::string(kBlobPayload)))); |
| 80 EXPECT_CALL(mock_cache_, Contains(kBlobId)) |
| 81 .WillOnce(Return(false)) |
| 82 .WillOnce(Return(true)); |
| 83 EXPECT_CALL(mock_cache_, Put(kBlobId, Pointee(RefCountedEqualsContainer( |
| 84 std::string(kBlobPayload))))); |
| 85 EXPECT_CALL(mock_cache_, Get(kBlobId)).WillOnce(Return(payload.get())); |
| 86 blob_sender_.Put( |
| 87 kBlobId, |
| 88 base::WrapUnique(new std::vector<uint8_t>(StringToVector(kBlobPayload)))); |
| 89 blob_sender_.Push(kBlobId); |
| 90 } |
| 91 |
| 92 TEST_F(BlobChannelSenderTest, TestPushUnknownBlobId) { |
| 93 EXPECT_CALL(mock_cache_, Contains(kBlobId)).WillOnce(Return(false)); |
| 94 blob_sender_.Push(kBlobId); |
| 95 } |
| 96 |
| 97 } // namespace |
| 98 } // namespace blimp |
| OLD | NEW |