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