| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sync/internal_api/public/attachments/attachment_store_handle.h" | 5 #include "sync/internal_api/public/attachments/attachment_store_handle.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "sync/api/attachments/attachment.h" | 14 #include "sync/api/attachments/attachment.h" |
| 15 #include "sync/api/attachments/attachment_id.h" | 15 #include "sync/api/attachments/attachment_id.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace syncer { | 18 namespace syncer { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 class MockAttachmentStore : public AttachmentStoreBase { | 22 class MockAttachmentStore : public AttachmentStoreBase { |
| 23 public: | 23 public: |
| 24 MockAttachmentStore(const base::Closure& read_called, | 24 MockAttachmentStore(const base::Closure& load_called, |
| 25 const base::Closure& read_called, |
| 25 const base::Closure& write_called, | 26 const base::Closure& write_called, |
| 26 const base::Closure& drop_called, | 27 const base::Closure& drop_called, |
| 27 const base::Closure& dtor_called) | 28 const base::Closure& dtor_called) |
| 28 : read_called_(read_called), | 29 : load_called_(load_called), |
| 30 read_called_(read_called), |
| 29 write_called_(write_called), | 31 write_called_(write_called), |
| 30 drop_called_(drop_called), | 32 drop_called_(drop_called), |
| 31 dtor_called_(dtor_called) {} | 33 dtor_called_(dtor_called) {} |
| 32 | 34 |
| 33 virtual ~MockAttachmentStore() { | 35 virtual ~MockAttachmentStore() { |
| 34 dtor_called_.Run(); | 36 dtor_called_.Run(); |
| 35 } | 37 } |
| 36 | 38 |
| 39 virtual void Load(const LoadCallback& callback) override { |
| 40 load_called_.Run(); |
| 41 } |
| 42 |
| 37 virtual void Read(const AttachmentIdList& ids, | 43 virtual void Read(const AttachmentIdList& ids, |
| 38 const ReadCallback& callback) override { | 44 const ReadCallback& callback) override { |
| 39 read_called_.Run(); | 45 read_called_.Run(); |
| 40 } | 46 } |
| 41 | 47 |
| 42 virtual void Write(const AttachmentList& attachments, | 48 virtual void Write(const AttachmentList& attachments, |
| 43 const WriteCallback& callback) override { | 49 const WriteCallback& callback) override { |
| 44 write_called_.Run(); | 50 write_called_.Run(); |
| 45 } | 51 } |
| 46 | 52 |
| 47 virtual void Drop(const AttachmentIdList& ids, | 53 virtual void Drop(const AttachmentIdList& ids, |
| 48 const DropCallback& callback) override { | 54 const DropCallback& callback) override { |
| 49 drop_called_.Run(); | 55 drop_called_.Run(); |
| 50 } | 56 } |
| 51 | 57 |
| 58 base::Closure load_called_; |
| 52 base::Closure read_called_; | 59 base::Closure read_called_; |
| 53 base::Closure write_called_; | 60 base::Closure write_called_; |
| 54 base::Closure drop_called_; | 61 base::Closure drop_called_; |
| 55 base::Closure dtor_called_; | 62 base::Closure dtor_called_; |
| 56 }; | 63 }; |
| 57 | 64 |
| 58 } // namespace | 65 } // namespace |
| 59 | 66 |
| 60 class AttachmentStoreHandleTest : public testing::Test { | 67 class AttachmentStoreHandleTest : public testing::Test { |
| 61 protected: | 68 protected: |
| 62 AttachmentStoreHandleTest() | 69 AttachmentStoreHandleTest() |
| 63 : read_call_count_(0), | 70 : load_call_count_(0), |
| 71 read_call_count_(0), |
| 64 write_call_count_(0), | 72 write_call_count_(0), |
| 65 drop_call_count_(0), | 73 drop_call_count_(0), |
| 66 dtor_call_count_(0) {} | 74 dtor_call_count_(0) {} |
| 67 | 75 |
| 68 virtual void SetUp() { | 76 virtual void SetUp() { |
| 69 scoped_ptr<AttachmentStoreBase> backend(new MockAttachmentStore( | 77 scoped_ptr<AttachmentStoreBase> backend(new MockAttachmentStore( |
| 78 base::Bind(&AttachmentStoreHandleTest::LoadCalled, |
| 79 base::Unretained(this)), |
| 70 base::Bind(&AttachmentStoreHandleTest::ReadCalled, | 80 base::Bind(&AttachmentStoreHandleTest::ReadCalled, |
| 71 base::Unretained(this)), | 81 base::Unretained(this)), |
| 72 base::Bind(&AttachmentStoreHandleTest::WriteCalled, | 82 base::Bind(&AttachmentStoreHandleTest::WriteCalled, |
| 73 base::Unretained(this)), | 83 base::Unretained(this)), |
| 74 base::Bind(&AttachmentStoreHandleTest::DropCalled, | 84 base::Bind(&AttachmentStoreHandleTest::DropCalled, |
| 75 base::Unretained(this)), | 85 base::Unretained(this)), |
| 76 base::Bind(&AttachmentStoreHandleTest::DtorCalled, | 86 base::Bind(&AttachmentStoreHandleTest::DtorCalled, |
| 77 base::Unretained(this)))); | 87 base::Unretained(this)))); |
| 78 attachment_store_handle_ = new AttachmentStoreHandle( | 88 attachment_store_handle_ = new AttachmentStoreHandle( |
| 79 backend.Pass(), base::ThreadTaskRunnerHandle::Get()); | 89 backend.Pass(), base::ThreadTaskRunnerHandle::Get()); |
| 80 } | 90 } |
| 81 | 91 |
| 92 static void DoneWithResult(const AttachmentStore::Result& result) { |
| 93 NOTREACHED(); |
| 94 } |
| 95 |
| 82 static void ReadDone(const AttachmentStore::Result& result, | 96 static void ReadDone(const AttachmentStore::Result& result, |
| 83 scoped_ptr<AttachmentMap> attachments, | 97 scoped_ptr<AttachmentMap> attachments, |
| 84 scoped_ptr<AttachmentIdList> unavailable_attachments) { | 98 scoped_ptr<AttachmentIdList> unavailable_attachments) { |
| 85 NOTREACHED(); | 99 NOTREACHED(); |
| 86 } | 100 } |
| 87 | 101 |
| 88 static void WriteDone(const AttachmentStore::Result& result) { | 102 void LoadCalled() { ++load_call_count_; } |
| 89 NOTREACHED(); | |
| 90 } | |
| 91 | |
| 92 static void DropDone(const AttachmentStore::Result& result) { | |
| 93 NOTREACHED(); | |
| 94 } | |
| 95 | 103 |
| 96 void ReadCalled() { ++read_call_count_; } | 104 void ReadCalled() { ++read_call_count_; } |
| 97 | 105 |
| 98 void WriteCalled() { ++write_call_count_; } | 106 void WriteCalled() { ++write_call_count_; } |
| 99 | 107 |
| 100 void DropCalled() { ++drop_call_count_; } | 108 void DropCalled() { ++drop_call_count_; } |
| 101 | 109 |
| 102 void DtorCalled() { ++dtor_call_count_; } | 110 void DtorCalled() { ++dtor_call_count_; } |
| 103 | 111 |
| 104 void RunMessageLoop() { | 112 void RunMessageLoop() { |
| 105 base::RunLoop run_loop; | 113 base::RunLoop run_loop; |
| 106 run_loop.RunUntilIdle(); | 114 run_loop.RunUntilIdle(); |
| 107 } | 115 } |
| 108 | 116 |
| 109 base::MessageLoop message_loop_; | 117 base::MessageLoop message_loop_; |
| 110 scoped_refptr<AttachmentStoreHandle> attachment_store_handle_; | 118 scoped_refptr<AttachmentStoreHandle> attachment_store_handle_; |
| 119 int load_call_count_; |
| 111 int read_call_count_; | 120 int read_call_count_; |
| 112 int write_call_count_; | 121 int write_call_count_; |
| 113 int drop_call_count_; | 122 int drop_call_count_; |
| 114 int dtor_call_count_; | 123 int dtor_call_count_; |
| 115 }; | 124 }; |
| 116 | 125 |
| 117 // Test that method calls are forwarded to backend loop | 126 // Test that method calls are forwarded to backend loop |
| 118 TEST_F(AttachmentStoreHandleTest, MethodsCalled) { | 127 TEST_F(AttachmentStoreHandleTest, MethodsCalled) { |
| 119 AttachmentIdList ids; | 128 AttachmentIdList ids; |
| 120 AttachmentList attachments; | 129 AttachmentList attachments; |
| 121 | 130 |
| 131 attachment_store_handle_->Load( |
| 132 base::Bind(&AttachmentStoreHandleTest::DoneWithResult)); |
| 133 EXPECT_EQ(load_call_count_, 0); |
| 134 RunMessageLoop(); |
| 135 EXPECT_EQ(load_call_count_, 1); |
| 136 |
| 122 attachment_store_handle_->Read( | 137 attachment_store_handle_->Read( |
| 123 ids, base::Bind(&AttachmentStoreHandleTest::ReadDone)); | 138 ids, base::Bind(&AttachmentStoreHandleTest::ReadDone)); |
| 124 EXPECT_EQ(read_call_count_, 0); | 139 EXPECT_EQ(read_call_count_, 0); |
| 125 RunMessageLoop(); | 140 RunMessageLoop(); |
| 126 EXPECT_EQ(read_call_count_, 1); | 141 EXPECT_EQ(read_call_count_, 1); |
| 127 | 142 |
| 128 attachment_store_handle_->Write( | 143 attachment_store_handle_->Write( |
| 129 attachments, base::Bind(&AttachmentStoreHandleTest::WriteDone)); | 144 attachments, base::Bind(&AttachmentStoreHandleTest::DoneWithResult)); |
| 130 EXPECT_EQ(write_call_count_, 0); | 145 EXPECT_EQ(write_call_count_, 0); |
| 131 RunMessageLoop(); | 146 RunMessageLoop(); |
| 132 EXPECT_EQ(write_call_count_, 1); | 147 EXPECT_EQ(write_call_count_, 1); |
| 133 | 148 |
| 134 attachment_store_handle_->Drop( | 149 attachment_store_handle_->Drop( |
| 135 ids, base::Bind(&AttachmentStoreHandleTest::DropDone)); | 150 ids, base::Bind(&AttachmentStoreHandleTest::DoneWithResult)); |
| 136 EXPECT_EQ(drop_call_count_, 0); | 151 EXPECT_EQ(drop_call_count_, 0); |
| 137 RunMessageLoop(); | 152 RunMessageLoop(); |
| 138 EXPECT_EQ(drop_call_count_, 1); | 153 EXPECT_EQ(drop_call_count_, 1); |
| 139 | 154 |
| 140 // Releasing referehce to AttachmentStoreHandle should result in | 155 // Releasing referehce to AttachmentStoreHandle should result in |
| 141 // MockAttachmentStore being deleted on backend loop. | 156 // MockAttachmentStore being deleted on backend loop. |
| 142 attachment_store_handle_ = NULL; | 157 attachment_store_handle_ = NULL; |
| 143 EXPECT_EQ(dtor_call_count_, 0); | 158 EXPECT_EQ(dtor_call_count_, 0); |
| 144 RunMessageLoop(); | 159 RunMessageLoop(); |
| 145 EXPECT_EQ(dtor_call_count_, 1); | 160 EXPECT_EQ(dtor_call_count_, 1); |
| 146 } | 161 } |
| 147 | 162 |
| 148 } // namespace syncer | 163 } // namespace syncer |
| OLD | NEW |