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