Chromium Code Reviews| 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 | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "blimp/common/blob_cache/mock_blob_cache.h" | |
| 11 #include "blimp/net/blob_channel/blob_channel_receiver.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace blimp { | |
| 16 namespace { | |
| 17 | |
| 18 using testing::_; | |
| 19 using testing::Return; | |
| 20 | |
| 21 const char kBlobId1[] = "blob-1"; | |
| 22 const char kBlobPayload1[] = "blob-1-payload"; | |
| 23 | |
| 24 // Helper function for creating a cache payload vector from a string. | |
| 25 BlobDataPtr CreatePayload(const std::string& input) { | |
| 26 return new BlobData(input); | |
|
Wez
2016/04/21 00:55:02
nit: Use make_scoped_refptr(...) here.
Kevin M
2016/04/21 21:24:08
Used BlobDataPtr instead, so that we're consistent
| |
| 27 } | |
| 28 | |
| 29 MATCHER_P(BlobDataEqual, expected, "") { | |
| 30 return expected->data == arg->data; | |
| 31 } | |
| 32 | |
| 33 class MockBlobReceiverDelegate : public BlobChannelReceiver::Delegate { | |
|
Wez
2016/04/21 00:55:02
Is there any value in this being a mock, since the
Kevin M
2016/04/21 21:24:08
It will eventually have mock methods, once we have
| |
| 34 public: | |
| 35 using BlobChannelReceiver::Delegate::OnBlobReceived; | |
| 36 }; | |
| 37 | |
| 38 class BlobChannelReceiverTest : public testing::Test { | |
| 39 public: | |
| 40 BlobChannelReceiverTest() | |
| 41 : delegate_(new testing::StrictMock<MockBlobReceiverDelegate>), | |
| 42 cache_(new testing::StrictMock<MockBlobCache>), | |
| 43 blob_receiver_(new BlobChannelReceiver(base::WrapUnique(cache_), | |
| 44 base::WrapUnique(delegate_))) | |
| 45 | |
| 46 {} | |
| 47 | |
| 48 ~BlobChannelReceiverTest() override {} | |
| 49 | |
| 50 testing::StrictMock<MockBlobReceiverDelegate>* delegate_; | |
| 51 testing::StrictMock<MockBlobCache>* cache_; | |
| 52 std::unique_ptr<BlobChannelReceiver> blob_receiver_; | |
| 53 }; | |
| 54 | |
| 55 TEST_F(BlobChannelReceiverTest, TestGetFromCache) { | |
|
Wez
2016/04/21 00:55:02
Don't include Test in a test's name; we already kn
Kevin M
2016/04/21 21:24:08
Done.
| |
| 56 auto payload = CreatePayload(kBlobPayload1); | |
| 57 EXPECT_CALL(*cache_, Get(kBlobId1)).WillOnce(Return(payload)); | |
| 58 | |
| 59 auto result = blob_receiver_->Get(kBlobId1); | |
| 60 | |
| 61 ASSERT_NE(result.get(), nullptr); | |
|
Wez
2016/04/21 00:55:02
nit: No need for this, since dereferencing result
Kevin M
2016/04/21 21:24:08
Acknowledged.
| |
| 62 EXPECT_EQ(result->data, payload->data); | |
| 63 } | |
| 64 | |
| 65 TEST_F(BlobChannelReceiverTest, TestGetFromTransport) { | |
|
Wez
2016/04/21 00:55:02
This isn't really getting-from-transport, but test
Kevin M
2016/04/21 21:24:08
Yes, renamed.
| |
| 66 auto payload = CreatePayload(kBlobPayload1); | |
| 67 | |
| 68 EXPECT_CALL(*cache_, Put(kBlobId1, BlobDataEqual(payload))); | |
| 69 delegate_->OnBlobReceived(kBlobId1, payload); | |
| 70 | |
| 71 EXPECT_CALL(*cache_, Get(kBlobId1)).WillOnce(Return(payload)); | |
| 72 auto result = blob_receiver_->Get(kBlobId1); | |
|
Wez
2016/04/21 00:55:02
If you used a real BlobCache instead of a mock the
Kevin M
2016/04/21 21:24:08
Done.
Wez
2016/04/21 22:52:50
After suggesting this, I realised that an advantag
| |
| 73 | |
| 74 ASSERT_NE(nullptr, result.get()); | |
| 75 EXPECT_EQ(payload->data, result->data); | |
| 76 } | |
| 77 | |
| 78 TEST_F(BlobChannelReceiverTest, TestGetUnknownBlob) { | |
| 79 EXPECT_CALL(*cache_, Get(kBlobId1)).WillOnce(Return(nullptr)); | |
| 80 auto result = blob_receiver_->Get(kBlobId1); | |
| 81 | |
| 82 ASSERT_EQ(nullptr, result.get()); | |
| 83 } | |
| 84 | |
| 85 } // namespace | |
| 86 } // namespace blimp | |
| OLD | NEW |