1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/drive/change_list_loader.h"
#include <stdint.h>
#include "base/callback_helpers.h"
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/testing_pref_service.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "components/drive/change_list_loader_observer.h"
#include "components/drive/drive_test_util.h"
#include "components/drive/event_logger.h"
#include "components/drive/file_cache.h"
#include "components/drive/file_change.h"
#include "components/drive/file_system_core_util.h"
#include "components/drive/job_scheduler.h"
#include "components/drive/resource_metadata.h"
#include "components/drive/service/fake_drive_service.h"
#include "components/drive/service/test_util.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "google_apis/drive/drive_api_parser.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace drive {
namespace internal {
class TestChangeListLoaderObserver : public ChangeListLoaderObserver {
public:
explicit TestChangeListLoaderObserver(ChangeListLoader* loader)
: loader_(loader),
load_from_server_complete_count_(0),
initial_load_complete_count_(0) {
loader_->AddObserver(this);
}
~TestChangeListLoaderObserver() override { loader_->RemoveObserver(this); }
const FileChange& changed_files() const { return changed_files_; }
void clear_changed_files() { changed_files_.ClearForTest(); }
int load_from_server_complete_count() const {
return load_from_server_complete_count_;
}
int initial_load_complete_count() const {
return initial_load_complete_count_;
}
// ChageListObserver overrides:
void OnFileChanged(const FileChange& changed_files) override {
changed_files_.Apply(changed_files);
}
void OnLoadFromServerComplete() override {
++load_from_server_complete_count_;
}
void OnInitialLoadComplete() override { ++initial_load_complete_count_; }
private:
ChangeListLoader* loader_;
FileChange changed_files_;
int load_from_server_complete_count_;
int initial_load_complete_count_;
DISALLOW_COPY_AND_ASSIGN(TestChangeListLoaderObserver);
};
class ChangeListLoaderTest : public testing::Test {
protected:
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
pref_service_.reset(new TestingPrefServiceSimple);
test_util::RegisterDrivePrefs(pref_service_->registry());
logger_.reset(new EventLogger);
drive_service_.reset(new FakeDriveService);
ASSERT_TRUE(test_util::SetUpTestEntries(drive_service_.get()));
scheduler_.reset(new JobScheduler(
pref_service_.get(),
logger_.get(),
drive_service_.get(),
base::ThreadTaskRunnerHandle::Get().get()));
metadata_storage_.reset(new ResourceMetadataStorage(
temp_dir_.path(), base::ThreadTaskRunnerHandle::Get().get()));
ASSERT_TRUE(metadata_storage_->Initialize());
cache_.reset(new FileCache(metadata_storage_.get(),
temp_dir_.path(),
base::ThreadTaskRunnerHandle::Get().get(),
NULL /* free_disk_space_getter */));
ASSERT_TRUE(cache_->Initialize());
metadata_.reset(new ResourceMetadata(
metadata_storage_.get(), cache_.get(),
base::ThreadTaskRunnerHandle::Get().get()));
ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
loader_controller_.reset(new LoaderController);
change_list_loader_.reset(
new ChangeListLoader(logger_.get(),
base::ThreadTaskRunnerHandle::Get().get(),
metadata_.get(),
scheduler_.get(),
about_resource_loader_.get(),
loader_controller_.get()));
}
// Adds a new file to the root directory of the service.
scoped_ptr<google_apis::FileResource> AddNewFile(const std::string& title) {
google_apis::DriveApiErrorCode error = google_apis::DRIVE_FILE_ERROR;
scoped_ptr<google_apis::FileResource> entry;
drive_service_->AddNewFile(
"text/plain",
"content text",
drive_service_->GetRootResourceId(),
title,
false, // shared_with_me
google_apis::test_util::CreateCopyResultCallback(&error, &entry));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(google_apis::HTTP_CREATED, error);
return entry;
}
content::TestBrowserThreadBundle thread_bundle_;
base::ScopedTempDir temp_dir_;
scoped_ptr<TestingPrefServiceSimple> pref_service_;
scoped_ptr<EventLogger> logger_;
scoped_ptr<FakeDriveService> drive_service_;
scoped_ptr<JobScheduler> scheduler_;
scoped_ptr<ResourceMetadataStorage,
test_util::DestroyHelperForTests> metadata_storage_;
scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
scoped_ptr<AboutResourceLoader> about_resource_loader_;
scoped_ptr<LoaderController> loader_controller_;
scoped_ptr<ChangeListLoader> change_list_loader_;
};
TEST_F(ChangeListLoaderTest, AboutResourceLoader) {
google_apis::DriveApiErrorCode error[6] = {};
scoped_ptr<google_apis::AboutResource> about[6];
// No resource is cached at the beginning.
ASSERT_FALSE(about_resource_loader_->cached_about_resource());
// Since no resource is cached, this "Get" should trigger the update.
about_resource_loader_->GetAboutResource(
google_apis::test_util::CreateCopyResultCallback(error + 0, about + 0));
// Since there is one in-flight update, the next "Get" just wait for it.
about_resource_loader_->GetAboutResource(
google_apis::test_util::CreateCopyResultCallback(error + 1, about + 1));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(google_apis::HTTP_SUCCESS, error[0]);
EXPECT_EQ(google_apis::HTTP_SUCCESS, error[1]);
const int64_t first_changestamp = about[0]->largest_change_id();
EXPECT_EQ(first_changestamp, about[1]->largest_change_id());
ASSERT_TRUE(about_resource_loader_->cached_about_resource());
EXPECT_EQ(
first_changestamp,
about_resource_loader_->cached_about_resource()->largest_change_id());
// Increment changestamp by 1.
AddNewFile("temp");
// Explicitly calling UpdateAboutResource will start another API call.
about_resource_loader_->UpdateAboutResource(
google_apis::test_util::CreateCopyResultCallback(error + 2, about + 2));
// It again waits for the in-flight UpdateAboutResoure call, even though this
// time there is a cached result.
about_resource_loader_->GetAboutResource(
google_apis::test_util::CreateCopyResultCallback(error + 3, about + 3));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(google_apis::HTTP_SUCCESS, error[2]);
EXPECT_EQ(google_apis::HTTP_SUCCESS, error[3]);
EXPECT_EQ(first_changestamp + 1, about[2]->largest_change_id());
EXPECT_EQ(first_changestamp + 1, about[3]->largest_change_id());
EXPECT_EQ(
first_changestamp + 1,
about_resource_loader_->cached_about_resource()->largest_change_id());
// Increment changestamp by 1.
AddNewFile("temp2");
// Now no UpdateAboutResource task is running. Returns the cached result.
about_resource_loader_->GetAboutResource(
google_apis::test_util::CreateCopyResultCallback(error + 4, about + 4));
// Explicitly calling UpdateAboutResource will start another API call.
about_resource_loader_->UpdateAboutResource(
google_apis::test_util::CreateCopyResultCallback(error + 5, about + 5));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(google_apis::HTTP_NO_CONTENT, error[4]);
EXPECT_EQ(google_apis::HTTP_SUCCESS, error[5]);
EXPECT_EQ(first_changestamp + 1, about[4]->largest_change_id());
EXPECT_EQ(first_changestamp + 2, about[5]->largest_change_id());
EXPECT_EQ(
first_changestamp + 2,
about_resource_loader_->cached_about_resource()->largest_change_id());
EXPECT_EQ(3, drive_service_->about_resource_load_count());
}
TEST_F(ChangeListLoaderTest, Load) {
EXPECT_FALSE(change_list_loader_->IsRefreshing());
// Start initial load.
TestChangeListLoaderObserver observer(change_list_loader_.get());
EXPECT_EQ(0, drive_service_->about_resource_load_count());
FileError error = FILE_ERROR_FAILED;
change_list_loader_->LoadIfNeeded(
google_apis::test_util::CreateCopyResultCallback(&error));
EXPECT_TRUE(change_list_loader_->IsRefreshing());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(FILE_ERROR_OK, error);
EXPECT_FALSE(change_list_loader_->IsRefreshing());
int64_t changestamp = 0;
EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
EXPECT_LT(0, changestamp);
EXPECT_EQ(1, drive_service_->file_list_load_count());
EXPECT_EQ(1, drive_service_->about_resource_load_count());
EXPECT_EQ(1, observer.initial_load_complete_count());
EXPECT_EQ(1, observer.load_from_server_complete_count());
EXPECT_TRUE(observer.changed_files().empty());
base::FilePath file_path =
util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
ResourceEntry entry;
EXPECT_EQ(FILE_ERROR_OK,
metadata_->GetResourceEntryByPath(file_path, &entry));
}
TEST_F(ChangeListLoaderTest, Load_LocalMetadataAvailable) {
// Prepare metadata.
FileError error = FILE_ERROR_FAILED;
change_list_loader_->LoadIfNeeded(
google_apis::test_util::CreateCopyResultCallback(&error));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(FILE_ERROR_OK, error);
// Reset loader.
about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
change_list_loader_.reset(
new ChangeListLoader(logger_.get(),
base::ThreadTaskRunnerHandle::Get().get(),
metadata_.get(),
scheduler_.get(),
about_resource_loader_.get(),
loader_controller_.get()));
// Add a file to the service.
scoped_ptr<google_apis::FileResource> gdata_entry = AddNewFile("New File");
ASSERT_TRUE(gdata_entry);
// Start loading. Because local metadata is available, the load results in
// returning FILE_ERROR_OK without fetching full list of resources.
const int previous_file_list_load_count =
drive_service_->file_list_load_count();
TestChangeListLoaderObserver observer(change_list_loader_.get());
change_list_loader_->LoadIfNeeded(
google_apis::test_util::CreateCopyResultCallback(&error));
EXPECT_TRUE(change_list_loader_->IsRefreshing());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(FILE_ERROR_OK, error);
EXPECT_EQ(previous_file_list_load_count,
drive_service_->file_list_load_count());
EXPECT_EQ(1, observer.initial_load_complete_count());
// Update should be checked by Load().
int64_t changestamp = 0;
EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
EXPECT_EQ(drive_service_->about_resource().largest_change_id(), changestamp);
EXPECT_EQ(1, drive_service_->change_list_load_count());
EXPECT_EQ(1, observer.load_from_server_complete_count());
EXPECT_TRUE(
observer.changed_files().CountDirectory(util::GetDriveMyDriveRootPath()));
base::FilePath file_path =
util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title());
ResourceEntry entry;
EXPECT_EQ(FILE_ERROR_OK,
metadata_->GetResourceEntryByPath(file_path, &entry));
}
TEST_F(ChangeListLoaderTest, CheckForUpdates) {
// CheckForUpdates() results in no-op before load.
FileError check_for_updates_error = FILE_ERROR_FAILED;
change_list_loader_->CheckForUpdates(
google_apis::test_util::CreateCopyResultCallback(
&check_for_updates_error));
EXPECT_FALSE(change_list_loader_->IsRefreshing());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(FILE_ERROR_FAILED,
check_for_updates_error); // Callback was not run.
int64_t changestamp = 0;
EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
EXPECT_EQ(0, changestamp);
EXPECT_EQ(0, drive_service_->file_list_load_count());
EXPECT_EQ(0, drive_service_->about_resource_load_count());
// Start initial load.
FileError load_error = FILE_ERROR_FAILED;
change_list_loader_->LoadIfNeeded(
google_apis::test_util::CreateCopyResultCallback(&load_error));
EXPECT_TRUE(change_list_loader_->IsRefreshing());
// CheckForUpdates() while loading.
change_list_loader_->CheckForUpdates(
google_apis::test_util::CreateCopyResultCallback(
&check_for_updates_error));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(change_list_loader_->IsRefreshing());
EXPECT_EQ(FILE_ERROR_OK, load_error);
EXPECT_EQ(FILE_ERROR_OK, check_for_updates_error);
EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
EXPECT_LT(0, changestamp);
EXPECT_EQ(1, drive_service_->file_list_load_count());
int64_t previous_changestamp = 0;
EXPECT_EQ(FILE_ERROR_OK,
metadata_->GetLargestChangestamp(&previous_changestamp));
// CheckForUpdates() results in no update.
change_list_loader_->CheckForUpdates(
google_apis::test_util::CreateCopyResultCallback(
&check_for_updates_error));
EXPECT_TRUE(change_list_loader_->IsRefreshing());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(change_list_loader_->IsRefreshing());
EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
EXPECT_EQ(previous_changestamp, changestamp);
// Add a file to the service.
scoped_ptr<google_apis::FileResource> gdata_entry = AddNewFile("New File");
ASSERT_TRUE(gdata_entry);
// CheckForUpdates() results in update.
TestChangeListLoaderObserver observer(change_list_loader_.get());
change_list_loader_->CheckForUpdates(
google_apis::test_util::CreateCopyResultCallback(
&check_for_updates_error));
EXPECT_TRUE(change_list_loader_->IsRefreshing());
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(change_list_loader_->IsRefreshing());
EXPECT_EQ(FILE_ERROR_OK, metadata_->GetLargestChangestamp(&changestamp));
EXPECT_LT(previous_changestamp, changestamp);
EXPECT_EQ(1, observer.load_from_server_complete_count());
EXPECT_TRUE(
observer.changed_files().CountDirectory(util::GetDriveMyDriveRootPath()));
// The new file is found in the local metadata.
base::FilePath new_file_path =
util::GetDriveMyDriveRootPath().AppendASCII(gdata_entry->title());
ResourceEntry entry;
EXPECT_EQ(FILE_ERROR_OK,
metadata_->GetResourceEntryByPath(new_file_path, &entry));
}
TEST_F(ChangeListLoaderTest, Lock) {
FileError error = FILE_ERROR_FAILED;
change_list_loader_->LoadIfNeeded(
google_apis::test_util::CreateCopyResultCallback(&error));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(FILE_ERROR_OK, error);
// Add a new file.
scoped_ptr<google_apis::FileResource> file = AddNewFile("New File");
ASSERT_TRUE(file);
// Lock the loader.
scoped_ptr<base::ScopedClosureRunner> lock = loader_controller_->GetLock();
// Start update.
TestChangeListLoaderObserver observer(change_list_loader_.get());
FileError check_for_updates_error = FILE_ERROR_FAILED;
change_list_loader_->CheckForUpdates(
google_apis::test_util::CreateCopyResultCallback(
&check_for_updates_error));
base::RunLoop().RunUntilIdle();
// Update is pending due to the lock.
EXPECT_TRUE(observer.changed_files().empty());
// Unlock the loader, this should resume the pending update.
lock.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(
observer.changed_files().CountDirectory(util::GetDriveMyDriveRootPath()));
}
} // namespace internal
} // namespace drive
|