| 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_frontend.h" | 5 #include "sync/internal_api/public/attachments/attachment_store_frontend.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 "sync/api/attachments/attachment_store_backend.h" | 16 #include "sync/api/attachments/attachment_store_backend.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace syncer { | 19 namespace syncer { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class MockAttachmentStore : public AttachmentStoreBackend { | 23 class MockAttachmentStore : public AttachmentStoreBackend { |
| 24 public: | 24 public: |
| 25 MockAttachmentStore(const base::Closure& init_called, | 25 MockAttachmentStore(const base::Closure& init_called, |
| 26 const base::Closure& read_called, | 26 const base::Closure& read_called, |
| 27 const base::Closure& write_called, | 27 const base::Closure& write_called, |
| 28 const base::Closure& drop_called, | 28 const base::Closure& set_reference_called, |
| 29 const base::Closure& drop_reference_called, |
| 29 const base::Closure& read_metadata_called, | 30 const base::Closure& read_metadata_called, |
| 30 const base::Closure& read_all_metadata_called, | 31 const base::Closure& read_all_metadata_called, |
| 31 const base::Closure& dtor_called) | 32 const base::Closure& dtor_called) |
| 32 : AttachmentStoreBackend(nullptr), | 33 : AttachmentStoreBackend(nullptr), |
| 33 init_called_(init_called), | 34 init_called_(init_called), |
| 34 read_called_(read_called), | 35 read_called_(read_called), |
| 35 write_called_(write_called), | 36 write_called_(write_called), |
| 36 drop_called_(drop_called), | 37 set_reference_called_(set_reference_called), |
| 38 drop_reference_called_(drop_reference_called), |
| 37 read_metadata_called_(read_metadata_called), | 39 read_metadata_called_(read_metadata_called), |
| 38 read_all_metadata_called_(read_all_metadata_called), | 40 read_all_metadata_called_(read_all_metadata_called), |
| 39 dtor_called_(dtor_called) {} | 41 dtor_called_(dtor_called) {} |
| 40 | 42 |
| 41 ~MockAttachmentStore() override { dtor_called_.Run(); } | 43 ~MockAttachmentStore() override { dtor_called_.Run(); } |
| 42 | 44 |
| 43 void Init(const AttachmentStore::InitCallback& callback) override { | 45 void Init(const AttachmentStore::InitCallback& callback) override { |
| 44 init_called_.Run(); | 46 init_called_.Run(); |
| 45 } | 47 } |
| 46 | 48 |
| 47 void Read(const AttachmentIdList& ids, | 49 void Read(const AttachmentIdList& ids, |
| 48 const AttachmentStore::ReadCallback& callback) override { | 50 const AttachmentStore::ReadCallback& callback) override { |
| 49 read_called_.Run(); | 51 read_called_.Run(); |
| 50 } | 52 } |
| 51 | 53 |
| 52 void Write(AttachmentStore::AttachmentReferrer referrer, | 54 void Write(AttachmentStore::Component component, |
| 53 const AttachmentList& attachments, | 55 const AttachmentList& attachments, |
| 54 const AttachmentStore::WriteCallback& callback) override { | 56 const AttachmentStore::WriteCallback& callback) override { |
| 55 write_called_.Run(); | 57 write_called_.Run(); |
| 56 } | 58 } |
| 57 | 59 |
| 58 void Drop(AttachmentStore::AttachmentReferrer referrer, | 60 void SetReference(AttachmentStore::Component component, |
| 59 const AttachmentIdList& ids, | 61 const AttachmentIdList& ids) override { |
| 60 const AttachmentStore::DropCallback& callback) override { | 62 set_reference_called_.Run(); |
| 61 drop_called_.Run(); | 63 } |
| 64 |
| 65 void DropReference(AttachmentStore::Component component, |
| 66 const AttachmentIdList& ids, |
| 67 const AttachmentStore::DropCallback& callback) override { |
| 68 drop_reference_called_.Run(); |
| 62 } | 69 } |
| 63 | 70 |
| 64 void ReadMetadata( | 71 void ReadMetadata( |
| 65 const AttachmentIdList& ids, | 72 const AttachmentIdList& ids, |
| 66 const AttachmentStore::ReadMetadataCallback& callback) override { | 73 const AttachmentStore::ReadMetadataCallback& callback) override { |
| 67 read_metadata_called_.Run(); | 74 read_metadata_called_.Run(); |
| 68 } | 75 } |
| 69 | 76 |
| 70 void ReadAllMetadata( | 77 void ReadAllMetadata( |
| 71 AttachmentStore::AttachmentReferrer referrer, | 78 AttachmentStore::Component component, |
| 72 const AttachmentStore::ReadMetadataCallback& callback) override { | 79 const AttachmentStore::ReadMetadataCallback& callback) override { |
| 73 read_all_metadata_called_.Run(); | 80 read_all_metadata_called_.Run(); |
| 74 } | 81 } |
| 75 | 82 |
| 76 base::Closure init_called_; | 83 base::Closure init_called_; |
| 77 base::Closure read_called_; | 84 base::Closure read_called_; |
| 78 base::Closure write_called_; | 85 base::Closure write_called_; |
| 79 base::Closure drop_called_; | 86 base::Closure set_reference_called_; |
| 87 base::Closure drop_reference_called_; |
| 80 base::Closure read_metadata_called_; | 88 base::Closure read_metadata_called_; |
| 81 base::Closure read_all_metadata_called_; | 89 base::Closure read_all_metadata_called_; |
| 82 base::Closure dtor_called_; | 90 base::Closure dtor_called_; |
| 83 }; | 91 }; |
| 84 | 92 |
| 85 } // namespace | 93 } // namespace |
| 86 | 94 |
| 87 class AttachmentStoreFrontendTest : public testing::Test { | 95 class AttachmentStoreFrontendTest : public testing::Test { |
| 88 protected: | 96 protected: |
| 89 AttachmentStoreFrontendTest() | 97 AttachmentStoreFrontendTest() |
| 90 : init_call_count_(0), | 98 : init_call_count_(0), |
| 91 read_call_count_(0), | 99 read_call_count_(0), |
| 92 write_call_count_(0), | 100 write_call_count_(0), |
| 101 add_component_call_count_(0), |
| 93 drop_call_count_(0), | 102 drop_call_count_(0), |
| 94 read_metadata_call_count_(0), | 103 read_metadata_call_count_(0), |
| 95 read_all_metadata_call_count_(0), | 104 read_all_metadata_call_count_(0), |
| 96 dtor_call_count_(0) {} | 105 dtor_call_count_(0) {} |
| 97 | 106 |
| 98 void SetUp() override { | 107 void SetUp() override { |
| 99 scoped_ptr<AttachmentStoreBackend> backend(new MockAttachmentStore( | 108 scoped_ptr<AttachmentStoreBackend> backend(new MockAttachmentStore( |
| 100 base::Bind(&AttachmentStoreFrontendTest::InitCalled, | 109 base::Bind(&AttachmentStoreFrontendTest::InitCalled, |
| 101 base::Unretained(this)), | 110 base::Unretained(this)), |
| 102 base::Bind(&AttachmentStoreFrontendTest::ReadCalled, | 111 base::Bind(&AttachmentStoreFrontendTest::ReadCalled, |
| 103 base::Unretained(this)), | 112 base::Unretained(this)), |
| 104 base::Bind(&AttachmentStoreFrontendTest::WriteCalled, | 113 base::Bind(&AttachmentStoreFrontendTest::WriteCalled, |
| 105 base::Unretained(this)), | 114 base::Unretained(this)), |
| 106 base::Bind(&AttachmentStoreFrontendTest::DropCalled, | 115 base::Bind(&AttachmentStoreFrontendTest::SetReferenceCalled, |
| 116 base::Unretained(this)), |
| 117 base::Bind(&AttachmentStoreFrontendTest::DropReferenceCalled, |
| 107 base::Unretained(this)), | 118 base::Unretained(this)), |
| 108 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataCalled, | 119 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataCalled, |
| 109 base::Unretained(this)), | 120 base::Unretained(this)), |
| 110 base::Bind(&AttachmentStoreFrontendTest::ReadAllMetadataCalled, | 121 base::Bind(&AttachmentStoreFrontendTest::ReadAllMetadataCalled, |
| 111 base::Unretained(this)), | 122 base::Unretained(this)), |
| 112 base::Bind(&AttachmentStoreFrontendTest::DtorCalled, | 123 base::Bind(&AttachmentStoreFrontendTest::DtorCalled, |
| 113 base::Unretained(this)))); | 124 base::Unretained(this)))); |
| 114 attachment_store_frontend_ = new AttachmentStoreFrontend( | 125 attachment_store_frontend_ = new AttachmentStoreFrontend( |
| 115 backend.Pass(), base::ThreadTaskRunnerHandle::Get()); | 126 backend.Pass(), base::ThreadTaskRunnerHandle::Get()); |
| 116 } | 127 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 129 scoped_ptr<AttachmentMetadataList> metadata) { | 140 scoped_ptr<AttachmentMetadataList> metadata) { |
| 130 NOTREACHED(); | 141 NOTREACHED(); |
| 131 } | 142 } |
| 132 | 143 |
| 133 void InitCalled() { ++init_call_count_; } | 144 void InitCalled() { ++init_call_count_; } |
| 134 | 145 |
| 135 void ReadCalled() { ++read_call_count_; } | 146 void ReadCalled() { ++read_call_count_; } |
| 136 | 147 |
| 137 void WriteCalled() { ++write_call_count_; } | 148 void WriteCalled() { ++write_call_count_; } |
| 138 | 149 |
| 139 void DropCalled() { ++drop_call_count_; } | 150 void SetReferenceCalled() { ++add_component_call_count_; } |
| 151 |
| 152 void DropReferenceCalled() { ++drop_call_count_; } |
| 140 | 153 |
| 141 void ReadMetadataCalled() { ++read_metadata_call_count_; } | 154 void ReadMetadataCalled() { ++read_metadata_call_count_; } |
| 142 | 155 |
| 143 void ReadAllMetadataCalled() { ++read_all_metadata_call_count_; } | 156 void ReadAllMetadataCalled() { ++read_all_metadata_call_count_; } |
| 144 | 157 |
| 145 void DtorCalled() { ++dtor_call_count_; } | 158 void DtorCalled() { ++dtor_call_count_; } |
| 146 | 159 |
| 147 void RunMessageLoop() { | 160 void RunMessageLoop() { |
| 148 base::RunLoop run_loop; | 161 base::RunLoop run_loop; |
| 149 run_loop.RunUntilIdle(); | 162 run_loop.RunUntilIdle(); |
| 150 } | 163 } |
| 151 | 164 |
| 152 base::MessageLoop message_loop_; | 165 base::MessageLoop message_loop_; |
| 153 scoped_refptr<AttachmentStoreFrontend> attachment_store_frontend_; | 166 scoped_refptr<AttachmentStoreFrontend> attachment_store_frontend_; |
| 154 int init_call_count_; | 167 int init_call_count_; |
| 155 int read_call_count_; | 168 int read_call_count_; |
| 156 int write_call_count_; | 169 int write_call_count_; |
| 170 int add_component_call_count_; |
| 157 int drop_call_count_; | 171 int drop_call_count_; |
| 158 int read_metadata_call_count_; | 172 int read_metadata_call_count_; |
| 159 int read_all_metadata_call_count_; | 173 int read_all_metadata_call_count_; |
| 160 int dtor_call_count_; | 174 int dtor_call_count_; |
| 161 }; | 175 }; |
| 162 | 176 |
| 163 // Test that method calls are forwarded to backend loop | 177 // Test that method calls are forwarded to backend loop |
| 164 TEST_F(AttachmentStoreFrontendTest, MethodsCalled) { | 178 TEST_F(AttachmentStoreFrontendTest, MethodsCalled) { |
| 165 AttachmentIdList ids; | 179 AttachmentIdList id_list; |
| 166 AttachmentList attachments; | 180 AttachmentList attachments; |
| 167 | 181 |
| 168 attachment_store_frontend_->Init( | 182 attachment_store_frontend_->Init( |
| 169 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult)); | 183 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult)); |
| 170 EXPECT_EQ(init_call_count_, 0); | 184 EXPECT_EQ(init_call_count_, 0); |
| 171 RunMessageLoop(); | 185 RunMessageLoop(); |
| 172 EXPECT_EQ(init_call_count_, 1); | 186 EXPECT_EQ(init_call_count_, 1); |
| 173 | 187 |
| 174 attachment_store_frontend_->Read( | 188 attachment_store_frontend_->Read( |
| 175 ids, base::Bind(&AttachmentStoreFrontendTest::ReadDone)); | 189 id_list, base::Bind(&AttachmentStoreFrontendTest::ReadDone)); |
| 176 EXPECT_EQ(read_call_count_, 0); | 190 EXPECT_EQ(read_call_count_, 0); |
| 177 RunMessageLoop(); | 191 RunMessageLoop(); |
| 178 EXPECT_EQ(read_call_count_, 1); | 192 EXPECT_EQ(read_call_count_, 1); |
| 179 | 193 |
| 180 attachment_store_frontend_->Write( | 194 attachment_store_frontend_->Write( |
| 181 AttachmentStore::SYNC, attachments, | 195 AttachmentStore::SYNC, attachments, |
| 182 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult)); | 196 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult)); |
| 183 EXPECT_EQ(write_call_count_, 0); | 197 EXPECT_EQ(write_call_count_, 0); |
| 184 RunMessageLoop(); | 198 RunMessageLoop(); |
| 185 EXPECT_EQ(write_call_count_, 1); | 199 EXPECT_EQ(write_call_count_, 1); |
| 186 | 200 |
| 187 attachment_store_frontend_->Drop( | 201 attachment_store_frontend_->SetReference(AttachmentStore::SYNC, id_list); |
| 188 AttachmentStore::SYNC, ids, | 202 |
| 203 attachment_store_frontend_->DropReference( |
| 204 AttachmentStore::SYNC, id_list, |
| 189 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult)); | 205 base::Bind(&AttachmentStoreFrontendTest::DoneWithResult)); |
| 190 EXPECT_EQ(drop_call_count_, 0); | 206 EXPECT_EQ(drop_call_count_, 0); |
| 191 RunMessageLoop(); | 207 RunMessageLoop(); |
| 192 EXPECT_EQ(drop_call_count_, 1); | 208 EXPECT_EQ(drop_call_count_, 1); |
| 193 | 209 |
| 194 attachment_store_frontend_->ReadMetadata( | 210 attachment_store_frontend_->ReadMetadata( |
| 195 ids, base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone)); | 211 id_list, base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone)); |
| 196 EXPECT_EQ(read_metadata_call_count_, 0); | 212 EXPECT_EQ(read_metadata_call_count_, 0); |
| 197 RunMessageLoop(); | 213 RunMessageLoop(); |
| 198 EXPECT_EQ(read_metadata_call_count_, 1); | 214 EXPECT_EQ(read_metadata_call_count_, 1); |
| 199 | 215 |
| 200 attachment_store_frontend_->ReadAllMetadata( | 216 attachment_store_frontend_->ReadAllMetadata( |
| 201 AttachmentStore::SYNC, | 217 AttachmentStore::SYNC, |
| 202 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone)); | 218 base::Bind(&AttachmentStoreFrontendTest::ReadMetadataDone)); |
| 203 EXPECT_EQ(read_all_metadata_call_count_, 0); | 219 EXPECT_EQ(read_all_metadata_call_count_, 0); |
| 204 RunMessageLoop(); | 220 RunMessageLoop(); |
| 205 EXPECT_EQ(read_all_metadata_call_count_, 1); | 221 EXPECT_EQ(read_all_metadata_call_count_, 1); |
| 206 | 222 |
| 207 // Releasing referehce to AttachmentStoreFrontend should result in | 223 // Releasing referehce to AttachmentStoreFrontend should result in |
| 208 // MockAttachmentStore being deleted on backend loop. | 224 // MockAttachmentStore being deleted on backend loop. |
| 209 attachment_store_frontend_ = NULL; | 225 attachment_store_frontend_ = NULL; |
| 210 EXPECT_EQ(dtor_call_count_, 0); | 226 EXPECT_EQ(dtor_call_count_, 0); |
| 211 RunMessageLoop(); | 227 RunMessageLoop(); |
| 212 EXPECT_EQ(dtor_call_count_, 1); | 228 EXPECT_EQ(dtor_call_count_, 1); |
| 213 } | 229 } |
| 214 | 230 |
| 215 } // namespace syncer | 231 } // namespace syncer |
| OLD | NEW |