blob: 474c7df3ebd346a6ae1e897ae79486ec8e408fa5 [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
avibc5337b2015-12-25 23:16:337#include <stddef.h>
8
[email protected]20f33e62014-01-30 10:50:399#include "base/callback_helpers.h"
lukaszab80bf262015-08-24 17:04:3410#include "components/drive/change_list_loader.h"
lukasza3fb22622015-08-27 21:04:3411#include "components/drive/file_system/operation_test_base.h"
lukasza8acc4eb2015-07-20 20:57:2012#include "components/drive/service/fake_drive_service.h"
[email protected]dd17fd102014-07-16 04:32:4913#include "content/public/test/test_utils.h"
[email protected]31258442014-06-05 03:37:4114#include "google_apis/drive/drive_api_parser.h"
[email protected]e196bef32013-12-03 05:33:1315#include "google_apis/drive/test_util.h"
[email protected]c6c5b682013-05-27 13:32:5916#include "testing/gtest/include/gtest/gtest.h"
17
18namespace drive {
19namespace file_system {
20
[email protected]c6c5b682013-05-27 13:32:5921typedef OperationTestBase SearchOperationTest;
22
23TEST_F(SearchOperationTest, ContentSearch) {
[email protected]20f33e62014-01-30 10:50:3924 SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
25 loader_controller());
[email protected]c6c5b682013-05-27 13:32:5926
[email protected]78f31792014-01-17 06:59:3727 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]c6c5b682013-05-27 13:32:5934
35 FileError error = FILE_ERROR_FAILED;
[email protected]b5f98182013-09-04 12:43:5336 GURL next_link;
[email protected]c6c5b682013-05-27 13:32:5937 scoped_ptr<std::vector<SearchResultInfo> > results;
38
[email protected]b5f98182013-09-04 12:43:5339 operation.Search("Directory", GURL(),
[email protected]c6c5b682013-05-27 13:32:5940 google_apis::test_util::CreateCopyResultCallback(
[email protected]b5f98182013-09-04 12:43:5341 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:4942 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]c6c5b682013-05-27 13:32:5943
44 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b5f98182013-09-04 12:43:5345 EXPECT_TRUE(next_link.is_empty());
[email protected]78f31792014-01-17 06:59:3746 EXPECT_EQ(expected_results.size(), results->size());
[email protected]c6c5b682013-05-27 13:32:5947 for (size_t i = 0; i < results->size(); i++) {
[email protected]78f31792014-01-17 06:59:3748 EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
49 << results->at(i).path.AsUTF8Unsafe();
[email protected]c6c5b682013-05-27 13:32:5950 }
51}
52
53TEST_F(SearchOperationTest, ContentSearchWithNewEntry) {
[email protected]20f33e62014-01-30 10:50:3954 SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
55 loader_controller());
[email protected]c6c5b682013-05-27 13:32:5956
57 // Create a new directory in the drive service.
satorux1e04b422015-01-29 07:50:5358 google_apis::DriveApiErrorCode status = google_apis::DRIVE_OTHER_ERROR;
[email protected]31258442014-06-05 03:37:4159 scoped_ptr<google_apis::FileResource> server_entry;
[email protected]c6c5b682013-05-27 13:32:5960 fake_service()->AddNewDirectory(
hirono516b766cd2015-03-31 02:23:4261 fake_service()->GetRootResourceId(), "New Directory 1!",
62 AddNewDirectoryOptions(),
[email protected]31258442014-06-05 03:37:4163 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
[email protected]dd17fd102014-07-16 04:32:4964 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]31258442014-06-05 03:37:4165 ASSERT_EQ(google_apis::HTTP_CREATED, status);
[email protected]c6c5b682013-05-27 13:32:5966
67 // As the result of the first Search(), only entries in the current file
[email protected]bd4a7abfc2013-05-30 05:58:5368 // 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]78f31792014-01-17 06:59:3770 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]c6c5b682013-05-27 13:32:5973
74 FileError error = FILE_ERROR_FAILED;
[email protected]b5f98182013-09-04 12:43:5375 GURL next_link;
[email protected]c6c5b682013-05-27 13:32:5976 scoped_ptr<std::vector<SearchResultInfo> > results;
77
[email protected]b5f98182013-09-04 12:43:5378 operation.Search("\"Directory 1\"", GURL(),
[email protected]c6c5b682013-05-27 13:32:5979 google_apis::test_util::CreateCopyResultCallback(
[email protected]b5f98182013-09-04 12:43:5380 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:4981 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]c6c5b682013-05-27 13:32:5982
83 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b5f98182013-09-04 12:43:5384 EXPECT_TRUE(next_link.is_empty());
[email protected]78f31792014-01-17 06:59:3785 ASSERT_EQ(expected_results.size(), results->size());
[email protected]bd4a7abfc2013-05-30 05:58:5386 for (size_t i = 0; i < results->size(); i++) {
[email protected]78f31792014-01-17 06:59:3787 EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
88 << results->at(i).path.AsUTF8Unsafe();
[email protected]bd4a7abfc2013-05-30 05:58:5389 }
90
91 // Load the change from FakeDriveService.
[email protected]bfacc132013-08-26 08:10:4792 ASSERT_EQ(FILE_ERROR_OK, CheckForUpdates());
[email protected]bd4a7abfc2013-05-30 05:58:5393
94 // Now the new entry must be reported to be in the right directory.
[email protected]78f31792014-01-17 06:59:3795 expected_results.clear();
96 expected_results.insert("drive/root/Directory 1");
97 expected_results.insert("drive/root/New Directory 1!");
[email protected]bd4a7abfc2013-05-30 05:58:5398 error = FILE_ERROR_FAILED;
[email protected]b5f98182013-09-04 12:43:5399 operation.Search("\"Directory 1\"", GURL(),
[email protected]bd4a7abfc2013-05-30 05:58:53100 google_apis::test_util::CreateCopyResultCallback(
[email protected]b5f98182013-09-04 12:43:53101 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:49102 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]bd4a7abfc2013-05-30 05:58:53103
104 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b5f98182013-09-04 12:43:53105 EXPECT_TRUE(next_link.is_empty());
[email protected]78f31792014-01-17 06:59:37106 ASSERT_EQ(expected_results.size(), results->size());
[email protected]bd4a7abfc2013-05-30 05:58:53107 for (size_t i = 0; i < results->size(); i++) {
[email protected]78f31792014-01-17 06:59:37108 EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
109 << results->at(i).path.AsUTF8Unsafe();
[email protected]bd4a7abfc2013-05-30 05:58:53110 }
[email protected]c6c5b682013-05-27 13:32:59111}
112
113TEST_F(SearchOperationTest, ContentSearchEmptyResult) {
[email protected]20f33e62014-01-30 10:50:39114 SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
115 loader_controller());
[email protected]c6c5b682013-05-27 13:32:59116
117 FileError error = FILE_ERROR_FAILED;
[email protected]b5f98182013-09-04 12:43:53118 GURL next_link;
[email protected]c6c5b682013-05-27 13:32:59119 scoped_ptr<std::vector<SearchResultInfo> > results;
120
[email protected]b5f98182013-09-04 12:43:53121 operation.Search("\"no-match query\"", GURL(),
[email protected]c6c5b682013-05-27 13:32:59122 google_apis::test_util::CreateCopyResultCallback(
[email protected]b5f98182013-09-04 12:43:53123 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:49124 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]c6c5b682013-05-27 13:32:59125
126 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b5f98182013-09-04 12:43:53127 EXPECT_TRUE(next_link.is_empty());
[email protected]c6c5b682013-05-27 13:32:59128 EXPECT_EQ(0U, results->size());
129}
130
[email protected]20f33e62014-01-30 10:50:39131TEST_F(SearchOperationTest, Lock) {
132 SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
133 loader_controller());
134
135 // Lock.
136 scoped_ptr<base::ScopedClosureRunner> lock = loader_controller()->GetLock();
137
138 // Search does not return the result as long as lock is alive.
139 FileError error = FILE_ERROR_FAILED;
140 GURL next_link;
141 scoped_ptr<std::vector<SearchResultInfo> > results;
142
143 operation.Search("\"Directory 1\"", GURL(),
144 google_apis::test_util::CreateCopyResultCallback(
145 &error, &next_link, &results));
[email protected]dd17fd102014-07-16 04:32:49146 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]20f33e62014-01-30 10:50:39147 EXPECT_EQ(FILE_ERROR_FAILED, error);
148 EXPECT_FALSE(results);
149
150 // Unlock, this should resume the pending search.
151 lock.reset();
[email protected]dd17fd102014-07-16 04:32:49152 content::RunAllBlockingPoolTasksUntilIdle();
[email protected]20f33e62014-01-30 10:50:39153 EXPECT_EQ(FILE_ERROR_OK, error);
154 ASSERT_TRUE(results);
155 EXPECT_EQ(1u, results->size());
156}
157
[email protected]c6c5b682013-05-27 13:32:59158} // namespace file_system
159} // namespace drive