OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <memory> | 6 #include <memory> |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 | 24 |
25 // Helper function for creating a cache payload vector from a string. | 25 // Helper function for creating a cache payload vector from a string. |
26 BlobDataPtr CreatePayload(const std::string& input) { | 26 BlobDataPtr CreatePayload(const std::string& input) { |
27 return BlobDataPtr(new BlobData(input)); | 27 return BlobDataPtr(new BlobData(input)); |
28 } | 28 } |
29 | 29 |
30 MATCHER_P(BlobDataEqual, expected, "") { | 30 MATCHER_P(BlobDataEqual, expected, "") { |
31 return expected->data == arg->data; | 31 return expected->data == arg->data; |
32 } | 32 } |
33 | 33 |
34 class NullBlobReceiverDelegate : public BlobChannelReceiver::Delegate { | 34 class NullBlobReceiverDelegate : public BlobChannelReceiver::Delegate { |
Wez
2016/05/25 02:47:44
nit: You could move this into test_common.h since
Kevin M
2016/05/25 20:23:38
Moved to mock_blob_receiver.h, added mock expectat
| |
35 public: | 35 public: |
36 using BlobChannelReceiver::Delegate::OnBlobReceived; | 36 void SetReceiver(BlobChannelReceiver* receiver) override {} |
37 }; | 37 }; |
38 | 38 |
39 class BlobChannelReceiverTest : public testing::Test { | 39 class BlobChannelReceiverTest : public testing::Test { |
40 public: | 40 public: |
41 BlobChannelReceiverTest() | 41 BlobChannelReceiverTest() |
42 : delegate_(new NullBlobReceiverDelegate), | 42 : cache_(new testing::StrictMock<MockBlobCache>), |
43 cache_(new testing::StrictMock<MockBlobCache>), | 43 blob_receiver_(BlobChannelReceiver::Create( |
44 blob_receiver_(new BlobChannelReceiver(base::WrapUnique(cache_), | 44 base::WrapUnique(cache_), |
45 base::WrapUnique(delegate_))) | 45 base::WrapUnique(new NullBlobReceiverDelegate))) |
46 | 46 |
47 {} | 47 {} |
48 | 48 |
49 ~BlobChannelReceiverTest() override {} | 49 ~BlobChannelReceiverTest() override {} |
50 | 50 |
51 NullBlobReceiverDelegate* delegate_; | |
52 testing::StrictMock<MockBlobCache>* cache_; | 51 testing::StrictMock<MockBlobCache>* cache_; |
53 std::unique_ptr<BlobChannelReceiver> blob_receiver_; | 52 std::unique_ptr<BlobChannelReceiver> blob_receiver_; |
54 }; | 53 }; |
55 | 54 |
56 TEST_F(BlobChannelReceiverTest, GetKnownBlob) { | 55 TEST_F(BlobChannelReceiverTest, GetKnownBlob) { |
57 auto payload = CreatePayload(kBlobPayload); | 56 auto payload = CreatePayload(kBlobPayload); |
58 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(payload)); | 57 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(payload)); |
59 | 58 |
60 auto result = blob_receiver_->Get(kBlobId); | 59 auto result = blob_receiver_->Get(kBlobId); |
61 | 60 |
62 ASSERT_NE(nullptr, result.get()); | 61 ASSERT_NE(nullptr, result.get()); |
63 EXPECT_EQ(result->data, payload->data); | 62 EXPECT_EQ(result->data, payload->data); |
64 } | 63 } |
65 | 64 |
66 TEST_F(BlobChannelReceiverTest, GetFromDelegateReceiveMethod) { | 65 TEST_F(BlobChannelReceiverTest, GetFromDelegateReceiveMethod) { |
67 auto payload = CreatePayload(kBlobPayload); | 66 auto payload = CreatePayload(kBlobPayload); |
68 | 67 |
69 EXPECT_CALL(*cache_, Put(kBlobId, BlobDataEqual(payload))); | 68 EXPECT_CALL(*cache_, Put(kBlobId, BlobDataEqual(payload))); |
70 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(payload)); | 69 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(payload)); |
71 | 70 |
72 delegate_->OnBlobReceived(kBlobId, payload); | 71 blob_receiver_->OnBlobReceived(kBlobId, payload); |
73 | 72 |
74 auto result = blob_receiver_->Get(kBlobId); | 73 auto result = blob_receiver_->Get(kBlobId); |
75 | 74 |
76 ASSERT_NE(nullptr, result.get()); | 75 ASSERT_NE(nullptr, result.get()); |
77 EXPECT_EQ(payload->data, result->data); | 76 EXPECT_EQ(payload->data, result->data); |
78 } | 77 } |
79 | 78 |
80 TEST_F(BlobChannelReceiverTest, GetUnknownBlob) { | 79 TEST_F(BlobChannelReceiverTest, GetUnknownBlob) { |
81 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(nullptr)); | 80 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(nullptr)); |
82 auto result = blob_receiver_->Get(kBlobId); | 81 auto result = blob_receiver_->Get(kBlobId); |
83 | 82 |
84 ASSERT_EQ(nullptr, result.get()); | 83 ASSERT_EQ(nullptr, result.get()); |
85 } | 84 } |
86 | 85 |
87 } // namespace | 86 } // namespace |
88 } // namespace blimp | 87 } // namespace blimp |
OLD | NEW |