| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/sync_file_system/drive_backend/metadata_database.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h" |
| 6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/macros.h" |
| 9 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 12 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.
h" | 15 #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" | 16 #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" | 17 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h" |
| 15 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" | 18 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" |
| 16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" | 19 #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" | 20 #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" | 21 #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" | 22 #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" | 23 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" |
| 21 #include "google_apis/drive/drive_api_parser.h" | 24 #include "google_apis/drive/drive_api_parser.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 26 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
| 24 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 27 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 25 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 28 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
| 26 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 29 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 27 | 30 |
| 28 #define FPL(a) FILE_PATH_LITERAL(a) | 31 #define FPL(a) FILE_PATH_LITERAL(a) |
| 29 | 32 |
| 30 namespace sync_file_system { | 33 namespace sync_file_system { |
| 31 namespace drive_backend { | 34 namespace drive_backend { |
| 32 | 35 |
| 33 namespace { | 36 namespace { |
| 34 | 37 |
| 35 typedef MetadataDatabase::FileIDList FileIDList; | 38 typedef MetadataDatabase::FileIDList FileIDList; |
| 36 | 39 |
| 37 const int64 kInitialChangeID = 1234; | 40 const int64_t kInitialChangeID = 1234; |
| 38 const int64 kSyncRootTrackerID = 100; | 41 const int64_t kSyncRootTrackerID = 100; |
| 39 const char kSyncRootFolderID[] = "sync_root_folder_id"; | 42 const char kSyncRootFolderID[] = "sync_root_folder_id"; |
| 40 | 43 |
| 41 // This struct is used to setup initial state of the database in the test and | 44 // 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 | 45 // also used to match to the modified content of the database as the |
| 43 // expectation. | 46 // expectation. |
| 44 struct TrackedFile { | 47 struct TrackedFile { |
| 45 // Holds the latest remote metadata which may be not-yet-synced to |tracker|. | 48 // Holds the latest remote metadata which may be not-yet-synced to |tracker|. |
| 46 FileMetadata metadata; | 49 FileMetadata metadata; |
| 47 FileTracker tracker; | 50 FileTracker tracker; |
| 48 | 51 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 75 | 78 |
| 76 void ExpectEquivalent(const FileTracker* left, const FileTracker* right) { | 79 void ExpectEquivalent(const FileTracker* left, const FileTracker* right) { |
| 77 if (!left) { | 80 if (!left) { |
| 78 ASSERT_FALSE(right); | 81 ASSERT_FALSE(right); |
| 79 return; | 82 return; |
| 80 } | 83 } |
| 81 ASSERT_TRUE(right); | 84 ASSERT_TRUE(right); |
| 82 test_util::ExpectEquivalentTrackers(*left, *right); | 85 test_util::ExpectEquivalentTrackers(*left, *right); |
| 83 } | 86 } |
| 84 | 87 |
| 85 void ExpectEquivalent(int64 left, int64 right) { | 88 void ExpectEquivalent(int64_t left, int64_t right) { |
| 86 EXPECT_EQ(left, right); | 89 EXPECT_EQ(left, right); |
| 87 } | 90 } |
| 88 | 91 |
| 89 template <typename Container> | 92 template <typename Container> |
| 90 void ExpectEquivalentMaps(const Container& left, const Container& right); | 93 void ExpectEquivalentMaps(const Container& left, const Container& right); |
| 91 | 94 |
| 92 template <typename Key, typename Value> | 95 template <typename Key, typename Value> |
| 93 void ExpectEquivalent(const std::map<Key, Value>& left, | 96 void ExpectEquivalent(const std::map<Key, Value>& left, |
| 94 const std::map<Key, Value>& right) { | 97 const std::map<Key, Value>& right) { |
| 95 ExpectEquivalentMaps(left, right); | 98 ExpectEquivalentMaps(left, right); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); | 188 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); |
| 186 } | 189 } |
| 187 | 190 |
| 188 void TearDown() override { DropDatabase(); } | 191 void TearDown() override { DropDatabase(); } |
| 189 | 192 |
| 190 protected: | 193 protected: |
| 191 std::string GenerateFileID() { | 194 std::string GenerateFileID() { |
| 192 return "file_id_" + base::Int64ToString(next_file_id_number_++); | 195 return "file_id_" + base::Int64ToString(next_file_id_number_++); |
| 193 } | 196 } |
| 194 | 197 |
| 195 int64 GetTrackerIDByFileID(const std::string& file_id) { | 198 int64_t GetTrackerIDByFileID(const std::string& file_id) { |
| 196 TrackerIDSet trackers; | 199 TrackerIDSet trackers; |
| 197 if (metadata_database_->FindTrackersByFileID(file_id, &trackers)) { | 200 if (metadata_database_->FindTrackersByFileID(file_id, &trackers)) { |
| 198 EXPECT_FALSE(trackers.empty()); | 201 EXPECT_FALSE(trackers.empty()); |
| 199 return *trackers.begin(); | 202 return *trackers.begin(); |
| 200 } | 203 } |
| 201 return 0; | 204 return 0; |
| 202 } | 205 } |
| 203 | 206 |
| 204 SyncStatusCode InitializeMetadataDatabase() { | 207 SyncStatusCode InitializeMetadataDatabase() { |
| 205 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 208 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 return metadata_database_->PopulateFolderByChildList( | 583 return metadata_database_->PopulateFolderByChildList( |
| 581 folder_id, listed_children); | 584 folder_id, listed_children); |
| 582 } | 585 } |
| 583 | 586 |
| 584 SyncStatusCode UpdateTracker(const FileTracker& tracker) { | 587 SyncStatusCode UpdateTracker(const FileTracker& tracker) { |
| 585 return metadata_database_->UpdateTracker( | 588 return metadata_database_->UpdateTracker( |
| 586 tracker.tracker_id(), tracker.synced_details()); | 589 tracker.tracker_id(), tracker.synced_details()); |
| 587 } | 590 } |
| 588 | 591 |
| 589 SyncStatusCode PopulateInitialData( | 592 SyncStatusCode PopulateInitialData( |
| 590 int64 largest_change_id, | 593 int64_t largest_change_id, |
| 591 const google_apis::FileResource& sync_root_folder, | 594 const google_apis::FileResource& sync_root_folder, |
| 592 const ScopedVector<google_apis::FileResource>& app_root_folders) { | 595 const ScopedVector<google_apis::FileResource>& app_root_folders) { |
| 593 return metadata_database_->PopulateInitialData( | 596 return metadata_database_->PopulateInitialData( |
| 594 largest_change_id, sync_root_folder, app_root_folders); | 597 largest_change_id, sync_root_folder, app_root_folders); |
| 595 } | 598 } |
| 596 | 599 |
| 597 void ResetTrackerID(FileTracker* tracker) { | 600 void ResetTrackerID(FileTracker* tracker) { |
| 598 tracker->set_tracker_id(GetTrackerIDByFileID(tracker->file_id())); | 601 tracker->set_tracker_id(GetTrackerIDByFileID(tracker->file_id())); |
| 599 } | 602 } |
| 600 | 603 |
| 601 int64 current_change_id() const { | 604 int64_t current_change_id() const { return current_change_id_; } |
| 602 return current_change_id_; | |
| 603 } | |
| 604 | 605 |
| 605 private: | 606 private: |
| 606 base::ScopedTempDir database_dir_; | 607 base::ScopedTempDir database_dir_; |
| 607 base::MessageLoop message_loop_; | 608 base::MessageLoop message_loop_; |
| 608 | 609 |
| 609 scoped_ptr<leveldb::Env> in_memory_env_; | 610 scoped_ptr<leveldb::Env> in_memory_env_; |
| 610 scoped_ptr<MetadataDatabase> metadata_database_; | 611 scoped_ptr<MetadataDatabase> metadata_database_; |
| 611 | 612 |
| 612 int64 current_change_id_; | 613 int64_t current_change_id_; |
| 613 int64 next_tracker_id_; | 614 int64_t next_tracker_id_; |
| 614 int64 next_file_id_number_; | 615 int64_t next_file_id_number_; |
| 615 int64 next_md5_sequence_number_; | 616 int64_t next_md5_sequence_number_; |
| 616 | 617 |
| 617 DISALLOW_COPY_AND_ASSIGN(MetadataDatabaseTest); | 618 DISALLOW_COPY_AND_ASSIGN(MetadataDatabaseTest); |
| 618 }; | 619 }; |
| 619 | 620 |
| 620 INSTANTIATE_TEST_CASE_P(MetadataDatabaseTestWithIndexesOnDisk, | 621 INSTANTIATE_TEST_CASE_P(MetadataDatabaseTestWithIndexesOnDisk, |
| 621 MetadataDatabaseTest, | 622 MetadataDatabaseTest, |
| 622 ::testing::Values(true, false)); | 623 ::testing::Values(true, false)); |
| 623 | 624 |
| 624 TEST_P(MetadataDatabaseTest, InitializationTest_Empty) { | 625 TEST_P(MetadataDatabaseTest, InitializationTest_Empty) { |
| 625 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase()); | 626 EXPECT_EQ(SYNC_STATUS_OK, InitializeMetadataDatabase()); |
| (...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1151 EXPECT_TRUE(file->HasKey("details")); | 1152 EXPECT_TRUE(file->HasKey("details")); |
| 1152 | 1153 |
| 1153 ASSERT_TRUE(files->GetDictionary(1, &file)); | 1154 ASSERT_TRUE(files->GetDictionary(1, &file)); |
| 1154 EXPECT_TRUE(file->GetString("title", &str) && str == "file_0"); | 1155 EXPECT_TRUE(file->GetString("title", &str) && str == "file_0"); |
| 1155 EXPECT_TRUE(file->GetString("type", &str) && str == "file"); | 1156 EXPECT_TRUE(file->GetString("type", &str) && str == "file"); |
| 1156 EXPECT_TRUE(file->HasKey("details")); | 1157 EXPECT_TRUE(file->HasKey("details")); |
| 1157 } | 1158 } |
| 1158 | 1159 |
| 1159 } // namespace drive_backend | 1160 } // namespace drive_backend |
| 1160 } // namespace sync_file_system | 1161 } // namespace sync_file_system |
| OLD | NEW |