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