PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/components/drive/file_system/search_operation_unittest.cc

https://gitlab.com/jonnialva90/iridium-browser
C++ | 157 lines | 117 code | 28 blank | 12 comment | 3 complexity | 8fd9b16fa725ca4b1d7b9c53f2ce23ec MD5 | raw file
  1. // Copyright 2013 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. #include "components/drive/file_system/search_operation.h"
  5. #include "base/callback_helpers.h"
  6. #include "components/drive/change_list_loader.h"
  7. #include "components/drive/file_system/operation_test_base.h"
  8. #include "components/drive/service/fake_drive_service.h"
  9. #include "content/public/test/test_utils.h"
  10. #include "google_apis/drive/drive_api_parser.h"
  11. #include "google_apis/drive/test_util.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace drive {
  14. namespace file_system {
  15. typedef OperationTestBase SearchOperationTest;
  16. TEST_F(SearchOperationTest, ContentSearch) {
  17. SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
  18. loader_controller());
  19. std::set<std::string> expected_results;
  20. expected_results.insert(
  21. "drive/root/Directory 1/Sub Directory Folder/Sub Sub Directory Folder");
  22. expected_results.insert("drive/root/Directory 1/Sub Directory Folder");
  23. expected_results.insert("drive/root/Directory 1/SubDirectory File 1.txt");
  24. expected_results.insert("drive/root/Directory 1");
  25. expected_results.insert("drive/root/Directory 2 excludeDir-test");
  26. FileError error = FILE_ERROR_FAILED;
  27. GURL next_link;
  28. scoped_ptr<std::vector<SearchResultInfo> > results;
  29. operation.Search("Directory", GURL(),
  30. google_apis::test_util::CreateCopyResultCallback(
  31. &error, &next_link, &results));
  32. content::RunAllBlockingPoolTasksUntilIdle();
  33. EXPECT_EQ(FILE_ERROR_OK, error);
  34. EXPECT_TRUE(next_link.is_empty());
  35. EXPECT_EQ(expected_results.size(), results->size());
  36. for (size_t i = 0; i < results->size(); i++) {
  37. EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
  38. << results->at(i).path.AsUTF8Unsafe();
  39. }
  40. }
  41. TEST_F(SearchOperationTest, ContentSearchWithNewEntry) {
  42. SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
  43. loader_controller());
  44. // Create a new directory in the drive service.
  45. google_apis::DriveApiErrorCode status = google_apis::DRIVE_OTHER_ERROR;
  46. scoped_ptr<google_apis::FileResource> server_entry;
  47. fake_service()->AddNewDirectory(
  48. fake_service()->GetRootResourceId(), "New Directory 1!",
  49. AddNewDirectoryOptions(),
  50. google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
  51. content::RunAllBlockingPoolTasksUntilIdle();
  52. ASSERT_EQ(google_apis::HTTP_CREATED, status);
  53. // As the result of the first Search(), only entries in the current file
  54. // system snapshot are expected to be returned in the "right" path. New
  55. // entries like "New Directory 1!" is temporarily added to "drive/other".
  56. std::set<std::string> expected_results;
  57. expected_results.insert("drive/root/Directory 1");
  58. expected_results.insert("drive/other/New Directory 1!");
  59. FileError error = FILE_ERROR_FAILED;
  60. GURL next_link;
  61. scoped_ptr<std::vector<SearchResultInfo> > results;
  62. operation.Search("\"Directory 1\"", GURL(),
  63. google_apis::test_util::CreateCopyResultCallback(
  64. &error, &next_link, &results));
  65. content::RunAllBlockingPoolTasksUntilIdle();
  66. EXPECT_EQ(FILE_ERROR_OK, error);
  67. EXPECT_TRUE(next_link.is_empty());
  68. ASSERT_EQ(expected_results.size(), results->size());
  69. for (size_t i = 0; i < results->size(); i++) {
  70. EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
  71. << results->at(i).path.AsUTF8Unsafe();
  72. }
  73. // Load the change from FakeDriveService.
  74. ASSERT_EQ(FILE_ERROR_OK, CheckForUpdates());
  75. // Now the new entry must be reported to be in the right directory.
  76. expected_results.clear();
  77. expected_results.insert("drive/root/Directory 1");
  78. expected_results.insert("drive/root/New Directory 1!");
  79. error = FILE_ERROR_FAILED;
  80. operation.Search("\"Directory 1\"", GURL(),
  81. google_apis::test_util::CreateCopyResultCallback(
  82. &error, &next_link, &results));
  83. content::RunAllBlockingPoolTasksUntilIdle();
  84. EXPECT_EQ(FILE_ERROR_OK, error);
  85. EXPECT_TRUE(next_link.is_empty());
  86. ASSERT_EQ(expected_results.size(), results->size());
  87. for (size_t i = 0; i < results->size(); i++) {
  88. EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
  89. << results->at(i).path.AsUTF8Unsafe();
  90. }
  91. }
  92. TEST_F(SearchOperationTest, ContentSearchEmptyResult) {
  93. SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
  94. loader_controller());
  95. FileError error = FILE_ERROR_FAILED;
  96. GURL next_link;
  97. scoped_ptr<std::vector<SearchResultInfo> > results;
  98. operation.Search("\"no-match query\"", GURL(),
  99. google_apis::test_util::CreateCopyResultCallback(
  100. &error, &next_link, &results));
  101. content::RunAllBlockingPoolTasksUntilIdle();
  102. EXPECT_EQ(FILE_ERROR_OK, error);
  103. EXPECT_TRUE(next_link.is_empty());
  104. EXPECT_EQ(0U, results->size());
  105. }
  106. TEST_F(SearchOperationTest, Lock) {
  107. SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
  108. loader_controller());
  109. // Lock.
  110. scoped_ptr<base::ScopedClosureRunner> lock = loader_controller()->GetLock();
  111. // Search does not return the result as long as lock is alive.
  112. FileError error = FILE_ERROR_FAILED;
  113. GURL next_link;
  114. scoped_ptr<std::vector<SearchResultInfo> > results;
  115. operation.Search("\"Directory 1\"", GURL(),
  116. google_apis::test_util::CreateCopyResultCallback(
  117. &error, &next_link, &results));
  118. content::RunAllBlockingPoolTasksUntilIdle();
  119. EXPECT_EQ(FILE_ERROR_FAILED, error);
  120. EXPECT_FALSE(results);
  121. // Unlock, this should resume the pending search.
  122. lock.reset();
  123. content::RunAllBlockingPoolTasksUntilIdle();
  124. EXPECT_EQ(FILE_ERROR_OK, error);
  125. ASSERT_TRUE(results);
  126. EXPECT_EQ(1u, results->size());
  127. }
  128. } // namespace file_system
  129. } // namespace drive