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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
|
// Copyright (c) 2012 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 "chrome/browser/chromeos/drive/drive_resource_metadata.h"
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include "base/message_loop.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/drive_cache.h"
#include "chrome/browser/chromeos/drive/drive_file_system_util.h"
#include "chrome/browser/chromeos/drive/drive_resource_metadata_storage.h"
#include "chrome/browser/chromeos/drive/drive_test_util.h"
#include "chrome/browser/google_apis/test_util.h"
#include "chrome/browser/google_apis/time_util.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace drive {
namespace {
// See drive.proto for the difference between the two URLs.
const char kResumableEditMediaUrl[] = "http://resumable-edit-media/";
const char kTestRootResourceId[] = "test_root";
// The changestamp of the resource metadata used in
// DriveResourceMetadataTest.
const int64 kTestChangestamp = 100;
// Returns the sorted base names from |entries|.
std::vector<std::string> GetSortedBaseNames(
const DriveEntryProtoVector& entries) {
std::vector<std::string> base_names;
for (size_t i = 0; i < entries.size(); ++i)
base_names.push_back(entries[i].base_name());
std::sort(base_names.begin(), base_names.end());
return base_names;
}
// Increments |count| if |entry| is a file.
void CountFile(int* count, const DriveEntryProto& entry) {
if (!entry.file_info().is_directory())
++*count;
}
} // namespace
class DriveResourceMetadataTest : public testing::Test {
protected:
DriveResourceMetadataTest()
: ui_thread_(content::BrowserThread::UI, &message_loop_) {
}
// Creates the following files/directories
// drive/root/dir1/
// drive/root/dir2/
// drive/root/dir1/dir3/
// drive/root/dir1/file4
// drive/root/dir1/file5
// drive/root/dir2/file6
// drive/root/dir2/file7
// drive/root/dir2/file8
// drive/root/dir1/dir3/file9
// drive/root/dir1/dir3/file10
static void Init(DriveResourceMetadata* resource_metadata);
// Creates a DriveEntryProto.
static DriveEntryProto CreateDriveEntryProto(
int sequence_id,
bool is_directory,
const std::string& parent_resource_id);
// Adds a DriveEntryProto to the metadata tree. Returns true on success.
static bool AddDriveEntryProto(DriveResourceMetadata* resource_metadata,
int sequence_id,
bool is_directory,
const std::string& parent_resource_id);
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
base::ThreadRestrictions::SetIOAllowed(false); // For strict thread check.
scoped_refptr<base::SequencedWorkerPool> pool =
content::BrowserThread::GetBlockingPool();
blocking_task_runner_ =
pool->GetSequencedTaskRunner(pool->GetSequenceToken());
resource_metadata_.reset(new DriveResourceMetadata(temp_dir_.path(),
blocking_task_runner_));
Init(resource_metadata_.get());
}
virtual void TearDown() OVERRIDE {
resource_metadata_.reset();
base::ThreadRestrictions::SetIOAllowed(true);
}
// Gets the entry info by path synchronously. Returns NULL on failure.
scoped_ptr<DriveEntryProto> GetEntryInfoByPathSync(
const base::FilePath& file_path) {
DriveFileError error = DRIVE_FILE_OK;
scoped_ptr<DriveEntryProto> entry_proto;
resource_metadata_->GetEntryInfoByPath(
file_path,
google_apis::test_util::CreateCopyResultCallback(&error, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_TRUE(error == DRIVE_FILE_OK || !entry_proto);
return entry_proto.Pass();
}
// Reads the directory contents by path synchronously. Returns NULL on
// failure.
scoped_ptr<DriveEntryProtoVector> ReadDirectoryByPathSync(
const base::FilePath& directory_path) {
DriveFileError error = DRIVE_FILE_OK;
scoped_ptr<DriveEntryProtoVector> entries;
resource_metadata_->ReadDirectoryByPath(
directory_path,
google_apis::test_util::CreateCopyResultCallback(&error, &entries));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_TRUE(error == DRIVE_FILE_OK || !entries);
return entries.Pass();
}
base::ScopedTempDir temp_dir_;
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
scoped_ptr<DriveResourceMetadata, test_util::DestroyHelperForTests>
resource_metadata_;
private:
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
};
// static
void DriveResourceMetadataTest::Init(DriveResourceMetadata* resource_metadata) {
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
resource_metadata->Initialize(
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
ASSERT_EQ(DRIVE_FILE_OK, error);
// Create mydrive root directory.
{
error = DRIVE_FILE_ERROR_FAILED;
base::FilePath drive_path;
resource_metadata->AddEntry(
util::CreateMyDriveRootEntry(kTestRootResourceId),
google_apis::test_util::CreateCopyResultCallback(&error, &drive_path));
google_apis::test_util::RunBlockingPoolTask();
ASSERT_EQ(DRIVE_FILE_OK, error);
}
int sequence_id = 1;
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, true, kTestRootResourceId));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, true, kTestRootResourceId));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, true, "resource_id:dir1"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, false, "resource_id:dir1"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, false, "resource_id:dir1"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, false, "resource_id:dir2"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, false, "resource_id:dir2"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, false, "resource_id:dir2"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, false, "resource_id:dir3"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata, sequence_id++, false, "resource_id:dir3"));
resource_metadata->SetLargestChangestamp(
kTestChangestamp,
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
}
// static
DriveEntryProto DriveResourceMetadataTest::CreateDriveEntryProto(
int sequence_id,
bool is_directory,
const std::string& parent_resource_id) {
DriveEntryProto entry_proto;
const std::string sequence_id_str = base::IntToString(sequence_id);
const std::string title = (is_directory ? "dir" : "file") + sequence_id_str;
const std::string resource_id = "resource_id:" + title;
entry_proto.set_title(title);
entry_proto.set_resource_id(resource_id);
entry_proto.set_parent_resource_id(parent_resource_id);
PlatformFileInfoProto* file_info = entry_proto.mutable_file_info();
file_info->set_is_directory(is_directory);
if (!is_directory) {
DriveFileSpecificInfo* file_specific_info =
entry_proto.mutable_file_specific_info();
file_info->set_size(sequence_id * 1024);
file_specific_info->set_file_md5(std::string("md5:") + title);
} else {
DriveDirectorySpecificInfo* directory_specific_info =
entry_proto.mutable_directory_specific_info();
directory_specific_info->set_changestamp(kTestChangestamp);
}
return entry_proto;
}
bool DriveResourceMetadataTest::AddDriveEntryProto(
DriveResourceMetadata* resource_metadata,
int sequence_id,
bool is_directory,
const std::string& parent_resource_id) {
DriveEntryProto entry_proto = CreateDriveEntryProto(sequence_id,
is_directory,
parent_resource_id);
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
base::FilePath drive_path;
resource_metadata->AddEntry(
entry_proto,
google_apis::test_util::CreateCopyResultCallback(&error, &drive_path));
google_apis::test_util::RunBlockingPoolTask();
return DRIVE_FILE_OK == error;
}
TEST_F(DriveResourceMetadataTest, LargestChangestamp) {
scoped_ptr<DriveResourceMetadata, test_util::DestroyHelperForTests>
resource_metadata(new DriveResourceMetadata(temp_dir_.path(),
blocking_task_runner_));
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
resource_metadata->Initialize(
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
ASSERT_EQ(DRIVE_FILE_OK, error);
int64 in_changestamp = 123456;
resource_metadata->SetLargestChangestamp(
in_changestamp,
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
int64 out_changestamp = 0;
resource_metadata->GetLargestChangestamp(
google_apis::test_util::CreateCopyResultCallback(&out_changestamp));
google_apis::test_util::RunBlockingPoolTask();
DCHECK_EQ(in_changestamp, out_changestamp);
}
TEST_F(DriveResourceMetadataTest, GetEntryInfoByResourceId_RootDirectory) {
scoped_ptr<DriveResourceMetadata, test_util::DestroyHelperForTests>
resource_metadata(new DriveResourceMetadata(temp_dir_.path(),
blocking_task_runner_));
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
resource_metadata->Initialize(
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
ASSERT_EQ(DRIVE_FILE_OK, error);
base::FilePath drive_file_path;
scoped_ptr<DriveEntryProto> entry_proto;
// Look up the root directory by its resource ID.
resource_metadata->GetEntryInfoByResourceId(
util::kDriveGrandRootSpecialResourceId,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive"), drive_file_path);
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("drive", entry_proto->base_name());
}
TEST_F(DriveResourceMetadataTest, GetEntryInfoByResourceId) {
// Confirm that an existing file is found.
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
base::FilePath drive_file_path;
scoped_ptr<DriveEntryProto> entry_proto;
resource_metadata_->GetEntryInfoByResourceId(
"resource_id:file4",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/file4"),
drive_file_path);
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("file4", entry_proto->base_name());
// Confirm that a non existing file is not found.
error = DRIVE_FILE_ERROR_FAILED;
entry_proto.reset();
resource_metadata_->GetEntryInfoByResourceId(
"file:non_existing",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_FALSE(entry_proto.get());
}
TEST_F(DriveResourceMetadataTest, GetEntryInfoByPath) {
// Confirm that an existing file is found.
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
scoped_ptr<DriveEntryProto> entry_proto;
resource_metadata_->GetEntryInfoByPath(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file4"),
google_apis::test_util::CreateCopyResultCallback(&error, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("file4", entry_proto->base_name());
// Confirm that a non existing file is not found.
error = DRIVE_FILE_ERROR_FAILED;
entry_proto.reset();
resource_metadata_->GetEntryInfoByPath(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/non_existing"),
google_apis::test_util::CreateCopyResultCallback(&error, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_FALSE(entry_proto.get());
// Confirm that the root is found.
error = DRIVE_FILE_ERROR_FAILED;
entry_proto.reset();
resource_metadata_->GetEntryInfoByPath(
base::FilePath::FromUTF8Unsafe("drive"),
google_apis::test_util::CreateCopyResultCallback(&error, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_TRUE(entry_proto.get());
// Confirm that a non existing file is not found at the root level.
error = DRIVE_FILE_ERROR_FAILED;
entry_proto.reset();
resource_metadata_->GetEntryInfoByPath(
base::FilePath::FromUTF8Unsafe("non_existing"),
google_apis::test_util::CreateCopyResultCallback(&error, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_FALSE(entry_proto.get());
// Confirm that an entry is not found with a wrong root.
error = DRIVE_FILE_ERROR_FAILED;
entry_proto.reset();
resource_metadata_->GetEntryInfoByPath(
base::FilePath::FromUTF8Unsafe("non_existing/root"),
google_apis::test_util::CreateCopyResultCallback(&error, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_FALSE(entry_proto.get());
}
TEST_F(DriveResourceMetadataTest, ReadDirectoryByPath) {
// Confirm that an existing directory is found.
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
scoped_ptr<DriveEntryProtoVector> entries;
resource_metadata_->ReadDirectoryByPath(
base::FilePath::FromUTF8Unsafe("drive/root/dir1"),
google_apis::test_util::CreateCopyResultCallback(&error, &entries));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
ASSERT_TRUE(entries.get());
ASSERT_EQ(3U, entries->size());
// The order is not guaranteed so we should sort the base names.
std::vector<std::string> base_names = GetSortedBaseNames(*entries);
EXPECT_EQ("dir3", base_names[0]);
EXPECT_EQ("file4", base_names[1]);
EXPECT_EQ("file5", base_names[2]);
// Confirm that a non existing directory is not found.
error = DRIVE_FILE_ERROR_FAILED;
entries.reset();
resource_metadata_->ReadDirectoryByPath(
base::FilePath::FromUTF8Unsafe("drive/root/non_existing"),
google_apis::test_util::CreateCopyResultCallback(&error, &entries));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_FALSE(entries.get());
// Confirm that reading a file results in DRIVE_FILE_ERROR_NOT_A_DIRECTORY.
error = DRIVE_FILE_ERROR_FAILED;
entries.reset();
resource_metadata_->ReadDirectoryByPath(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file4"),
google_apis::test_util::CreateCopyResultCallback(&error, &entries));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_A_DIRECTORY, error);
EXPECT_FALSE(entries.get());
}
TEST_F(DriveResourceMetadataTest, GetEntryInfoPairByPaths) {
// Confirm that existing two files are found.
scoped_ptr<EntryInfoPairResult> pair_result;
resource_metadata_->GetEntryInfoPairByPaths(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file4"),
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file5"),
google_apis::test_util::CreateCopyResultCallback(&pair_result));
google_apis::test_util::RunBlockingPoolTask();
// The first entry should be found.
EXPECT_EQ(DRIVE_FILE_OK, pair_result->first.error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/file4"),
pair_result->first.path);
ASSERT_TRUE(pair_result->first.proto.get());
EXPECT_EQ("file4", pair_result->first.proto->base_name());
// The second entry should be found.
EXPECT_EQ(DRIVE_FILE_OK, pair_result->second.error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/file5"),
pair_result->second.path);
ASSERT_TRUE(pair_result->second.proto.get());
EXPECT_EQ("file5", pair_result->second.proto->base_name());
// Confirm that the first non existent file is not found.
pair_result.reset();
resource_metadata_->GetEntryInfoPairByPaths(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/non_existent"),
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file5"),
google_apis::test_util::CreateCopyResultCallback(&pair_result));
google_apis::test_util::RunBlockingPoolTask();
// The first entry should not be found.
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, pair_result->first.error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/non_existent"),
pair_result->first.path);
ASSERT_FALSE(pair_result->first.proto.get());
// The second entry should not be found, because the first one failed.
EXPECT_EQ(DRIVE_FILE_ERROR_FAILED, pair_result->second.error);
EXPECT_EQ(base::FilePath(), pair_result->second.path);
ASSERT_FALSE(pair_result->second.proto.get());
// Confirm that the second non existent file is not found.
pair_result.reset();
resource_metadata_->GetEntryInfoPairByPaths(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file4"),
base::FilePath::FromUTF8Unsafe("drive/root/dir1/non_existent"),
google_apis::test_util::CreateCopyResultCallback(&pair_result));
google_apis::test_util::RunBlockingPoolTask();
// The first entry should be found.
EXPECT_EQ(DRIVE_FILE_OK, pair_result->first.error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/file4"),
pair_result->first.path);
ASSERT_TRUE(pair_result->first.proto.get());
EXPECT_EQ("file4", pair_result->first.proto->base_name());
// The second entry should not be found.
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, pair_result->second.error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/non_existent"),
pair_result->second.path);
ASSERT_FALSE(pair_result->second.proto.get());
}
TEST_F(DriveResourceMetadataTest, RemoveEntry) {
// Make sure file9 is found.
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
base::FilePath drive_file_path;
const std::string file9_resource_id = "resource_id:file9";
scoped_ptr<DriveEntryProto> entry_proto;
resource_metadata_->GetEntryInfoByResourceId(
file9_resource_id,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3/file9"),
drive_file_path);
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("file9", entry_proto->base_name());
// Remove file9 using RemoveEntry.
resource_metadata_->RemoveEntry(
file9_resource_id,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3"),
drive_file_path);
// file9 should no longer exist.
resource_metadata_->GetEntryInfoByResourceId(
file9_resource_id,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_FALSE(entry_proto.get());
// Look for dir3.
const std::string dir3_resource_id = "resource_id:dir3";
resource_metadata_->GetEntryInfoByResourceId(
dir3_resource_id,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3"),
drive_file_path);
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("dir3", entry_proto->base_name());
// Remove dir3 using RemoveEntry.
resource_metadata_->RemoveEntry(
dir3_resource_id,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1"), drive_file_path);
// dir3 should no longer exist.
resource_metadata_->GetEntryInfoByResourceId(
dir3_resource_id,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_FALSE(entry_proto.get());
// Remove unknown resource_id using RemoveEntry.
resource_metadata_->RemoveEntry(
"foo",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
// Try removing root. This should fail.
resource_metadata_->RemoveEntry(
util::kDriveGrandRootSpecialResourceId,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_ACCESS_DENIED, error);
}
TEST_F(DriveResourceMetadataTest, MoveEntryToDirectory) {
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
base::FilePath drive_file_path;
scoped_ptr<DriveEntryProto> entry_proto;
// Move file8 to drive/dir1.
resource_metadata_->MoveEntryToDirectory(
base::FilePath::FromUTF8Unsafe("drive/root/dir2/file8"),
base::FilePath::FromUTF8Unsafe("drive/root/dir1"),
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/file8"),
drive_file_path);
// Look up the entry by its resource id and make sure it really moved.
resource_metadata_->GetEntryInfoByResourceId(
"resource_id:file8",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/file8"),
drive_file_path);
// Move non-existent file to drive/dir1. This should fail.
resource_metadata_->MoveEntryToDirectory(
base::FilePath::FromUTF8Unsafe("drive/root/dir2/file8"),
base::FilePath::FromUTF8Unsafe("drive/root/dir1"),
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_EQ(base::FilePath(), drive_file_path);
// Move existing file to non-existent directory. This should fail.
resource_metadata_->MoveEntryToDirectory(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file8"),
base::FilePath::FromUTF8Unsafe("drive/root/dir4"),
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_EQ(base::FilePath(), drive_file_path);
// Move existing file to existing file (non-directory). This should fail.
resource_metadata_->MoveEntryToDirectory(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file8"),
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file4"),
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_A_DIRECTORY, error);
EXPECT_EQ(base::FilePath(), drive_file_path);
// Move the file to root.
resource_metadata_->MoveEntryToDirectory(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/file8"),
base::FilePath::FromUTF8Unsafe("drive/root"),
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/file8"),
drive_file_path);
// Move the file from root.
resource_metadata_->MoveEntryToDirectory(
base::FilePath::FromUTF8Unsafe("drive/root/file8"),
base::FilePath::FromUTF8Unsafe("drive/root/dir2"),
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir2/file8"),
drive_file_path);
// Make sure file is still ok.
resource_metadata_->GetEntryInfoByResourceId(
"resource_id:file8",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir2/file8"),
drive_file_path);
}
TEST_F(DriveResourceMetadataTest, RenameEntry) {
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
base::FilePath drive_file_path;
scoped_ptr<DriveEntryProto> entry_proto;
// Rename file8 to file11.
resource_metadata_->RenameEntry(
base::FilePath::FromUTF8Unsafe("drive/root/dir2/file8"),
"file11",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir2/file11"),
drive_file_path);
// Lookup the file by resource id to make sure the file actually got renamed.
resource_metadata_->GetEntryInfoByResourceId(
"resource_id:file8",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir2/file11"),
drive_file_path);
// Rename to file7 to force a duplicate name.
resource_metadata_->RenameEntry(
base::FilePath::FromUTF8Unsafe("drive/root/dir2/file11"),
"file7",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir2/file7 (2)"),
drive_file_path);
// Rename to same name. This should fail.
resource_metadata_->RenameEntry(
base::FilePath::FromUTF8Unsafe("drive/root/dir2/file7 (2)"),
"file7 (2)",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_EXISTS, error);
EXPECT_EQ(base::FilePath(), drive_file_path);
// Rename non-existent.
resource_metadata_->RenameEntry(
base::FilePath::FromUTF8Unsafe("drive/root/dir2/file11"),
"file11",
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
EXPECT_EQ(base::FilePath(), drive_file_path);
}
TEST_F(DriveResourceMetadataTest, RefreshEntry) {
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
base::FilePath drive_file_path;
scoped_ptr<DriveEntryProto> entry_proto;
// Get file9.
entry_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3/file9"));
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("file9", entry_proto->base_name());
ASSERT_TRUE(!entry_proto->file_info().is_directory());
EXPECT_EQ("md5:file9", entry_proto->file_specific_info().file_md5());
// Rename it and change the file size.
DriveEntryProto file_entry_proto(*entry_proto);
const std::string updated_md5("md5:updated");
file_entry_proto.mutable_file_specific_info()->set_file_md5(updated_md5);
file_entry_proto.set_title("file100");
entry_proto.reset();
resource_metadata_->RefreshEntry(
file_entry_proto,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3/file100"),
drive_file_path);
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("file100", entry_proto->base_name());
ASSERT_TRUE(!entry_proto->file_info().is_directory());
EXPECT_EQ(updated_md5, entry_proto->file_specific_info().file_md5());
// Make sure we get the same thing from GetEntryInfoByPath.
entry_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3/file100"));
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("file100", entry_proto->base_name());
ASSERT_TRUE(!entry_proto->file_info().is_directory());
EXPECT_EQ(updated_md5, entry_proto->file_specific_info().file_md5());
// Get dir2.
entry_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir2"));
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("dir2", entry_proto->base_name());
ASSERT_TRUE(entry_proto->file_info().is_directory());
// Change the name to dir100 and change the parent to drive/dir1/dir3.
DriveEntryProto dir_entry_proto(*entry_proto);
dir_entry_proto.set_title("dir100");
dir_entry_proto.set_parent_resource_id("resource_id:dir3");
entry_proto.reset();
resource_metadata_->RefreshEntry(
dir_entry_proto,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3/dir100"),
drive_file_path);
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("dir100", entry_proto->base_name());
EXPECT_TRUE(entry_proto->file_info().is_directory());
EXPECT_EQ("resource_id:dir2", entry_proto->resource_id());
// Make sure the children have moved over. Test file6.
entry_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3/dir100/file6"));
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("file6", entry_proto->base_name());
// Make sure dir2 no longer exists.
entry_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir2"));
EXPECT_FALSE(entry_proto.get());
// Cannot refresh root.
dir_entry_proto.Clear();
dir_entry_proto.set_resource_id(util::kDriveGrandRootSpecialResourceId);
dir_entry_proto.set_title("new-root-name");
dir_entry_proto.set_parent_resource_id("resource_id:dir1");
entry_proto.reset();
resource_metadata_->RefreshEntry(
dir_entry_proto,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path, &entry_proto));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_INVALID_OPERATION, error);
}
TEST_F(DriveResourceMetadataTest, RefreshDirectory_EmtpyMap) {
base::FilePath kDirectoryPath(FILE_PATH_LITERAL("drive/root/dir1"));
const int64 kNewChangestamp = kTestChangestamp + 1;
// Read the directory.
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
scoped_ptr<DriveEntryProtoVector> entries;
entries = ReadDirectoryByPathSync(base::FilePath(kDirectoryPath));
ASSERT_TRUE(entries.get());
// "file4", "file5", "dir3" should exist in drive/dir1.
ASSERT_EQ(3U, entries->size());
std::vector<std::string> base_names = GetSortedBaseNames(*entries);
EXPECT_EQ("dir3", base_names[0]);
EXPECT_EQ("file4", base_names[1]);
EXPECT_EQ("file5", base_names[2]);
// Get the directory.
scoped_ptr<DriveEntryProto> dir1_proto;
dir1_proto = GetEntryInfoByPathSync(kDirectoryPath);
ASSERT_TRUE(dir1_proto.get());
// The changestamp should be initially kTestChangestamp.
EXPECT_EQ(kTestChangestamp,
dir1_proto->directory_specific_info().changestamp());
// Update the directory with an empty map.
base::FilePath file_path;
DriveEntryProtoMap entry_map;
resource_metadata_->RefreshDirectory(
DirectoryFetchInfo(dir1_proto->resource_id(), kNewChangestamp),
entry_map,
google_apis::test_util::CreateCopyResultCallback(&error, &file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(kDirectoryPath, file_path);
// Get the directory again.
dir1_proto = GetEntryInfoByPathSync(kDirectoryPath);
ASSERT_TRUE(dir1_proto.get());
// The new changestamp should be set.
EXPECT_EQ(kNewChangestamp,
dir1_proto->directory_specific_info().changestamp());
// Read the directory again.
entries = ReadDirectoryByPathSync(base::FilePath(kDirectoryPath));
ASSERT_TRUE(entries.get());
// All entries ("file4", "file5", "dir3") should be gone now, as
// RefreshDirectory() was called with an empty map.
ASSERT_TRUE(entries->empty());
}
TEST_F(DriveResourceMetadataTest, RefreshDirectory_NonEmptyMap) {
base::FilePath kDirectoryPath(FILE_PATH_LITERAL("drive/root/dir1"));
const int64 kNewChangestamp = kTestChangestamp + 1;
// Read the directory.
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
scoped_ptr<DriveEntryProtoVector> entries;
entries = ReadDirectoryByPathSync(kDirectoryPath);
ASSERT_TRUE(entries.get());
// "file4", "file5", "dir3" should exist in drive/dir1.
ASSERT_EQ(3U, entries->size());
std::vector<std::string> base_names = GetSortedBaseNames(*entries);
EXPECT_EQ("dir3", base_names[0]);
EXPECT_EQ("file4", base_names[1]);
EXPECT_EQ("file5", base_names[2]);
// Get the directory dir1.
scoped_ptr<DriveEntryProto> dir1_proto;
dir1_proto = GetEntryInfoByPathSync(kDirectoryPath);
ASSERT_TRUE(dir1_proto.get());
// The changestamp should be initially kTestChangestamp.
EXPECT_EQ(kTestChangestamp,
dir1_proto->directory_specific_info().changestamp());
// Get the directory dir2 (existing non-child directory).
// This directory will be moved to "drive/dir1/dir2".
scoped_ptr<DriveEntryProto> dir2_proto;
dir2_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir2"));
ASSERT_TRUE(dir2_proto.get());
EXPECT_EQ(kTestChangestamp,
dir2_proto->directory_specific_info().changestamp());
// Change the parent resource ID, as dir2 will be moved to "drive/dir1/dir2".
dir2_proto->set_parent_resource_id(dir1_proto->resource_id());
// Get the directory dir3 (existing child directory).
// This directory will remain as "drive/dir1/dir3".
scoped_ptr<DriveEntryProto> dir3_proto;
dir3_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3"));
ASSERT_TRUE(dir3_proto.get());
EXPECT_EQ(kTestChangestamp,
dir3_proto->directory_specific_info().changestamp());
// Create a map.
DriveEntryProtoMap entry_map;
// Add a new file to the map.
DriveEntryProto new_file;
new_file.set_title("new_file");
new_file.set_resource_id("new_file_id");
new_file.set_parent_resource_id(dir1_proto->resource_id());
entry_map["new_file_id"] = new_file;
// Add a new directory to the map.
DriveEntryProto new_directory;
new_directory.set_title("new_directory");
new_directory.set_resource_id("new_directory_id");
new_directory.set_parent_resource_id(dir1_proto->resource_id());
new_directory.mutable_file_info()->set_is_directory(true);
entry_map["new_directory_id"] = new_directory;
// Add dir2 and dir3 as well.
entry_map[dir2_proto->resource_id()] = *dir2_proto;
entry_map[dir3_proto->resource_id()] = *dir3_proto;
// Update the directory with the map.
base::FilePath file_path;
resource_metadata_->RefreshDirectory(
DirectoryFetchInfo(dir1_proto->resource_id(), kNewChangestamp),
entry_map,
google_apis::test_util::CreateCopyResultCallback(&error, &file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(kDirectoryPath, file_path);
// Get the directory again.
dir1_proto = GetEntryInfoByPathSync(kDirectoryPath);
ASSERT_TRUE(dir1_proto.get());
// The new changestamp should be set.
EXPECT_EQ(kNewChangestamp,
dir1_proto->directory_specific_info().changestamp());
// Read the directory again.
entries = ReadDirectoryByPathSync(kDirectoryPath);
ASSERT_TRUE(entries.get());
// "file4", "file5", should be gone now. "dir3" should remain. "new_file"
// "new_directory", "dir2" should now be added.
ASSERT_EQ(4U, entries->size());
base_names = GetSortedBaseNames(*entries);
EXPECT_EQ("dir2", base_names[0]);
EXPECT_EQ("dir3", base_names[1]);
EXPECT_EQ("new_directory", base_names[2]);
EXPECT_EQ("new_file", base_names[3]);
// Get the new directory.
scoped_ptr<DriveEntryProto> new_directory_proto;
new_directory_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/new_directory"));
ASSERT_TRUE(new_directory_proto.get());
// The changestamp should be 0 for a new directory.
EXPECT_EQ(0, new_directory_proto->directory_specific_info().changestamp());
// Get the directory dir3 (existing child directory) again.
dir3_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3"));
ASSERT_TRUE(dir3_proto.get());
// The changestamp should not be changed.
EXPECT_EQ(kTestChangestamp,
dir3_proto->directory_specific_info().changestamp());
// Read the directory dir3. The contents should remain.
// See the comment at Init() for the contents of the dir3.
entries = ReadDirectoryByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3"));
ASSERT_TRUE(entries.get());
ASSERT_EQ(2U, entries->size());
// Get the directory dir2 (existing non-child directory) again using the
// old path. This should fail, as dir2 is now moved to drive/dir1/dir2.
dir2_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir2"));
ASSERT_FALSE(dir2_proto.get());
// Get the directory dir2 (existing non-child directory) again using the
// new path. This should succeed.
dir2_proto = GetEntryInfoByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir2"));
ASSERT_TRUE(dir2_proto.get());
// The changestamp should not be changed.
EXPECT_EQ(kTestChangestamp,
dir2_proto->directory_specific_info().changestamp());
// Read the directory dir2. The contents should remain.
// See the comment at Init() for the contents of the dir2.
entries = ReadDirectoryByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir2"));
ASSERT_TRUE(entries.get());
ASSERT_EQ(3U, entries->size());
}
TEST_F(DriveResourceMetadataTest, RefreshDirectory_WrongParentResourceId) {
base::FilePath kDirectoryPath(FILE_PATH_LITERAL("drive/root/dir1"));
const int64 kNewChangestamp = kTestChangestamp + 1;
// Get the directory dir1.
scoped_ptr<DriveEntryProto> dir1_proto;
dir1_proto = GetEntryInfoByPathSync(kDirectoryPath);
ASSERT_TRUE(dir1_proto.get());
// Create a map and add a new file to it.
DriveEntryProtoMap entry_map;
DriveEntryProto new_file;
new_file.set_title("new_file");
new_file.set_resource_id("new_file_id");
// Set a random parent resource ID. This entry should not be added because
// the parent resource ID does not match dir1_proto->resource_id().
new_file.set_parent_resource_id("some-random-resource-id");
entry_map["new_file_id"] = new_file;
// Update the directory with the map.
base::FilePath file_path;
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
resource_metadata_->RefreshDirectory(
DirectoryFetchInfo(dir1_proto->resource_id(), kNewChangestamp),
entry_map,
google_apis::test_util::CreateCopyResultCallback(&error, &file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(kDirectoryPath, file_path);
// Read the directory. Confirm that the new file is not added.
scoped_ptr<DriveEntryProtoVector> entries;
entries = ReadDirectoryByPathSync(kDirectoryPath);
ASSERT_TRUE(entries.get());
ASSERT_TRUE(entries->empty());
}
TEST_F(DriveResourceMetadataTest, AddEntry) {
int sequence_id = 100;
DriveEntryProto file_entry_proto = CreateDriveEntryProto(
sequence_id++, false, "resource_id:dir3");
DriveFileError error = DRIVE_FILE_ERROR_FAILED;
base::FilePath drive_file_path;
// Add to dir3.
resource_metadata_->AddEntry(
file_entry_proto,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3/file100"),
drive_file_path);
// Add a directory.
DriveEntryProto dir_entry_proto = CreateDriveEntryProto(
sequence_id++, true, "resource_id:dir1");
resource_metadata_->AddEntry(
dir_entry_proto,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_OK, error);
EXPECT_EQ(base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir101"),
drive_file_path);
// Add to an invalid parent.
DriveEntryProto file_entry_proto3 = CreateDriveEntryProto(
sequence_id++, false, "resource_id:invalid");
resource_metadata_->AddEntry(
file_entry_proto3,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
// Add an existing file.
resource_metadata_->AddEntry(
file_entry_proto,
google_apis::test_util::CreateCopyResultCallback(
&error, &drive_file_path));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(DRIVE_FILE_ERROR_EXISTS, error);
}
TEST_F(DriveResourceMetadataTest, GetChildDirectories) {
std::set<base::FilePath> child_directories;
// file9: not a directory, so no children.
resource_metadata_->GetChildDirectories(
"resource_id:file9",
google_apis::test_util::CreateCopyResultCallback(&child_directories));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_TRUE(child_directories.empty());
// dir2: no child directories.
resource_metadata_->GetChildDirectories(
"resource_id:dir2",
google_apis::test_util::CreateCopyResultCallback(&child_directories));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_TRUE(child_directories.empty());
// dir1: dir3 is the only child
resource_metadata_->GetChildDirectories(
"resource_id:dir1",
google_apis::test_util::CreateCopyResultCallback(&child_directories));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(1u, child_directories.size());
EXPECT_EQ(1u, child_directories.count(
base::FilePath::FromUTF8Unsafe("drive/root/dir1/dir3")));
// Add a few more directories to make sure deeper nesting works.
// dir2/dir100
// dir2/dir101
// dir2/dir101/dir102
// dir2/dir101/dir103
// dir2/dir101/dir104
// dir2/dir101/dir102/dir105
// dir2/dir101/dir102/dir105/dir106
// dir2/dir101/dir102/dir105/dir106/dir107
int sequence_id = 100;
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata_.get(), sequence_id++, true, "resource_id:dir2"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata_.get(), sequence_id++, true, "resource_id:dir2"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata_.get(), sequence_id++, true, "resource_id:dir101"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata_.get(), sequence_id++, true, "resource_id:dir101"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata_.get(), sequence_id++, true, "resource_id:dir101"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata_.get(), sequence_id++, true, "resource_id:dir102"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata_.get(), sequence_id++, true, "resource_id:dir105"));
ASSERT_TRUE(AddDriveEntryProto(
resource_metadata_.get(), sequence_id++, true, "resource_id:dir106"));
resource_metadata_->GetChildDirectories(
"resource_id:dir2",
google_apis::test_util::CreateCopyResultCallback(&child_directories));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(8u, child_directories.size());
EXPECT_EQ(1u, child_directories.count(base::FilePath::FromUTF8Unsafe(
"drive/root/dir2/dir101")));
EXPECT_EQ(1u, child_directories.count(base::FilePath::FromUTF8Unsafe(
"drive/root/dir2/dir101/dir104")));
EXPECT_EQ(1u, child_directories.count(base::FilePath::FromUTF8Unsafe(
"drive/root/dir2/dir101/dir102/dir105/dir106/dir107")));
}
TEST_F(DriveResourceMetadataTest, RemoveAll) {
// The grand root has "root" which is not empty.
scoped_ptr<DriveEntryProtoVector> entries;
entries = ReadDirectoryByPathSync(
base::FilePath::FromUTF8Unsafe("drive/root"));
ASSERT_TRUE(entries.get());
ASSERT_FALSE(entries->empty());
// remove all children.
resource_metadata_->RemoveAll(base::Bind(&base::DoNothing));
google_apis::test_util::RunBlockingPoolTask();
base::FilePath drive_file_path;
scoped_ptr<DriveEntryProto> entry_proto;
// root should continue to exist.
entry_proto = GetEntryInfoByPathSync(base::FilePath::FromUTF8Unsafe("drive"));
ASSERT_TRUE(entry_proto.get());
EXPECT_EQ("drive", entry_proto->base_name());
ASSERT_TRUE(entry_proto->file_info().is_directory());
EXPECT_EQ(util::kDriveGrandRootSpecialResourceId, entry_proto->resource_id());
// There is "other", which are both empty.
entries = ReadDirectoryByPathSync(base::FilePath::FromUTF8Unsafe("drive"));
ASSERT_TRUE(entries.get());
EXPECT_EQ(1U, entries->size());
scoped_ptr<DriveEntryProtoVector> entries_in_other =
ReadDirectoryByPathSync(base::FilePath::FromUTF8Unsafe("drive/other"));
ASSERT_TRUE(entries_in_other.get());
EXPECT_TRUE(entries_in_other->empty());
}
TEST_F(DriveResourceMetadataTest, IterateEntries) {
int count = 0;
bool completed = false;
resource_metadata_->IterateEntries(
base::Bind(&CountFile, &count),
base::Bind(google_apis::test_util::CreateCopyResultCallback(&completed),
true));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(7, count);
EXPECT_TRUE(completed);
}
} // namespace drive
|