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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "blimp/common/blob_cache/id_util.h" | 10 #include "blimp/common/blob_cache/id_util.h" |
11 #include "blimp/common/blob_cache/in_memory_blob_cache.h" | 11 #include "blimp/common/blob_cache/in_memory_blob_cache.h" |
12 #include "blimp/common/blob_cache/test_util.h" | 12 #include "blimp/common/blob_cache/test_util.h" |
13 #include "blimp/net/blob_channel/blob_channel_receiver.h" | 13 #include "blimp/net/blob_channel/blob_channel_receiver.h" |
14 #include "blimp/net/blob_channel/blob_channel_sender.h" | 14 #include "blimp/net/blob_channel/blob_channel_sender.h" |
| 15 #include "blimp/net/blob_channel/mock_blob_channel_receiver.h" |
15 #include "blimp/net/test_common.h" | 16 #include "blimp/net/test_common.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace blimp { | 19 namespace blimp { |
19 namespace { | 20 namespace { |
20 | 21 |
21 using testing::_; | 22 using testing::_; |
22 using testing::SaveArg; | 23 using testing::SaveArg; |
23 | 24 |
24 const char kBlobId[] = "foo1"; | 25 const char kBlobId[] = "foo1"; |
25 const char kBlobPayload[] = "bar1"; | 26 const char kBlobPayload[] = "bar1"; |
26 | 27 |
27 // Routes sender delegate calls to a receiver delegate object. | 28 // Routes sender delegate calls to a receiver delegate object. |
28 // The caller is responsible for ensuring that the receiver delegate is deleted | 29 // The caller is responsible for ensuring that the receiver delegate is deleted |
29 // after |this| is deleted. | 30 // after |this| is deleted. |
30 class SenderDelegateProxy : public BlobChannelSender::Delegate { | 31 class SenderDelegateProxy : public BlobChannelSender::Delegate { |
31 public: | 32 public: |
32 SenderDelegateProxy() {} | 33 explicit SenderDelegateProxy(BlobChannelReceiver* receiver) |
| 34 : receiver_(receiver) {} |
33 ~SenderDelegateProxy() override {} | 35 ~SenderDelegateProxy() override {} |
34 | 36 |
35 // Returns a receiver object that will receive proxied calls sent to |this|. | |
36 std::unique_ptr<BlobChannelReceiver::Delegate> GetReceiverDelegate() { | |
37 DCHECK(!receiver_); | |
38 receiver_ = new ReceiverDelegate; | |
39 return base::WrapUnique(receiver_); | |
40 } | |
41 | |
42 private: | 37 private: |
43 class ReceiverDelegate : public BlobChannelReceiver::Delegate { | |
44 public: | |
45 using BlobChannelReceiver::Delegate::OnBlobReceived; | |
46 }; | |
47 | |
48 // BlobChannelSender implementation. | 38 // BlobChannelSender implementation. |
49 void DeliverBlob(const BlobId& id, BlobDataPtr data) override { | 39 void DeliverBlob(const BlobId& id, BlobDataPtr data) override { |
50 base::MessageLoop::current()->PostTask( | 40 base::MessageLoop::current()->PostTask( |
51 FROM_HERE, base::Bind(&ReceiverDelegate::OnBlobReceived, | 41 FROM_HERE, base::Bind(&BlobChannelReceiver::OnBlobReceived, |
52 base::Unretained(receiver_), id, data)); | 42 base::Unretained(receiver_), id, data)); |
53 } | 43 } |
54 | 44 |
55 ReceiverDelegate* receiver_ = nullptr; | 45 BlobChannelReceiver* receiver_; |
56 | 46 |
57 DISALLOW_COPY_AND_ASSIGN(SenderDelegateProxy); | 47 DISALLOW_COPY_AND_ASSIGN(SenderDelegateProxy); |
58 }; | 48 }; |
59 | 49 |
60 // Verifies compatibility between the sender and receiver, independent of | 50 // Verifies compatibility between the sender and receiver, independent of |
61 // transport-level implementation details. | 51 // transport-level implementation details. |
62 class BlobChannelIntegrationTest : public testing::Test { | 52 class BlobChannelIntegrationTest : public testing::Test { |
63 public: | 53 public: |
64 BlobChannelIntegrationTest() { | 54 BlobChannelIntegrationTest() { |
65 std::unique_ptr<SenderDelegateProxy> sender_delegate( | 55 BlobChannelReceiver* stored_receiver; |
66 new SenderDelegateProxy); | 56 std::unique_ptr<MockBlobChannelReceiverDelegate> receiver_delegate( |
67 receiver_.reset( | 57 new MockBlobChannelReceiverDelegate); |
68 new BlobChannelReceiver(base::WrapUnique(new InMemoryBlobCache), | 58 receiver_delegate_ = receiver_delegate.get(); |
69 sender_delegate->GetReceiverDelegate())); | 59 |
70 sender_.reset(new BlobChannelSender(base::WrapUnique(new InMemoryBlobCache), | 60 EXPECT_CALL(*receiver_delegate, SetReceiver(_)) |
71 std::move(sender_delegate))); | 61 .WillOnce(SaveArg<0>(&stored_receiver)); |
| 62 |
| 63 receiver_ = BlobChannelReceiver::Create( |
| 64 base::WrapUnique(new InMemoryBlobCache), std::move(receiver_delegate)); |
| 65 |
| 66 EXPECT_EQ(receiver_.get(), stored_receiver); |
| 67 |
| 68 sender_.reset(new BlobChannelSender( |
| 69 base::WrapUnique(new InMemoryBlobCache), |
| 70 base::WrapUnique(new SenderDelegateProxy(receiver_.get())))); |
72 } | 71 } |
73 | 72 |
74 ~BlobChannelIntegrationTest() override {} | 73 ~BlobChannelIntegrationTest() override {} |
75 | 74 |
76 protected: | 75 protected: |
| 76 MockBlobChannelReceiverDelegate* receiver_delegate_; |
77 std::unique_ptr<BlobChannelReceiver> receiver_; | 77 std::unique_ptr<BlobChannelReceiver> receiver_; |
78 std::unique_ptr<BlobChannelSender> sender_; | 78 std::unique_ptr<BlobChannelSender> sender_; |
79 base::MessageLoop message_loop_; | 79 base::MessageLoop message_loop_; |
80 | 80 |
81 private: | 81 private: |
82 DISALLOW_COPY_AND_ASSIGN(BlobChannelIntegrationTest); | 82 DISALLOW_COPY_AND_ASSIGN(BlobChannelIntegrationTest); |
83 }; | 83 }; |
84 | 84 |
85 TEST_F(BlobChannelIntegrationTest, Deliver) { | 85 TEST_F(BlobChannelIntegrationTest, Deliver) { |
86 const std::string blob_id = CalculateBlobId(kBlobId); | 86 const std::string blob_id = CalculateBlobId(kBlobId); |
87 | 87 |
88 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); | 88 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); |
89 sender_->PutBlob(blob_id, CreateBlobDataPtr(kBlobPayload)); | 89 sender_->PutBlob(blob_id, CreateBlobDataPtr(kBlobPayload)); |
90 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); | 90 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); |
91 | 91 |
92 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); | 92 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); |
93 sender_->DeliverBlob(blob_id); | 93 sender_->DeliverBlob(blob_id); |
94 | 94 |
95 base::RunLoop().RunUntilIdle(); | 95 base::RunLoop().RunUntilIdle(); |
96 EXPECT_EQ(kBlobPayload, receiver_->Get(blob_id)->data); | 96 EXPECT_EQ(kBlobPayload, receiver_->Get(blob_id)->data); |
97 } | 97 } |
98 | 98 |
99 } // namespace | 99 } // namespace |
100 } // namespace blimp | 100 } // namespace blimp |
OLD | NEW |