Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: chrome/browser/chromeos/drive/drive_resource_metadata_unittest.cc

Issue 11227020: Set root resource ID upon full feed update. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: make root_resource_id() work when root_ is not initialized. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h" 5 #include "chrome/browser/chromeos/drive/drive_resource_metadata.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/sequenced_task_runner.h" 12 #include "base/sequenced_task_runner.h"
13 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
14 #include "base/threading/sequenced_worker_pool.h" 14 #include "base/threading/sequenced_worker_pool.h"
15 #include "base/message_loop.h" 15 #include "base/message_loop.h"
16 #include "chrome/browser/chromeos/drive/drive.pb.h" 16 #include "chrome/browser/chromeos/drive/drive.pb.h"
17 #include "chrome/browser/chromeos/drive/drive_cache.h" 17 #include "chrome/browser/chromeos/drive/drive_cache.h"
18 #include "chrome/browser/chromeos/drive/drive_files.h" 18 #include "chrome/browser/chromeos/drive/drive_files.h"
19 #include "chrome/browser/chromeos/drive/drive_test_util.h" 19 #include "chrome/browser/chromeos/drive/drive_test_util.h"
20 #include "chrome/browser/google_apis/gdata_util.h"
20 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
21 #include "content/public/test/test_browser_thread.h" 22 #include "content/public/test/test_browser_thread.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
24 namespace drive { 25 namespace drive {
25 namespace { 26 namespace {
26 27
28 // The root directory resource ID for WAPI.
29 // TODO(haruki): Make Drive API equivalent work. http://crbug.com/157114
30 const char kTestRootDirectoryResourceId[] = "folder:fakeroot";
satorux1 2012/10/23 08:06:26 I guess we don't need this. What's wrong with usin
kochi 2012/10/23 08:21:21 The idea is that we should not assume that the roo
31
27 // See drive.proto for the difference between the two URLs. 32 // See drive.proto for the difference between the two URLs.
28 const char kResumableEditMediaUrl[] = "http://resumable-edit-media/"; 33 const char kResumableEditMediaUrl[] = "http://resumable-edit-media/";
29 const char kResumableCreateMediaUrl[] = "http://resumable-create-media/"; 34 const char kResumableCreateMediaUrl[] = "http://resumable-create-media/";
30 35
31 // Callback for DriveResourceMetadata::InitFromDB. 36 // Callback for DriveResourceMetadata::InitFromDB.
32 void InitFromDBCallback(DriveFileError expected_error, 37 void InitFromDBCallback(DriveFileError expected_error,
33 DriveFileError actual_error) { 38 DriveFileError actual_error) {
34 EXPECT_EQ(expected_error, actual_error); 39 EXPECT_EQ(expected_error, actual_error);
35 } 40 }
36 41
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 MessageLoopForUI message_loop_; 82 MessageLoopForUI message_loop_;
78 content::TestBrowserThread ui_thread_; 83 content::TestBrowserThread ui_thread_;
79 }; 84 };
80 85
81 DriveResourceMetadataTest::DriveResourceMetadataTest() 86 DriveResourceMetadataTest::DriveResourceMetadataTest()
82 : ui_thread_(content::BrowserThread::UI, &message_loop_) { 87 : ui_thread_(content::BrowserThread::UI, &message_loop_) {
83 Init(); 88 Init();
84 } 89 }
85 90
86 void DriveResourceMetadataTest::Init() { 91 void DriveResourceMetadataTest::Init() {
92 resource_metadata_.InitializeRootEntry(kTestRootDirectoryResourceId);
93
87 int sequence_id = 1; 94 int sequence_id = 1;
88 DriveDirectory* dir1 = AddDirectory(resource_metadata_.root(), sequence_id++); 95 DriveDirectory* dir1 = AddDirectory(resource_metadata_.root(), sequence_id++);
89 DriveDirectory* dir2 = AddDirectory(resource_metadata_.root(), sequence_id++); 96 DriveDirectory* dir2 = AddDirectory(resource_metadata_.root(), sequence_id++);
90 DriveDirectory* dir3 = AddDirectory(dir1, sequence_id++); 97 DriveDirectory* dir3 = AddDirectory(dir1, sequence_id++);
91 98
92 AddFile(dir1, sequence_id++); 99 AddFile(dir1, sequence_id++);
93 AddFile(dir1, sequence_id++); 100 AddFile(dir1, sequence_id++);
94 101
95 AddFile(dir2, sequence_id++); 102 AddFile(dir2, sequence_id++);
96 AddFile(dir2, sequence_id++); 103 AddFile(dir2, sequence_id++);
(...skipping 29 matching lines...) Expand all
126 133
127 return file.release(); 134 return file.release();
128 } 135 }
129 136
130 TEST_F(DriveResourceMetadataTest, VersionCheck) { 137 TEST_F(DriveResourceMetadataTest, VersionCheck) {
131 // Set up the root directory. 138 // Set up the root directory.
132 DriveRootDirectoryProto proto; 139 DriveRootDirectoryProto proto;
133 DriveEntryProto* mutable_entry = 140 DriveEntryProto* mutable_entry =
134 proto.mutable_drive_directory()->mutable_drive_entry(); 141 proto.mutable_drive_directory()->mutable_drive_entry();
135 mutable_entry->mutable_file_info()->set_is_directory(true); 142 mutable_entry->mutable_file_info()->set_is_directory(true);
136 mutable_entry->set_resource_id(kDriveRootDirectoryResourceId); 143 mutable_entry->set_resource_id(kTestRootDirectoryResourceId);
137 mutable_entry->set_upload_url(kResumableCreateMediaUrl); 144 mutable_entry->set_upload_url(kResumableCreateMediaUrl);
138 mutable_entry->set_title("drive"); 145 mutable_entry->set_title("drive");
139 146
140 DriveResourceMetadata resource_metadata; 147 DriveResourceMetadata resource_metadata;
148 resource_metadata.InitializeRootEntry(kTestRootDirectoryResourceId);
141 149
142 std::string serialized_proto; 150 std::string serialized_proto;
143 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); 151 ASSERT_TRUE(proto.SerializeToString(&serialized_proto));
144 // This should fail as the version is emtpy. 152 // This should fail as the version is emtpy.
145 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto)); 153 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto));
146 154
147 // Set an older version, and serialize. 155 // Set an older version, and serialize.
148 proto.set_version(kProtoVersion - 1); 156 proto.set_version(kProtoVersion - 1);
149 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); 157 ASSERT_TRUE(proto.SerializeToString(&serialized_proto));
150 // This should fail as the version is older. 158 // This should fail as the version is older.
151 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto)); 159 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto));
152 160
153 // Set the current version, and serialize. 161 // Set the current version, and serialize.
154 proto.set_version(kProtoVersion); 162 proto.set_version(kProtoVersion);
155 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); 163 ASSERT_TRUE(proto.SerializeToString(&serialized_proto));
156 // This should succeed as the version matches the current number. 164 // This should succeed as the version matches the current number.
157 ASSERT_TRUE(resource_metadata.ParseFromString(serialized_proto)); 165 ASSERT_TRUE(resource_metadata.ParseFromString(serialized_proto));
158 166
159 // Set a newer version, and serialize. 167 // Set a newer version, and serialize.
160 proto.set_version(kProtoVersion + 1); 168 proto.set_version(kProtoVersion + 1);
161 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); 169 ASSERT_TRUE(proto.SerializeToString(&serialized_proto));
162 // This should fail as the version is newer. 170 // This should fail as the version is newer.
163 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto)); 171 ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto));
164 } 172 }
165 173
166 TEST_F(DriveResourceMetadataTest, GetEntryByResourceId_RootDirectory) { 174 TEST_F(DriveResourceMetadataTest, GetEntryByResourceId_RootDirectory) {
167 DriveResourceMetadata resource_metadata; 175 DriveResourceMetadata resource_metadata;
176 resource_metadata.InitializeRootEntry(kTestRootDirectoryResourceId);
177 EXPECT_EQ(kTestRootDirectoryResourceId,
178 resource_metadata.root_resource_id());
168 // Look up the root directory by its resource ID. 179 // Look up the root directory by its resource ID.
169 DriveEntry* entry = resource_metadata.GetEntryByResourceId( 180 DriveEntry* entry = resource_metadata.GetEntryByResourceId(
170 kDriveRootDirectoryResourceId); 181 resource_metadata.root_resource_id());
171 ASSERT_TRUE(entry); 182 ASSERT_TRUE(entry);
172 EXPECT_EQ(kDriveRootDirectoryResourceId, entry->resource_id()); 183 EXPECT_EQ(kTestRootDirectoryResourceId, entry->resource_id());
173 } 184 }
174 185
175 TEST_F(DriveResourceMetadataTest, GetEntryInfoByResourceId) { 186 TEST_F(DriveResourceMetadataTest, GetEntryInfoByResourceId) {
176 // Confirm that an existing file is found. 187 // Confirm that an existing file is found.
177 DriveFileError error = DRIVE_FILE_ERROR_FAILED; 188 DriveFileError error = DRIVE_FILE_ERROR_FAILED;
178 FilePath drive_file_path; 189 FilePath drive_file_path;
179 scoped_ptr<DriveEntryProto> entry_proto; 190 scoped_ptr<DriveEntryProto> entry_proto;
180 resource_metadata_.GetEntryInfoByResourceId( 191 resource_metadata_.GetEntryInfoByResourceId(
181 "file_resource_id:file4", 192 "file_resource_id:file4",
182 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, 193 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback,
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 resource_metadata_.SaveToDB(); 364 resource_metadata_.SaveToDB();
354 google_apis::test_util::RunBlockingPoolTask(); 365 google_apis::test_util::RunBlockingPoolTask();
355 366
356 // InitFromDB should fail with DRIVE_FILE_ERROR_IN_USE. 367 // InitFromDB should fail with DRIVE_FILE_ERROR_IN_USE.
357 resource_metadata_.InitFromDB(db_path, blocking_task_runner, 368 resource_metadata_.InitFromDB(db_path, blocking_task_runner,
358 base::Bind(&InitFromDBCallback, DRIVE_FILE_ERROR_IN_USE)); 369 base::Bind(&InitFromDBCallback, DRIVE_FILE_ERROR_IN_USE));
359 google_apis::test_util::RunBlockingPoolTask(); 370 google_apis::test_util::RunBlockingPoolTask();
360 371
361 // InitFromDB should succeed. 372 // InitFromDB should succeed.
362 DriveResourceMetadata test_resource_metadata; 373 DriveResourceMetadata test_resource_metadata;
374 test_resource_metadata.InitializeRootEntry(kTestRootDirectoryResourceId);
363 test_resource_metadata.InitFromDB(db_path, blocking_task_runner, 375 test_resource_metadata.InitFromDB(db_path, blocking_task_runner,
364 base::Bind(&InitFromDBCallback, DRIVE_FILE_OK)); 376 base::Bind(&InitFromDBCallback, DRIVE_FILE_OK));
365 google_apis::test_util::RunBlockingPoolTask(); 377 google_apis::test_util::RunBlockingPoolTask();
366 378
367 // Verify by checking for drive/dir2, which should have 3 children. 379 // Verify by checking for drive/dir2, which should have 3 children.
368 DriveFileError error = DRIVE_FILE_ERROR_FAILED; 380 DriveFileError error = DRIVE_FILE_ERROR_FAILED;
369 scoped_ptr<DriveEntryProtoVector> entries; 381 scoped_ptr<DriveEntryProtoVector> entries;
370 test_resource_metadata.ReadDirectoryByPath( 382 test_resource_metadata.ReadDirectoryByPath(
371 FilePath::FromUTF8Unsafe("drive/dir2"), 383 FilePath::FromUTF8Unsafe("drive/dir2"),
372 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, 384 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback,
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 // Remove unknown resource_id using RemoveEntryFromParent. 456 // Remove unknown resource_id using RemoveEntryFromParent.
445 resource_metadata_.RemoveEntryFromParent( 457 resource_metadata_.RemoveEntryFromParent(
446 "foo", 458 "foo",
447 base::Bind(&test_util::CopyResultsFromFileMoveCallback, 459 base::Bind(&test_util::CopyResultsFromFileMoveCallback,
448 &error, &drive_file_path)); 460 &error, &drive_file_path));
449 google_apis::test_util::RunBlockingPoolTask(); 461 google_apis::test_util::RunBlockingPoolTask();
450 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); 462 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
451 463
452 // Try removing root. This should fail. 464 // Try removing root. This should fail.
453 resource_metadata_.RemoveEntryFromParent( 465 resource_metadata_.RemoveEntryFromParent(
454 kDriveRootDirectoryResourceId, 466 resource_metadata_.root_resource_id(),
455 base::Bind(&test_util::CopyResultsFromFileMoveCallback, 467 base::Bind(&test_util::CopyResultsFromFileMoveCallback,
456 &error, &drive_file_path)); 468 &error, &drive_file_path));
457 google_apis::test_util::RunBlockingPoolTask(); 469 google_apis::test_util::RunBlockingPoolTask();
458 EXPECT_EQ(DRIVE_FILE_ERROR_ACCESS_DENIED, error); 470 EXPECT_EQ(DRIVE_FILE_ERROR_ACCESS_DENIED, error);
459 } 471 }
460 472
461 TEST_F(DriveResourceMetadataTest, MoveEntryToDirectory) { 473 TEST_F(DriveResourceMetadataTest, MoveEntryToDirectory) {
462 DriveFileError error = DRIVE_FILE_ERROR_FAILED; 474 DriveFileError error = DRIVE_FILE_ERROR_FAILED;
463 FilePath drive_file_path; 475 FilePath drive_file_path;
464 scoped_ptr<DriveEntryProto> entry_proto; 476 scoped_ptr<DriveEntryProto> entry_proto;
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 FilePath::FromUTF8Unsafe("drive/dir2/file11"), 603 FilePath::FromUTF8Unsafe("drive/dir2/file11"),
592 "file11", 604 "file11",
593 base::Bind(&test_util::CopyResultsFromFileMoveCallback, 605 base::Bind(&test_util::CopyResultsFromFileMoveCallback,
594 &error, &drive_file_path)); 606 &error, &drive_file_path));
595 google_apis::test_util::RunBlockingPoolTask(); 607 google_apis::test_util::RunBlockingPoolTask();
596 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); 608 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error);
597 EXPECT_EQ(FilePath(), drive_file_path); 609 EXPECT_EQ(FilePath(), drive_file_path);
598 } 610 }
599 611
600 } // namespace drive 612 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698