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 / register_app_task_unittest.cc
blob7c3b0aacdc4dfb07d9defca1922178edc1cb7530
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/register_app_task.h"
7 #include <vector>
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/format_macros.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
16 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h"
17 #include "chrome/browser/sync_file_system/drive_backend/fake_drive_service_helper.h"
18 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h"
19 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
20 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
21 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
22 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
23 #include "components/drive/drive_uploader.h"
24 #include "components/drive/service/fake_drive_service.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "google_apis/drive/drive_api_parser.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
29 #include "third_party/leveldatabase/src/include/leveldb/db.h"
30 #include "third_party/leveldatabase/src/include/leveldb/env.h"
32 namespace sync_file_system {
33 namespace drive_backend {
35 namespace {
36 const int64 kSyncRootTrackerID = 100;
37 } // namespace
39 class RegisterAppTaskTest : public testing::Test {
40 public:
41 RegisterAppTaskTest()
42 : next_file_id_(1000),
43 next_tracker_id_(10000) {}
44 ~RegisterAppTaskTest() override {}
46 void SetUp() override {
47 ASSERT_TRUE(database_dir_.CreateUniqueTempDir());
48 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
50 scoped_ptr<drive::FakeDriveService>
51 fake_drive_service(new drive::FakeDriveService);
52 scoped_ptr<drive::DriveUploaderInterface>
53 drive_uploader(new drive::DriveUploader(
54 fake_drive_service.get(),
55 base::ThreadTaskRunnerHandle::Get()));
57 fake_drive_service_helper_.reset(new FakeDriveServiceHelper(
58 fake_drive_service.get(), drive_uploader.get(),
59 kSyncRootFolderTitle));
61 context_.reset(new SyncEngineContext(fake_drive_service.Pass(),
62 drive_uploader.Pass(),
63 nullptr /* task_logger */,
64 base::ThreadTaskRunnerHandle::Get(),
65 base::ThreadTaskRunnerHandle::Get(),
66 nullptr /* worker_pool */));
68 ASSERT_EQ(google_apis::HTTP_CREATED,
69 fake_drive_service_helper_->AddOrphanedFolder(
70 kSyncRootFolderTitle, &sync_root_folder_id_));
73 void TearDown() override {
74 context_.reset();
75 base::RunLoop().RunUntilIdle();
78 protected:
79 scoped_ptr<LevelDBWrapper> OpenLevelDB() {
80 leveldb::DB* db = nullptr;
81 leveldb::Options options;
82 options.create_if_missing = true;
83 options.env = in_memory_env_.get();
84 leveldb::Status status =
85 leveldb::DB::Open(options, database_dir_.path().AsUTF8Unsafe(), &db);
86 EXPECT_TRUE(status.ok());
87 return make_scoped_ptr(new LevelDBWrapper(make_scoped_ptr(db)));
90 void SetUpInitialData(LevelDBWrapper* db) {
91 ServiceMetadata service_metadata;
92 service_metadata.set_largest_change_id(100);
93 service_metadata.set_sync_root_tracker_id(kSyncRootTrackerID);
94 service_metadata.set_next_tracker_id(next_tracker_id_);
96 FileDetails sync_root_details;
97 sync_root_details.set_title(kSyncRootFolderTitle);
98 sync_root_details.set_file_kind(FILE_KIND_FOLDER);
99 sync_root_details.set_change_id(1);
101 FileMetadata sync_root_metadata;
102 sync_root_metadata.set_file_id(sync_root_folder_id_);
103 *sync_root_metadata.mutable_details() = sync_root_details;
105 FileTracker sync_root_tracker;
106 sync_root_tracker.set_tracker_id(service_metadata.sync_root_tracker_id());
107 sync_root_tracker.set_parent_tracker_id(0);
108 sync_root_tracker.set_file_id(sync_root_metadata.file_id());
109 sync_root_tracker.set_tracker_kind(TRACKER_KIND_REGULAR);
110 *sync_root_tracker.mutable_synced_details() = sync_root_details;
111 sync_root_tracker.set_active(true);
113 db->Put(kDatabaseVersionKey,
114 base::Int64ToString(kCurrentDatabaseVersion));
115 PutServiceMetadataToDB(service_metadata, db);
116 PutFileMetadataToDB(sync_root_metadata, db);
117 PutFileTrackerToDB(sync_root_tracker, db);
118 EXPECT_TRUE(db->Commit().ok());
121 void CreateMetadataDatabase(scoped_ptr<LevelDBWrapper> db) {
122 ASSERT_TRUE(db);
123 ASSERT_FALSE(context_->GetMetadataDatabase());
124 scoped_ptr<MetadataDatabase> metadata_db;
125 ASSERT_EQ(SYNC_STATUS_OK,
126 MetadataDatabase::CreateForTesting(
127 db.Pass(), true /* enable_on_disk_index */, &metadata_db));
128 context_->SetMetadataDatabase(metadata_db.Pass());
131 SyncStatusCode RunRegisterAppTask(const std::string& app_id) {
132 RegisterAppTask task(context_.get(), app_id);
133 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
134 task.RunExclusive(CreateResultReceiver(&status));
135 base::RunLoop().RunUntilIdle();
136 return status;
139 void SetUpRegisteredAppRoot(
140 const std::string& app_id,
141 LevelDBWrapper* db) {
142 FileDetails details;
143 details.set_title(app_id);
144 details.set_file_kind(FILE_KIND_FOLDER);
145 details.add_parent_folder_ids(sync_root_folder_id_);
147 FileMetadata metadata;
148 metadata.set_file_id(GenerateFileID());
149 *metadata.mutable_details() = details;
151 FileTracker tracker;
152 tracker.set_parent_tracker_id(kSyncRootTrackerID);
153 tracker.set_tracker_id(next_tracker_id_++);
154 tracker.set_file_id(metadata.file_id());
155 tracker.set_tracker_kind(TRACKER_KIND_APP_ROOT);
156 tracker.set_app_id(app_id);
157 *tracker.mutable_synced_details() = details;
158 tracker.set_active(true);
160 PutFileMetadataToDB(metadata, db);
161 PutFileTrackerToDB(tracker, db);
162 EXPECT_TRUE(db->Commit().ok());
165 void SetUpUnregisteredAppRoot(const std::string& app_id,
166 LevelDBWrapper* db) {
167 FileDetails details;
168 details.set_title(app_id);
169 details.set_file_kind(FILE_KIND_FOLDER);
170 details.add_parent_folder_ids(sync_root_folder_id_);
172 FileMetadata metadata;
173 metadata.set_file_id(GenerateFileID());
174 *metadata.mutable_details() = details;
176 FileTracker tracker;
177 tracker.set_parent_tracker_id(kSyncRootTrackerID);
178 tracker.set_tracker_id(next_tracker_id_++);
179 tracker.set_file_id(metadata.file_id());
180 tracker.set_tracker_kind(TRACKER_KIND_REGULAR);
181 *tracker.mutable_synced_details() = details;
182 tracker.set_active(false);
184 PutFileMetadataToDB(metadata, db);
185 PutFileTrackerToDB(tracker, db);
186 EXPECT_TRUE(db->Commit().ok());
189 size_t CountRegisteredAppRoot() {
190 std::vector<std::string> app_ids;
191 context_->GetMetadataDatabase()->GetRegisteredAppIDs(&app_ids);
192 return app_ids.size();
195 bool IsAppRegistered(const std::string& app_id) {
196 TrackerIDSet trackers;
197 if (!context_->GetMetadataDatabase()->FindTrackersByParentAndTitle(
198 kSyncRootTrackerID, app_id, &trackers))
199 return false;
200 return trackers.has_active();
203 size_t CountRemoteFileInSyncRoot() {
204 ScopedVector<google_apis::FileResource> files;
205 EXPECT_EQ(google_apis::HTTP_SUCCESS,
206 fake_drive_service_helper_->ListFilesInFolder(
207 sync_root_folder_id_, &files));
208 return files.size();
211 bool HasRemoteAppRoot(const std::string& app_id) {
212 TrackerIDSet files;
213 if (!context_->GetMetadataDatabase()->FindTrackersByParentAndTitle(
214 kSyncRootTrackerID, app_id, &files) ||
215 !files.has_active())
216 return false;
218 FileTracker app_root_tracker;
219 EXPECT_TRUE(context_->GetMetadataDatabase()->FindTrackerByTrackerID(
220 files.active_tracker(), &app_root_tracker));
221 std::string app_root_folder_id = app_root_tracker.file_id();
222 scoped_ptr<google_apis::FileResource> entry;
223 if (google_apis::HTTP_SUCCESS !=
224 fake_drive_service_helper_->GetFileResource(app_root_folder_id, &entry))
225 return false;
227 return !entry->labels().is_trashed();
230 private:
231 std::string GenerateFileID() {
232 return base::StringPrintf("file_id_%" PRId64, next_file_id_++);
235 scoped_ptr<leveldb::Env> in_memory_env_;
237 std::string sync_root_folder_id_;
239 int64 next_file_id_;
240 int64 next_tracker_id_;
242 content::TestBrowserThreadBundle browser_threads_;
243 base::ScopedTempDir database_dir_;
245 scoped_ptr<SyncEngineContext> context_;
246 scoped_ptr<FakeDriveServiceHelper> fake_drive_service_helper_;
248 DISALLOW_COPY_AND_ASSIGN(RegisterAppTaskTest);
251 TEST_F(RegisterAppTaskTest, AlreadyRegistered) {
252 scoped_ptr<LevelDBWrapper> db = OpenLevelDB();
253 ASSERT_TRUE(db);
254 SetUpInitialData(db.get());
256 const std::string kAppID = "app_id";
257 SetUpRegisteredAppRoot(kAppID, db.get());
259 CreateMetadataDatabase(db.Pass());
260 EXPECT_EQ(SYNC_STATUS_OK, RunRegisterAppTask(kAppID));
262 EXPECT_EQ(1u, CountRegisteredAppRoot());
263 EXPECT_TRUE(IsAppRegistered(kAppID));
266 TEST_F(RegisterAppTaskTest, CreateAppFolder) {
267 scoped_ptr<LevelDBWrapper> db = OpenLevelDB();
268 ASSERT_TRUE(db);
269 SetUpInitialData(db.get());
271 const std::string kAppID = "app_id";
272 CreateMetadataDatabase(db.Pass());
273 RunRegisterAppTask(kAppID);
275 EXPECT_EQ(1u, CountRegisteredAppRoot());
276 EXPECT_TRUE(IsAppRegistered(kAppID));
278 EXPECT_EQ(1u, CountRemoteFileInSyncRoot());
279 EXPECT_TRUE(HasRemoteAppRoot(kAppID));
282 TEST_F(RegisterAppTaskTest, RegisterExistingFolder) {
283 scoped_ptr<LevelDBWrapper> db = OpenLevelDB();
284 ASSERT_TRUE(db);
285 SetUpInitialData(db.get());
287 const std::string kAppID = "app_id";
288 SetUpUnregisteredAppRoot(kAppID, db.get());
290 CreateMetadataDatabase(db.Pass());
291 RunRegisterAppTask(kAppID);
293 EXPECT_EQ(1u, CountRegisteredAppRoot());
294 EXPECT_TRUE(IsAppRegistered(kAppID));
297 TEST_F(RegisterAppTaskTest, RegisterExistingFolder_MultipleCandidate) {
298 scoped_ptr<LevelDBWrapper> db = OpenLevelDB();
299 ASSERT_TRUE(db);
300 SetUpInitialData(db.get());
302 const std::string kAppID = "app_id";
303 SetUpUnregisteredAppRoot(kAppID, db.get());
304 SetUpUnregisteredAppRoot(kAppID, db.get());
306 CreateMetadataDatabase(db.Pass());
307 RunRegisterAppTask(kAppID);
309 EXPECT_EQ(1u, CountRegisteredAppRoot());
310 EXPECT_TRUE(IsAppRegistered(kAppID));
313 } // namespace drive_backend
314 } // namespace sync_file_system