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" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 using testing::SaveArg; | 22 using testing::SaveArg; |
23 | 23 |
24 const char kBlobId[] = "foo1"; | 24 const char kBlobId[] = "foo1"; |
25 const char kBlobPayload[] = "bar1"; | 25 const char kBlobPayload[] = "bar1"; |
26 | 26 |
27 // Routes sender delegate calls to a receiver delegate object. | 27 // Routes sender delegate calls to a receiver delegate object. |
28 // The caller is responsible for ensuring that the receiver delegate is deleted | 28 // The caller is responsible for ensuring that the receiver delegate is deleted |
29 // after |this| is deleted. | 29 // after |this| is deleted. |
30 class SenderDelegateProxy : public BlobChannelSender::Delegate { | 30 class SenderDelegateProxy : public BlobChannelSender::Delegate { |
31 public: | 31 public: |
32 SenderDelegateProxy() {} | 32 explicit SenderDelegateProxy(BlobChannelReceiver* receiver) |
33 : receiver_(receiver) {} | |
33 ~SenderDelegateProxy() override {} | 34 ~SenderDelegateProxy() override {} |
34 | 35 |
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: | 36 private: |
43 class ReceiverDelegate : public BlobChannelReceiver::Delegate { | |
44 public: | |
45 using BlobChannelReceiver::Delegate::OnBlobReceived; | |
46 }; | |
47 | |
48 // BlobChannelSender implementation. | 37 // BlobChannelSender implementation. |
49 void DeliverBlob(const BlobId& id, BlobDataPtr data) override { | 38 void DeliverBlob(const BlobId& id, BlobDataPtr data) override { |
50 base::MessageLoop::current()->PostTask( | 39 base::MessageLoop::current()->PostTask( |
51 FROM_HERE, base::Bind(&ReceiverDelegate::OnBlobReceived, | 40 FROM_HERE, base::Bind(&BlobChannelReceiver::OnBlobReceived, |
52 base::Unretained(receiver_), id, data)); | 41 base::Unretained(receiver_), id, data)); |
53 } | 42 } |
54 | 43 |
55 ReceiverDelegate* receiver_ = nullptr; | 44 BlobChannelReceiver* receiver_; |
56 | 45 |
57 DISALLOW_COPY_AND_ASSIGN(SenderDelegateProxy); | 46 DISALLOW_COPY_AND_ASSIGN(SenderDelegateProxy); |
58 }; | 47 }; |
59 | 48 |
60 // Verifies compatibility between the sender and receiver, independent of | 49 // Verifies compatibility between the sender and receiver, independent of |
61 // transport-level implementation details. | 50 // transport-level implementation details. |
62 class BlobChannelIntegrationTest : public testing::Test { | 51 class BlobChannelIntegrationTest : public testing::Test { |
63 public: | 52 public: |
53 class NullBlobReceiverDelegate : public BlobChannelReceiver::Delegate { | |
Wez
2016/05/25 02:47:44
nit: Seems more natural to declare this in a top-l
Kevin M
2016/05/25 20:23:38
Done.
| |
54 public: | |
55 void SetReceiver(BlobChannelReceiver* receiver) override {} | |
56 }; | |
57 | |
64 BlobChannelIntegrationTest() { | 58 BlobChannelIntegrationTest() { |
65 std::unique_ptr<SenderDelegateProxy> sender_delegate( | 59 receiver_ = BlobChannelReceiver::Create( |
66 new SenderDelegateProxy); | 60 base::WrapUnique(new InMemoryBlobCache), |
67 receiver_.reset( | 61 base::WrapUnique(new NullBlobReceiverDelegate)); |
68 new BlobChannelReceiver(base::WrapUnique(new InMemoryBlobCache), | 62 sender_.reset(new BlobChannelSender( |
69 sender_delegate->GetReceiverDelegate())); | 63 base::WrapUnique(new InMemoryBlobCache), |
70 sender_.reset(new BlobChannelSender(base::WrapUnique(new InMemoryBlobCache), | 64 base::WrapUnique(new SenderDelegateProxy(receiver_.get())))); |
71 std::move(sender_delegate))); | |
72 } | 65 } |
73 | 66 |
74 ~BlobChannelIntegrationTest() override {} | 67 ~BlobChannelIntegrationTest() override {} |
75 | 68 |
76 protected: | 69 protected: |
77 std::unique_ptr<BlobChannelReceiver> receiver_; | 70 std::unique_ptr<BlobChannelReceiver> receiver_; |
78 std::unique_ptr<BlobChannelSender> sender_; | 71 std::unique_ptr<BlobChannelSender> sender_; |
79 base::MessageLoop message_loop_; | 72 base::MessageLoop message_loop_; |
80 | 73 |
81 private: | 74 private: |
82 DISALLOW_COPY_AND_ASSIGN(BlobChannelIntegrationTest); | 75 DISALLOW_COPY_AND_ASSIGN(BlobChannelIntegrationTest); |
83 }; | 76 }; |
84 | 77 |
85 TEST_F(BlobChannelIntegrationTest, Deliver) { | 78 TEST_F(BlobChannelIntegrationTest, Deliver) { |
86 const std::string blob_id = CalculateBlobId(kBlobId); | 79 const std::string blob_id = CalculateBlobId(kBlobId); |
87 | 80 |
88 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); | 81 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); |
89 sender_->PutBlob(blob_id, CreateBlobDataPtr(kBlobPayload)); | 82 sender_->PutBlob(blob_id, CreateBlobDataPtr(kBlobPayload)); |
90 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); | 83 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); |
91 | 84 |
92 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); | 85 EXPECT_EQ(nullptr, receiver_->Get(blob_id).get()); |
93 sender_->DeliverBlob(blob_id); | 86 sender_->DeliverBlob(blob_id); |
94 | 87 |
95 base::RunLoop().RunUntilIdle(); | 88 base::RunLoop().RunUntilIdle(); |
96 EXPECT_EQ(kBlobPayload, receiver_->Get(blob_id)->data); | 89 EXPECT_EQ(kBlobPayload, receiver_->Get(blob_id)->data); |
97 } | 90 } |
98 | 91 |
99 } // namespace | 92 } // namespace |
100 } // namespace blimp | 93 } // namespace blimp |
OLD | NEW |