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

Side by Side Diff: chrome/browser/google_apis/fake_drive_service_unittest.cc

Issue 11825021: google_apis: Implement more functions in FakeDriveService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months 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/google_apis/fake_drive_service.h" 5 #include "chrome/browser/google_apis/fake_drive_service.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/google_apis/gdata_wapi_parser.h" 9 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
9 #include "chrome/browser/google_apis/test_util.h" 10 #include "chrome/browser/google_apis/test_util.h"
10 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
11 #include "content/public/test/test_browser_thread.h" 12 #include "content/public/test/test_browser_thread.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace google_apis { 15 namespace google_apis {
15 16
16 class FakeDriveServiceTest : public testing::Test { 17 class FakeDriveServiceTest : public testing::Test {
17 protected: 18 protected:
18 FakeDriveServiceTest() 19 FakeDriveServiceTest()
19 : ui_thread_(content::BrowserThread::UI, &message_loop_) { 20 : ui_thread_(content::BrowserThread::UI, &message_loop_) {
20 } 21 }
21 22
22 // Returns true if the resource identified by |resource_id| exists. 23 // Returns the resource entry that matches |resource_id|.
23 bool Exists(const std::string& resource_id) { 24 scoped_ptr<ResourceEntry> FindEntry(const std::string& resource_id) {
24 GDataErrorCode error = GDATA_OTHER_ERROR; 25 GDataErrorCode error = GDATA_OTHER_ERROR;
25 scoped_ptr<ResourceEntry> resource_entry; 26 scoped_ptr<ResourceEntry> resource_entry;
26 fake_service_.GetResourceEntry( 27 fake_service_.GetResourceEntry(
27 resource_id, 28 resource_id,
28 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback, 29 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
29 &error, 30 &error,
30 &resource_entry)); 31 &resource_entry));
31 message_loop_.RunUntilIdle(); 32 message_loop_.RunUntilIdle();
32 return error == HTTP_SUCCESS; 33 return resource_entry.Pass();
34 }
35
36 // Returns true if the resource identified by |resource_id| exists.
37 bool Exists(const std::string& resource_id) {
38 scoped_ptr<ResourceEntry> resource_entry = FindEntry(resource_id);
39 return resource_entry;
33 } 40 }
34 41
35 MessageLoopForUI message_loop_; 42 MessageLoopForUI message_loop_;
36 content::TestBrowserThread ui_thread_; 43 content::TestBrowserThread ui_thread_;
37 FakeDriveService fake_service_; 44 FakeDriveService fake_service_;
38 }; 45 };
39 46
40 TEST_F(FakeDriveServiceTest, GetResourceList) { 47 TEST_F(FakeDriveServiceTest, GetResourceList) {
41 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json")); 48 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
42 49
(...skipping 28 matching lines...) Expand all
71 &account_metadata)); 78 &account_metadata));
72 message_loop_.RunUntilIdle(); 79 message_loop_.RunUntilIdle();
73 80
74 EXPECT_EQ(HTTP_SUCCESS, error); 81 EXPECT_EQ(HTTP_SUCCESS, error);
75 82
76 ASSERT_TRUE(account_metadata); 83 ASSERT_TRUE(account_metadata);
77 // Do some sanity check. 84 // Do some sanity check.
78 EXPECT_EQ(2U, account_metadata->installed_apps().size()); 85 EXPECT_EQ(2U, account_metadata->installed_apps().size());
79 } 86 }
80 87
88 TEST_F(FakeDriveServiceTest, GetApplicationInfo) {
89 ASSERT_TRUE(fake_service_.LoadApplicationInfoForDriveApi(
90 "drive/applist.json"));
91
92 GDataErrorCode error = GDATA_OTHER_ERROR;
93 scoped_ptr<base::Value> app_info;
94 fake_service_.GetApplicationInfo(
95 base::Bind(&test_util::CopyResultsFromGetDataCallback,
96 &error,
97 &app_info));
98 message_loop_.RunUntilIdle();
99
100 EXPECT_EQ(HTTP_SUCCESS, error);
101
102 ASSERT_TRUE(app_info);
103 }
104
81 TEST_F(FakeDriveServiceTest, GetResourceEntry_ExistingFile) { 105 TEST_F(FakeDriveServiceTest, GetResourceEntry_ExistingFile) {
82 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json")); 106 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
83 107
84 const std::string kResourceId = "file:2_file_resource_id"; 108 const std::string kResourceId = "file:2_file_resource_id";
85 GDataErrorCode error = GDATA_OTHER_ERROR; 109 GDataErrorCode error = GDATA_OTHER_ERROR;
86 scoped_ptr<ResourceEntry> resource_entry; 110 scoped_ptr<ResourceEntry> resource_entry;
87 fake_service_.GetResourceEntry( 111 fake_service_.GetResourceEntry(
88 kResourceId, 112 kResourceId,
89 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback, 113 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
90 &error, 114 &error,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 GDataErrorCode error = GDATA_OTHER_ERROR; 162 GDataErrorCode error = GDATA_OTHER_ERROR;
139 fake_service_.DeleteResource( 163 fake_service_.DeleteResource(
140 GURL("https://file1_link_self/file:nonexisting_resource_id"), 164 GURL("https://file1_link_self/file:nonexisting_resource_id"),
141 base::Bind(&test_util::CopyResultsFromEntryActionCallback, 165 base::Bind(&test_util::CopyResultsFromEntryActionCallback,
142 &error)); 166 &error));
143 message_loop_.RunUntilIdle(); 167 message_loop_.RunUntilIdle();
144 168
145 EXPECT_EQ(HTTP_NOT_FOUND, error); 169 EXPECT_EQ(HTTP_NOT_FOUND, error);
146 } 170 }
147 171
172 TEST_F(FakeDriveServiceTest, CopyHostedDocument_ExistingHostedDocument) {
173 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
174
175 const std::string kResourceId = "document:5_document_resource_id";
176 GDataErrorCode error = GDATA_OTHER_ERROR;
177 scoped_ptr<ResourceEntry> resource_entry;
178 fake_service_.CopyHostedDocument(
179 kResourceId,
180 FILE_PATH_LITERAL("new name"),
181 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
182 &error,
183 &resource_entry));
184 message_loop_.RunUntilIdle();
185
186 EXPECT_EQ(HTTP_SUCCESS, error);
187 ASSERT_TRUE(resource_entry);
188 // The copied entry should have the new resource ID and the title.
189 EXPECT_EQ(kResourceId + "_copied", resource_entry->resource_id());
190 EXPECT_EQ("new name", UTF16ToUTF8(resource_entry->title()));
191 }
192
193 TEST_F(FakeDriveServiceTest, CopyHostedDocument_NonexistingHostedDocument) {
194 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
195
196 const std::string kResourceId = "document:nonexisting_resource_id";
197 GDataErrorCode error = GDATA_OTHER_ERROR;
198 scoped_ptr<ResourceEntry> resource_entry;
199 fake_service_.CopyHostedDocument(
200 kResourceId,
201 FILE_PATH_LITERAL("new name"),
202 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
203 &error,
204 &resource_entry));
205 message_loop_.RunUntilIdle();
206
207 EXPECT_EQ(HTTP_NOT_FOUND, error);
208 EXPECT_FALSE(resource_entry);
209 }
210
211 TEST_F(FakeDriveServiceTest, CopyHostedDocument_ExistingRegularFile) {
212 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
213
214 const std::string kResourceId = "file:2_file_resource_id";
215 GDataErrorCode error = GDATA_OTHER_ERROR;
216 scoped_ptr<ResourceEntry> resource_entry;
217 fake_service_.CopyHostedDocument(
218 kResourceId,
219 FILE_PATH_LITERAL("new name"),
220 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
221 &error,
222 &resource_entry));
223 message_loop_.RunUntilIdle();
224
225 // The copy should fail as this is not a hosted document.
226 EXPECT_EQ(HTTP_NOT_FOUND, error);
227 EXPECT_FALSE(resource_entry);
228 }
229
230 TEST_F(FakeDriveServiceTest, RenameResource_ExistingFile) {
231 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
232
233 const std::string kResourceId = "file:2_file_resource_id";
234 const GURL kEditUrl("https://file1_link_self/file:2_file_resource_id");
235
236 GDataErrorCode error = GDATA_OTHER_ERROR;
237 fake_service_.RenameResource(
238 kEditUrl,
239 FILE_PATH_LITERAL("new name"),
240 base::Bind(&test_util::CopyResultsFromEntryActionCallback,
241 &error));
242 message_loop_.RunUntilIdle();
243
244 EXPECT_EQ(HTTP_SUCCESS, error);
245
246 scoped_ptr<ResourceEntry> resource_entry = FindEntry(kResourceId);
247 ASSERT_TRUE(resource_entry);
248 EXPECT_EQ("new name", UTF16ToUTF8(resource_entry->title()));
249 }
250
251 TEST_F(FakeDriveServiceTest, RenameResource_NonexistingFile) {
252 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
253
254 const GURL kEditUrl("https://file1_link_self/file:nonexisting_file");
255
256 GDataErrorCode error = GDATA_OTHER_ERROR;
257 fake_service_.RenameResource(
258 kEditUrl,
259 FILE_PATH_LITERAL("new name"),
260 base::Bind(&test_util::CopyResultsFromEntryActionCallback,
261 &error));
262 message_loop_.RunUntilIdle();
263
264 EXPECT_EQ(HTTP_NOT_FOUND, error);
265 }
266
267 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInRootDirectory) {
268 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
269
270 const std::string kResourceId = "file:2_file_resource_id";
271 const GURL kEditUrl("https://file1_link_self/file:2_file_resource_id");
272 const GURL kNewParentContentUrl("https://new_url");
273
274 scoped_ptr<ResourceEntry> resource_entry = FindEntry(kResourceId);
275 ASSERT_TRUE(resource_entry);
276 // The parent link should not exist as this file is in the root directory.
277 const google_apis::Link* parent_link =
278 resource_entry->GetLinkByType(Link::LINK_PARENT);
279 ASSERT_FALSE(parent_link);
280
281 GDataErrorCode error = GDATA_OTHER_ERROR;
282 fake_service_.AddResourceToDirectory(
283 kNewParentContentUrl,
284 kEditUrl,
285 base::Bind(&test_util::CopyResultsFromEntryActionCallback,
286 &error));
287 message_loop_.RunUntilIdle();
288
289 EXPECT_EQ(HTTP_SUCCESS, error);
290
291 resource_entry = FindEntry(kResourceId);
292 ASSERT_TRUE(resource_entry);
293 // The parent link should now exist as the parent directory is changed.
294 parent_link = resource_entry->GetLinkByType(Link::LINK_PARENT);
295 ASSERT_TRUE(parent_link);
296 EXPECT_EQ(kNewParentContentUrl, parent_link->href());
297 }
298
299 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_FileInNonRootDirectory) {
300 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
301
302 const std::string kResourceId = "file:subdirectory_file_1_id";
303 const GURL kEditUrl(
304 "https://dir1_file_link_self/file:subdirectory_file_1_id");
305 const GURL kNewParentContentUrl("https://new_url");
306
307 scoped_ptr<ResourceEntry> resource_entry = FindEntry(kResourceId);
308 ASSERT_TRUE(resource_entry);
309 // Here's the original parent link.
310 const google_apis::Link* parent_link =
311 resource_entry->GetLinkByType(Link::LINK_PARENT);
312 ASSERT_TRUE(parent_link);
313 EXPECT_EQ("https://dir_1_self_link/folder:1_folder_resource_id",
314 parent_link->href().spec());
315
316 GDataErrorCode error = GDATA_OTHER_ERROR;
317 fake_service_.AddResourceToDirectory(
318 kNewParentContentUrl,
319 kEditUrl,
320 base::Bind(&test_util::CopyResultsFromEntryActionCallback,
321 &error));
322 message_loop_.RunUntilIdle();
323
324 EXPECT_EQ(HTTP_SUCCESS, error);
325
326 resource_entry = FindEntry(kResourceId);
327 ASSERT_TRUE(resource_entry);
328 // The parent link should now be changed.
329 parent_link = resource_entry->GetLinkByType(Link::LINK_PARENT);
330 ASSERT_TRUE(parent_link);
331 EXPECT_EQ(kNewParentContentUrl, parent_link->href());
332 }
333
334 TEST_F(FakeDriveServiceTest, AddResourceToDirectory_NonexistingFile) {
335 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
336
337 const GURL kEditUrl("https://file1_link_self/file:nonexisting_file");
338 const GURL kNewParentContentUrl("https://new_url");
339
340 GDataErrorCode error = GDATA_OTHER_ERROR;
341 fake_service_.AddResourceToDirectory(
342 kNewParentContentUrl,
343 kEditUrl,
344 base::Bind(&test_util::CopyResultsFromEntryActionCallback,
345 &error));
346 message_loop_.RunUntilIdle();
347
348 EXPECT_EQ(HTTP_NOT_FOUND, error);
349 }
350
351 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_ExistingFile) {
352 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
353
354 const std::string kResourceId("file:subdirectory_file_1_id");
355 const GURL kParentContentUrl(
356 "https://dir_1_self_link/folder:1_folder_resource_id");
357
358 scoped_ptr<ResourceEntry> resource_entry = FindEntry(kResourceId);
359 ASSERT_TRUE(resource_entry);
360 // The parent link should exist now.
361 const google_apis::Link* parent_link =
362 resource_entry->GetLinkByType(Link::LINK_PARENT);
363 ASSERT_TRUE(parent_link);
364
365 GDataErrorCode error = GDATA_OTHER_ERROR;
366 fake_service_.RemoveResourceFromDirectory(
367 kParentContentUrl,
368 kResourceId,
369 base::Bind(&test_util::CopyResultsFromEntryActionCallback,
370 &error));
371 message_loop_.RunUntilIdle();
372
373 EXPECT_EQ(HTTP_SUCCESS, error);
374
375 resource_entry = FindEntry(kResourceId);
376 ASSERT_TRUE(resource_entry);
377 // The parent link should be gone now.
378 parent_link = resource_entry->GetLinkByType(Link::LINK_PARENT);
379 ASSERT_FALSE(parent_link);
380 }
381
382 TEST_F(FakeDriveServiceTest, RemoveResourceFromDirectory_NonexistingFile) {
383 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
384
385 const std::string kResourceId("file:nonexisting_file");
386 const GURL kParentContentUrl(
387 "https://dir_1_self_link/folder:1_folder_resource_id");
388
389 GDataErrorCode error = GDATA_OTHER_ERROR;
390 fake_service_.RemoveResourceFromDirectory(
391 kParentContentUrl,
392 kResourceId,
393 base::Bind(&test_util::CopyResultsFromEntryActionCallback,
394 &error));
395 message_loop_.RunUntilIdle();
396
397 EXPECT_EQ(HTTP_NOT_FOUND, error);
398 }
399
400 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToRootDirectory) {
401 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
402
403 GDataErrorCode error = GDATA_OTHER_ERROR;
404 scoped_ptr<ResourceEntry> resource_entry;
405 fake_service_.AddNewDirectory(
406 GURL(), // Empty means add it to the root directory.
407 FILE_PATH_LITERAL("new directory"),
408 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
409 &error,
410 &resource_entry));
411 message_loop_.RunUntilIdle();
412
413 EXPECT_EQ(HTTP_SUCCESS, error);
414 ASSERT_TRUE(resource_entry);
415 EXPECT_EQ("resource_id_1", resource_entry->resource_id());
416 EXPECT_EQ("new directory", UTF16ToUTF8(resource_entry->title()));
417 // The parent link should not exist as the new directory was added in the
418 // root.
419 const google_apis::Link* parent_link =
420 resource_entry->GetLinkByType(Link::LINK_PARENT);
421 ASSERT_FALSE(parent_link);
422 }
423
424 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonRootDirectory) {
425 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
426
427 const GURL kParentContentUrl(
428 "https://dir_1_self_link/folder:1_folder_resource_id");
429
430 GDataErrorCode error = GDATA_OTHER_ERROR;
431 scoped_ptr<ResourceEntry> resource_entry;
432 fake_service_.AddNewDirectory(
433 kParentContentUrl,
434 FILE_PATH_LITERAL("new directory"),
435 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
436 &error,
437 &resource_entry));
438 message_loop_.RunUntilIdle();
439
440 EXPECT_EQ(HTTP_SUCCESS, error);
441 ASSERT_TRUE(resource_entry);
442 EXPECT_EQ("resource_id_1", resource_entry->resource_id());
443 EXPECT_EQ("new directory", UTF16ToUTF8(resource_entry->title()));
444 const google_apis::Link* parent_link =
445 resource_entry->GetLinkByType(Link::LINK_PARENT);
446 ASSERT_TRUE(parent_link);
447 EXPECT_EQ(kParentContentUrl, parent_link->href());
448 }
449
450 TEST_F(FakeDriveServiceTest, AddNewDirectory_ToNonexistingDirectory) {
451 ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
452
453 const GURL kParentContentUrl(
454 "https://dir_1_self_link/folder:nonexisting_resource_id");
455
456 GDataErrorCode error = GDATA_OTHER_ERROR;
457 scoped_ptr<ResourceEntry> resource_entry;
458 fake_service_.AddNewDirectory(
459 kParentContentUrl,
460 FILE_PATH_LITERAL("new directory"),
461 base::Bind(&test_util::CopyResultsFromGetResourceEntryCallback,
462 &error,
463 &resource_entry));
464 message_loop_.RunUntilIdle();
465
466 EXPECT_EQ(HTTP_NOT_FOUND, error);
467 EXPECT_FALSE(resource_entry);
468 }
469
148 } // namespace google_apis 470 } // namespace google_apis
OLDNEW
« no previous file with comments | « chrome/browser/google_apis/fake_drive_service.cc ('k') | chrome/test/data/chromeos/gdata/root_feed.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698