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 "blimp/net/blob_channel/mock_blob_channel_receiver.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace blimp { | |
18 namespace { | |
19 | |
20 using testing::_; | |
21 using testing::Return; | |
22 using testing::SaveArg; | |
23 | |
24 const char kBlobId[] = | |
25 "\x20\x1e\x33\xb2\x2a\xa4\xf5\x5a\x98\xfc\x6b\x5b\x14\xc6\xab\x2b" | |
26 "\x99\xbc\xcc\x1b\x1c\xa0\xa1\x8a\xf0\x45\x4a\x04\x7f\x6b\x06\x72"; | |
27 const char kBlobPayload[] = "blob-1-payload"; | |
28 | |
29 // Helper function for creating a cache payload vector from a string. | |
30 BlobDataPtr CreatePayload(const std::string& input) { | |
31 return BlobDataPtr(new BlobData(input)); | |
32 } | |
33 | |
34 MATCHER_P(BlobDataEqual, expected, "") { | |
35 return expected->data == arg->data; | |
36 } | |
37 | |
38 class BlobChannelReceiverTest : public testing::Test { | |
39 public: | |
40 BlobChannelReceiverTest() : cache_(new testing::StrictMock<MockBlobCache>) { | |
41 BlobChannelReceiver* stored_receiver; | |
42 std::unique_ptr<MockBlobChannelReceiverDelegate> receiver_delegate( | |
43 new MockBlobChannelReceiverDelegate); | |
44 receiver_delegate_ = receiver_delegate.get(); | |
45 | |
46 EXPECT_CALL(*receiver_delegate, SetReceiver(_)) | |
47 .WillOnce(SaveArg<0>(&stored_receiver)); | |
48 | |
49 blob_receiver_ = BlobChannelReceiver::Create(base::WrapUnique(cache_), | |
50 std::move(receiver_delegate)); | |
51 } | |
52 | |
53 ~BlobChannelReceiverTest() override {} | |
54 | |
55 testing::StrictMock<MockBlobCache>* cache_; | |
56 std::unique_ptr<BlobChannelReceiver> blob_receiver_; | |
57 MockBlobChannelReceiverDelegate* receiver_delegate_; | |
58 }; | |
59 | |
60 TEST_F(BlobChannelReceiverTest, GetKnownBlob) { | |
61 auto payload = CreatePayload(kBlobPayload); | |
62 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(payload)); | |
63 | |
64 auto result = blob_receiver_->Get(kBlobId); | |
65 | |
66 ASSERT_NE(nullptr, result.get()); | |
67 EXPECT_EQ(result->data, payload->data); | |
68 } | |
69 | |
70 TEST_F(BlobChannelReceiverTest, GetFromDelegateReceiveMethod) { | |
71 auto payload = CreatePayload(kBlobPayload); | |
72 | |
73 EXPECT_CALL(*cache_, Put(kBlobId, BlobDataEqual(payload))); | |
74 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(payload)); | |
75 | |
76 blob_receiver_->OnBlobReceived(kBlobId, payload); | |
77 | |
78 auto result = blob_receiver_->Get(kBlobId); | |
79 | |
80 ASSERT_NE(nullptr, result.get()); | |
81 EXPECT_EQ(payload->data, result->data); | |
82 } | |
83 | |
84 TEST_F(BlobChannelReceiverTest, GetUnknownBlob) { | |
85 EXPECT_CALL(*cache_, Get(kBlobId)).WillOnce(Return(nullptr)); | |
86 auto result = blob_receiver_->Get(kBlobId); | |
87 | |
88 ASSERT_EQ(nullptr, result.get()); | |
89 } | |
90 | |
91 } // namespace | |
92 } // namespace blimp | |
OLD | NEW |