| 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 #include <string> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "blimp/common/blob_cache/mock_blob_cache.h" |
| 12 #include "blimp/net/blob_channel/blob_channel_receiver.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace blimp { |
| 17 namespace { |
| 18 |
| 19 using testing::_; |
| 20 using testing::Return; |
| 21 |
| 22 const char kBlobId[] = "blob-1"; |
| 23 const char kBlobPayload[] = "blob-1-payload"; |
| 24 |
| 25 // Helper function for creating a cache payload vector from a string. |
| 26 BlobDataPtr CreatePayload(const std::string& input) { |
| 27 return BlobDataPtr(new BlobData(input)); |
| 28 } |
| 29 |
| 30 MATCHER_P(BlobDataEqual, expected, "") { |
| 31 return expected->data == arg->data; |
| 32 } |
| 33 |
| 34 class NullBlobReceiverDelegate : public BlobChannelReceiver::Delegate { |
| 35 public: |
| 36 using BlobChannelReceiver::Delegate::OnBlobReceived; |
| 37 }; |
| 38 |
| 39 class BlobChannelReceiverTest : public testing::Test { |
| 40 public: |
| 41 BlobChannelReceiverTest() |
| 42 : delegate_(new NullBlobReceiverDelegate), |
| 43 cache_(new testing::StrictMock<MockBlobCache>), |
| 44 blob_receiver_(new BlobChannelReceiver(base::WrapUnique(cache_), |
| 45 base::WrapUnique(delegate_))) |
| 46 |
| 47 {} |
| 48 |
| 49 ~BlobChannelReceiverTest() override {} |
| 50 |
| 51 NullBlobReceiverDelegate* delegate_; |
| 52 testing::StrictMock<MockBlobCache>* cache_; |
| 53 std::unique_ptr<BlobChannelReceiver> blob_receiver_; |
| 54 }; |
| 55 |
| 56 TEST_F(BlobChannelReceiverTest, GetKnownBlob) { |
| 57 auto payload = CreatePayload(kBlobPayload); |
| 58 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(payload)); |
| 59 |
| 60 auto result = blob_receiver_->Get(kBlobId); |
| 61 |
| 62 ASSERT_NE(nullptr, result.get()); |
| 63 EXPECT_EQ(result->data, payload->data); |
| 64 } |
| 65 |
| 66 TEST_F(BlobChannelReceiverTest, GetFromDelegateReceiveMethod) { |
| 67 auto payload = CreatePayload(kBlobPayload); |
| 68 |
| 69 EXPECT_CALL(*cache_, Put(kBlobId, BlobDataEqual(payload))); |
| 70 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(payload)); |
| 71 |
| 72 delegate_->OnBlobReceived(kBlobId, payload); |
| 73 |
| 74 auto result = blob_receiver_->Get(kBlobId); |
| 75 |
| 76 ASSERT_NE(nullptr, result.get()); |
| 77 EXPECT_EQ(payload->data, result->data); |
| 78 } |
| 79 |
| 80 TEST_F(BlobChannelReceiverTest, GetUnknownBlob) { |
| 81 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(nullptr)); |
| 82 auto result = blob_receiver_->Get(kBlobId); |
| 83 |
| 84 ASSERT_EQ(nullptr, result.get()); |
| 85 } |
| 86 |
| 87 } // namespace |
| 88 } // namespace blimp |
| OLD | NEW |