1// Copyright (c) 2012 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 "chrome/browser/drive/fake_drive_service.h"
6
7#include <string>
8#include <vector>
9
10#include "base/files/file_util.h"
11#include "base/files/scoped_temp_dir.h"
12#include "base/md5.h"
13#include "base/run_loop.h"
14#include "base/stl_util.h"
15#include "base/strings/stringprintf.h"
16#include "base/strings/utf_string_conversions.h"
17#include "chrome/browser/drive/test_util.h"
18#include "content/public/test/test_browser_thread_bundle.h"
19#include "google_apis/drive/drive_api_parser.h"
20#include "google_apis/drive/test_util.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23using google_apis::AboutResource;
24using google_apis::AppList;
25using google_apis::ChangeList;
26using google_apis::ChangeResource;
27using google_apis::FileList;
28using google_apis::FileResource;
29using google_apis::GDATA_NO_CONNECTION;
30using google_apis::GDATA_OTHER_ERROR;
31using google_apis::GDataErrorCode;
32using google_apis::GetContentCallback;
33using google_apis::HTTP_CREATED;
34using google_apis::HTTP_FORBIDDEN;
35using google_apis::HTTP_NOT_FOUND;
36using google_apis::HTTP_NO_CONTENT;
37using google_apis::HTTP_PRECONDITION;
38using google_apis::HTTP_RESUME_INCOMPLETE;
39using google_apis::HTTP_SUCCESS;
40using google_apis::ProgressCallback;
41using google_apis::UploadRangeResponse;
42
43namespace drive {
44
45namespace test_util {
46
47using google_apis::test_util::AppendProgressCallbackResult;
48using google_apis::test_util::CreateCopyResultCallback;
49using google_apis::test_util::ProgressInfo;
50using google_apis::test_util::TestGetContentCallback;
51using google_apis::test_util::WriteStringToFile;
52
53}  // namespace test_util
54
55namespace {
56
57class FakeDriveServiceTest : public testing::Test {
58 protected:
59  // Returns the resource entry that matches |resource_id|.
60  scoped_ptr<FileResource> FindEntry(const std::string& resource_id) {
61    GDataErrorCode error = GDATA_OTHER_ERROR;
62    scoped_ptr<FileResource> entry;
63    fake_service_.GetFileResource(
64        resource_id, test_util::CreateCopyResultCallback(&error, &entry));
65    base::RunLoop().RunUntilIdle();
66    return entry.Pass();
67  }
68
69  // Returns true if the resource identified by |resource_id| exists.
70  bool Exists(const std::string& resource_id) {
71    scoped_ptr<FileResource> entry = FindEntry(resource_id);
72    return entry && !entry->labels().is_trashed();
73  }
74
75  // Adds a new directory at |parent_resource_id| with the given name.
76  // Returns true on success.
77  bool AddNewDirectory(const std::string& parent_resource_id,
78                       const std::string& directory_title) {
79    GDataErrorCode error = GDATA_OTHER_ERROR;
80    scoped_ptr<FileResource> entry;
81    fake_service_.AddNewDirectory(
82        parent_resource_id,
83        directory_title,
84        DriveServiceInterface::AddNewDirectoryOptions(),
85        test_util::CreateCopyResultCallback(&error, &entry));
86    base::RunLoop().RunUntilIdle();
87    return error == HTTP_CREATED;
88  }
89
90  // Returns true if the resource identified by |resource_id| has a parent
91  // identified by |parent_id|.
92  bool HasParent(const std::string& resource_id, const std::string& parent_id) {
93    scoped_ptr<FileResource> entry = FindEntry(resource_id);
94    if (entry) {
95      for (size_t i = 0; i < entry->parents().size(); ++i) {
96        if (entry->parents()[i].file_id() == parent_id)
97          return true;
98      }
99    }
100    return false;
101  }
102
103  int64 GetLargestChangeByAboutResource() {
104    GDataErrorCode error;
105    scoped_ptr<AboutResource> about_resource;
106    fake_service_.GetAboutResource(
107        test_util::CreateCopyResultCallback(&error, &about_resource));
108    base::RunLoop().RunUntilIdle();
109    return about_resource->largest_change_id();
110  }
111
112  content::TestBrowserThreadBundle thread_bundle_;
113  FakeDriveService fake_service_;
114};
115
116TEST_F(FakeDriveServiceTest, GetAllFileList) {
117  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
118
119  GDataErrorCode error = GDATA_OTHER_ERROR;
120  scoped_ptr<FileList> file_list;
121  fake_service_.GetAllFileList(
122      test_util::CreateCopyResultCallback(&error, &file_list));
123  base::RunLoop().RunUntilIdle();
124
125  EXPECT_EQ(HTTP_SUCCESS, error);
126  ASSERT_TRUE(file_list);
127  // Do some sanity check.
128  EXPECT_EQ(15U, file_list->items().size());
129  EXPECT_EQ(1, fake_service_.file_list_load_count());
130}
131
132TEST_F(FakeDriveServiceTest, GetAllFileList_Offline) {
133  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
134  fake_service_.set_offline(true);
135
136  GDataErrorCode error = GDATA_OTHER_ERROR;
137  scoped_ptr<FileList> file_list;
138  fake_service_.GetAllFileList(
139      test_util::CreateCopyResultCallback(&error, &file_list));
140  base::RunLoop().RunUntilIdle();
141
142  EXPECT_EQ(GDATA_NO_CONNECTION, error);
143  EXPECT_FALSE(file_list);
144}
145
146TEST_F(FakeDriveServiceTest, GetFileListInDirectory_InRootDirectory) {
147  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
148
149  GDataErrorCode error = GDATA_OTHER_ERROR;
150  scoped_ptr<FileList> file_list;
151  fake_service_.GetFileListInDirectory(
152      fake_service_.GetRootResourceId(),
153      test_util::CreateCopyResultCallback(&error, &file_list));
154  base::RunLoop().RunUntilIdle();
155
156  EXPECT_EQ(HTTP_SUCCESS, error);
157  ASSERT_TRUE(file_list);
158  // Do some sanity check. There are 8 entries in the root directory.
159  EXPECT_EQ(8U, file_list->items().size());
160  EXPECT_EQ(1, fake_service_.directory_load_count());
161}
162
163TEST_F(FakeDriveServiceTest, GetFileListInDirectory_InNonRootDirectory) {
164  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
165
166  GDataErrorCode error = GDATA_OTHER_ERROR;
167  scoped_ptr<FileList> file_list;
168  fake_service_.GetFileListInDirectory(
169      "1_folder_resource_id",
170      test_util::CreateCopyResultCallback(&error, &file_list));
171  base::RunLoop().RunUntilIdle();
172
173  EXPECT_EQ(HTTP_SUCCESS, error);
174  ASSERT_TRUE(file_list);
175  // Do some sanity check. There is three entries in 1_folder_resource_id
176  // directory.
177  EXPECT_EQ(3U, file_list->items().size());
178  EXPECT_EQ(1, fake_service_.directory_load_count());
179}
180
181TEST_F(FakeDriveServiceTest, GetFileListInDirectory_Offline) {
182  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
183  fake_service_.set_offline(true);
184
185  GDataErrorCode error = GDATA_OTHER_ERROR;
186  scoped_ptr<FileList> file_list;
187  fake_service_.GetFileListInDirectory(
188      fake_service_.GetRootResourceId(),
189      test_util::CreateCopyResultCallback(&error, &file_list));
190  base::RunLoop().RunUntilIdle();
191
192  EXPECT_EQ(GDATA_NO_CONNECTION, error);
193  EXPECT_FALSE(file_list);
194}
195
196TEST_F(FakeDriveServiceTest, Search) {
197  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
198
199  GDataErrorCode error = GDATA_OTHER_ERROR;
200  scoped_ptr<FileList> file_list;
201  fake_service_.Search(
202      "File",  // search_query
203      test_util::CreateCopyResultCallback(&error, &file_list));
204  base::RunLoop().RunUntilIdle();
205
206  EXPECT_EQ(HTTP_SUCCESS, error);
207  ASSERT_TRUE(file_list);
208  // Do some sanity check. There are 4 entries that contain "File" in their
209  // titles.
210  EXPECT_EQ(4U, file_list->items().size());
211}
212
213TEST_F(FakeDriveServiceTest, Search_WithAttribute) {
214  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
215
216  GDataErrorCode error = GDATA_OTHER_ERROR;
217  scoped_ptr<FileList> file_list;
218  fake_service_.Search(
219      "title:1.txt",  // search_query
220      test_util::CreateCopyResultCallback(&error, &file_list));
221  base::RunLoop().RunUntilIdle();
222
223  EXPECT_EQ(HTTP_SUCCESS, error);
224  ASSERT_TRUE(file_list);
225  // Do some sanity check. There are 4 entries that contain "1.txt" in their
226  // titles.
227  EXPECT_EQ(4U, file_list->items().size());
228}
229
230TEST_F(FakeDriveServiceTest, Search_MultipleQueries) {
231  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
232
233  GDataErrorCode error = GDATA_OTHER_ERROR;
234  scoped_ptr<FileList> file_list;
235  fake_service_.Search(
236      "Directory 1",  // search_query
237      test_util::CreateCopyResultCallback(&error, &file_list));
238  base::RunLoop().RunUntilIdle();
239
240  EXPECT_EQ(HTTP_SUCCESS, error);
241  ASSERT_TRUE(file_list);
242  // There are 2 entries that contain both "Directory" and "1" in their titles.
243  EXPECT_EQ(2U, file_list->items().size());
244
245  fake_service_.Search(
246      "\"Directory 1\"",  // search_query
247      test_util::CreateCopyResultCallback(&error, &file_list));
248  base::RunLoop().RunUntilIdle();
249
250  EXPECT_EQ(HTTP_SUCCESS, error);
251  ASSERT_TRUE(file_list);
252  // There is 1 entry that contain "Directory 1" in its title.
253  EXPECT_EQ(1U, file_list->items().size());
254}
255
256TEST_F(FakeDriveServiceTest, Search_Offline) {
257  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
258  fake_service_.set_offline(true);
259
260  GDataErrorCode error = GDATA_OTHER_ERROR;
261  scoped_ptr<FileList> file_list;
262  fake_service_.Search(
263      "Directory 1",  // search_query
264      test_util::CreateCopyResultCallback(&error, &file_list));
265  base::RunLoop().RunUntilIdle();
266
267  EXPECT_EQ(GDATA_NO_CONNECTION, error);
268  EXPECT_FALSE(file_list);
269}
270
271TEST_F(FakeDriveServiceTest, Search_Deleted) {
272  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
273
274  GDataErrorCode error = GDATA_OTHER_ERROR;
275  fake_service_.DeleteResource("2_file_resource_id",
276                               std::string(),  // etag
277                               test_util::CreateCopyResultCallback(&error));
278  base::RunLoop().RunUntilIdle();
279  EXPECT_EQ(HTTP_NO_CONTENT, error);
280
281  error = GDATA_OTHER_ERROR;
282  scoped_ptr<FileList> file_list;
283  fake_service_.Search(
284      "File",  // search_query
285      test_util::CreateCopyResultCallback(&error, &file_list));
286  base::RunLoop().RunUntilIdle();
287
288  EXPECT_EQ(HTTP_SUCCESS, error);
289  ASSERT_TRUE(file_list);
290  // Do some sanity check. There are 4 entries that contain "File" in their
291  // titles and one of them is deleted.
292  EXPECT_EQ(3U, file_list->items().size());
293}
294
295TEST_F(FakeDriveServiceTest, Search_Trashed) {
296  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
297
298  GDataErrorCode error = GDATA_OTHER_ERROR;
299  fake_service_.TrashResource("2_file_resource_id",
300                              test_util::CreateCopyResultCallback(&error));
301  base::RunLoop().RunUntilIdle();
302  EXPECT_EQ(HTTP_SUCCESS, error);
303
304  error = GDATA_OTHER_ERROR;
305  scoped_ptr<FileList> file_list;
306  fake_service_.Search(
307      "File",  // search_query
308      test_util::CreateCopyResultCallback(&error, &file_list));
309  base::RunLoop().RunUntilIdle();
310
311  EXPECT_EQ(HTTP_SUCCESS, error);
312  ASSERT_TRUE(file_list);
313  // Do some sanity check. There are 4 entries that contain "File" in their
314  // titles and one of them is deleted.
315  EXPECT_EQ(3U, file_list->items().size());
316}
317
318TEST_F(FakeDriveServiceTest, SearchByTitle) {
319  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
320
321  GDataErrorCode error = GDATA_OTHER_ERROR;
322  scoped_ptr<FileList> file_list;
323  fake_service_.SearchByTitle(
324      "1.txt",  // title
325      fake_service_.GetRootResourceId(),  // directory_resource_id
326      test_util::CreateCopyResultCallback(&error, &file_list));
327  base::RunLoop().RunUntilIdle();
328
329  EXPECT_EQ(HTTP_SUCCESS, error);
330  ASSERT_TRUE(file_list);
331  // Do some sanity check. There are 2 entries that contain "1.txt" in their
332  // titles directly under the root directory.
333  EXPECT_EQ(2U, file_list->items().size());
334}
335
336TEST_F(FakeDriveServiceTest, SearchByTitle_EmptyDirectoryResourceId) {
337  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
338
339  GDataErrorCode error = GDATA_OTHER_ERROR;
340  scoped_ptr<FileList> file_list;
341  fake_service_.SearchByTitle(
342      "1.txt",  // title
343      "",  // directory resource id
344      test_util::CreateCopyResultCallback(&error, &file_list));
345  base::RunLoop().RunUntilIdle();
346
347  EXPECT_EQ(HTTP_SUCCESS, error);
348  ASSERT_TRUE(file_list);
349  // Do some sanity check. There are 4 entries that contain "1.txt" in their
350  // titles.
351  EXPECT_EQ(4U, file_list->items().size());
352}
353
354TEST_F(FakeDriveServiceTest, SearchByTitle_Offline) {
355  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
356  fake_service_.set_offline(true);
357
358  GDataErrorCode error = GDATA_OTHER_ERROR;
359  scoped_ptr<FileList> file_list;
360  fake_service_.SearchByTitle(
361      "Directory 1",  // title
362      fake_service_.GetRootResourceId(),  // directory_resource_id
363      test_util::CreateCopyResultCallback(&error, &file_list));
364  base::RunLoop().RunUntilIdle();
365
366  EXPECT_EQ(GDATA_NO_CONNECTION, error);
367  EXPECT_FALSE(file_list);
368}
369
370TEST_F(FakeDriveServiceTest, GetChangeList_NoNewEntries) {
371  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
372
373  GDataErrorCode error = GDATA_OTHER_ERROR;
374  scoped_ptr<ChangeList> change_list;
375  fake_service_.GetChangeList(
376      fake_service_.about_resource().largest_change_id() + 1,
377      test_util::CreateCopyResultCallback(&error, &change_list));
378  base::RunLoop().RunUntilIdle();
379
380  EXPECT_EQ(HTTP_SUCCESS, error);
381  ASSERT_TRUE(change_list);
382  EXPECT_EQ(fake_service_.about_resource().largest_change_id(),
383            change_list->largest_change_id());
384  // This should be empty as the latest changestamp was passed to
385  // GetChangeList(), hence there should be no new entries.
386  EXPECT_EQ(0U, change_list->items().size());
387  // It's considered loaded even if the result is empty.
388  EXPECT_EQ(1, fake_service_.change_list_load_count());
389}
390
391TEST_F(FakeDriveServiceTest, GetChangeList_WithNewEntry) {
392  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
393  const int64 old_largest_change_id =
394      fake_service_.about_resource().largest_change_id();
395
396  // Add a new directory in the root directory.
397  ASSERT_TRUE(AddNewDirectory(
398      fake_service_.GetRootResourceId(), "new directory"));
399
400  // Get the resource list newer than old_largest_change_id.
401  GDataErrorCode error = GDATA_OTHER_ERROR;
402  scoped_ptr<ChangeList> change_list;
403  fake_service_.GetChangeList(
404      old_largest_change_id + 1,
405      test_util::CreateCopyResultCallback(&error, &change_list));
406  base::RunLoop().RunUntilIdle();
407
408  EXPECT_EQ(HTTP_SUCCESS, error);
409  ASSERT_TRUE(change_list);
410  EXPECT_EQ(fake_service_.about_resource().largest_change_id(),
411            change_list->largest_change_id());
412  // The result should only contain the newly created directory.
413  ASSERT_EQ(1U, change_list->items().size());
414  ASSERT_TRUE(change_list->items()[0]->file());
415  EXPECT_EQ("new directory", change_list->items()[0]->file()->title());
416  EXPECT_EQ(1, fake_service_.change_list_load_count());
417}
418
419TEST_F(FakeDriveServiceTest, GetChangeList_Offline) {
420  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
421  fake_service_.set_offline(true);
422
423  GDataErrorCode error = GDATA_OTHER_ERROR;
424  scoped_ptr<ChangeList> change_list;
425  fake_service_.GetChangeList(
426      654321,  // start_changestamp
427      test_util::CreateCopyResultCallback(&error, &change_list));
428  base::RunLoop().RunUntilIdle();
429
430  EXPECT_EQ(GDATA_NO_CONNECTION, error);
431  EXPECT_FALSE(change_list);
432}
433
434TEST_F(FakeDriveServiceTest, GetChangeList_DeletedEntry) {
435  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
436  ASSERT_TRUE(Exists("2_file_resource_id"));
437  const int64 old_largest_change_id =
438      fake_service_.about_resource().largest_change_id();
439
440  GDataErrorCode error = GDATA_OTHER_ERROR;
441  fake_service_.DeleteResource("2_file_resource_id",
442                               std::string(),  // etag
443                               test_util::CreateCopyResultCallback(&error));
444  base::RunLoop().RunUntilIdle();
445  ASSERT_EQ(HTTP_NO_CONTENT, error);
446  ASSERT_FALSE(Exists("2_file_resource_id"));
447
448  // Get the resource list newer than old_largest_change_id.
449  error = GDATA_OTHER_ERROR;
450  scoped_ptr<ChangeList> change_list;
451  fake_service_.GetChangeList(
452      old_largest_change_id + 1,
453      test_util::CreateCopyResultCallback(&error, &change_list));
454  base::RunLoop().RunUntilIdle();
455
456  EXPECT_EQ(HTTP_SUCCESS, error);
457  ASSERT_TRUE(change_list);
458  EXPECT_EQ(fake_service_.about_resource().largest_change_id(),
459            change_list->largest_change_id());
460  // The result should only contain the deleted file.
461  ASSERT_EQ(1U, change_list->items().size());
462  const ChangeResource& item = *change_list->items()[0];
463  EXPECT_EQ("2_file_resource_id", item.file_id());
464  EXPECT_FALSE(item.file());
465  EXPECT_TRUE(item.is_deleted());
466  EXPECT_EQ(1, fake_service_.change_list_load_count());
467}
468
469TEST_F(FakeDriveServiceTest, GetChangeList_TrashedEntry) {
470  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
471  ASSERT_TRUE(Exists("2_file_resource_id"));
472  const int64 old_largest_change_id =
473      fake_service_.about_resource().largest_change_id();
474
475  GDataErrorCode error = GDATA_OTHER_ERROR;
476  fake_service_.TrashResource("2_file_resource_id",
477                              test_util::CreateCopyResultCallback(&error));
478  base::RunLoop().RunUntilIdle();
479  ASSERT_EQ(HTTP_SUCCESS, error);
480  ASSERT_FALSE(Exists("2_file_resource_id"));
481
482  // Get the resource list newer than old_largest_change_id.
483  error = GDATA_OTHER_ERROR;
484  scoped_ptr<ChangeList> change_list;
485  fake_service_.GetChangeList(
486      old_largest_change_id + 1,
487      test_util::CreateCopyResultCallback(&error, &change_list));
488  base::RunLoop().RunUntilIdle();
489
490  EXPECT_EQ(HTTP_SUCCESS, error);
491  ASSERT_TRUE(change_list);
492  EXPECT_EQ(fake_service_.about_resource().largest_change_id(),
493            change_list->largest_change_id());
494  // The result should only contain the trashed file.
495  ASSERT_EQ(1U, change_list->items().size());
496  const ChangeResource& item = *change_list->items()[0];
497  EXPECT_EQ("2_file_resource_id", item.file_id());
498  ASSERT_TRUE(item.file());
499  EXPECT_TRUE(item.file()->labels().is_trashed());
500  EXPECT_EQ(1, fake_service_.change_list_load_count());
501}
502
503TEST_F(FakeDriveServiceTest, GetRemainingFileList_GetAllFileList) {
504  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
505  fake_service_.set_default_max_results(6);
506
507  GDataErrorCode error = GDATA_OTHER_ERROR;
508  scoped_ptr<FileList> file_list;
509  fake_service_.GetAllFileList(
510      test_util::CreateCopyResultCallback(&error, &file_list));
511  base::RunLoop().RunUntilIdle();
512  EXPECT_EQ(HTTP_SUCCESS, error);
513  ASSERT_TRUE(file_list);
514
515  // Do some sanity check.
516  // The number of results is 14 entries. Thus, it should split into three
517  // chunks: 6, 6, and then 2.
518  EXPECT_EQ(6U, file_list->items().size());
519  EXPECT_EQ(1, fake_service_.file_list_load_count());
520
521  // Second page loading.
522  // Keep the next url before releasing the |file_list|.
523  GURL next_url(file_list->next_link());
524
525  error = GDATA_OTHER_ERROR;
526  file_list.reset();
527  fake_service_.GetRemainingFileList(
528      next_url,
529      test_util::CreateCopyResultCallback(&error, &file_list));
530  base::RunLoop().RunUntilIdle();
531
532  EXPECT_EQ(HTTP_SUCCESS, error);
533  ASSERT_TRUE(file_list);
534
535  EXPECT_EQ(6U, file_list->items().size());
536  EXPECT_EQ(1, fake_service_.file_list_load_count());
537
538  // Third page loading.
539  next_url = file_list->next_link();
540
541  error = GDATA_OTHER_ERROR;
542  file_list.reset();
543  fake_service_.GetRemainingFileList(
544      next_url,
545      test_util::CreateCopyResultCallback(&error, &file_list));
546  base::RunLoop().RunUntilIdle();
547
548  EXPECT_EQ(HTTP_SUCCESS, error);
549  ASSERT_TRUE(file_list);
550
551  EXPECT_EQ(3U, file_list->items().size());
552  EXPECT_EQ(1, fake_service_.file_list_load_count());
553}
554
555TEST_F(FakeDriveServiceTest, GetRemainingFileList_GetFileListInDirectory) {
556  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
557  fake_service_.set_default_max_results(3);
558
559  GDataErrorCode error = GDATA_OTHER_ERROR;
560  scoped_ptr<FileList> file_list;
561  fake_service_.GetFileListInDirectory(
562      fake_service_.GetRootResourceId(),
563      test_util::CreateCopyResultCallback(&error, &file_list));
564  base::RunLoop().RunUntilIdle();
565  EXPECT_EQ(HTTP_SUCCESS, error);
566  ASSERT_TRUE(file_list);
567
568  // Do some sanity check.
569  // The number of results is 8 entries. Thus, it should split into three
570  // chunks: 3, 3, and then 2.
571  EXPECT_EQ(3U, file_list->items().size());
572  EXPECT_EQ(1, fake_service_.directory_load_count());
573
574  // Second page loading.
575  // Keep the next url before releasing the |file_list|.
576  GURL next_url = file_list->next_link();
577
578  error = GDATA_OTHER_ERROR;
579  file_list.reset();
580  fake_service_.GetRemainingFileList(
581      next_url,
582      test_util::CreateCopyResultCallback(&error, &file_list));
583  base::RunLoop().RunUntilIdle();
584
585  EXPECT_EQ(HTTP_SUCCESS, error);
586  ASSERT_TRUE(file_list);
587
588  EXPECT_EQ(3U, file_list->items().size());
589  EXPECT_EQ(1, fake_service_.directory_load_count());
590
591  // Third page loading.
592  next_url = file_list->next_link();
593
594  error = GDATA_OTHER_ERROR;
595  file_list.reset();
596  fake_service_.GetRemainingFileList(
597      next_url,
598      test_util::CreateCopyResultCallback(&error, &file_list));
599  base::RunLoop().RunUntilIdle();
600
601  EXPECT_EQ(HTTP_SUCCESS, error);
602  ASSERT_TRUE(file_list);
603
604  EXPECT_EQ(2U, file_list->items().size());
605  EXPECT_EQ(1, fake_service_.directory_load_count());
606}
607
608TEST_F(FakeDriveServiceTest, GetRemainingFileList_Search) {
609  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
610  fake_service_.set_default_max_results(2);
611
612  GDataErrorCode error = GDATA_OTHER_ERROR;
613  scoped_ptr<FileList> file_list;
614  fake_service_.Search(
615      "File",  // search_query
616      test_util::CreateCopyResultCallback(&error, &file_list));
617  base::RunLoop().RunUntilIdle();
618  EXPECT_EQ(HTTP_SUCCESS, error);
619  ASSERT_TRUE(file_list);
620
621  // Do some sanity check.
622  // The number of results is 4 entries. Thus, it should split into two
623  // chunks: 2, and then 2
624  EXPECT_EQ(2U, file_list->items().size());
625
626  // Second page loading.
627  // Keep the next url before releasing the |file_list|.
628  GURL next_url = file_list->next_link();
629
630  error = GDATA_OTHER_ERROR;
631  file_list.reset();
632  fake_service_.GetRemainingFileList(
633      next_url,
634      test_util::CreateCopyResultCallback(&error, &file_list));
635  base::RunLoop().RunUntilIdle();
636
637  EXPECT_EQ(HTTP_SUCCESS, error);
638  ASSERT_TRUE(file_list);
639
640  EXPECT_EQ(2U, file_list->items().size());
641}
642
643TEST_F(FakeDriveServiceTest, GetRemainingChangeList_GetChangeList) {
644  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
645  fake_service_.set_default_max_results(2);
646  const int64 old_largest_change_id =
647      fake_service_.about_resource().largest_change_id();
648
649  // Add 5 new directory in the root directory.
650  for (int i = 0; i < 5; ++i) {
651    ASSERT_TRUE(AddNewDirectory(
652        fake_service_.GetRootResourceId(),
653        base::StringPrintf("new directory %d", i)));
654  }
655
656  GDataErrorCode error = GDATA_OTHER_ERROR;
657  scoped_ptr<ChangeList> change_list;
658  fake_service_.GetChangeList(
659      old_largest_change_id + 1,  // start_changestamp
660      test_util::CreateCopyResultCallback(&error, &change_list));
661  base::RunLoop().RunUntilIdle();
662  EXPECT_EQ(HTTP_SUCCESS, error);
663  ASSERT_TRUE(change_list);
664
665  // Do some sanity check.
666  // The number of results is 5 entries. Thus, it should split into three
667  // chunks: 2, 2 and then 1.
668  EXPECT_EQ(2U, change_list->items().size());
669  EXPECT_EQ(1, fake_service_.change_list_load_count());
670
671  // Second page loading.
672  // Keep the next url before releasing the |change_list|.
673  GURL next_url = change_list->next_link();
674
675  error = GDATA_OTHER_ERROR;
676  change_list.reset();
677  fake_service_.GetRemainingChangeList(
678      next_url,
679      test_util::CreateCopyResultCallback(&error, &change_list));
680  base::RunLoop().RunUntilIdle();
681
682  EXPECT_EQ(HTTP_SUCCESS, error);
683  ASSERT_TRUE(change_list);
684
685  EXPECT_EQ(2U, change_list->items().size());
686  EXPECT_EQ(1, fake_service_.change_list_load_count());
687
688  // Third page loading.
689  next_url = change_list->next_link();
690
691  error = GDATA_OTHER_ERROR;
692  change_list.reset();
693  fake_service_.GetRemainingChangeList(
694      next_url,
695      test_util::CreateCopyResultCallback(&error, &change_list));
696  base::RunLoop().RunUntilIdle();
697
698  EXPECT_EQ(HTTP_SUCCESS, error);
699  ASSERT_TRUE(change_list);
700
701  EXPECT_EQ(1U, change_list->items().size());
702  EXPECT_EQ(1, fake_service_.change_list_load_count());
703}
704
705TEST_F(FakeDriveServiceTest, GetAboutResource) {
706  GDataErrorCode error = GDATA_OTHER_ERROR;
707  scoped_ptr<AboutResource> about_resource;
708  fake_service_.GetAboutResource(
709      test_util::CreateCopyResultCallback(&error, &about_resource));
710  base::RunLoop().RunUntilIdle();
711
712  EXPECT_EQ(HTTP_SUCCESS, error);
713
714  ASSERT_TRUE(about_resource);
715  // Do some sanity check.
716  EXPECT_EQ(fake_service_.GetRootResourceId(),
717            about_resource->root_folder_id());
718  EXPECT_EQ(1, fake_service_.about_resource_load_count());
719}
720
721TEST_F(FakeDriveServiceTest, GetAboutResource_Offline) {
722  fake_service_.set_offline(true);
723
724  GDataErrorCode error = GDATA_OTHER_ERROR;
725  scoped_ptr<AboutResource> about_resource;
726  fake_service_.GetAboutResource(
727      test_util::CreateCopyResultCallback(&error, &about_resource));
728  base::RunLoop().RunUntilIdle();
729
730  EXPECT_EQ(GDATA_NO_CONNECTION, error);
731  EXPECT_FALSE(about_resource);
732}
733
734TEST_F(FakeDriveServiceTest, GetAppList) {
735  ASSERT_TRUE(fake_service_.LoadAppListForDriveApi(
736      "drive/applist.json"));
737
738  GDataErrorCode error = GDATA_OTHER_ERROR;
739  scoped_ptr<AppList> app_list;
740  fake_service_.GetAppList(
741      test_util::CreateCopyResultCallback(&error, &app_list));
742  base::RunLoop().RunUntilIdle();
743
744  EXPECT_EQ(HTTP_SUCCESS, error);
745
746  ASSERT_TRUE(app_list);
747  EXPECT_EQ(1, fake_service_.app_list_load_count());
748}
749
750TEST_F(FakeDriveServiceTest, GetAppList_Offline) {
751  ASSERT_TRUE(fake_service_.LoadAppListForDriveApi(
752      "drive/applist.json"));
753  fake_service_.set_offline(true);
754
755  GDataErrorCode error = GDATA_OTHER_ERROR;
756  scoped_ptr<AppList> app_list;
757  fake_service_.GetAppList(
758      test_util::CreateCopyResultCallback(&error, &app_list));
759  base::RunLoop().RunUntilIdle();
760
761  EXPECT_EQ(GDATA_NO_CONNECTION, error);
762  EXPECT_FALSE(app_list);
763}
764
765TEST_F(FakeDriveServiceTest, GetFileResource_ExistingFile) {
766  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
767
768  const std::string kResourceId = "2_file_resource_id";
769  GDataErrorCode error = GDATA_OTHER_ERROR;
770  scoped_ptr<FileResource> entry;
771  fake_service_.GetFileResource(
772      kResourceId, test_util::CreateCopyResultCallback(&error, &entry));
773  base::RunLoop().RunUntilIdle();
774
775  EXPECT_EQ(HTTP_SUCCESS, error);
776  ASSERT_TRUE(entry);
777  // Do some sanity check.
778  EXPECT_EQ(kResourceId, entry->file_id());
779}
780
781TEST_F(FakeDriveServiceTest, GetFileResource_NonexistingFile) {
782  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
783
784  const std::string kResourceId = "nonexisting_resource_id";
785  GDataErrorCode error = GDATA_OTHER_ERROR;
786  scoped_ptr<FileResource> entry;
787  fake_service_.GetFileResource(
788      kResourceId, test_util::CreateCopyResultCallback(&error, &entry));
789  base::RunLoop().RunUntilIdle();
790
791  EXPECT_EQ(HTTP_NOT_FOUND, error);
792  ASSERT_FALSE(entry);
793}
794
795TEST_F(FakeDriveServiceTest, GetFileResource_Offline) {
796  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
797  fake_service_.set_offline(true);
798
799  const std::string kResourceId = "2_file_resource_id";
800  GDataErrorCode error = GDATA_OTHER_ERROR;
801  scoped_ptr<FileResource> entry;
802  fake_service_.GetFileResource(
803      kResourceId, test_util::CreateCopyResultCallback(&error, &entry));
804  base::RunLoop().RunUntilIdle();
805
806  EXPECT_EQ(GDATA_NO_CONNECTION, error);
807  EXPECT_FALSE(entry);
808}
809
810TEST_F(FakeDriveServiceTest, GetShareUrl) {
811  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
812
813  const std::string kResourceId = "2_file_resource_id";
814  GDataErrorCode error = GDATA_OTHER_ERROR;
815  GURL share_url;
816  fake_service_.GetShareUrl(
817      kResourceId,
818      GURL(),  // embed origin
819      test_util::CreateCopyResultCallback(&error, &share_url));
820  base::RunLoop().RunUntilIdle();
821
822  EXPECT_EQ(HTTP_SUCCESS, error);
823  EXPECT_FALSE(share_url.is_empty());
824}
825
826TEST_F(FakeDriveServiceTest, DeleteResource_ExistingFile) {
827  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
828
829  // Resource "2_file_resource_id" should now exist.
830  ASSERT_TRUE(Exists("2_file_resource_id"));
831
832  GDataErrorCode error = GDATA_OTHER_ERROR;
833  fake_service_.DeleteResource("2_file_resource_id",
834                               std::string(),  // etag
835                               test_util::CreateCopyResultCallback(&error));
836  base::RunLoop().RunUntilIdle();
837
838  EXPECT_EQ(HTTP_NO_CONTENT, error);
839  // Resource "2_file_resource_id" should be gone now.
840  EXPECT_FALSE(Exists("2_file_resource_id"));
841
842  error = GDATA_OTHER_ERROR;
843  fake_service_.DeleteResource("2_file_resource_id",
844                               std::string(),  // etag
845                               test_util::CreateCopyResultCallback(&error));
846  base::RunLoop().RunUntilIdle();
847  EXPECT_EQ(HTTP_NOT_FOUND, error);
848  EXPECT_FALSE(Exists("2_file_resource_id"));
849}
850
851TEST_F(FakeDriveServiceTest, DeleteResource_NonexistingFile) {
852  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
853
854  GDataErrorCode error = GDATA_OTHER_ERROR;
855  fake_service_.DeleteResource("nonexisting_resource_id",
856                               std::string(),  // etag
857                               test_util::CreateCopyResultCallback(&error));
858  base::RunLoop().RunUntilIdle();
859
860  EXPECT_EQ(HTTP_NOT_FOUND, error);
861}
862
863TEST_F(FakeDriveServiceTest, DeleteResource_ETagMatch) {
864  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
865
866  // Resource "2_file_resource_id" should now exist.
867  scoped_ptr<FileResource> entry = FindEntry("2_file_resource_id");
868  ASSERT_TRUE(entry);
869  ASSERT_FALSE(entry->labels().is_trashed());
870  ASSERT_FALSE(entry->etag().empty());
871
872  GDataErrorCode error = GDATA_OTHER_ERROR;
873  fake_service_.DeleteResource("2_file_resource_id",
874                               entry->etag() + "_mismatch",
875                               test_util::CreateCopyResultCallback(&error));
876  base::RunLoop().RunUntilIdle();
877
878  EXPECT_EQ(HTTP_PRECONDITION, error);
879  // Resource "2_file_resource_id" should still exist.
880  EXPECT_TRUE(Exists("2_file_resource_id"));
881
882  error = GDATA_OTHER_ERROR;
883  fake_service_.DeleteResource("2_file_resource_id",
884                               entry->etag(),
885                               test_util::CreateCopyResultCallback(&error));
886  base::RunLoop().RunUntilIdle();
887  EXPECT_EQ(HTTP_NO_CONTENT, error);
888  // Resource "2_file_resource_id" should be gone now.
889  EXPECT_FALSE(Exists("2_file_resource_id"));
890}
891
892TEST_F(FakeDriveServiceTest, DeleteResource_Offline) {
893  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
894  fake_service_.set_offline(true);
895
896  GDataErrorCode error = GDATA_OTHER_ERROR;
897  fake_service_.DeleteResource("2_file_resource_id",
898                               std::string(),  // etag
899                               test_util::CreateCopyResultCallback(&error));
900  base::RunLoop().RunUntilIdle();
901
902  EXPECT_EQ(GDATA_NO_CONNECTION, error);
903}
904
905TEST_F(FakeDriveServiceTest, DeleteResource_Forbidden) {
906  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
907
908  EXPECT_EQ(HTTP_SUCCESS, fake_service_.SetUserPermission(
909      "2_file_resource_id", google_apis::drive::PERMISSION_ROLE_READER));
910
911  GDataErrorCode error = GDATA_OTHER_ERROR;
912  fake_service_.DeleteResource("2_file_resource_id",
913                               std::string(),  // etag
914                               test_util::CreateCopyResultCallback(&error));
915  base::RunLoop().RunUntilIdle();
916
917  EXPECT_EQ(HTTP_FORBIDDEN, error);
918}
919
920TEST_F(FakeDriveServiceTest, TrashResource_ExistingFile) {
921  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
922
923  // Resource "2_file_resource_id" should now exist.
924  ASSERT_TRUE(Exists("2_file_resource_id"));
925
926  GDataErrorCode error = GDATA_OTHER_ERROR;
927  fake_service_.TrashResource("2_file_resource_id",
928                              test_util::CreateCopyResultCallback(&error));
929  base::RunLoop().RunUntilIdle();
930
931  EXPECT_EQ(HTTP_SUCCESS, error);
932  // Resource "2_file_resource_id" should be gone now.
933  EXPECT_FALSE(Exists("2_file_resource_id"));
934
935  error = GDATA_OTHER_ERROR;
936  fake_service_.TrashResource("2_file_resource_id",
937                              test_util::CreateCopyResultCallback(&error));
938  base::RunLoop().RunUntilIdle();
939  EXPECT_EQ(HTTP_NOT_FOUND, error);
940  EXPECT_FALSE(Exists("2_file_resource_id"));
941}
942
943TEST_F(FakeDriveServiceTest, TrashResource_NonexistingFile) {
944  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
945
946  GDataErrorCode error = GDATA_OTHER_ERROR;
947  fake_service_.TrashResource("nonexisting_resource_id",
948                              test_util::CreateCopyResultCallback(&error));
949  base::RunLoop().RunUntilIdle();
950
951  EXPECT_EQ(HTTP_NOT_FOUND, error);
952}
953
954TEST_F(FakeDriveServiceTest, TrashResource_Offline) {
955  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
956  fake_service_.set_offline(true);
957
958  GDataErrorCode error = GDATA_OTHER_ERROR;
959  fake_service_.TrashResource("2_file_resource_id",
960                              test_util::CreateCopyResultCallback(&error));
961  base::RunLoop().RunUntilIdle();
962
963  EXPECT_EQ(GDATA_NO_CONNECTION, error);
964}
965
966TEST_F(FakeDriveServiceTest, TrashResource_Forbidden) {
967  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
968
969  EXPECT_EQ(HTTP_SUCCESS, fake_service_.SetUserPermission(
970      "2_file_resource_id", google_apis::drive::PERMISSION_ROLE_READER));
971
972  GDataErrorCode error = GDATA_OTHER_ERROR;
973  fake_service_.TrashResource("2_file_resource_id",
974                              test_util::CreateCopyResultCallback(&error));
975  base::RunLoop().RunUntilIdle();
976
977  EXPECT_EQ(HTTP_FORBIDDEN, error);
978}
979
980TEST_F(FakeDriveServiceTest, DownloadFile_ExistingFile) {
981  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
982
983  base::ScopedTempDir temp_dir;
984  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
985
986  std::vector<test_util::ProgressInfo> download_progress_values;
987
988  const base::FilePath kOutputFilePath =
989      temp_dir.path().AppendASCII("whatever.txt");
990  GDataErrorCode error = GDATA_OTHER_ERROR;
991  base::FilePath output_file_path;
992  test_util::TestGetContentCallback get_content_callback;
993  fake_service_.DownloadFile(
994      kOutputFilePath,
995      "2_file_resource_id",
996      test_util::CreateCopyResultCallback(&error, &output_file_path),
997      get_content_callback.callback(),
998      base::Bind(&test_util::AppendProgressCallbackResult,
999                 &download_progress_values));
1000  base::RunLoop().RunUntilIdle();
1001
1002  EXPECT_EQ(HTTP_SUCCESS, error);
1003  EXPECT_EQ(output_file_path, kOutputFilePath);
1004  std::string content;
1005  ASSERT_TRUE(base::ReadFileToString(output_file_path, &content));
1006  EXPECT_EQ("This is some test content.", content);
1007  ASSERT_TRUE(!download_progress_values.empty());
1008  EXPECT_TRUE(base::STLIsSorted(download_progress_values));
1009  EXPECT_LE(0, download_progress_values.front().first);
1010  EXPECT_GE(26, download_progress_values.back().first);
1011  EXPECT_EQ(content, get_content_callback.GetConcatenatedData());
1012}
1013
1014TEST_F(FakeDriveServiceTest, DownloadFile_NonexistingFile) {
1015  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1016
1017  base::ScopedTempDir temp_dir;
1018  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1019
1020  const base::FilePath kOutputFilePath =
1021      temp_dir.path().AppendASCII("whatever.txt");
1022  GDataErrorCode error = GDATA_OTHER_ERROR;
1023  base::FilePath output_file_path;
1024  fake_service_.DownloadFile(
1025      kOutputFilePath,
1026      "non_existent_file_resource_id",
1027      test_util::CreateCopyResultCallback(&error, &output_file_path),
1028      GetContentCallback(),
1029      ProgressCallback());
1030  base::RunLoop().RunUntilIdle();
1031
1032  EXPECT_EQ(HTTP_NOT_FOUND, error);
1033}
1034
1035TEST_F(FakeDriveServiceTest, DownloadFile_Offline) {
1036  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1037  fake_service_.set_offline(true);
1038
1039  base::ScopedTempDir temp_dir;
1040  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1041
1042  const base::FilePath kOutputFilePath =
1043      temp_dir.path().AppendASCII("whatever.txt");
1044  GDataErrorCode error = GDATA_OTHER_ERROR;
1045  base::FilePath output_file_path;
1046  fake_service_.DownloadFile(
1047      kOutputFilePath,
1048      "2_file_resource_id",
1049      test_util::CreateCopyResultCallback(&error, &output_file_path),
1050      GetContentCallback(),
1051      ProgressCallback());
1052  base::RunLoop().RunUntilIdle();
1053
1054  EXPECT_EQ(GDATA_NO_CONNECTION, error);
1055}
1056
1057TEST_F(FakeDriveServiceTest, CopyResource) {
1058  const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123};
1059
1060  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1061
1062  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1063
1064  const std::string kResourceId = "2_file_resource_id";
1065  const std::string kParentResourceId = "2_folder_resource_id";
1066  GDataErrorCode error = GDATA_OTHER_ERROR;
1067  scoped_ptr<FileResource> entry;
1068  fake_service_.CopyResource(
1069      kResourceId,
1070      kParentResourceId,
1071      "new title",
1072      base::Time::FromUTCExploded(kModifiedDate),
1073      test_util::CreateCopyResultCallback(&error, &entry));
1074  base::RunLoop().RunUntilIdle();
1075
1076  EXPECT_EQ(HTTP_SUCCESS, error);
1077  ASSERT_TRUE(entry);
1078  // The copied entry should have the new resource ID and the title.
1079  EXPECT_NE(kResourceId, entry->file_id());
1080  EXPECT_EQ("new title", entry->title());
1081  EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate), entry->modified_date());
1082  EXPECT_TRUE(HasParent(entry->file_id(), kParentResourceId));
1083  // Should be incremented as a new hosted document was created.
1084  EXPECT_EQ(old_largest_change_id + 1,
1085            fake_service_.about_resource().largest_change_id());
1086  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1087}
1088
1089TEST_F(FakeDriveServiceTest, CopyResource_NonExisting) {
1090  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1091
1092  const std::string kResourceId = "nonexisting_resource_id";
1093  GDataErrorCode error = GDATA_OTHER_ERROR;
1094  scoped_ptr<FileResource> entry;
1095  fake_service_.CopyResource(
1096      kResourceId,
1097      "1_folder_resource_id",
1098      "new title",
1099      base::Time(),
1100      test_util::CreateCopyResultCallback(&error, &entry));
1101  base::RunLoop().RunUntilIdle();
1102
1103  EXPECT_EQ(HTTP_NOT_FOUND, error);
1104}
1105
1106TEST_F(FakeDriveServiceTest, CopyResource_EmptyParentResourceId) {
1107  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1108
1109  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1110
1111  const std::string kResourceId = "2_file_resource_id";
1112  GDataErrorCode error = GDATA_OTHER_ERROR;
1113  scoped_ptr<FileResource> entry;
1114  fake_service_.CopyResource(
1115      kResourceId,
1116      std::string(),
1117      "new title",
1118      base::Time(),
1119      test_util::CreateCopyResultCallback(&error, &entry));
1120  base::RunLoop().RunUntilIdle();
1121
1122  EXPECT_EQ(HTTP_SUCCESS, error);
1123  ASSERT_TRUE(entry);
1124  // The copied entry should have the new resource ID and the title.
1125  EXPECT_NE(kResourceId, entry->file_id());
1126  EXPECT_EQ("new title", entry->title());
1127  EXPECT_TRUE(HasParent(kResourceId, fake_service_.GetRootResourceId()));
1128  // Should be incremented as a new hosted document was created.
1129  EXPECT_EQ(old_largest_change_id + 1,
1130            fake_service_.about_resource().largest_change_id());
1131  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1132}
1133
1134TEST_F(FakeDriveServiceTest, CopyResource_Offline) {
1135  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1136  fake_service_.set_offline(true);
1137
1138  const std::string kResourceId = "2_file_resource_id";
1139  GDataErrorCode error = GDATA_OTHER_ERROR;
1140  scoped_ptr<FileResource> entry;
1141  fake_service_.CopyResource(
1142      kResourceId,
1143      "1_folder_resource_id",
1144      "new title",
1145      base::Time(),
1146      test_util::CreateCopyResultCallback(&error, &entry));
1147  base::RunLoop().RunUntilIdle();
1148
1149  EXPECT_EQ(GDATA_NO_CONNECTION, error);
1150  EXPECT_FALSE(entry);
1151}
1152
1153TEST_F(FakeDriveServiceTest, UpdateResource) {
1154  const base::Time::Exploded kModifiedDate = {2012, 7, 0, 19, 15, 59, 13, 123};
1155  const base::Time::Exploded kViewedDate = {2013, 8, 1, 20, 16, 00, 14, 234};
1156
1157  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1158
1159  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1160
1161  const std::string kResourceId = "2_file_resource_id";
1162  const std::string kParentResourceId = "2_folder_resource_id";
1163  GDataErrorCode error = GDATA_OTHER_ERROR;
1164  scoped_ptr<FileResource> entry;
1165  fake_service_.UpdateResource(
1166      kResourceId,
1167      kParentResourceId,
1168      "new title",
1169      base::Time::FromUTCExploded(kModifiedDate),
1170      base::Time::FromUTCExploded(kViewedDate),
1171      test_util::CreateCopyResultCallback(&error, &entry));
1172  base::RunLoop().RunUntilIdle();
1173
1174  EXPECT_EQ(HTTP_SUCCESS, error);
1175  ASSERT_TRUE(entry);
1176  // The updated entry should have the new title.
1177  EXPECT_EQ(kResourceId, entry->file_id());
1178  EXPECT_EQ("new title", entry->title());
1179  EXPECT_EQ(base::Time::FromUTCExploded(kModifiedDate),
1180            entry->modified_date());
1181  EXPECT_EQ(base::Time::FromUTCExploded(kViewedDate),
1182            entry->last_viewed_by_me_date());
1183  EXPECT_TRUE(HasParent(kResourceId, kParentResourceId));
1184  // Should be incremented as a new hosted document was created.
1185  EXPECT_EQ(old_largest_change_id + 1,
1186            fake_service_.about_resource().largest_change_id());
1187  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1188}
1189
1190TEST_F(FakeDriveServiceTest, UpdateResource_NonExisting) {
1191  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1192
1193  const std::string kResourceId = "nonexisting_resource_id";
1194  GDataErrorCode error = GDATA_OTHER_ERROR;
1195  scoped_ptr<FileResource> entry;
1196  fake_service_.UpdateResource(
1197      kResourceId,
1198      "1_folder_resource_id",
1199      "new title",
1200      base::Time(),
1201      base::Time(),
1202      test_util::CreateCopyResultCallback(&error, &entry));
1203  base::RunLoop().RunUntilIdle();
1204
1205  EXPECT_EQ(HTTP_NOT_FOUND, error);
1206}
1207
1208TEST_F(FakeDriveServiceTest, UpdateResource_EmptyParentResourceId) {
1209  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1210
1211  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1212
1213  const std::string kResourceId = "2_file_resource_id";
1214
1215  // Just make sure that the resource is under root.
1216  ASSERT_TRUE(HasParent(kResourceId, "fake_root"));
1217
1218  GDataErrorCode error = GDATA_OTHER_ERROR;
1219  scoped_ptr<FileResource> entry;
1220  fake_service_.UpdateResource(
1221      kResourceId,
1222      std::string(),
1223      "new title",
1224      base::Time(),
1225      base::Time(),
1226      test_util::CreateCopyResultCallback(&error, &entry));
1227  base::RunLoop().RunUntilIdle();
1228
1229  EXPECT_EQ(HTTP_SUCCESS, error);
1230  ASSERT_TRUE(entry);
1231  // The updated entry should have the new title.
1232  EXPECT_EQ(kResourceId, entry->file_id());
1233  EXPECT_EQ("new title", entry->title());
1234  EXPECT_TRUE(HasParent(kResourceId, "fake_root"));
1235  // Should be incremented as a new hosted document was created.
1236  EXPECT_EQ(old_largest_change_id + 1,
1237            fake_service_.about_resource().largest_change_id());
1238  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1239}
1240
1241TEST_F(FakeDriveServiceTest, UpdateResource_Offline) {
1242  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1243  fake_service_.set_offline(true);
1244
1245  const std::string kResourceId = "2_file_resource_id";
1246  GDataErrorCode error = GDATA_OTHER_ERROR;
1247  scoped_ptr<FileResource> entry;
1248  fake_service_.UpdateResource(
1249      kResourceId,
1250      std::string(),
1251      "new title",
1252      base::Time(),
1253      base::Time(),
1254      test_util::CreateCopyResultCallback(&error, &entry));
1255  base::RunLoop().RunUntilIdle();
1256
1257  EXPECT_EQ(GDATA_NO_CONNECTION, error);
1258  EXPECT_FALSE(entry);
1259}
1260
1261TEST_F(FakeDriveServiceTest, UpdateResource_Forbidden) {
1262  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1263
1264  const std::string kResourceId = "2_file_resource_id";
1265  EXPECT_EQ(HTTP_SUCCESS, fake_service_.SetUserPermission(
1266      kResourceId, google_apis::drive::PERMISSION_ROLE_READER));
1267
1268  GDataErrorCode error = GDATA_OTHER_ERROR;
1269  scoped_ptr<FileResource> entry;
1270  fake_service_.UpdateResource(
1271      kResourceId,
1272      std::string(),
1273      "new title",
1274      base::Time(),
1275      base::Time(),
1276      test_util::CreateCopyResultCallback(&error, &entry));
1277  base::RunLoop().RunUntilIdle();
1278
1279  EXPECT_EQ(HTTP_FORBIDDEN, error);
1280  EXPECT_FALSE(entry);
1281}
1282
1283TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInRootDirectory) {
1284  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1285
1286  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1287
1288  const std::string kResourceId = "2_file_resource_id";
1289  const std::string kOldParentResourceId = fake_service_.GetRootResourceId();
1290  const std::string kNewParentResourceId = "1_folder_resource_id";
1291
1292  // Here's the original parent link.
1293  EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
1294  EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId));
1295
1296  GDataErrorCode error = GDATA_OTHER_ERROR;
1297  fake_service_.AddResourceToDirectory(
1298      kNewParentResourceId,
1299      kResourceId,
1300      test_util::CreateCopyResultCallback(&error));
1301  base::RunLoop().RunUntilIdle();
1302
1303  EXPECT_EQ(HTTP_SUCCESS, error);
1304
1305  // The parent link should now be changed.
1306  EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
1307  EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId));
1308  // Should be incremented as a file was moved.
1309  EXPECT_EQ(old_largest_change_id + 1,
1310            fake_service_.about_resource().largest_change_id());
1311  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1312}
1313
1314TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInNonRootDirectory) {
1315  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1316
1317  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1318
1319  const std::string kResourceId = "subdirectory_file_1_id";
1320  const std::string kOldParentResourceId = "1_folder_resource_id";
1321  const std::string kNewParentResourceId = "2_folder_resource_id";
1322
1323  // Here's the original parent link.
1324  EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
1325  EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId));
1326
1327  GDataErrorCode error = GDATA_OTHER_ERROR;
1328  fake_service_.AddResourceToDirectory(
1329      kNewParentResourceId,
1330      kResourceId,
1331      test_util::CreateCopyResultCallback(&error));
1332  base::RunLoop().RunUntilIdle();
1333
1334  EXPECT_EQ(HTTP_SUCCESS, error);
1335
1336  // The parent link should now be changed.
1337  EXPECT_TRUE(HasParent(kResourceId, kOldParentResourceId));
1338  EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId));
1339  // Should be incremented as a file was moved.
1340  EXPECT_EQ(old_largest_change_id + 1,
1341            fake_service_.about_resource().largest_change_id());
1342  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1343}
1344
1345TEST_F(FakeDriveServiceTest, AddResourceToDirectory_NonexistingFile) {
1346  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1347
1348  const std::string kResourceId = "nonexisting_file";
1349  const std::string kNewParentResourceId = "1_folder_resource_id";
1350
1351  GDataErrorCode error = GDATA_OTHER_ERROR;
1352  fake_service_.AddResourceToDirectory(
1353      kNewParentResourceId,
1354      kResourceId,
1355      test_util::CreateCopyResultCallback(&error));
1356  base::RunLoop().RunUntilIdle();
1357
1358  EXPECT_EQ(HTTP_NOT_FOUND, error);
1359}
1360
1361TEST_F(FakeDriveServiceTest, AddResourceToDirectory_OrphanFile) {
1362  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1363
1364  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1365
1366  const std::string kResourceId = "1_orphanfile_resource_id";
1367  const std::string kNewParentResourceId = "1_folder_resource_id";
1368
1369  // The file does not belong to any directory, even to the root.
1370  EXPECT_FALSE(HasParent(kResourceId, kNewParentResourceId));
1371  EXPECT_FALSE(HasParent(kResourceId, fake_service_.GetRootResourceId()));
1372
1373  GDataErrorCode error = GDATA_OTHER_ERROR;
1374  fake_service_.AddResourceToDirectory(
1375      kNewParentResourceId,
1376      kResourceId,
1377      test_util::CreateCopyResultCallback(&error));
1378  base::RunLoop().RunUntilIdle();
1379
1380  EXPECT_EQ(HTTP_SUCCESS, error);
1381
1382  // The parent link should now be changed.
1383  EXPECT_TRUE(HasParent(kResourceId, kNewParentResourceId));
1384  EXPECT_FALSE(HasParent(kResourceId, fake_service_.GetRootResourceId()));
1385  // Should be incremented as a file was moved.
1386  EXPECT_EQ(old_largest_change_id + 1,
1387            fake_service_.about_resource().largest_change_id());
1388  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1389}
1390
1391TEST_F(FakeDriveServiceTest, AddResourceToDirectory_Offline) {
1392  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1393  fake_service_.set_offline(true);
1394
1395  const std::string kResourceId = "2_file_resource_id";
1396  const std::string kNewParentResourceId = "1_folder_resource_id";
1397
1398  GDataErrorCode error = GDATA_OTHER_ERROR;
1399  fake_service_.AddResourceToDirectory(
1400      kNewParentResourceId,
1401      kResourceId,
1402      test_util::CreateCopyResultCallback(&error));
1403  base::RunLoop().RunUntilIdle();
1404
1405  EXPECT_EQ(GDATA_NO_CONNECTION, error);
1406}
1407
1408TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_ExistingFile) {
1409  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1410
1411  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1412
1413  const std::string kResourceId = "subdirectory_file_1_id";
1414  const std::string kParentResourceId = "1_folder_resource_id";
1415
1416  scoped_ptr<FileResource> entry = FindEntry(kResourceId);
1417  ASSERT_TRUE(entry);
1418  // The entry should have a parent now.
1419  ASSERT_FALSE(entry->parents().empty());
1420
1421  GDataErrorCode error = GDATA_OTHER_ERROR;
1422  fake_service_.RemoveResourceFromDirectory(
1423      kParentResourceId,
1424      kResourceId,
1425      test_util::CreateCopyResultCallback(&error));
1426  base::RunLoop().RunUntilIdle();
1427
1428  EXPECT_EQ(HTTP_NO_CONTENT, error);
1429
1430  entry = FindEntry(kResourceId);
1431  ASSERT_TRUE(entry);
1432  // The entry should have no parent now.
1433  ASSERT_TRUE(entry->parents().empty());
1434  // Should be incremented as a file was moved to the root directory.
1435  EXPECT_EQ(old_largest_change_id + 1,
1436            fake_service_.about_resource().largest_change_id());
1437  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1438}
1439
1440TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_NonexistingFile) {
1441  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1442
1443  const std::string kResourceId = "nonexisting_file";
1444  const std::string kParentResourceId = "1_folder_resource_id";
1445
1446  GDataErrorCode error = GDATA_OTHER_ERROR;
1447  fake_service_.RemoveResourceFromDirectory(
1448      kParentResourceId,
1449      kResourceId,
1450      test_util::CreateCopyResultCallback(&error));
1451  base::RunLoop().RunUntilIdle();
1452
1453  EXPECT_EQ(HTTP_NOT_FOUND, error);
1454}
1455
1456TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_OrphanFile) {
1457  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1458
1459  const std::string kResourceId = "1_orphanfile_resource_id";
1460  const std::string kParentResourceId = fake_service_.GetRootResourceId();
1461
1462  GDataErrorCode error = GDATA_OTHER_ERROR;
1463  fake_service_.RemoveResourceFromDirectory(
1464      kParentResourceId,
1465      kResourceId,
1466      test_util::CreateCopyResultCallback(&error));
1467  base::RunLoop().RunUntilIdle();
1468
1469  EXPECT_EQ(HTTP_NOT_FOUND, error);
1470}
1471
1472TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_Offline) {
1473  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1474  fake_service_.set_offline(true);
1475
1476  const std::string kResourceId = "subdirectory_file_1_id";
1477  const std::string kParentResourceId = "1_folder_resource_id";
1478
1479  GDataErrorCode error = GDATA_OTHER_ERROR;
1480  fake_service_.RemoveResourceFromDirectory(
1481      kParentResourceId,
1482      kResourceId,
1483      test_util::CreateCopyResultCallback(&error));
1484  base::RunLoop().RunUntilIdle();
1485
1486  EXPECT_EQ(GDATA_NO_CONNECTION, error);
1487}
1488
1489TEST_F(FakeDriveServiceTest, AddNewDirectory_EmptyParent) {
1490  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1491
1492  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1493
1494  GDataErrorCode error = GDATA_OTHER_ERROR;
1495  scoped_ptr<FileResource> entry;
1496  fake_service_.AddNewDirectory(
1497      std::string(),
1498      "new directory",
1499      DriveServiceInterface::AddNewDirectoryOptions(),
1500      test_util::CreateCopyResultCallback(&error, &entry));
1501  base::RunLoop().RunUntilIdle();
1502
1503  EXPECT_EQ(HTTP_CREATED, error);
1504  ASSERT_TRUE(entry);
1505  EXPECT_TRUE(entry->IsDirectory());
1506  EXPECT_EQ("resource_id_1", entry->file_id());
1507  EXPECT_EQ("new directory", entry->title());
1508  EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
1509  // Should be incremented as a new directory was created.
1510  EXPECT_EQ(old_largest_change_id + 1,
1511            fake_service_.about_resource().largest_change_id());
1512  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1513}
1514
1515TEST_F(FakeDriveServiceTest, AddNewDirectory_ToRootDirectory) {
1516  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1517
1518  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1519
1520  GDataErrorCode error = GDATA_OTHER_ERROR;
1521  scoped_ptr<FileResource> entry;
1522  fake_service_.AddNewDirectory(
1523      fake_service_.GetRootResourceId(),
1524      "new directory",
1525      DriveServiceInterface::AddNewDirectoryOptions(),
1526      test_util::CreateCopyResultCallback(&error, &entry));
1527  base::RunLoop().RunUntilIdle();
1528
1529  EXPECT_EQ(HTTP_CREATED, error);
1530  ASSERT_TRUE(entry);
1531  EXPECT_TRUE(entry->IsDirectory());
1532  EXPECT_EQ("resource_id_1", entry->file_id());
1533  EXPECT_EQ("new directory", entry->title());
1534  EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
1535  // Should be incremented as a new directory was created.
1536  EXPECT_EQ(old_largest_change_id + 1,
1537            fake_service_.about_resource().largest_change_id());
1538  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1539}
1540
1541TEST_F(FakeDriveServiceTest, AddNewDirectory_ToRootDirectoryOnEmptyFileSystem) {
1542  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1543
1544  GDataErrorCode error = GDATA_OTHER_ERROR;
1545  scoped_ptr<FileResource> entry;
1546  fake_service_.AddNewDirectory(
1547      fake_service_.GetRootResourceId(),
1548      "new directory",
1549      DriveServiceInterface::AddNewDirectoryOptions(),
1550      test_util::CreateCopyResultCallback(&error, &entry));
1551  base::RunLoop().RunUntilIdle();
1552
1553  EXPECT_EQ(HTTP_CREATED, error);
1554  ASSERT_TRUE(entry);
1555  EXPECT_TRUE(entry->IsDirectory());
1556  EXPECT_EQ("resource_id_1", entry->file_id());
1557  EXPECT_EQ("new directory", entry->title());
1558  EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
1559  // Should be incremented as a new directory was created.
1560  EXPECT_EQ(old_largest_change_id + 1,
1561            fake_service_.about_resource().largest_change_id());
1562  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1563}
1564
1565TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonRootDirectory) {
1566  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1567
1568  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1569
1570  const std::string kParentResourceId = "1_folder_resource_id";
1571
1572  GDataErrorCode error = GDATA_OTHER_ERROR;
1573  scoped_ptr<FileResource> entry;
1574  fake_service_.AddNewDirectory(
1575      kParentResourceId,
1576      "new directory",
1577      DriveServiceInterface::AddNewDirectoryOptions(),
1578      test_util::CreateCopyResultCallback(&error, &entry));
1579  base::RunLoop().RunUntilIdle();
1580
1581  EXPECT_EQ(HTTP_CREATED, error);
1582  ASSERT_TRUE(entry);
1583  EXPECT_TRUE(entry->IsDirectory());
1584  EXPECT_EQ("resource_id_1", entry->file_id());
1585  EXPECT_EQ("new directory", entry->title());
1586  EXPECT_TRUE(HasParent(entry->file_id(), kParentResourceId));
1587  // Should be incremented as a new directory was created.
1588  EXPECT_EQ(old_largest_change_id + 1,
1589            fake_service_.about_resource().largest_change_id());
1590  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
1591}
1592
1593TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonexistingDirectory) {
1594  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1595
1596  const std::string kParentResourceId = "nonexisting_resource_id";
1597
1598  GDataErrorCode error = GDATA_OTHER_ERROR;
1599  scoped_ptr<FileResource> entry;
1600  fake_service_.AddNewDirectory(
1601      kParentResourceId,
1602      "new directory",
1603      DriveServiceInterface::AddNewDirectoryOptions(),
1604      test_util::CreateCopyResultCallback(&error, &entry));
1605  base::RunLoop().RunUntilIdle();
1606
1607  EXPECT_EQ(HTTP_NOT_FOUND, error);
1608  EXPECT_FALSE(entry);
1609}
1610
1611TEST_F(FakeDriveServiceTest, AddNewDirectory_Offline) {
1612  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1613  fake_service_.set_offline(true);
1614
1615  GDataErrorCode error = GDATA_OTHER_ERROR;
1616  scoped_ptr<FileResource> entry;
1617  fake_service_.AddNewDirectory(
1618      fake_service_.GetRootResourceId(),
1619      "new directory",
1620      DriveServiceInterface::AddNewDirectoryOptions(),
1621      test_util::CreateCopyResultCallback(&error, &entry));
1622  base::RunLoop().RunUntilIdle();
1623
1624  EXPECT_EQ(GDATA_NO_CONNECTION, error);
1625  EXPECT_FALSE(entry);
1626}
1627
1628TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_Offline) {
1629  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1630  fake_service_.set_offline(true);
1631
1632  GDataErrorCode error = GDATA_OTHER_ERROR;
1633  GURL upload_location;
1634  fake_service_.InitiateUploadNewFile(
1635      "test/foo",
1636      13,
1637      "1_folder_resource_id",
1638      "new file.foo",
1639      FakeDriveService::InitiateUploadNewFileOptions(),
1640      test_util::CreateCopyResultCallback(&error, &upload_location));
1641  base::RunLoop().RunUntilIdle();
1642
1643  EXPECT_EQ(GDATA_NO_CONNECTION, error);
1644  EXPECT_TRUE(upload_location.is_empty());
1645}
1646
1647TEST_F(FakeDriveServiceTest, InitiateUploadNewFile_NotFound) {
1648  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1649
1650  GDataErrorCode error = GDATA_OTHER_ERROR;
1651  GURL upload_location;
1652  fake_service_.InitiateUploadNewFile(
1653      "test/foo",
1654      13,
1655      "non_existent",
1656      "new file.foo",
1657      FakeDriveService::InitiateUploadNewFileOptions(),
1658      test_util::CreateCopyResultCallback(&error, &upload_location));
1659  base::RunLoop().RunUntilIdle();
1660
1661  EXPECT_EQ(HTTP_NOT_FOUND, error);
1662  EXPECT_TRUE(upload_location.is_empty());
1663}
1664
1665TEST_F(FakeDriveServiceTest, InitiateUploadNewFile) {
1666  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1667
1668  GDataErrorCode error = GDATA_OTHER_ERROR;
1669  GURL upload_location;
1670  fake_service_.InitiateUploadNewFile(
1671      "test/foo",
1672      13,
1673      "1_folder_resource_id",
1674      "new file.foo",
1675      FakeDriveService::InitiateUploadNewFileOptions(),
1676      test_util::CreateCopyResultCallback(&error, &upload_location));
1677  base::RunLoop().RunUntilIdle();
1678
1679  EXPECT_EQ(HTTP_SUCCESS, error);
1680  EXPECT_FALSE(upload_location.is_empty());
1681  EXPECT_NE(GURL("https://1_folder_resumable_create_media_link?mode=newfile"),
1682            upload_location);
1683}
1684
1685TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_Offline) {
1686  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1687  fake_service_.set_offline(true);
1688
1689  GDataErrorCode error = GDATA_OTHER_ERROR;
1690  GURL upload_location;
1691  fake_service_.InitiateUploadExistingFile(
1692      "test/foo",
1693      13,
1694      "2_file_resource_id",
1695      FakeDriveService::InitiateUploadExistingFileOptions(),
1696      test_util::CreateCopyResultCallback(&error, &upload_location));
1697  base::RunLoop().RunUntilIdle();
1698
1699  EXPECT_EQ(GDATA_NO_CONNECTION, error);
1700  EXPECT_TRUE(upload_location.is_empty());
1701}
1702
1703TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_Forbidden) {
1704  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1705
1706  EXPECT_EQ(HTTP_SUCCESS, fake_service_.SetUserPermission(
1707      "2_file_resource_id", google_apis::drive::PERMISSION_ROLE_READER));
1708
1709  GDataErrorCode error = GDATA_OTHER_ERROR;
1710  GURL upload_location;
1711  fake_service_.InitiateUploadExistingFile(
1712      "test/foo",
1713      13,
1714      "2_file_resource_id",
1715      FakeDriveService::InitiateUploadExistingFileOptions(),
1716      test_util::CreateCopyResultCallback(&error, &upload_location));
1717  base::RunLoop().RunUntilIdle();
1718
1719  EXPECT_EQ(HTTP_FORBIDDEN, error);
1720  EXPECT_TRUE(upload_location.is_empty());
1721}
1722
1723TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_NotFound) {
1724  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1725
1726  GDataErrorCode error = GDATA_OTHER_ERROR;
1727  GURL upload_location;
1728  fake_service_.InitiateUploadExistingFile(
1729      "test/foo",
1730      13,
1731      "non_existent",
1732      FakeDriveService::InitiateUploadExistingFileOptions(),
1733      test_util::CreateCopyResultCallback(&error, &upload_location));
1734  base::RunLoop().RunUntilIdle();
1735
1736  EXPECT_EQ(HTTP_NOT_FOUND, error);
1737  EXPECT_TRUE(upload_location.is_empty());
1738}
1739
1740TEST_F(FakeDriveServiceTest, InitiateUploadExistingFile_WrongETag) {
1741  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1742
1743  FakeDriveService::InitiateUploadExistingFileOptions options;
1744  options.etag = "invalid_etag";
1745
1746  GDataErrorCode error = GDATA_OTHER_ERROR;
1747  GURL upload_location;
1748  fake_service_.InitiateUploadExistingFile(
1749      "text/plain",
1750      13,
1751      "2_file_resource_id",
1752      options,
1753      test_util::CreateCopyResultCallback(&error, &upload_location));
1754  base::RunLoop().RunUntilIdle();
1755
1756  EXPECT_EQ(HTTP_PRECONDITION, error);
1757  EXPECT_TRUE(upload_location.is_empty());
1758}
1759
1760TEST_F(FakeDriveServiceTest, InitiateUpload_ExistingFile) {
1761  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1762
1763  scoped_ptr<FileResource> entry = FindEntry("2_file_resource_id");
1764  ASSERT_TRUE(entry);
1765
1766  FakeDriveService::InitiateUploadExistingFileOptions options;
1767  options.etag = entry->etag();
1768
1769  GDataErrorCode error = GDATA_OTHER_ERROR;
1770  GURL upload_location;
1771  fake_service_.InitiateUploadExistingFile(
1772      "text/plain",
1773      13,
1774      "2_file_resource_id",
1775      options,
1776      test_util::CreateCopyResultCallback(&error, &upload_location));
1777  base::RunLoop().RunUntilIdle();
1778
1779  EXPECT_EQ(HTTP_SUCCESS, error);
1780  EXPECT_TRUE(upload_location.is_valid());
1781}
1782
1783TEST_F(FakeDriveServiceTest, ResumeUpload_Offline) {
1784  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1785
1786  GDataErrorCode error = GDATA_OTHER_ERROR;
1787  GURL upload_location;
1788  fake_service_.InitiateUploadNewFile(
1789      "test/foo",
1790      15,
1791      "1_folder_resource_id",
1792      "new file.foo",
1793      FakeDriveService::InitiateUploadNewFileOptions(),
1794      test_util::CreateCopyResultCallback(&error, &upload_location));
1795  base::RunLoop().RunUntilIdle();
1796
1797  EXPECT_EQ(HTTP_SUCCESS, error);
1798  EXPECT_FALSE(upload_location.is_empty());
1799  EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
1800            upload_location);
1801
1802  fake_service_.set_offline(true);
1803
1804  UploadRangeResponse response;
1805  scoped_ptr<FileResource> entry;
1806  fake_service_.ResumeUpload(
1807      upload_location,
1808      0, 13, 15, "test/foo",
1809      base::FilePath(),
1810      test_util::CreateCopyResultCallback(&response, &entry),
1811      ProgressCallback());
1812  base::RunLoop().RunUntilIdle();
1813
1814  EXPECT_EQ(GDATA_NO_CONNECTION, response.code);
1815  EXPECT_FALSE(entry.get());
1816}
1817
1818TEST_F(FakeDriveServiceTest, ResumeUpload_NotFound) {
1819  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1820
1821  GDataErrorCode error = GDATA_OTHER_ERROR;
1822  GURL upload_location;
1823  fake_service_.InitiateUploadNewFile(
1824      "test/foo",
1825      15,
1826      "1_folder_resource_id",
1827      "new file.foo",
1828      FakeDriveService::InitiateUploadNewFileOptions(),
1829      test_util::CreateCopyResultCallback(&error, &upload_location));
1830  base::RunLoop().RunUntilIdle();
1831
1832  ASSERT_EQ(HTTP_SUCCESS, error);
1833
1834  UploadRangeResponse response;
1835  scoped_ptr<FileResource> entry;
1836  fake_service_.ResumeUpload(
1837      GURL("https://foo.com/"),
1838      0, 13, 15, "test/foo",
1839      base::FilePath(),
1840      test_util::CreateCopyResultCallback(&response, &entry),
1841      ProgressCallback());
1842  base::RunLoop().RunUntilIdle();
1843
1844  EXPECT_EQ(HTTP_NOT_FOUND, response.code);
1845  EXPECT_FALSE(entry.get());
1846}
1847
1848TEST_F(FakeDriveServiceTest, ResumeUpload_ExistingFile) {
1849  base::ScopedTempDir temp_dir;
1850  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1851  base::FilePath local_file_path =
1852      temp_dir.path().Append(FILE_PATH_LITERAL("File 1.txt"));
1853  std::string contents("hogefugapiyo");
1854  ASSERT_TRUE(test_util::WriteStringToFile(local_file_path, contents));
1855
1856  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1857
1858  scoped_ptr<FileResource> entry = FindEntry("2_file_resource_id");
1859  ASSERT_TRUE(entry);
1860
1861  FakeDriveService::InitiateUploadExistingFileOptions options;
1862  options.etag = entry->etag();
1863
1864  GDataErrorCode error = GDATA_OTHER_ERROR;
1865  GURL upload_location;
1866  fake_service_.InitiateUploadExistingFile(
1867      "text/plain",
1868      contents.size(),
1869      "2_file_resource_id",
1870      options,
1871      test_util::CreateCopyResultCallback(&error, &upload_location));
1872  base::RunLoop().RunUntilIdle();
1873
1874  ASSERT_EQ(HTTP_SUCCESS, error);
1875
1876  UploadRangeResponse response;
1877  entry.reset();
1878  std::vector<test_util::ProgressInfo> upload_progress_values;
1879  fake_service_.ResumeUpload(
1880      upload_location,
1881      0, contents.size() / 2, contents.size(), "text/plain",
1882      local_file_path,
1883      test_util::CreateCopyResultCallback(&response, &entry),
1884      base::Bind(&test_util::AppendProgressCallbackResult,
1885                 &upload_progress_values));
1886  base::RunLoop().RunUntilIdle();
1887
1888  EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1889  EXPECT_FALSE(entry.get());
1890  ASSERT_TRUE(!upload_progress_values.empty());
1891  EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
1892  EXPECT_LE(0, upload_progress_values.front().first);
1893  EXPECT_GE(static_cast<int64>(contents.size() / 2),
1894            upload_progress_values.back().first);
1895
1896  upload_progress_values.clear();
1897  fake_service_.ResumeUpload(
1898      upload_location,
1899      contents.size() / 2, contents.size(), contents.size(), "text/plain",
1900      local_file_path,
1901      test_util::CreateCopyResultCallback(&response, &entry),
1902      base::Bind(&test_util::AppendProgressCallbackResult,
1903                 &upload_progress_values));
1904  base::RunLoop().RunUntilIdle();
1905
1906  EXPECT_EQ(HTTP_SUCCESS, response.code);
1907  EXPECT_TRUE(entry.get());
1908  EXPECT_EQ(static_cast<int64>(contents.size()), entry->file_size());
1909  EXPECT_TRUE(Exists(entry->file_id()));
1910  ASSERT_TRUE(!upload_progress_values.empty());
1911  EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
1912  EXPECT_LE(0, upload_progress_values.front().first);
1913  EXPECT_GE(static_cast<int64>(contents.size() - contents.size() / 2),
1914            upload_progress_values.back().first);
1915  EXPECT_EQ(base::MD5String(contents), entry->md5_checksum());
1916}
1917
1918TEST_F(FakeDriveServiceTest, ResumeUpload_NewFile) {
1919  base::ScopedTempDir temp_dir;
1920  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
1921  base::FilePath local_file_path =
1922      temp_dir.path().Append(FILE_PATH_LITERAL("new file.foo"));
1923  std::string contents("hogefugapiyo");
1924  ASSERT_TRUE(test_util::WriteStringToFile(local_file_path, contents));
1925
1926  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1927
1928  GDataErrorCode error = GDATA_OTHER_ERROR;
1929  GURL upload_location;
1930  fake_service_.InitiateUploadNewFile(
1931      "test/foo",
1932      contents.size(),
1933      "1_folder_resource_id",
1934      "new file.foo",
1935      FakeDriveService::InitiateUploadNewFileOptions(),
1936      test_util::CreateCopyResultCallback(&error, &upload_location));
1937  base::RunLoop().RunUntilIdle();
1938
1939  EXPECT_EQ(HTTP_SUCCESS, error);
1940  EXPECT_FALSE(upload_location.is_empty());
1941  EXPECT_NE(GURL("https://1_folder_resumable_create_media_link"),
1942            upload_location);
1943
1944  UploadRangeResponse response;
1945  scoped_ptr<FileResource> entry;
1946  std::vector<test_util::ProgressInfo> upload_progress_values;
1947  fake_service_.ResumeUpload(
1948      upload_location,
1949      0, contents.size() / 2, contents.size(), "test/foo",
1950      local_file_path,
1951      test_util::CreateCopyResultCallback(&response, &entry),
1952      base::Bind(&test_util::AppendProgressCallbackResult,
1953                 &upload_progress_values));
1954  base::RunLoop().RunUntilIdle();
1955
1956  EXPECT_EQ(HTTP_RESUME_INCOMPLETE, response.code);
1957  EXPECT_FALSE(entry.get());
1958  ASSERT_TRUE(!upload_progress_values.empty());
1959  EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
1960  EXPECT_LE(0, upload_progress_values.front().first);
1961  EXPECT_GE(static_cast<int64>(contents.size() / 2),
1962            upload_progress_values.back().first);
1963
1964  upload_progress_values.clear();
1965  fake_service_.ResumeUpload(
1966      upload_location,
1967      contents.size() / 2, contents.size(), contents.size(), "test/foo",
1968      local_file_path,
1969      test_util::CreateCopyResultCallback(&response, &entry),
1970      base::Bind(&test_util::AppendProgressCallbackResult,
1971                 &upload_progress_values));
1972  base::RunLoop().RunUntilIdle();
1973
1974  EXPECT_EQ(HTTP_CREATED, response.code);
1975  EXPECT_TRUE(entry.get());
1976  EXPECT_EQ(static_cast<int64>(contents.size()), entry->file_size());
1977  EXPECT_TRUE(Exists(entry->file_id()));
1978  ASSERT_TRUE(!upload_progress_values.empty());
1979  EXPECT_TRUE(base::STLIsSorted(upload_progress_values));
1980  EXPECT_LE(0, upload_progress_values.front().first);
1981  EXPECT_GE(static_cast<int64>(contents.size() - contents.size() / 2),
1982            upload_progress_values.back().first);
1983  EXPECT_EQ(base::MD5String(contents), entry->md5_checksum());
1984}
1985
1986TEST_F(FakeDriveServiceTest, AddNewFile_ToRootDirectory) {
1987  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
1988
1989  int64 old_largest_change_id = GetLargestChangeByAboutResource();
1990
1991  const std::string kContentType = "text/plain";
1992  const std::string kContentData = "This is some test content.";
1993  const std::string kTitle = "new file";
1994
1995  GDataErrorCode error = GDATA_OTHER_ERROR;
1996  scoped_ptr<FileResource> entry;
1997  fake_service_.AddNewFile(
1998      kContentType,
1999      kContentData,
2000      fake_service_.GetRootResourceId(),
2001      kTitle,
2002      false,  // shared_with_me
2003      test_util::CreateCopyResultCallback(&error, &entry));
2004  base::RunLoop().RunUntilIdle();
2005
2006  EXPECT_EQ(HTTP_CREATED, error);
2007  ASSERT_TRUE(entry);
2008  EXPECT_EQ(kContentType, entry->mime_type());
2009  EXPECT_EQ(static_cast<int64>(kContentData.size()), entry->file_size());
2010  EXPECT_EQ("resource_id_1", entry->file_id());
2011  EXPECT_EQ(kTitle, entry->title());
2012  EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
2013  // Should be incremented as a new directory was created.
2014  EXPECT_EQ(old_largest_change_id + 1,
2015            fake_service_.about_resource().largest_change_id());
2016  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
2017  EXPECT_EQ(base::MD5String(kContentData), entry->md5_checksum());
2018}
2019
2020TEST_F(FakeDriveServiceTest, AddNewFile_ToRootDirectoryOnEmptyFileSystem) {
2021  int64 old_largest_change_id = GetLargestChangeByAboutResource();
2022
2023  const std::string kContentType = "text/plain";
2024  const std::string kContentData = "This is some test content.";
2025  const std::string kTitle = "new file";
2026
2027  GDataErrorCode error = GDATA_OTHER_ERROR;
2028  scoped_ptr<FileResource> entry;
2029  fake_service_.AddNewFile(
2030      kContentType,
2031      kContentData,
2032      fake_service_.GetRootResourceId(),
2033      kTitle,
2034      false,  // shared_with_me
2035      test_util::CreateCopyResultCallback(&error, &entry));
2036  base::RunLoop().RunUntilIdle();
2037
2038  EXPECT_EQ(HTTP_CREATED, error);
2039  ASSERT_TRUE(entry);
2040  EXPECT_EQ(kContentType, entry->mime_type());
2041  EXPECT_EQ(static_cast<int64>(kContentData.size()), entry->file_size());
2042  EXPECT_EQ("resource_id_1", entry->file_id());
2043  EXPECT_EQ(kTitle, entry->title());
2044  EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
2045  // Should be incremented as a new directory was created.
2046  EXPECT_EQ(old_largest_change_id + 1,
2047            fake_service_.about_resource().largest_change_id());
2048  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
2049  EXPECT_EQ(base::MD5String(kContentData), entry->md5_checksum());
2050}
2051
2052TEST_F(FakeDriveServiceTest, AddNewFile_ToNonRootDirectory) {
2053  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
2054
2055  int64 old_largest_change_id = GetLargestChangeByAboutResource();
2056
2057  const std::string kContentType = "text/plain";
2058  const std::string kContentData = "This is some test content.";
2059  const std::string kTitle = "new file";
2060  const std::string kParentResourceId = "1_folder_resource_id";
2061
2062  GDataErrorCode error = GDATA_OTHER_ERROR;
2063  scoped_ptr<FileResource> entry;
2064  fake_service_.AddNewFile(
2065      kContentType,
2066      kContentData,
2067      kParentResourceId,
2068      kTitle,
2069      false,  // shared_with_me
2070      test_util::CreateCopyResultCallback(&error, &entry));
2071  base::RunLoop().RunUntilIdle();
2072
2073  EXPECT_EQ(HTTP_CREATED, error);
2074  ASSERT_TRUE(entry);
2075  EXPECT_EQ(kContentType, entry->mime_type());
2076  EXPECT_EQ(static_cast<int64>(kContentData.size()), entry->file_size());
2077  EXPECT_EQ("resource_id_1", entry->file_id());
2078  EXPECT_EQ(kTitle, entry->title());
2079  EXPECT_TRUE(HasParent(entry->file_id(), kParentResourceId));
2080  // Should be incremented as a new directory was created.
2081  EXPECT_EQ(old_largest_change_id + 1,
2082            fake_service_.about_resource().largest_change_id());
2083  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
2084  EXPECT_EQ(base::MD5String(kContentData), entry->md5_checksum());
2085}
2086
2087TEST_F(FakeDriveServiceTest, AddNewFile_ToNonexistingDirectory) {
2088  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
2089
2090  const std::string kContentType = "text/plain";
2091  const std::string kContentData = "This is some test content.";
2092  const std::string kTitle = "new file";
2093  const std::string kParentResourceId = "nonexisting_resource_id";
2094
2095  GDataErrorCode error = GDATA_OTHER_ERROR;
2096  scoped_ptr<FileResource> entry;
2097  fake_service_.AddNewFile(
2098      kContentType,
2099      kContentData,
2100      kParentResourceId,
2101      kTitle,
2102      false,  // shared_with_me
2103      test_util::CreateCopyResultCallback(&error, &entry));
2104  base::RunLoop().RunUntilIdle();
2105
2106  EXPECT_EQ(HTTP_NOT_FOUND, error);
2107  EXPECT_FALSE(entry);
2108}
2109
2110TEST_F(FakeDriveServiceTest, AddNewFile_Offline) {
2111  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
2112  fake_service_.set_offline(true);
2113
2114  const std::string kContentType = "text/plain";
2115  const std::string kContentData = "This is some test content.";
2116  const std::string kTitle = "new file";
2117
2118  GDataErrorCode error = GDATA_OTHER_ERROR;
2119  scoped_ptr<FileResource> entry;
2120  fake_service_.AddNewFile(
2121      kContentType,
2122      kContentData,
2123      fake_service_.GetRootResourceId(),
2124      kTitle,
2125      false,  // shared_with_me
2126      test_util::CreateCopyResultCallback(&error, &entry));
2127  base::RunLoop().RunUntilIdle();
2128
2129  EXPECT_EQ(GDATA_NO_CONNECTION, error);
2130  EXPECT_FALSE(entry);
2131}
2132
2133TEST_F(FakeDriveServiceTest, AddNewFile_SharedWithMeLabel) {
2134  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
2135
2136  const std::string kContentType = "text/plain";
2137  const std::string kContentData = "This is some test content.";
2138  const std::string kTitle = "new file";
2139
2140  int64 old_largest_change_id = GetLargestChangeByAboutResource();
2141
2142  GDataErrorCode error = GDATA_OTHER_ERROR;
2143  scoped_ptr<FileResource> entry;
2144  fake_service_.AddNewFile(
2145      kContentType,
2146      kContentData,
2147      fake_service_.GetRootResourceId(),
2148      kTitle,
2149      true,  // shared_with_me
2150      test_util::CreateCopyResultCallback(&error, &entry));
2151  base::RunLoop().RunUntilIdle();
2152
2153  EXPECT_EQ(HTTP_CREATED, error);
2154  ASSERT_TRUE(entry);
2155  EXPECT_EQ(kContentType, entry->mime_type());
2156  EXPECT_EQ(static_cast<int64>(kContentData.size()), entry->file_size());
2157  EXPECT_EQ("resource_id_1", entry->file_id());
2158  EXPECT_EQ(kTitle, entry->title());
2159  EXPECT_TRUE(HasParent(entry->file_id(), fake_service_.GetRootResourceId()));
2160  EXPECT_FALSE(entry->shared_with_me_date().is_null());
2161  // Should be incremented as a new directory was created.
2162  EXPECT_EQ(old_largest_change_id + 1,
2163            fake_service_.about_resource().largest_change_id());
2164  EXPECT_EQ(old_largest_change_id + 1, GetLargestChangeByAboutResource());
2165  EXPECT_EQ(base::MD5String(kContentData), entry->md5_checksum());
2166}
2167
2168TEST_F(FakeDriveServiceTest, SetLastModifiedTime_ExistingFile) {
2169  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
2170
2171  const std::string kResourceId = "2_file_resource_id";
2172  base::Time time;
2173  ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time));
2174
2175  GDataErrorCode error = GDATA_OTHER_ERROR;
2176  scoped_ptr<FileResource> entry;
2177  fake_service_.SetLastModifiedTime(
2178      kResourceId,
2179      time,
2180      test_util::CreateCopyResultCallback(&error, &entry));
2181  base::RunLoop().RunUntilIdle();
2182
2183  EXPECT_EQ(HTTP_SUCCESS, error);
2184  ASSERT_TRUE(entry);
2185  EXPECT_EQ(time, entry->modified_date());
2186}
2187
2188TEST_F(FakeDriveServiceTest, SetLastModifiedTime_NonexistingFile) {
2189  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
2190
2191  const std::string kResourceId = "nonexisting_resource_id";
2192  base::Time time;
2193  ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time));
2194
2195  GDataErrorCode error = GDATA_OTHER_ERROR;
2196  scoped_ptr<FileResource> entry;
2197  fake_service_.SetLastModifiedTime(
2198      kResourceId,
2199      time,
2200      test_util::CreateCopyResultCallback(&error, &entry));
2201  base::RunLoop().RunUntilIdle();
2202
2203  EXPECT_EQ(HTTP_NOT_FOUND, error);
2204  EXPECT_FALSE(entry);
2205}
2206
2207TEST_F(FakeDriveServiceTest, SetLastModifiedTime_Offline) {
2208  ASSERT_TRUE(test_util::SetUpTestEntries(&fake_service_));
2209  fake_service_.set_offline(true);
2210
2211  const std::string kResourceId = "2_file_resource_id";
2212  base::Time time;
2213  ASSERT_TRUE(base::Time::FromString("1 April 2013 12:34:56", &time));
2214
2215  GDataErrorCode error = GDATA_OTHER_ERROR;
2216  scoped_ptr<FileResource> entry;
2217  fake_service_.SetLastModifiedTime(
2218      kResourceId,
2219      time,
2220      test_util::CreateCopyResultCallback(&error, &entry));
2221  base::RunLoop().RunUntilIdle();
2222
2223  EXPECT_EQ(GDATA_NO_CONNECTION, error);
2224  EXPECT_FALSE(entry);
2225}
2226
2227}  // namespace
2228
2229}  // namespace drive
2230