blob: 83ab746b9e4542253f7d3ccd47efcffd46d98b5f [file] [log] [blame]
[email protected]c6c5b682013-05-27 13:32:591// 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
lukasza3fb22622015-08-27 21:04:345#include "components/drive/file_system/search_operation.h"
[email protected]c6c5b682013-05-27 13:32:596
[email protected]20f33e62014-01-30 10:50:397#include "base/callback_helpers.h"
lukaszab80bf262015-08-24 17:04:348#include "components/drive/change_list_loader.h"
lukasza3fb22622015-08-27 21:04:349#include "components/drive/file_system/operation_test_base.h"
lukasza8acc4eb2015-07-20 20:57:2010#include "components/drive/service/fake_drive_service.h"
[email protected]dd17fd102014-07-16 04:32:4911#include "content/public/test/test_utils.h"
[email protected]31258442014-06-05 03:37:4112#include "google_apis/drive/drive_api_parser.h"
[email protected]e196bef32013-12-03 05:33:1313#include "google_apis/drive/test_util.h"
[email protected]c6c5b682013-05-27 13:32:5914#include "testing/gtest/include/gtest/gtest.h"
15
16namespace drive {
17namespace file_system {
18
[email protected]c6c5b682013-05-27 13:32:5919typedef OperationTestBase SearchOperationTest;
20
21TEST_F(SearchOperationTest, ContentSearch) {
[email protected]20f33e62014-01-30 10:50:3922 SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
23 loader_controller());
[email protected]c6c5b682013-05-27 13:32:5924
[email protected]78f31792014-01-17 06:59:3725 std::set<std::string> expected_results;
26 expected_results.insert(
27 "drive/root/Directory 1/Sub Directory Folder/Sub Sub Directory Folder");
28 expected_results.insert("drive/root/Directory 1/Sub Directory Folder");
29 expected_results.insert("drive/root/Directory 1/SubDirectory File 1.txt");
30 expected_results.insert("drive/root/Directory 1");
31 expected_results.insert("drive/root/Directory 2 excludeDir-test");
[email protected]c6c5b682013-05-27 13:32:5932
33 FileError error = FILE_ERROR_FAILED;
[email protected]b5f98182013-09-04 12:43:5334 GURL next_link;
[email protected]c6c5b682013-05-27 13:32:5935 scoped_ptr<std::vector<SearchResultInfo> > results;
36
[email protected]b5f98182013-09-04 12:43:5337 operation.Search("Directory", GURL(),
[email protected]c6c5b682013-05-27 13:32:5938 google_apis::test_util::CreateCopyResultCallback(
[email protected]b5f98182013-09-04 12:43:5339 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:4940 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]c6c5b682013-05-27 13:32:5941
42 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b5f98182013-09-04 12:43:5343 EXPECT_TRUE(next_link.is_empty());
[email protected]78f31792014-01-17 06:59:3744 EXPECT_EQ(expected_results.size(), results->size());
[email protected]c6c5b682013-05-27 13:32:5945 for (size_t i = 0; i < results->size(); i++) {
[email protected]78f31792014-01-17 06:59:3746 EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
47 << results->at(i).path.AsUTF8Unsafe();
[email protected]c6c5b682013-05-27 13:32:5948 }
49}
50
51TEST_F(SearchOperationTest, ContentSearchWithNewEntry) {
[email protected]20f33e62014-01-30 10:50:3952 SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
53 loader_controller());
[email protected]c6c5b682013-05-27 13:32:5954
55 // Create a new directory in the drive service.
satorux1e04b422015-01-29 07:50:5356 google_apis::DriveApiErrorCode status = google_apis::DRIVE_OTHER_ERROR;
[email protected]31258442014-06-05 03:37:4157 scoped_ptr<google_apis::FileResource> server_entry;
[email protected]c6c5b682013-05-27 13:32:5958 fake_service()->AddNewDirectory(
hirono516b766cd2015-03-31 02:23:4259 fake_service()->GetRootResourceId(), "New Directory 1!",
60 AddNewDirectoryOptions(),
[email protected]31258442014-06-05 03:37:4161 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
[email protected]dd17fd102014-07-16 04:32:4962 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]31258442014-06-05 03:37:4163 ASSERT_EQ(google_apis::HTTP_CREATED, status);
[email protected]c6c5b682013-05-27 13:32:5964
65 // As the result of the first Search(), only entries in the current file
[email protected]bd4a7abfc2013-05-30 05:58:5366 // system snapshot are expected to be returned in the "right" path. New
67 // entries like "New Directory 1!" is temporarily added to "drive/other".
[email protected]78f31792014-01-17 06:59:3768 std::set<std::string> expected_results;
69 expected_results.insert("drive/root/Directory 1");
70 expected_results.insert("drive/other/New Directory 1!");
[email protected]c6c5b682013-05-27 13:32:5971
72 FileError error = FILE_ERROR_FAILED;
[email protected]b5f98182013-09-04 12:43:5373 GURL next_link;
[email protected]c6c5b682013-05-27 13:32:5974 scoped_ptr<std::vector<SearchResultInfo> > results;
75
[email protected]b5f98182013-09-04 12:43:5376 operation.Search("\"Directory 1\"", GURL(),
[email protected]c6c5b682013-05-27 13:32:5977 google_apis::test_util::CreateCopyResultCallback(
[email protected]b5f98182013-09-04 12:43:5378 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:4979 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]c6c5b682013-05-27 13:32:5980
81 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b5f98182013-09-04 12:43:5382 EXPECT_TRUE(next_link.is_empty());
[email protected]78f31792014-01-17 06:59:3783 ASSERT_EQ(expected_results.size(), results->size());
[email protected]bd4a7abfc2013-05-30 05:58:5384 for (size_t i = 0; i < results->size(); i++) {
[email protected]78f31792014-01-17 06:59:3785 EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
86 << results->at(i).path.AsUTF8Unsafe();
[email protected]bd4a7abfc2013-05-30 05:58:5387 }
88
89 // Load the change from FakeDriveService.
[email protected]bfacc132013-08-26 08:10:4790 ASSERT_EQ(FILE_ERROR_OK, CheckForUpdates());
[email protected]bd4a7abfc2013-05-30 05:58:5391
92 // Now the new entry must be reported to be in the right directory.
[email protected]78f31792014-01-17 06:59:3793 expected_results.clear();
94 expected_results.insert("drive/root/Directory 1");
95 expected_results.insert("drive/root/New Directory 1!");
[email protected]bd4a7abfc2013-05-30 05:58:5396 error = FILE_ERROR_FAILED;
[email protected]b5f98182013-09-04 12:43:5397 operation.Search("\"Directory 1\"", GURL(),
[email protected]bd4a7abfc2013-05-30 05:58:5398 google_apis::test_util::CreateCopyResultCallback(
[email protected]b5f98182013-09-04 12:43:5399 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:49100 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]bd4a7abfc2013-05-30 05:58:53101
102 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b5f98182013-09-04 12:43:53103 EXPECT_TRUE(next_link.is_empty());
[email protected]78f31792014-01-17 06:59:37104 ASSERT_EQ(expected_results.size(), results->size());
[email protected]bd4a7abfc2013-05-30 05:58:53105 for (size_t i = 0; i < results->size(); i++) {
[email protected]78f31792014-01-17 06:59:37106 EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
107 << results->at(i).path.AsUTF8Unsafe();
[email protected]bd4a7abfc2013-05-30 05:58:53108 }
[email protected]c6c5b682013-05-27 13:32:59109}
110
111TEST_F(SearchOperationTest, ContentSearchEmptyResult) {
[email protected]20f33e62014-01-30 10:50:39112 SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
113 loader_controller());
[email protected]c6c5b682013-05-27 13:32:59114
115 FileError error = FILE_ERROR_FAILED;
[email protected]b5f98182013-09-04 12:43:53116 GURL next_link;
[email protected]c6c5b682013-05-27 13:32:59117 scoped_ptr<std::vector<SearchResultInfo> > results;
118
[email protected]b5f98182013-09-04 12:43:53119 operation.Search("\"no-match query\"", GURL(),
[email protected]c6c5b682013-05-27 13:32:59120 google_apis::test_util::CreateCopyResultCallback(
[email protected]b5f98182013-09-04 12:43:53121 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:49122 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]c6c5b682013-05-27 13:32:59123
124 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b5f98182013-09-04 12:43:53125 EXPECT_TRUE(next_link.is_empty());
[email protected]c6c5b682013-05-27 13:32:59126 EXPECT_EQ(0U, results->size());
127}
128
[email protected]20f33e62014-01-30 10:50:39129TEST_F(SearchOperationTest, Lock) {
130 SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
131 loader_controller());
132
133 // Lock.
134 scoped_ptr<base::ScopedClosureRunner> lock = loader_controller()->GetLock();
135
136 // Search does not return the result as long as lock is alive.
137 FileError error = FILE_ERROR_FAILED;
138 GURL next_link;
139 scoped_ptr<std::vector<SearchResultInfo> > results;
140
141 operation.Search("\"Directory 1\"", GURL(),
142 google_apis::test_util::CreateCopyResultCallback(
143 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:49144 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]20f33e62014-01-30 10:50:39145 EXPECT_EQ(FILE_ERROR_FAILED, error);
146 EXPECT_FALSE(results);
147
148 // Unlock, this should resume the pending search.
149 lock.reset();
[email protected]dd17fd102014-07-16 04:32:49150 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]20f33e62014-01-30 10:50:39151 EXPECT_EQ(FILE_ERROR_OK, error);
152 ASSERT_TRUE(results);
153 EXPECT_EQ(1u, results->size());
154}
155
[email protected]c6c5b682013-05-27 13:32:59156} // namespace file_system
157} // namespace drive