summaryrefslogtreecommitdiffstats
path: root/components/drive/file_system/search_operation_unittest.cc
blob: 474c7df3ebd346a6ae1e897ae79486ec8e408fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/drive/file_system/search_operation.h"

#include <stddef.h>

#include "base/callback_helpers.h"
#include "components/drive/change_list_loader.h"
#include "components/drive/file_system/operation_test_base.h"
#include "components/drive/service/fake_drive_service.h"
#include "content/public/test/test_utils.h"
#include "google_apis/drive/drive_api_parser.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drive {
namespace file_system {

typedef OperationTestBase SearchOperationTest;

TEST_F(SearchOperationTest, ContentSearch) {
  SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
                            loader_controller());

  std::set<std::string> expected_results;
  expected_results.insert(
      "drive/root/Directory 1/Sub Directory Folder/Sub Sub Directory Folder");
  expected_results.insert("drive/root/Directory 1/Sub Directory Folder");
  expected_results.insert("drive/root/Directory 1/SubDirectory File 1.txt");
  expected_results.insert("drive/root/Directory 1");
  expected_results.insert("drive/root/Directory 2 excludeDir-test");

  FileError error = FILE_ERROR_FAILED;
  GURL next_link;
  scoped_ptr<std::vector<SearchResultInfo> > results;

  operation.Search("Directory", GURL(),
                   google_apis::test_util::CreateCopyResultCallback(
                       &error, &next_link, &results));
  content::RunAllBlockingPoolTasksUntilIdle();

  EXPECT_EQ(FILE_ERROR_OK, error);
  EXPECT_TRUE(next_link.is_empty());
  EXPECT_EQ(expected_results.size(), results->size());
  for (size_t i = 0; i < results->size(); i++) {
    EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
        << results->at(i).path.AsUTF8Unsafe();
  }
}

TEST_F(SearchOperationTest, ContentSearchWithNewEntry) {
  SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
                            loader_controller());

  // Create a new directory in the drive service.
  google_apis::DriveApiErrorCode status = google_apis::DRIVE_OTHER_ERROR;
  scoped_ptr<google_apis::FileResource> server_entry;
  fake_service()->AddNewDirectory(
      fake_service()->GetRootResourceId(), "New Directory 1!",
      AddNewDirectoryOptions(),
      google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
  content::RunAllBlockingPoolTasksUntilIdle();
  ASSERT_EQ(google_apis::HTTP_CREATED, status);

  // As the result of the first Search(), only entries in the current file
  // system snapshot are expected to be returned in the "right" path. New
  // entries like "New Directory 1!" is temporarily added to "drive/other".
  std::set<std::string> expected_results;
  expected_results.insert("drive/root/Directory 1");
  expected_results.insert("drive/other/New Directory 1!");

  FileError error = FILE_ERROR_FAILED;
  GURL next_link;
  scoped_ptr<std::vector<SearchResultInfo> > results;

  operation.Search("\"Directory 1\"", GURL(),
                   google_apis::test_util::CreateCopyResultCallback(
                       &error, &next_link, &results));
  content::RunAllBlockingPoolTasksUntilIdle();

  EXPECT_EQ(FILE_ERROR_OK, error);
  EXPECT_TRUE(next_link.is_empty());
  ASSERT_EQ(expected_results.size(), results->size());
  for (size_t i = 0; i < results->size(); i++) {
    EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
        << results->at(i).path.AsUTF8Unsafe();
  }

  // Load the change from FakeDriveService.
  ASSERT_EQ(FILE_ERROR_OK, CheckForUpdates());

  // Now the new entry must be reported to be in the right directory.
  expected_results.clear();
  expected_results.insert("drive/root/Directory 1");
  expected_results.insert("drive/root/New Directory 1!");
  error = FILE_ERROR_FAILED;
  operation.Search("\"Directory 1\"", GURL(),
                   google_apis::test_util::CreateCopyResultCallback(
                       &error, &next_link, &results));
  content::RunAllBlockingPoolTasksUntilIdle();

  EXPECT_EQ(FILE_ERROR_OK, error);
  EXPECT_TRUE(next_link.is_empty());
  ASSERT_EQ(expected_results.size(), results->size());
  for (size_t i = 0; i < results->size(); i++) {
    EXPECT_TRUE(expected_results.count(results->at(i).path.AsUTF8Unsafe()))
        << results->at(i).path.AsUTF8Unsafe();
  }
}

TEST_F(SearchOperationTest, ContentSearchEmptyResult) {
  SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
                            loader_controller());

  FileError error = FILE_ERROR_FAILED;
  GURL next_link;
  scoped_ptr<std::vector<SearchResultInfo> > results;

  operation.Search("\"no-match query\"", GURL(),
                   google_apis::test_util::CreateCopyResultCallback(
                       &error, &next_link, &results));
  content::RunAllBlockingPoolTasksUntilIdle();

  EXPECT_EQ(FILE_ERROR_OK, error);
  EXPECT_TRUE(next_link.is_empty());
  EXPECT_EQ(0U, results->size());
}

TEST_F(SearchOperationTest, Lock) {
  SearchOperation operation(blocking_task_runner(), scheduler(), metadata(),
                            loader_controller());

  // Lock.
  scoped_ptr<base::ScopedClosureRunner> lock = loader_controller()->GetLock();

  // Search does not return the result as long as lock is alive.
  FileError error = FILE_ERROR_FAILED;
  GURL next_link;
  scoped_ptr<std::vector<SearchResultInfo> > results;

  operation.Search("\"Directory 1\"", GURL(),
                   google_apis::test_util::CreateCopyResultCallback(
                       &error, &next_link, &results));
  content::RunAllBlockingPoolTasksUntilIdle();
  EXPECT_EQ(FILE_ERROR_FAILED, error);
  EXPECT_FALSE(results);

  // Unlock, this should resume the pending search.
  lock.reset();
  content::RunAllBlockingPoolTasksUntilIdle();
  EXPECT_EQ(FILE_ERROR_OK, error);
  ASSERT_TRUE(results);
  EXPECT_EQ(1u, results->size());
}

}  // namespace file_system
}  // namespace drive