Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / metadata_database_unittest.cc
blob90f43f5581fa1bd61372726de4c1e79b4f06a8f6
1 // Copyright 2013 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.
5 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
7 #include "base/bind.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
13 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_test_util.h"
14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h"
15 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h"
16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index.h"
18 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index_interface.h"
19 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index_on_disk.h"
20 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
21 #include "google_apis/drive/drive_api_parser.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
24 #include "third_party/leveldatabase/src/include/leveldb/db.h"
25 #include "third_party/leveldatabase/src/include/leveldb/env.h"
26 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
28 #define FPL(a) FILE_PATH_LITERAL(a)
30 namespace sync_file_system {
31 namespace drive_backend {
33 namespace {
35 typedef MetadataDatabase::FileIDList FileIDList;
37 const int64 kInitialChangeID = 1234;
38 const int64 kSyncRootTrackerID = 100;
39 const char kSyncRootFolderID[] = "sync_root_folder_id";
41 // This struct is used to setup initial state of the database in the test and
42 // also used to match to the modified content of the database as the
43 // expectation.
44 struct TrackedFile {
45 // Holds the latest remote metadata which may be not-yet-synced to |tracker|.
46 FileMetadata metadata;
47 FileTracker tracker;
49 // Implies the file should not in the database.
50 bool should_be_absent;
52 // Implies the file should have a tracker in the database but should have no
53 // metadata.
54 bool tracker_only;
56 TrackedFile() : should_be_absent(false), tracker_only(false) {}
59 void ExpectEquivalentServiceMetadata(
60 const MetadataDatabaseIndexInterface* left,
61 const MetadataDatabaseIndexInterface* right) {
62 EXPECT_EQ(left->GetLargestChangeID(), right->GetLargestChangeID());
63 EXPECT_EQ(left->GetSyncRootTrackerID(), right->GetSyncRootTrackerID());
64 EXPECT_EQ(left->GetNextTrackerID(), right->GetNextTrackerID());
67 void ExpectEquivalent(const FileMetadata* left, const FileMetadata* right) {
68 if (!left) {
69 ASSERT_FALSE(right);
70 return;
72 ASSERT_TRUE(right);
73 test_util::ExpectEquivalentMetadata(*left, *right);
76 void ExpectEquivalent(const FileTracker* left, const FileTracker* right) {
77 if (!left) {
78 ASSERT_FALSE(right);
79 return;
81 ASSERT_TRUE(right);
82 test_util::ExpectEquivalentTrackers(*left, *right);
85 void ExpectEquivalent(int64 left, int64 right) {
86 EXPECT_EQ(left, right);
89 template <typename Container>
90 void ExpectEquivalentMaps(const Container& left, const Container& right);
92 template <typename Key, typename Value>
93 void ExpectEquivalent(const std::map<Key, Value>& left,
94 const std::map<Key, Value>& right) {
95 ExpectEquivalentMaps(left, right);
98 template <typename Key, typename Value>
99 void ExpectEquivalent(const base::hash_map<Key, Value>& left,
100 const base::hash_map<Key, Value>& right) {
101 ExpectEquivalentMaps(std::map<Key, Value>(left.begin(), left.end()),
102 std::map<Key, Value>(right.begin(), right.end()));
105 template <typename Key, typename Value>
106 void ExpectEquivalent(
107 const base::ScopedPtrHashMap<Key, scoped_ptr<Value>>& left,
108 const base::ScopedPtrHashMap<Key, scoped_ptr<Value>>& right) {
109 ExpectEquivalentMaps(std::map<Key, Value*>(left.begin(), left.end()),
110 std::map<Key, Value*>(right.begin(), right.end()));
113 template <typename Container>
114 void ExpectEquivalentSets(const Container& left, const Container& right);
116 template <typename Value, typename Comparator>
117 void ExpectEquivalent(const std::set<Value, Comparator>& left,
118 const std::set<Value, Comparator>& right) {
119 return ExpectEquivalentSets(left, right);
122 template <typename Value>
123 void ExpectEquivalent(const base::hash_set<Value>& left,
124 const base::hash_set<Value>& right) {
125 return ExpectEquivalentSets(std::set<Value>(left.begin(), left.end()),
126 std::set<Value>(right.begin(), right.end()));
129 void ExpectEquivalent(const TrackerIDSet& left,
130 const TrackerIDSet& right) {
132 SCOPED_TRACE("Expect equivalent active_tracker");
133 EXPECT_EQ(left.active_tracker(), right.active_tracker());
135 ExpectEquivalent(left.tracker_set(), right.tracker_set());
138 template <typename Container>
139 void ExpectEquivalentMaps(const Container& left, const Container& right) {
140 ASSERT_EQ(left.size(), right.size());
142 typedef typename Container::const_iterator const_iterator;
143 const_iterator left_itr = left.begin();
144 const_iterator right_itr = right.begin();
145 while (left_itr != left.end()) {
146 EXPECT_EQ(left_itr->first, right_itr->first);
147 ExpectEquivalent(left_itr->second, right_itr->second);
148 ++left_itr;
149 ++right_itr;
153 template <typename Container>
154 void ExpectEquivalentSets(const Container& left, const Container& right) {
155 ASSERT_EQ(left.size(), right.size());
157 typedef typename Container::const_iterator const_iterator;
158 const_iterator left_itr = left.begin();
159 const_iterator right_itr = right.begin();
160 while (left_itr != left.end()) {
161 ExpectEquivalent(*left_itr, *right_itr);
162 ++left_itr;
163 ++right_itr;
167 base::FilePath CreateNormalizedPath(const base::FilePath::StringType& path) {
168 return base::FilePath(path).NormalizePathSeparators();
171 } // namespace
173 class MetadataDatabaseTest : public testing::TestWithParam<bool> {
174 public:
175 MetadataDatabaseTest()
176 : current_change_id_(kInitialChangeID),
177 next_tracker_id_(kSyncRootTrackerID + 1),
178 next_file_id_number_(1),
179 next_md5_sequence_number_(1) {}
181 virtual ~MetadataDatabaseTest() {}
183 void SetUp() override {
184 ASSERT_TRUE(database_dir_.CreateUniqueTempDir());
185 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
188 void TearDown() override { DropDatabase(); }
190 protected:
191 std::string GenerateFileID() {
192 return "file_id_" + base::Int64ToString(next_file_id_number_++);
195 int64 GetTrackerIDByFileID(const std::string& file_id) {
196 TrackerIDSet trackers;
197 if (metadata_database_->FindTrackersByFileID(file_id, &trackers)) {
198 EXPECT_FALSE(trackers.empty());
199 return *trackers.begin();
201 return 0;
204 SyncStatusCode InitializeMetadataDatabase() {
205 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
206 metadata_database_ = MetadataDatabase::CreateInternal(
207 database_dir_.path(), in_memory_env_.get(),
208 GetParam(), &status);
209 return status;
212 void DropDatabase() {
213 metadata_database_.reset();
214 message_loop_.RunUntilIdle();
217 void SetUpDatabaseByTrackedFiles(const TrackedFile** tracked_files,
218 int size) {
219 scoped_ptr<LevelDBWrapper> db = InitializeLevelDB();
220 ASSERT_TRUE(db);
222 for (int i = 0; i < size; ++i) {
223 const TrackedFile* file = tracked_files[i];
224 if (file->should_be_absent)
225 continue;
226 if (!file->tracker_only)
227 EXPECT_TRUE(PutFileToDB(db.get(), file->metadata).ok());
228 EXPECT_TRUE(PutTrackerToDB(db.get(), file->tracker).ok());
232 void VerifyTrackedFile(const TrackedFile& file) {
233 if (!file.should_be_absent) {
234 if (file.tracker_only) {
235 EXPECT_FALSE(metadata_database()->FindFileByFileID(
236 file.metadata.file_id(), nullptr));
237 } else {
238 VerifyFile(file.metadata);
240 VerifyTracker(file.tracker);
241 return;
244 EXPECT_FALSE(metadata_database()->FindFileByFileID(
245 file.metadata.file_id(), nullptr));
246 EXPECT_FALSE(metadata_database()->FindTrackerByTrackerID(
247 file.tracker.tracker_id(), nullptr));
250 void VerifyTrackedFiles(const TrackedFile** tracked_files, int size) {
251 for (int i = 0; i < size; ++i)
252 VerifyTrackedFile(*tracked_files[i]);
255 MetadataDatabase* metadata_database() { return metadata_database_.get(); }
257 scoped_ptr<LevelDBWrapper> InitializeLevelDB() {
258 leveldb::DB* db = nullptr;
259 leveldb::Options options;
260 options.create_if_missing = true;
261 options.max_open_files = 0; // Use minimum.
262 options.env = in_memory_env_.get();
263 leveldb::Status status =
264 leveldb::DB::Open(options, database_dir_.path().AsUTF8Unsafe(), &db);
265 EXPECT_TRUE(status.ok());
267 scoped_ptr<LevelDBWrapper> wrapper(new LevelDBWrapper(make_scoped_ptr(db)));
269 wrapper->Put(kDatabaseVersionKey, base::Int64ToString(3));
270 SetUpServiceMetadata(wrapper.get());
272 return wrapper.Pass();
275 void SetUpServiceMetadata(LevelDBWrapper* db) {
276 ServiceMetadata service_metadata;
277 service_metadata.set_largest_change_id(kInitialChangeID);
278 service_metadata.set_sync_root_tracker_id(kSyncRootTrackerID);
279 service_metadata.set_next_tracker_id(next_tracker_id_);
280 PutServiceMetadataToDB(service_metadata, db);
281 EXPECT_TRUE(db->Commit().ok());
284 FileMetadata CreateSyncRootMetadata() {
285 FileMetadata sync_root;
286 sync_root.set_file_id(kSyncRootFolderID);
287 FileDetails* details = sync_root.mutable_details();
288 details->set_title(kSyncRootFolderTitle);
289 details->set_file_kind(FILE_KIND_FOLDER);
290 details->set_change_id(current_change_id_);
291 return sync_root;
294 FileMetadata CreateFileMetadata(const FileMetadata& parent,
295 const std::string& title) {
296 FileMetadata file;
297 file.set_file_id(GenerateFileID());
298 FileDetails* details = file.mutable_details();
299 details->add_parent_folder_ids(parent.file_id());
300 details->set_title(title);
301 details->set_file_kind(FILE_KIND_FILE);
302 details->set_md5(
303 "md5_value_" + base::Int64ToString(next_md5_sequence_number_++));
304 details->set_change_id(current_change_id_);
305 return file;
308 FileMetadata CreateFolderMetadata(const FileMetadata& parent,
309 const std::string& title) {
310 FileMetadata folder;
311 folder.set_file_id(GenerateFileID());
312 FileDetails* details = folder.mutable_details();
313 details->add_parent_folder_ids(parent.file_id());
314 details->set_title(title);
315 details->set_file_kind(FILE_KIND_FOLDER);
316 details->set_change_id(current_change_id_);
317 return folder;
320 FileTracker CreateSyncRootTracker(const FileMetadata& sync_root) {
321 FileTracker sync_root_tracker;
322 sync_root_tracker.set_tracker_id(kSyncRootTrackerID);
323 sync_root_tracker.set_parent_tracker_id(0);
324 sync_root_tracker.set_file_id(sync_root.file_id());
325 sync_root_tracker.set_dirty(false);
326 sync_root_tracker.set_active(true);
327 sync_root_tracker.set_needs_folder_listing(false);
328 *sync_root_tracker.mutable_synced_details() = sync_root.details();
329 return sync_root_tracker;
332 FileTracker CreateTracker(const FileTracker& parent_tracker,
333 const FileMetadata& file) {
334 FileTracker tracker;
335 tracker.set_tracker_id(next_tracker_id_++);
336 tracker.set_parent_tracker_id(parent_tracker.tracker_id());
337 tracker.set_file_id(file.file_id());
338 tracker.set_app_id(parent_tracker.app_id());
339 tracker.set_tracker_kind(TRACKER_KIND_REGULAR);
340 tracker.set_dirty(false);
341 tracker.set_active(true);
342 tracker.set_needs_folder_listing(false);
343 *tracker.mutable_synced_details() = file.details();
344 return tracker;
347 TrackedFile CreateTrackedSyncRoot() {
348 TrackedFile sync_root;
349 sync_root.metadata = CreateSyncRootMetadata();
350 sync_root.tracker = CreateSyncRootTracker(sync_root.metadata);
351 return sync_root;
354 TrackedFile CreateTrackedAppRoot(const TrackedFile& sync_root,
355 const std::string& app_id) {
356 TrackedFile app_root(CreateTrackedFolder(sync_root, app_id));
357 app_root.tracker.set_app_id(app_id);
358 app_root.tracker.set_tracker_kind(TRACKER_KIND_APP_ROOT);
359 return app_root;
362 TrackedFile CreateTrackedFile(const TrackedFile& parent,
363 const std::string& title) {
364 TrackedFile file;
365 file.metadata = CreateFileMetadata(parent.metadata, title);
366 file.tracker = CreateTracker(parent.tracker, file.metadata);
367 return file;
370 TrackedFile CreateTrackedFolder(const TrackedFile& parent,
371 const std::string& title) {
372 TrackedFile folder;
373 folder.metadata = CreateFolderMetadata(parent.metadata, title);
374 folder.tracker = CreateTracker(parent.tracker, folder.metadata);
375 return folder;
378 scoped_ptr<google_apis::FileResource> CreateFileResourceFromMetadata(
379 const FileMetadata& file) {
380 scoped_ptr<google_apis::FileResource> file_resource(
381 new google_apis::FileResource);
382 for (int i = 0; i < file.details().parent_folder_ids_size(); ++i) {
383 google_apis::ParentReference parent;
384 parent.set_file_id(file.details().parent_folder_ids(i));
385 file_resource->mutable_parents()->push_back(parent);
388 file_resource->set_file_id(file.file_id());
389 file_resource->set_title(file.details().title());
390 if (file.details().file_kind() == FILE_KIND_FOLDER) {
391 file_resource->set_mime_type("application/vnd.google-apps.folder");
392 } else if (file.details().file_kind() == FILE_KIND_FILE) {
393 file_resource->set_mime_type("text/plain");
394 file_resource->set_file_size(0);
395 } else {
396 file_resource->set_mime_type("application/vnd.google-apps.document");
398 file_resource->set_md5_checksum(file.details().md5());
399 file_resource->set_etag(file.details().etag());
400 file_resource->set_created_date(base::Time::FromInternalValue(
401 file.details().creation_time()));
402 file_resource->set_modified_date(base::Time::FromInternalValue(
403 file.details().modification_time()));
405 return file_resource.Pass();
408 scoped_ptr<google_apis::ChangeResource> CreateChangeResourceFromMetadata(
409 const FileMetadata& file) {
410 scoped_ptr<google_apis::ChangeResource> change(
411 new google_apis::ChangeResource);
412 change->set_change_id(file.details().change_id());
413 change->set_file_id(file.file_id());
414 change->set_deleted(file.details().missing());
415 if (change->is_deleted())
416 return change.Pass();
418 change->set_file(CreateFileResourceFromMetadata(file));
419 return change.Pass();
422 void ApplyRenameChangeToMetadata(const std::string& new_title,
423 FileMetadata* file) {
424 FileDetails* details = file->mutable_details();
425 details->set_title(new_title);
426 details->set_change_id(++current_change_id_);
429 void ApplyReorganizeChangeToMetadata(const std::string& new_parent,
430 FileMetadata* file) {
431 FileDetails* details = file->mutable_details();
432 details->clear_parent_folder_ids();
433 details->add_parent_folder_ids(new_parent);
434 details->set_change_id(++current_change_id_);
437 void ApplyContentChangeToMetadata(FileMetadata* file) {
438 FileDetails* details = file->mutable_details();
439 details->set_md5(
440 "md5_value_" + base::Int64ToString(next_md5_sequence_number_++));
441 details->set_change_id(++current_change_id_);
444 void ApplyNoopChangeToMetadata(FileMetadata* file) {
445 file->mutable_details()->set_change_id(++current_change_id_);
448 void PushToChangeList(scoped_ptr<google_apis::ChangeResource> change,
449 ScopedVector<google_apis::ChangeResource>* changes) {
450 changes->push_back(change.release());
453 leveldb::Status PutFileToDB(LevelDBWrapper* db, const FileMetadata& file) {
454 PutFileMetadataToDB(file, db);
455 return db->Commit();
458 leveldb::Status PutTrackerToDB(LevelDBWrapper* db,
459 const FileTracker& tracker) {
460 PutFileTrackerToDB(tracker, db);
461 return db->Commit();
464 void VerifyReloadConsistencyForOnMemory(MetadataDatabaseIndex* index1,
465 MetadataDatabaseIndex* index2) {
466 ExpectEquivalentServiceMetadata(index1, index2);
468 SCOPED_TRACE("Expect equivalent metadata_by_id_ contents.");
469 ExpectEquivalent(index1->metadata_by_id_, index2->metadata_by_id_);
472 SCOPED_TRACE("Expect equivalent tracker_by_id_ contents.");
473 ExpectEquivalent(index1->tracker_by_id_, index2->tracker_by_id_);
476 SCOPED_TRACE("Expect equivalent trackers_by_file_id_ contents.");
477 ExpectEquivalent(index1->trackers_by_file_id_,
478 index2->trackers_by_file_id_);
481 SCOPED_TRACE("Expect equivalent app_root_by_app_id_ contents.");
482 ExpectEquivalent(index1->app_root_by_app_id_,
483 index2->app_root_by_app_id_);
486 SCOPED_TRACE("Expect equivalent trackers_by_parent_and_title_ contents.");
487 ExpectEquivalent(index1->trackers_by_parent_and_title_,
488 index2->trackers_by_parent_and_title_);
491 SCOPED_TRACE("Expect equivalent dirty_trackers_ contents.");
492 ExpectEquivalent(index1->dirty_trackers_, index2->dirty_trackers_);
496 void VerifyReloadConsistencyForOnDisk(
497 MetadataDatabaseIndexOnDisk* index1,
498 MetadataDatabaseIndexOnDisk* index2) {
499 ExpectEquivalentServiceMetadata(index1, index2);
500 scoped_ptr<LevelDBWrapper::Iterator> itr1 =
501 index1->GetDBForTesting()->NewIterator();
502 scoped_ptr<LevelDBWrapper::Iterator> itr2 =
503 index2->GetDBForTesting()->NewIterator();
504 for (itr1->SeekToFirst(), itr2->SeekToFirst();
505 itr1->Valid() && itr2->Valid();
506 itr1->Next(), itr2->Next()) {
507 EXPECT_EQ(itr1->key().ToString(), itr2->key().ToString());
508 EXPECT_EQ(itr1->value().ToString(), itr2->value().ToString());
510 EXPECT_TRUE(!itr1->Valid());
511 EXPECT_TRUE(!itr2->Valid());
514 void VerifyReloadConsistency() {
515 scoped_ptr<MetadataDatabase> metadata_database_2;
516 ASSERT_EQ(SYNC_STATUS_OK,
517 MetadataDatabase::CreateForTesting(
518 metadata_database_->db_.Pass(),
519 metadata_database_->enable_on_disk_index_,
520 &metadata_database_2));
521 metadata_database_->db_ = metadata_database_2->db_.Pass();
523 MetadataDatabaseIndexInterface* index1 = metadata_database_->index_.get();
524 MetadataDatabaseIndexInterface* index2 = metadata_database_2->index_.get();
525 if (GetParam()) {
526 VerifyReloadConsistencyForOnDisk(
527 static_cast<MetadataDatabaseIndexOnDisk*>(index1),
528 static_cast<MetadataDatabaseIndexOnDisk*>(index2));
529 } else {
530 VerifyReloadConsistencyForOnMemory(
531 static_cast<MetadataDatabaseIndex*>(index1),
532 static_cast<MetadataDatabaseIndex*>(index2));
536 void VerifyFile(const FileMetadata& file) {
537 FileMetadata file_in_metadata_database;
538 ASSERT_TRUE(metadata_database()->FindFileByFileID(
539 file.file_id(), &file_in_metadata_database));
541 SCOPED_TRACE("Expect equivalent " + file.file_id());
542 ExpectEquivalent(&file, &file_in_metadata_database);
545 void VerifyTracker(const FileTracker& tracker) {
546 FileTracker tracker_in_metadata_database;
547 ASSERT_TRUE(metadata_database()->FindTrackerByTrackerID(
548 tracker.tracker_id(), &tracker_in_metadata_database));
550 SCOPED_TRACE("Expect equivalent tracker[" +
551 base::Int64ToString(tracker.tracker_id()) + "]");
552 ExpectEquivalent(&tracker, &tracker_in_metadata_database);
555 SyncStatusCode RegisterApp(const std::string& app_id,
556 const std::string& folder_id) {
557 return metadata_database_->RegisterApp(app_id, folder_id);
560 SyncStatusCode DisableApp(const std::string& app_id) {
561 return metadata_database_->DisableApp(app_id);
564 SyncStatusCode EnableApp(const std::string& app_id) {
565 return metadata_database_->EnableApp(app_id);
568 SyncStatusCode UnregisterApp(const std::string& app_id) {
569 return metadata_database_->UnregisterApp(app_id);
572 SyncStatusCode UpdateByChangeList(
573 ScopedVector<google_apis::ChangeResource> changes) {
574 return metadata_database_->UpdateByChangeList(
575 current_change_id_, changes.Pass());
578 SyncStatusCode PopulateFolder(const std::string& folder_id,
579 const FileIDList& listed_children) {
580 return metadata_database_->PopulateFolderByChildList(
581 folder_id, listed_children);
584 SyncStatusCode UpdateTracker(const FileTracker& tracker) {
585 return metadata_database_->UpdateTracker(
586 tracker.tracker_id(), tracker.synced_details());
589 SyncStatusCode PopulateInitialData(
590 int64 largest_change_id,
591 const google_apis::FileResource& sync_root_folder,
592 const ScopedVector<google_apis::FileResource>& app_root_folders) {
593 return metadata_database_->PopulateInitialData(
594 largest_change_id, sync_root_folder, app_root_folders);
597 void ResetTrackerID(FileTracker* tracker) {
598 tracker->set_tracker_id(GetTrackerIDByFileID(tracker->file_id()));
601 int64 current_change_id() const {
602 return current_change_id_;
605 private:
606 base::ScopedTempDir database_dir_;
607 base::MessageLoop message_loop_;
609 scoped_ptr<leveldb::Env> in_memory_env_;
610 scoped_ptr<MetadataDatabase> metadata_database_;
612 int64 current_change_id_;
613 int64 next_tracker_id_;
614 int64 next_file_id_number_;
615 int64 next_md5_sequence_number_;
617 DISALLOW_COPY_AND_ASSIGN(MetadataDatabaseTest);
620 INSTANTIATE_TEST_CASE_P(MetadataDatabaseTestWithIndexesOnDisk,
621 MetadataDatabaseTest,
622 ::testing::Values(true, false));
624 TEST_P(MetadataDatabaseTest, InitializationTest_Empty) {
625 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
626 DropDatabase();
627 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
629 DropDatabase();
631 scoped_ptr<LevelDBWrapper> db = InitializeLevelDB();
632 db->Put(kServiceMetadataKey, "Unparsable string");
633 EXPECT_TRUE(db->Commit().ok());
634 db.reset();
636 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
639 TEST_P(MetadataDatabaseTest, InitializationTest_SimpleTree) {
640 TrackedFile sync_root(CreateTrackedSyncRoot());
641 TrackedFile app_root(CreateTrackedFolder(sync_root, "app_id"));
642 app_root.tracker.set_app_id(app_root.metadata.details().title());
643 app_root.tracker.set_tracker_kind(TRACKER_KIND_APP_ROOT);
645 TrackedFile file(CreateTrackedFile(app_root, "file"));
646 TrackedFile folder(CreateTrackedFolder(app_root, "folder"));
647 TrackedFile file_in_folder(CreateTrackedFile(folder, "file_in_folder"));
648 TrackedFile orphaned_file(CreateTrackedFile(sync_root, "orphaned_file"));
649 orphaned_file.metadata.mutable_details()->clear_parent_folder_ids();
650 orphaned_file.tracker.set_parent_tracker_id(0);
652 const TrackedFile* tracked_files[] = {
653 &sync_root, &app_root, &file, &folder, &file_in_folder, &orphaned_file
656 SetUpDatabaseByTrackedFiles(tracked_files, arraysize(tracked_files));
657 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
659 orphaned_file.should_be_absent = true;
660 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
663 TEST_P(MetadataDatabaseTest, AppManagementTest) {
664 TrackedFile sync_root(CreateTrackedSyncRoot());
665 TrackedFile app_root(CreateTrackedFolder(sync_root, "app_id"));
666 app_root.tracker.set_app_id(app_root.metadata.details().title());
667 app_root.tracker.set_tracker_kind(TRACKER_KIND_APP_ROOT);
669 TrackedFile file(CreateTrackedFile(app_root, "file"));
670 TrackedFile folder(CreateTrackedFolder(sync_root, "folder"));
671 folder.tracker.set_active(false);
673 const TrackedFile* tracked_files[] = {
674 &sync_root, &app_root, &file, &folder,
676 SetUpDatabaseByTrackedFiles(tracked_files, arraysize(tracked_files));
677 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
678 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
680 folder.tracker.set_app_id("foo");
681 EXPECT_EQ(SYNC_STATUS_OK, RegisterApp(
682 folder.tracker.app_id(), folder.metadata.file_id()));
683 folder.tracker.set_tracker_kind(TRACKER_KIND_APP_ROOT);
684 folder.tracker.set_active(true);
685 folder.tracker.set_dirty(true);
686 folder.tracker.set_needs_folder_listing(true);
687 VerifyTrackedFile(folder);
688 VerifyReloadConsistency();
690 EXPECT_EQ(SYNC_STATUS_OK, DisableApp(folder.tracker.app_id()));
691 folder.tracker.set_tracker_kind(TRACKER_KIND_DISABLED_APP_ROOT);
692 VerifyTrackedFile(folder);
693 VerifyReloadConsistency();
695 EXPECT_EQ(SYNC_STATUS_OK, EnableApp(folder.tracker.app_id()));
696 folder.tracker.set_tracker_kind(TRACKER_KIND_APP_ROOT);
697 VerifyTrackedFile(folder);
698 VerifyReloadConsistency();
700 EXPECT_EQ(SYNC_STATUS_OK, UnregisterApp(folder.tracker.app_id()));
701 folder.tracker.set_app_id(std::string());
702 folder.tracker.set_tracker_kind(TRACKER_KIND_REGULAR);
703 folder.tracker.set_active(false);
704 VerifyTrackedFile(folder);
705 VerifyReloadConsistency();
707 EXPECT_EQ(SYNC_STATUS_OK, UnregisterApp(app_root.tracker.app_id()));
708 app_root.tracker.set_app_id(std::string());
709 app_root.tracker.set_tracker_kind(TRACKER_KIND_REGULAR);
710 app_root.tracker.set_active(false);
711 app_root.tracker.set_dirty(true);
712 file.should_be_absent = true;
713 VerifyTrackedFile(app_root);
714 VerifyTrackedFile(file);
715 VerifyReloadConsistency();
718 TEST_P(MetadataDatabaseTest, BuildPathTest) {
719 FileMetadata sync_root(CreateSyncRootMetadata());
720 FileTracker sync_root_tracker(CreateSyncRootTracker(sync_root));
722 FileMetadata app_root(CreateFolderMetadata(sync_root, "app_id"));
723 FileTracker app_root_tracker(
724 CreateTracker(sync_root_tracker, app_root));
725 app_root_tracker.set_app_id(app_root.details().title());
726 app_root_tracker.set_tracker_kind(TRACKER_KIND_APP_ROOT);
728 FileMetadata folder(CreateFolderMetadata(app_root, "folder"));
729 FileTracker folder_tracker(CreateTracker(app_root_tracker, folder));
731 FileMetadata file(CreateFileMetadata(folder, "file"));
732 FileTracker file_tracker(CreateTracker(folder_tracker, file));
734 FileMetadata inactive_folder(CreateFolderMetadata(app_root, "folder"));
735 FileTracker inactive_folder_tracker(CreateTracker(app_root_tracker,
736 inactive_folder));
737 inactive_folder_tracker.set_active(false);
740 scoped_ptr<LevelDBWrapper> db = InitializeLevelDB();
741 ASSERT_TRUE(db);
743 EXPECT_TRUE(PutFileToDB(db.get(), sync_root).ok());
744 EXPECT_TRUE(PutTrackerToDB(db.get(), sync_root_tracker).ok());
745 EXPECT_TRUE(PutFileToDB(db.get(), app_root).ok());
746 EXPECT_TRUE(PutTrackerToDB(db.get(), app_root_tracker).ok());
747 EXPECT_TRUE(PutFileToDB(db.get(), folder).ok());
748 EXPECT_TRUE(PutTrackerToDB(db.get(), folder_tracker).ok());
749 EXPECT_TRUE(PutFileToDB(db.get(), file).ok());
750 EXPECT_TRUE(PutTrackerToDB(db.get(), file_tracker).ok());
753 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
755 base::FilePath path;
756 EXPECT_FALSE(metadata_database()->BuildPathForTracker(
757 sync_root_tracker.tracker_id(), &path));
758 EXPECT_TRUE(metadata_database()->BuildPathForTracker(
759 app_root_tracker.tracker_id(), &path));
760 EXPECT_EQ(base::FilePath(FPL("/")).NormalizePathSeparators(), path);
761 EXPECT_TRUE(metadata_database()->BuildPathForTracker(
762 file_tracker.tracker_id(), &path));
763 EXPECT_EQ(base::FilePath(FPL("/folder/file")).NormalizePathSeparators(),
764 path);
767 TEST_P(MetadataDatabaseTest, FindNearestActiveAncestorTest) {
768 const std::string kAppID = "app_id";
770 FileMetadata sync_root(CreateSyncRootMetadata());
771 FileTracker sync_root_tracker(CreateSyncRootTracker(sync_root));
773 FileMetadata app_root(CreateFolderMetadata(sync_root, kAppID));
774 FileTracker app_root_tracker(
775 CreateTracker(sync_root_tracker, app_root));
776 app_root_tracker.set_app_id(app_root.details().title());
777 app_root_tracker.set_tracker_kind(TRACKER_KIND_APP_ROOT);
779 // Create directory structure like this: "/folder1/folder2/file"
780 FileMetadata folder1(CreateFolderMetadata(app_root, "folder1"));
781 FileTracker folder_tracker1(CreateTracker(app_root_tracker, folder1));
782 FileMetadata folder2(CreateFolderMetadata(folder1, "folder2"));
783 FileTracker folder_tracker2(CreateTracker(folder_tracker1, folder2));
784 FileMetadata file(CreateFileMetadata(folder2, "file"));
785 FileTracker file_tracker(CreateTracker(folder_tracker2, file));
787 FileMetadata inactive_folder(CreateFolderMetadata(app_root, "folder1"));
788 FileTracker inactive_folder_tracker(CreateTracker(app_root_tracker,
789 inactive_folder));
790 inactive_folder_tracker.set_active(false);
793 scoped_ptr<LevelDBWrapper> db = InitializeLevelDB();
794 ASSERT_TRUE(db);
796 EXPECT_TRUE(PutFileToDB(db.get(), sync_root).ok());
797 EXPECT_TRUE(PutTrackerToDB(db.get(), sync_root_tracker).ok());
798 EXPECT_TRUE(PutFileToDB(db.get(), app_root).ok());
799 EXPECT_TRUE(PutTrackerToDB(db.get(), app_root_tracker).ok());
800 EXPECT_TRUE(PutFileToDB(db.get(), folder1).ok());
801 EXPECT_TRUE(PutTrackerToDB(db.get(), folder_tracker1).ok());
802 EXPECT_TRUE(PutFileToDB(db.get(), folder2).ok());
803 EXPECT_TRUE(PutTrackerToDB(db.get(), folder_tracker2).ok());
804 EXPECT_TRUE(PutFileToDB(db.get(), file).ok());
805 EXPECT_TRUE(PutTrackerToDB(db.get(), file_tracker).ok());
806 EXPECT_TRUE(PutFileToDB(db.get(), inactive_folder).ok());
807 EXPECT_TRUE(PutTrackerToDB(db.get(), inactive_folder_tracker).ok());
810 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
813 base::FilePath path;
814 FileTracker tracker;
815 EXPECT_FALSE(metadata_database()->FindNearestActiveAncestor(
816 "non_registered_app_id",
817 CreateNormalizedPath(FPL("folder1/folder2/file")),
818 &tracker, &path));
822 base::FilePath path;
823 FileTracker tracker;
824 EXPECT_TRUE(metadata_database()->FindNearestActiveAncestor(
825 kAppID, CreateNormalizedPath(FPL("")), &tracker, &path));
826 EXPECT_EQ(app_root_tracker.tracker_id(), tracker.tracker_id());
827 EXPECT_EQ(CreateNormalizedPath(FPL("")), path);
831 base::FilePath path;
832 FileTracker tracker;
833 EXPECT_TRUE(metadata_database()->FindNearestActiveAncestor(
834 kAppID, CreateNormalizedPath(FPL("folder1/folder2")),
835 &tracker, &path));
836 EXPECT_EQ(folder_tracker2.tracker_id(), tracker.tracker_id());
837 EXPECT_EQ(CreateNormalizedPath(FPL("folder1/folder2")), path);
841 base::FilePath path;
842 FileTracker tracker;
843 EXPECT_TRUE(metadata_database()->FindNearestActiveAncestor(
844 kAppID, CreateNormalizedPath(FPL("folder1/folder2/file")),
845 &tracker, &path));
846 EXPECT_EQ(file_tracker.tracker_id(), tracker.tracker_id());
847 EXPECT_EQ(CreateNormalizedPath(FPL("folder1/folder2/file")), path);
851 base::FilePath path;
852 FileTracker tracker;
853 EXPECT_TRUE(metadata_database()->FindNearestActiveAncestor(
854 kAppID,
855 CreateNormalizedPath(FPL("folder1/folder2/folder3/folder4/file")),
856 &tracker, &path));
857 EXPECT_EQ(folder_tracker2.tracker_id(), tracker.tracker_id());
858 EXPECT_EQ(CreateNormalizedPath(FPL("folder1/folder2")), path);
862 base::FilePath path;
863 FileTracker tracker;
864 EXPECT_TRUE(metadata_database()->FindNearestActiveAncestor(
865 kAppID, CreateNormalizedPath(FPL("folder1/folder2/file/folder4/file")),
866 &tracker, &path));
867 EXPECT_EQ(folder_tracker2.tracker_id(), tracker.tracker_id());
868 EXPECT_EQ(CreateNormalizedPath(FPL("folder1/folder2")), path);
872 TEST_P(MetadataDatabaseTest, UpdateByChangeListTest) {
873 TrackedFile sync_root(CreateTrackedSyncRoot());
874 TrackedFile app_root(CreateTrackedFolder(sync_root, "app_id"));
875 TrackedFile disabled_app_root(CreateTrackedFolder(sync_root, "disabled_app"));
876 TrackedFile file(CreateTrackedFile(app_root, "file"));
877 TrackedFile renamed_file(CreateTrackedFile(app_root, "to be renamed"));
878 TrackedFile folder(CreateTrackedFolder(app_root, "folder"));
879 TrackedFile reorganized_file(
880 CreateTrackedFile(app_root, "to be reorganized"));
881 TrackedFile updated_file(
882 CreateTrackedFile(app_root, "to be updated"));
883 TrackedFile noop_file(CreateTrackedFile(app_root, "has noop change"));
884 TrackedFile new_file(CreateTrackedFile(app_root, "to be added later"));
885 new_file.should_be_absent = true;
887 const TrackedFile* tracked_files[] = {
888 &sync_root, &app_root, &disabled_app_root,
889 &file, &renamed_file, &folder, &reorganized_file, &updated_file, &noop_file,
890 &new_file,
893 SetUpDatabaseByTrackedFiles(tracked_files, arraysize(tracked_files));
894 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
896 ApplyRenameChangeToMetadata("renamed", &renamed_file.metadata);
897 ApplyReorganizeChangeToMetadata(folder.metadata.file_id(),
898 &reorganized_file.metadata);
899 ApplyContentChangeToMetadata(&updated_file.metadata);
901 // Update change ID.
902 ApplyNoopChangeToMetadata(&noop_file.metadata);
904 ScopedVector<google_apis::ChangeResource> changes;
905 PushToChangeList(
906 CreateChangeResourceFromMetadata(renamed_file.metadata), &changes);
907 PushToChangeList(
908 CreateChangeResourceFromMetadata(reorganized_file.metadata), &changes);
909 PushToChangeList(
910 CreateChangeResourceFromMetadata(updated_file.metadata), &changes);
911 PushToChangeList(
912 CreateChangeResourceFromMetadata(noop_file.metadata), &changes);
913 PushToChangeList(
914 CreateChangeResourceFromMetadata(new_file.metadata), &changes);
915 EXPECT_EQ(SYNC_STATUS_OK, UpdateByChangeList(changes.Pass()));
917 renamed_file.tracker.set_dirty(true);
918 reorganized_file.tracker.set_dirty(true);
919 updated_file.tracker.set_dirty(true);
920 noop_file.tracker.set_dirty(true);
921 new_file.tracker.mutable_synced_details()->set_missing(true);
922 new_file.tracker.mutable_synced_details()->clear_md5();
923 new_file.tracker.set_active(false);
924 new_file.tracker.set_dirty(true);
925 ResetTrackerID(&new_file.tracker);
926 EXPECT_NE(0, new_file.tracker.tracker_id());
928 new_file.should_be_absent = false;
930 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
931 VerifyReloadConsistency();
934 TEST_P(MetadataDatabaseTest, PopulateFolderTest_RegularFolder) {
935 TrackedFile sync_root(CreateTrackedSyncRoot());
936 TrackedFile app_root(CreateTrackedAppRoot(sync_root, "app_id"));
937 app_root.tracker.set_app_id(app_root.metadata.details().title());
939 TrackedFile folder_to_populate(
940 CreateTrackedFolder(app_root, "folder_to_populate"));
941 folder_to_populate.tracker.set_needs_folder_listing(true);
942 folder_to_populate.tracker.set_dirty(true);
944 TrackedFile known_file(CreateTrackedFile(folder_to_populate, "known_file"));
945 TrackedFile new_file(CreateTrackedFile(folder_to_populate, "new_file"));
946 new_file.should_be_absent = true;
948 const TrackedFile* tracked_files[] = {
949 &sync_root, &app_root, &folder_to_populate, &known_file, &new_file
952 SetUpDatabaseByTrackedFiles(tracked_files, arraysize(tracked_files));
953 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
954 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
956 FileIDList listed_children;
957 listed_children.push_back(known_file.metadata.file_id());
958 listed_children.push_back(new_file.metadata.file_id());
960 EXPECT_EQ(SYNC_STATUS_OK,
961 PopulateFolder(folder_to_populate.metadata.file_id(),
962 listed_children));
964 folder_to_populate.tracker.set_dirty(false);
965 folder_to_populate.tracker.set_needs_folder_listing(false);
966 ResetTrackerID(&new_file.tracker);
967 new_file.tracker.set_dirty(true);
968 new_file.tracker.set_active(false);
969 new_file.tracker.clear_synced_details();
970 new_file.should_be_absent = false;
971 new_file.tracker_only = true;
972 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
973 VerifyReloadConsistency();
976 TEST_P(MetadataDatabaseTest, PopulateFolderTest_InactiveFolder) {
977 TrackedFile sync_root(CreateTrackedSyncRoot());
978 TrackedFile app_root(CreateTrackedAppRoot(sync_root, "app_id"));
980 TrackedFile inactive_folder(CreateTrackedFolder(app_root, "inactive_folder"));
981 inactive_folder.tracker.set_active(false);
982 inactive_folder.tracker.set_dirty(true);
984 TrackedFile new_file(
985 CreateTrackedFile(inactive_folder, "file_in_inactive_folder"));
986 new_file.should_be_absent = true;
988 const TrackedFile* tracked_files[] = {
989 &sync_root, &app_root, &inactive_folder, &new_file,
992 SetUpDatabaseByTrackedFiles(tracked_files, arraysize(tracked_files));
993 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
994 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
996 FileIDList listed_children;
997 listed_children.push_back(new_file.metadata.file_id());
999 EXPECT_EQ(SYNC_STATUS_OK,
1000 PopulateFolder(inactive_folder.metadata.file_id(),
1001 listed_children));
1002 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
1003 VerifyReloadConsistency();
1006 TEST_P(MetadataDatabaseTest, PopulateFolderTest_DisabledAppRoot) {
1007 TrackedFile sync_root(CreateTrackedSyncRoot());
1008 TrackedFile disabled_app_root(
1009 CreateTrackedAppRoot(sync_root, "disabled_app"));
1010 disabled_app_root.tracker.set_dirty(true);
1011 disabled_app_root.tracker.set_needs_folder_listing(true);
1013 TrackedFile known_file(CreateTrackedFile(disabled_app_root, "known_file"));
1014 TrackedFile file(CreateTrackedFile(disabled_app_root, "file"));
1015 file.should_be_absent = true;
1017 const TrackedFile* tracked_files[] = {
1018 &sync_root, &disabled_app_root, &disabled_app_root, &known_file, &file,
1021 SetUpDatabaseByTrackedFiles(tracked_files, arraysize(tracked_files));
1022 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
1023 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
1025 FileIDList disabled_app_children;
1026 disabled_app_children.push_back(file.metadata.file_id());
1027 EXPECT_EQ(SYNC_STATUS_OK, PopulateFolder(
1028 disabled_app_root.metadata.file_id(), disabled_app_children));
1029 ResetTrackerID(&file.tracker);
1030 file.tracker.clear_synced_details();
1031 file.tracker.set_dirty(true);
1032 file.tracker.set_active(false);
1033 file.should_be_absent = false;
1034 file.tracker_only = true;
1036 disabled_app_root.tracker.set_dirty(false);
1037 disabled_app_root.tracker.set_needs_folder_listing(false);
1038 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
1039 VerifyReloadConsistency();
1042 // TODO(tzik): Fix expectation and re-enable this test.
1043 TEST_P(MetadataDatabaseTest, DISABLED_UpdateTrackerTest) {
1044 TrackedFile sync_root(CreateTrackedSyncRoot());
1045 TrackedFile app_root(CreateTrackedAppRoot(sync_root, "app_root"));
1046 TrackedFile file(CreateTrackedFile(app_root, "file"));
1047 file.tracker.set_dirty(true);
1048 file.metadata.mutable_details()->set_title("renamed file");
1050 TrackedFile inactive_file(CreateTrackedFile(app_root, "inactive_file"));
1051 inactive_file.tracker.set_active(false);
1052 inactive_file.tracker.set_dirty(true);
1053 inactive_file.metadata.mutable_details()->set_title("renamed inactive file");
1054 inactive_file.metadata.mutable_details()->set_md5("modified_md5");
1056 TrackedFile new_conflict(CreateTrackedFile(app_root, "new conflict file"));
1057 new_conflict.tracker.set_dirty(true);
1058 new_conflict.metadata.mutable_details()->set_title("renamed file");
1060 const TrackedFile* tracked_files[] = {
1061 &sync_root, &app_root, &file, &inactive_file, &new_conflict
1064 SetUpDatabaseByTrackedFiles(tracked_files, arraysize(tracked_files));
1065 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
1066 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
1067 VerifyReloadConsistency();
1069 *file.tracker.mutable_synced_details() = file.metadata.details();
1070 file.tracker.set_dirty(false);
1071 EXPECT_EQ(SYNC_STATUS_OK, UpdateTracker(file.tracker));
1072 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
1073 VerifyReloadConsistency();
1075 *inactive_file.tracker.mutable_synced_details() =
1076 inactive_file.metadata.details();
1077 inactive_file.tracker.set_dirty(false);
1078 inactive_file.tracker.set_active(true);
1079 EXPECT_EQ(SYNC_STATUS_OK, UpdateTracker(inactive_file.tracker));
1080 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
1081 VerifyReloadConsistency();
1083 *new_conflict.tracker.mutable_synced_details() =
1084 new_conflict.metadata.details();
1085 new_conflict.tracker.set_dirty(false);
1086 new_conflict.tracker.set_active(true);
1087 file.tracker.set_dirty(true);
1088 file.tracker.set_active(false);
1089 EXPECT_EQ(SYNC_STATUS_OK, UpdateTracker(new_conflict.tracker));
1090 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
1091 VerifyReloadConsistency();
1094 TEST_P(MetadataDatabaseTest, PopulateInitialDataTest) {
1095 TrackedFile sync_root(CreateTrackedSyncRoot());
1096 TrackedFile app_root(CreateTrackedFolder(sync_root, "app_root"));
1097 app_root.tracker.set_active(false);
1099 const TrackedFile* tracked_files[] = {
1100 &sync_root, &app_root
1103 scoped_ptr<google_apis::FileResource> sync_root_folder(
1104 CreateFileResourceFromMetadata(sync_root.metadata));
1105 scoped_ptr<google_apis::FileResource> app_root_folder(
1106 CreateFileResourceFromMetadata(app_root.metadata));
1108 ScopedVector<google_apis::FileResource> app_root_folders;
1109 app_root_folders.push_back(app_root_folder.release());
1111 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
1112 EXPECT_EQ(SYNC_STATUS_OK, PopulateInitialData(
1113 current_change_id(),
1114 *sync_root_folder,
1115 app_root_folders));
1117 ResetTrackerID(&sync_root.tracker);
1118 ResetTrackerID(&app_root.tracker);
1119 app_root.tracker.set_parent_tracker_id(sync_root.tracker.tracker_id());
1121 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
1122 VerifyReloadConsistency();
1125 TEST_P(MetadataDatabaseTest, DumpFiles) {
1126 TrackedFile sync_root(CreateTrackedSyncRoot());
1127 TrackedFile app_root(CreateTrackedAppRoot(sync_root, "app_id"));
1128 app_root.tracker.set_app_id(app_root.metadata.details().title());
1130 TrackedFile folder_0(CreateTrackedFolder(app_root, "folder_0"));
1131 TrackedFile file_0(CreateTrackedFile(folder_0, "file_0"));
1133 const TrackedFile* tracked_files[] = {
1134 &sync_root, &app_root, &folder_0, &file_0
1137 SetUpDatabaseByTrackedFiles(tracked_files, arraysize(tracked_files));
1138 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase());
1139 VerifyTrackedFiles(tracked_files, arraysize(tracked_files));
1141 scoped_ptr<base::ListValue> files =
1142 metadata_database()->DumpFiles(app_root.tracker.app_id());
1143 ASSERT_EQ(2u, files->GetSize());
1145 base::DictionaryValue* file = nullptr;
1146 std::string str;
1148 ASSERT_TRUE(files->GetDictionary(0, &file));
1149 EXPECT_TRUE(file->GetString("title", &str) && str == "folder_0");
1150 EXPECT_TRUE(file->GetString("type", &str) && str == "folder");
1151 EXPECT_TRUE(file->HasKey("details"));
1153 ASSERT_TRUE(files->GetDictionary(1, &file));
1154 EXPECT_TRUE(file->GetString("title", &str) && str == "file_0");
1155 EXPECT_TRUE(file->GetString("type", &str) && str == "file");
1156 EXPECT_TRUE(file->HasKey("details"));
1159 } // namespace drive_backend
1160 } // namespace sync_file_system