blob: 28c1bb7c19be11a1f1fdcd1f299c42d5768d3b0a [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
yawano3513e142016-04-20 00:42:425#include "components/drive/chromeos/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"
Stuart Langleyf51f3412018-05-08 23:43:0410#include "components/drive/chromeos/loader_controller.h"
msramek4cfdf21b2016-01-06 11:59:0211#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;
dchengf42750232016-04-12 04:12:2737 std::unique_ptr<std::vector<SearchResultInfo>> results;
[email protected]c6c5b682013-05-27 13:32:5938
[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));
Gabriel Charette01507a22017-09-27 21:30:0842 content::RunAllTasksUntilIdle();
[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;
dchengf42750232016-04-12 04:12:2759 std::unique_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));
Gabriel Charette01507a22017-09-27 21:30:0864 content::RunAllTasksUntilIdle();
[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;
dchengf42750232016-04-12 04:12:2776 std::unique_ptr<std::vector<SearchResultInfo>> results;
[email protected]c6c5b682013-05-27 13:32:5977
[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));
Gabriel Charette01507a22017-09-27 21:30:0881 content::RunAllTasksUntilIdle();
[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));
Gabriel Charette01507a22017-09-27 21:30:08102 content::RunAllTasksUntilIdle();
[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;
dchengf42750232016-04-12 04:12:27119 std::unique_ptr<std::vector<SearchResultInfo>> results;
[email protected]c6c5b682013-05-27 13:32:59120
[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));
Gabriel Charette01507a22017-09-27 21:30:08124 content::RunAllTasksUntilIdle();
[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.
dchengf42750232016-04-12 04:12:27136 std::unique_ptr<base::ScopedClosureRunner> lock =
137 loader_controller()->GetLock();
[email protected]20f33e62014-01-30 10:50:39138
139 // Search does not return the result as long as lock is alive.
140 FileError error = FILE_ERROR_FAILED;
141 GURL next_link;
dchengf42750232016-04-12 04:12:27142 std::unique_ptr<std::vector<SearchResultInfo>> results;
[email protected]20f33e62014-01-30 10:50:39143
144 operation.Search("\"Directory 1\"", GURL(),
145 google_apis::test_util::CreateCopyResultCallback(
146 &error, &next_link, &results));
Gabriel Charette01507a22017-09-27 21:30:08147 content::RunAllTasksUntilIdle();
[email protected]20f33e62014-01-30 10:50:39148 EXPECT_EQ(FILE_ERROR_FAILED, error);
149 EXPECT_FALSE(results);
150
151 // Unlock, this should resume the pending search.
152 lock.reset();
Gabriel Charette01507a22017-09-27 21:30:08153 content::RunAllTasksUntilIdle();
[email protected]20f33e62014-01-30 10:50:39154 EXPECT_EQ(FILE_ERROR_OK, error);
155 ASSERT_TRUE(results);
156 EXPECT_EQ(1u, results->size());
157}
158
[email protected]c6c5b682013-05-27 13:32:59159} // namespace file_system
160} // namespace drive