1 // Copyright (c) 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.
5 #include "chrome/browser/chromeos/drive/search_metadata.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/i18n/string_search.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "components/drive/drive_api_util.h"
15 #include "components/drive/drive_test_util.h"
16 #include "components/drive/fake_free_disk_space_getter.h"
17 #include "components/drive/file_cache.h"
18 #include "components/drive/file_system_core_util.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gtest/include/gtest/gtest.h"
27 const int kDefaultAtMostNumMatches
= 10;
29 // A simple wrapper for testing FindAndHighlightWrapper(). It just converts the
30 // query text parameter to FixedPatternStringSearchIgnoringCaseAndAccents.
31 bool FindAndHighlightWrapper(
32 const std::string
& text
,
33 const std::string
& query_text
,
34 std::string
* highlighted_text
) {
35 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents
query(
36 base::UTF8ToUTF16(query_text
));
37 return FindAndHighlight(text
, &query
, highlighted_text
);
42 class SearchMetadataTest
: public testing::Test
{
44 void SetUp() override
{
45 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
46 fake_free_disk_space_getter_
.reset(new FakeFreeDiskSpaceGetter
);
48 metadata_storage_
.reset(new ResourceMetadataStorage(
49 temp_dir_
.path(), base::ThreadTaskRunnerHandle::Get().get()));
50 ASSERT_TRUE(metadata_storage_
->Initialize());
52 cache_
.reset(new FileCache(metadata_storage_
.get(),
54 base::ThreadTaskRunnerHandle::Get().get(),
55 fake_free_disk_space_getter_
.get()));
56 ASSERT_TRUE(cache_
->Initialize());
58 resource_metadata_
.reset(
59 new ResourceMetadata(metadata_storage_
.get(),
61 base::ThreadTaskRunnerHandle::Get().get()));
62 ASSERT_EQ(FILE_ERROR_OK
, resource_metadata_
->Initialize());
64 AddEntriesToMetadata();
67 void AddEntriesToMetadata() {
68 base::FilePath temp_file
;
69 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &temp_file
));
70 const std::string temp_file_md5
= "md5";
76 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->GetIdByPath(
77 util::GetDriveMyDriveRootPath(), &local_id
));
78 const std::string root_local_id
= local_id
;
80 // drive/root/Directory 1
81 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetDirectoryEntry(
82 "Directory 1", "dir1", 1, root_local_id
), &local_id
));
83 const std::string dir1_local_id
= local_id
;
85 // drive/root/Directory 1/SubDirectory File 1.txt
86 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetFileEntry(
87 "SubDirectory File 1.txt", "file1a", 2, dir1_local_id
), &local_id
));
88 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Store(
89 local_id
, temp_file_md5
, temp_file
, FileCache::FILE_OPERATION_COPY
));
91 // drive/root/Directory 1/Shared To The Account Owner.txt
93 "Shared To The Account Owner.txt", "file1b", 3, dir1_local_id
);
94 entry
.set_shared_with_me(true);
95 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
97 // drive/root/Directory 2 excludeDir-test
98 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetDirectoryEntry(
99 "Directory 2 excludeDir-test", "dir2", 4, root_local_id
), &local_id
));
101 // drive/root/Slash \xE2\x88\x95 in directory
102 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(
103 GetDirectoryEntry("Slash \xE2\x88\x95 in directory", "dir3", 5,
104 root_local_id
), &local_id
));
105 const std::string dir3_local_id
= local_id
;
107 // drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt
108 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetFileEntry(
109 "Slash SubDir File.txt", "file3a", 6, dir3_local_id
), &local_id
));
111 // drive/root/File 2.txt
112 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetFileEntry(
113 "File 2.txt", "file2", 7, root_local_id
), &local_id
));
114 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Store(
115 local_id
, temp_file_md5
, temp_file
, FileCache::FILE_OPERATION_COPY
));
117 // drive/root/Document 1 excludeDir-test
118 entry
= GetFileEntry(
119 "Document 1 excludeDir-test", "doc1", 8, root_local_id
);
120 entry
.mutable_file_specific_info()->set_is_hosted_document(true);
121 entry
.mutable_file_specific_info()->set_document_extension(".gdoc");
122 entry
.mutable_file_specific_info()->set_content_mime_type(
123 drive::util::kGoogleDocumentMimeType
);
124 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
127 ResourceEntry
GetFileEntry(const std::string
& name
,
128 const std::string
& resource_id
,
130 const std::string
& parent_local_id
) {
132 entry
.set_title(name
);
133 entry
.set_resource_id(resource_id
);
134 entry
.set_parent_local_id(parent_local_id
);
135 entry
.mutable_file_info()->set_last_accessed(last_accessed
);
139 ResourceEntry
GetDirectoryEntry(const std::string
& name
,
140 const std::string
& resource_id
,
142 const std::string
& parent_local_id
) {
144 entry
.set_title(name
);
145 entry
.set_resource_id(resource_id
);
146 entry
.set_parent_local_id(parent_local_id
);
147 entry
.mutable_file_info()->set_last_accessed(last_accessed
);
148 entry
.mutable_file_info()->set_is_directory(true);
152 content::TestBrowserThreadBundle thread_bundle_
;
153 base::ScopedTempDir temp_dir_
;
154 scoped_ptr
<FakeFreeDiskSpaceGetter
> fake_free_disk_space_getter_
;
155 scoped_ptr
<ResourceMetadataStorage
,
156 test_util::DestroyHelperForTests
> metadata_storage_
;
157 scoped_ptr
<ResourceMetadata
, test_util::DestroyHelperForTests
>
159 scoped_ptr
<FileCache
, test_util::DestroyHelperForTests
> cache_
;
162 TEST_F(SearchMetadataTest
, SearchMetadata_ZeroMatches
) {
163 FileError error
= FILE_ERROR_FAILED
;
164 scoped_ptr
<MetadataSearchResultVector
> result
;
167 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(),
168 "NonExistent", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
169 kDefaultAtMostNumMatches
,
170 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
171 base::RunLoop().RunUntilIdle();
172 EXPECT_EQ(FILE_ERROR_OK
, error
);
174 ASSERT_EQ(0U, result
->size());
177 TEST_F(SearchMetadataTest
, SearchMetadata_RegularFile
) {
178 FileError error
= FILE_ERROR_FAILED
;
179 scoped_ptr
<MetadataSearchResultVector
> result
;
182 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(),
183 "SubDirectory File 1.txt", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
184 kDefaultAtMostNumMatches
,
185 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
186 base::RunLoop().RunUntilIdle();
187 EXPECT_EQ(FILE_ERROR_OK
, error
);
189 ASSERT_EQ(1U, result
->size());
190 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
191 result
->at(0).path
.AsUTF8Unsafe());
194 // This test checks if |FindAndHighlightWrapper| does case-insensitive search.
195 // Tricker test cases for |FindAndHighlightWrapper| can be found below.
196 TEST_F(SearchMetadataTest
, SearchMetadata_CaseInsensitiveSearch
) {
197 FileError error
= FILE_ERROR_FAILED
;
198 scoped_ptr
<MetadataSearchResultVector
> result
;
200 // The query is all in lower case.
202 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(),
203 "subdirectory file 1.txt", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
204 kDefaultAtMostNumMatches
,
205 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
206 base::RunLoop().RunUntilIdle();
207 EXPECT_EQ(FILE_ERROR_OK
, error
);
209 ASSERT_EQ(1U, result
->size());
210 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
211 result
->at(0).path
.AsUTF8Unsafe());
214 TEST_F(SearchMetadataTest
, SearchMetadata_RegularFiles
) {
215 FileError error
= FILE_ERROR_FAILED
;
216 scoped_ptr
<MetadataSearchResultVector
> result
;
219 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(), "SubDir",
220 base::Bind(&MatchesType
, SEARCH_METADATA_ALL
), kDefaultAtMostNumMatches
,
221 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
222 base::RunLoop().RunUntilIdle();
223 EXPECT_EQ(FILE_ERROR_OK
, error
);
225 ASSERT_EQ(2U, result
->size());
227 // All base names should contain "File". The results should be sorted by the
228 // last accessed time in descending order.
229 EXPECT_EQ("drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt",
230 result
->at(0).path
.AsUTF8Unsafe());
231 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
232 result
->at(1).path
.AsUTF8Unsafe());
235 TEST_F(SearchMetadataTest
, SearchMetadata_AtMostOneFile
) {
236 FileError error
= FILE_ERROR_FAILED
;
237 scoped_ptr
<MetadataSearchResultVector
> result
;
239 // There are two files matching "SubDir" but only one file should be
242 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(), "SubDir",
243 base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
244 1, // at_most_num_matches
245 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
246 base::RunLoop().RunUntilIdle();
247 EXPECT_EQ(FILE_ERROR_OK
, error
);
249 ASSERT_EQ(1U, result
->size());
250 EXPECT_EQ("drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt",
251 result
->at(0).path
.AsUTF8Unsafe());
254 TEST_F(SearchMetadataTest
, SearchMetadata_Directory
) {
255 FileError error
= FILE_ERROR_FAILED
;
256 scoped_ptr
<MetadataSearchResultVector
> result
;
259 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(),
260 "Directory 1", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
261 kDefaultAtMostNumMatches
,
262 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
263 base::RunLoop().RunUntilIdle();
264 EXPECT_EQ(FILE_ERROR_OK
, error
);
266 ASSERT_EQ(1U, result
->size());
267 EXPECT_EQ("drive/root/Directory 1", result
->at(0).path
.AsUTF8Unsafe());
270 TEST_F(SearchMetadataTest
, SearchMetadata_HostedDocument
) {
271 FileError error
= FILE_ERROR_FAILED
;
272 scoped_ptr
<MetadataSearchResultVector
> result
;
275 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(), "Document",
276 base::Bind(&MatchesType
, SEARCH_METADATA_ALL
), kDefaultAtMostNumMatches
,
277 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
278 base::RunLoop().RunUntilIdle();
279 EXPECT_EQ(FILE_ERROR_OK
, error
);
281 ASSERT_EQ(1U, result
->size());
283 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
284 result
->at(0).path
.AsUTF8Unsafe());
287 TEST_F(SearchMetadataTest
, SearchMetadata_ExcludeHostedDocument
) {
288 FileError error
= FILE_ERROR_FAILED
;
289 scoped_ptr
<MetadataSearchResultVector
> result
;
292 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(), "Document",
293 base::Bind(&MatchesType
, SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS
),
294 kDefaultAtMostNumMatches
,
295 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
296 base::RunLoop().RunUntilIdle();
297 EXPECT_EQ(FILE_ERROR_OK
, error
);
299 ASSERT_EQ(0U, result
->size());
302 TEST_F(SearchMetadataTest
, SearchMetadata_SharedWithMe
) {
303 FileError error
= FILE_ERROR_FAILED
;
304 scoped_ptr
<MetadataSearchResultVector
> result
;
307 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(), "",
308 base::Bind(&MatchesType
, SEARCH_METADATA_SHARED_WITH_ME
),
309 kDefaultAtMostNumMatches
,
310 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
311 base::RunLoop().RunUntilIdle();
312 EXPECT_EQ(FILE_ERROR_OK
, error
);
314 ASSERT_EQ(1U, result
->size());
315 EXPECT_EQ("drive/root/Directory 1/Shared To The Account Owner.txt",
316 result
->at(0).path
.AsUTF8Unsafe());
319 TEST_F(SearchMetadataTest
, SearchMetadata_FileAndDirectory
) {
320 FileError error
= FILE_ERROR_FAILED
;
321 scoped_ptr
<MetadataSearchResultVector
> result
;
324 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(),
325 "excludeDir-test", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
326 kDefaultAtMostNumMatches
,
327 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
329 base::RunLoop().RunUntilIdle();
330 EXPECT_EQ(FILE_ERROR_OK
, error
);
332 ASSERT_EQ(2U, result
->size());
334 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
335 result
->at(0).path
.AsUTF8Unsafe());
336 EXPECT_EQ("drive/root/Directory 2 excludeDir-test",
337 result
->at(1).path
.AsUTF8Unsafe());
340 TEST_F(SearchMetadataTest
, SearchMetadata_ExcludeDirectory
) {
341 FileError error
= FILE_ERROR_FAILED
;
342 scoped_ptr
<MetadataSearchResultVector
> result
;
345 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(),
347 base::Bind(&MatchesType
, SEARCH_METADATA_EXCLUDE_DIRECTORIES
),
348 kDefaultAtMostNumMatches
,
349 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
351 base::RunLoop().RunUntilIdle();
352 EXPECT_EQ(FILE_ERROR_OK
, error
);
354 ASSERT_EQ(1U, result
->size());
356 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
357 result
->at(0).path
.AsUTF8Unsafe());
360 // "drive", "drive/root", "drive/other" should be excluded.
361 TEST_F(SearchMetadataTest
, SearchMetadata_ExcludeSpecialDirectories
) {
362 const char* const kQueries
[] = { "drive", "root", "other" };
363 for (size_t i
= 0; i
< arraysize(kQueries
); ++i
) {
364 FileError error
= FILE_ERROR_FAILED
;
365 scoped_ptr
<MetadataSearchResultVector
> result
;
367 const std::string query
= kQueries
[i
];
369 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(), query
,
370 base::Bind(&MatchesType
, SEARCH_METADATA_ALL
), kDefaultAtMostNumMatches
,
371 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
373 base::RunLoop().RunUntilIdle();
374 EXPECT_EQ(FILE_ERROR_OK
, error
);
376 ASSERT_TRUE(result
->empty()) << ": " << query
<< " should not match";
380 TEST_F(SearchMetadataTest
, SearchMetadata_Offline
) {
381 FileError error
= FILE_ERROR_FAILED
;
382 scoped_ptr
<MetadataSearchResultVector
> result
;
385 base::ThreadTaskRunnerHandle::Get(), resource_metadata_
.get(), "",
386 base::Bind(&MatchesType
, SEARCH_METADATA_OFFLINE
),
387 kDefaultAtMostNumMatches
,
388 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
389 base::RunLoop().RunUntilIdle();
390 EXPECT_EQ(FILE_ERROR_OK
, error
);
391 ASSERT_EQ(3U, result
->size());
393 // This is not included in the cache but is a hosted document.
394 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
395 result
->at(0).path
.AsUTF8Unsafe());
397 EXPECT_EQ("drive/root/File 2.txt",
398 result
->at(1).path
.AsUTF8Unsafe());
399 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
400 result
->at(2).path
.AsUTF8Unsafe());
403 TEST(SearchMetadataSimpleTest
, FindAndHighlight_ZeroMatches
) {
404 std::string highlighted_text
;
405 EXPECT_FALSE(FindAndHighlightWrapper("text", "query", &highlighted_text
));
408 TEST(SearchMetadataSimpleTest
, FindAndHighlight_EmptyText
) {
409 std::string highlighted_text
;
410 EXPECT_FALSE(FindAndHighlightWrapper("", "query", &highlighted_text
));
413 TEST(SearchMetadataSimpleTest
, FindAndHighlight_FullMatch
) {
414 std::string highlighted_text
;
415 EXPECT_TRUE(FindAndHighlightWrapper("hello", "hello", &highlighted_text
));
416 EXPECT_EQ("<b>hello</b>", highlighted_text
);
419 TEST(SearchMetadataSimpleTest
, FindAndHighlight_StartWith
) {
420 std::string highlighted_text
;
421 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "hello",
423 EXPECT_EQ("<b>hello</b>, world", highlighted_text
);
426 TEST(SearchMetadataSimpleTest
, FindAndHighlight_EndWith
) {
427 std::string highlighted_text
;
428 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "world",
430 EXPECT_EQ("hello, <b>world</b>", highlighted_text
);
433 TEST(SearchMetadataSimpleTest
, FindAndHighlight_InTheMiddle
) {
434 std::string highlighted_text
;
435 EXPECT_TRUE(FindAndHighlightWrapper("yo hello, world", "hello",
437 EXPECT_EQ("yo <b>hello</b>, world", highlighted_text
);
440 TEST(SearchMetadataSimpleTest
, FindAndHighlight_MultipeMatches
) {
441 std::string highlighted_text
;
442 EXPECT_TRUE(FindAndHighlightWrapper("yoyoyoyoy", "yoy", &highlighted_text
));
443 // Only the first match is highlighted.
444 EXPECT_EQ("<b>yoy</b>oyoyoy", highlighted_text
);
447 TEST(SearchMetadataSimpleTest
, FindAndHighlight_IgnoreCase
) {
448 std::string highlighted_text
;
449 EXPECT_TRUE(FindAndHighlightWrapper("HeLLo", "hello", &highlighted_text
));
450 EXPECT_EQ("<b>HeLLo</b>", highlighted_text
);
453 TEST(SearchMetadataSimpleTest
, FindAndHighlight_IgnoreCaseNonASCII
) {
454 std::string highlighted_text
;
456 // Case and accent ignorance in Greek. Find "socra" in "Socra'tes".
457 EXPECT_TRUE(FindAndHighlightWrapper(
458 "\xCE\xA3\xCF\x89\xCE\xBA\xCF\x81\xCE\xAC\xCF\x84\xCE\xB7\xCF\x82",
459 "\xCF\x83\xCF\x89\xCE\xBA\xCF\x81\xCE\xB1", &highlighted_text
));
461 "<b>\xCE\xA3\xCF\x89\xCE\xBA\xCF\x81\xCE\xAC</b>\xCF\x84\xCE\xB7\xCF\x82",
464 // In Japanese characters.
465 // Find Hiragana "pi" + "(small)ya" in Katakana "hi" + semi-voiced-mark + "ya"
466 EXPECT_TRUE(FindAndHighlightWrapper(
467 "\xE3\x81\xB2\xE3\x82\x9A\xE3\x82\x83\xE3\x83\xBC",
468 "\xE3\x83\x94\xE3\x83\xA4",
471 "<b>\xE3\x81\xB2\xE3\x82\x9A\xE3\x82\x83</b>\xE3\x83\xBC",
475 TEST(SearchMetadataSimpleTest
, MultiTextBySingleQuery
) {
476 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents
query(
477 base::UTF8ToUTF16("hello"));
479 std::string highlighted_text
;
480 EXPECT_TRUE(FindAndHighlight("hello", &query
, &highlighted_text
));
481 EXPECT_EQ("<b>hello</b>", highlighted_text
);
482 EXPECT_FALSE(FindAndHighlight("goodbye", &query
, &highlighted_text
));
483 EXPECT_TRUE(FindAndHighlight("1hello2", &query
, &highlighted_text
));
484 EXPECT_EQ("1<b>hello</b>2", highlighted_text
);
487 TEST(SearchMetadataSimpleTest
, FindAndHighlight_MetaChars
) {
488 std::string highlighted_text
;
489 EXPECT_TRUE(FindAndHighlightWrapper("<hello>", "hello", &highlighted_text
));
490 EXPECT_EQ("<<b>hello</b>>", highlighted_text
);
493 TEST(SearchMetadataSimpleTest
, FindAndHighlight_MoreMetaChars
) {
494 std::string highlighted_text
;
495 EXPECT_TRUE(FindAndHighlightWrapper("a&b&c&d", "b&c", &highlighted_text
));
496 EXPECT_EQ("a&<b>b&c</b>&d", highlighted_text
);
499 } // namespace internal