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/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/i18n/string_search.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h"
14 #include "chrome/browser/chromeos/drive/file_cache.h"
15 #include "chrome/browser/chromeos/drive/file_system_util.h"
16 #include "chrome/browser/chromeos/drive/test_util.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "testing/gtest/include/gtest/gtest.h"
25 const int kDefaultAtMostNumMatches
= 10;
27 // A simple wrapper for testing FindAndHighlightWrapper(). It just converts the
28 // query text parameter to FixedPatternStringSearchIgnoringCaseAndAccents.
29 bool FindAndHighlightWrapper(
30 const std::string
& text
,
31 const std::string
& query_text
,
32 std::string
* highlighted_text
) {
33 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents
query(
34 base::UTF8ToUTF16(query_text
));
35 return FindAndHighlight(text
, &query
, highlighted_text
);
40 class SearchMetadataTest
: public testing::Test
{
42 virtual void SetUp() OVERRIDE
{
43 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
44 fake_free_disk_space_getter_
.reset(new FakeFreeDiskSpaceGetter
);
46 metadata_storage_
.reset(new ResourceMetadataStorage(
47 temp_dir_
.path(), base::MessageLoopProxy::current().get()));
48 ASSERT_TRUE(metadata_storage_
->Initialize());
50 cache_
.reset(new FileCache(metadata_storage_
.get(),
52 base::MessageLoopProxy::current().get(),
53 fake_free_disk_space_getter_
.get()));
54 ASSERT_TRUE(cache_
->Initialize());
56 resource_metadata_
.reset(
57 new ResourceMetadata(metadata_storage_
.get(),
58 base::MessageLoopProxy::current()));
59 ASSERT_EQ(FILE_ERROR_OK
, resource_metadata_
->Initialize());
61 AddEntriesToMetadata();
64 void AddEntriesToMetadata() {
65 base::FilePath temp_file
;
66 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &temp_file
));
67 const std::string temp_file_md5
= "md5";
73 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->GetIdByPath(
74 util::GetDriveMyDriveRootPath(), &local_id
));
75 const std::string root_local_id
= local_id
;
77 // drive/root/Directory 1
78 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetDirectoryEntry(
79 "Directory 1", "dir1", 1, root_local_id
), &local_id
));
80 const std::string dir1_local_id
= local_id
;
82 // drive/root/Directory 1/SubDirectory File 1.txt
83 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetFileEntry(
84 "SubDirectory File 1.txt", "file1a", 2, dir1_local_id
), &local_id
));
85 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Store(
86 local_id
, temp_file_md5
, temp_file
, FileCache::FILE_OPERATION_COPY
));
88 // drive/root/Directory 1/Shared To The Account Owner.txt
90 "Shared To The Account Owner.txt", "file1b", 3, dir1_local_id
);
91 entry
.set_shared_with_me(true);
92 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
94 // drive/root/Directory 2 excludeDir-test
95 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetDirectoryEntry(
96 "Directory 2 excludeDir-test", "dir2", 4, root_local_id
), &local_id
));
98 // drive/root/Slash \xE2\x88\x95 in directory
99 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(
100 GetDirectoryEntry("Slash \xE2\x88\x95 in directory", "dir3", 5,
101 root_local_id
), &local_id
));
102 const std::string dir3_local_id
= local_id
;
104 // drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt
105 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetFileEntry(
106 "Slash SubDir File.txt", "file3a", 6, dir3_local_id
), &local_id
));
108 // drive/root/File 2.txt
109 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetFileEntry(
110 "File 2.txt", "file2", 7, root_local_id
), &local_id
));
111 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Store(
112 local_id
, temp_file_md5
, temp_file
, FileCache::FILE_OPERATION_COPY
));
114 // drive/root/Document 1 excludeDir-test
115 entry
= GetFileEntry(
116 "Document 1 excludeDir-test", "doc1", 8, root_local_id
);
117 entry
.mutable_file_specific_info()->set_is_hosted_document(true);
118 entry
.mutable_file_specific_info()->set_document_extension(".gdoc");
119 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
123 ResourceEntry
GetFileEntry(const std::string
& name
,
124 const std::string
& resource_id
,
126 const std::string
& parent_local_id
) {
128 entry
.set_title(name
);
129 entry
.set_resource_id(resource_id
);
130 entry
.set_parent_local_id(parent_local_id
);
131 entry
.mutable_file_info()->set_last_accessed(last_accessed
);
135 ResourceEntry
GetDirectoryEntry(const std::string
& name
,
136 const std::string
& resource_id
,
138 const std::string
& parent_local_id
) {
140 entry
.set_title(name
);
141 entry
.set_resource_id(resource_id
);
142 entry
.set_parent_local_id(parent_local_id
);
143 entry
.mutable_file_info()->set_last_accessed(last_accessed
);
144 entry
.mutable_file_info()->set_is_directory(true);
148 content::TestBrowserThreadBundle thread_bundle_
;
149 base::ScopedTempDir temp_dir_
;
150 scoped_ptr
<FakeFreeDiskSpaceGetter
> fake_free_disk_space_getter_
;
151 scoped_ptr
<ResourceMetadataStorage
,
152 test_util::DestroyHelperForTests
> metadata_storage_
;
153 scoped_ptr
<ResourceMetadata
, test_util::DestroyHelperForTests
>
155 scoped_ptr
<FileCache
, test_util::DestroyHelperForTests
> cache_
;
158 TEST_F(SearchMetadataTest
, SearchMetadata_ZeroMatches
) {
159 FileError error
= FILE_ERROR_FAILED
;
160 scoped_ptr
<MetadataSearchResultVector
> result
;
162 SearchMetadata(base::MessageLoopProxy::current(),
163 resource_metadata_
.get(),
166 kDefaultAtMostNumMatches
,
167 google_apis::test_util::CreateCopyResultCallback(
169 base::RunLoop().RunUntilIdle();
170 EXPECT_EQ(FILE_ERROR_OK
, error
);
172 ASSERT_EQ(0U, result
->size());
175 TEST_F(SearchMetadataTest
, SearchMetadata_RegularFile
) {
176 FileError error
= FILE_ERROR_FAILED
;
177 scoped_ptr
<MetadataSearchResultVector
> result
;
179 SearchMetadata(base::MessageLoopProxy::current(),
180 resource_metadata_
.get(),
181 "SubDirectory File 1.txt",
183 kDefaultAtMostNumMatches
,
184 google_apis::test_util::CreateCopyResultCallback(
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.
201 SearchMetadata(base::MessageLoopProxy::current(),
202 resource_metadata_
.get(),
203 "subdirectory file 1.txt",
205 kDefaultAtMostNumMatches
,
206 google_apis::test_util::CreateCopyResultCallback(
208 base::RunLoop().RunUntilIdle();
209 EXPECT_EQ(FILE_ERROR_OK
, error
);
211 ASSERT_EQ(1U, result
->size());
212 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
213 result
->at(0).path
.AsUTF8Unsafe());
216 TEST_F(SearchMetadataTest
, SearchMetadata_RegularFiles
) {
217 FileError error
= FILE_ERROR_FAILED
;
218 scoped_ptr
<MetadataSearchResultVector
> result
;
220 SearchMetadata(base::MessageLoopProxy::current(),
221 resource_metadata_
.get(),
224 kDefaultAtMostNumMatches
,
225 google_apis::test_util::CreateCopyResultCallback(
227 base::RunLoop().RunUntilIdle();
228 EXPECT_EQ(FILE_ERROR_OK
, error
);
230 ASSERT_EQ(2U, result
->size());
232 // The results should be sorted by the last accessed time in descending order.
233 EXPECT_EQ(6, result
->at(0).entry
.file_info().last_accessed());
234 EXPECT_EQ(2, result
->at(1).entry
.file_info().last_accessed());
236 // All base names should contain "File".
237 EXPECT_EQ("drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt",
238 result
->at(0).path
.AsUTF8Unsafe());
239 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
240 result
->at(1).path
.AsUTF8Unsafe());
243 TEST_F(SearchMetadataTest
, SearchMetadata_AtMostOneFile
) {
244 FileError error
= FILE_ERROR_FAILED
;
245 scoped_ptr
<MetadataSearchResultVector
> result
;
247 // There are two files matching "SubDir" but only one file should be
249 SearchMetadata(base::MessageLoopProxy::current(),
250 resource_metadata_
.get(),
253 1, // at_most_num_matches
254 google_apis::test_util::CreateCopyResultCallback(
256 base::RunLoop().RunUntilIdle();
257 EXPECT_EQ(FILE_ERROR_OK
, error
);
259 ASSERT_EQ(1U, result
->size());
260 EXPECT_EQ("drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt",
261 result
->at(0).path
.AsUTF8Unsafe());
264 TEST_F(SearchMetadataTest
, SearchMetadata_Directory
) {
265 FileError error
= FILE_ERROR_FAILED
;
266 scoped_ptr
<MetadataSearchResultVector
> result
;
268 SearchMetadata(base::MessageLoopProxy::current(),
269 resource_metadata_
.get(),
272 kDefaultAtMostNumMatches
,
273 google_apis::test_util::CreateCopyResultCallback(
275 base::RunLoop().RunUntilIdle();
276 EXPECT_EQ(FILE_ERROR_OK
, error
);
278 ASSERT_EQ(1U, result
->size());
279 EXPECT_EQ("drive/root/Directory 1", result
->at(0).path
.AsUTF8Unsafe());
282 TEST_F(SearchMetadataTest
, SearchMetadata_HostedDocument
) {
283 FileError error
= FILE_ERROR_FAILED
;
284 scoped_ptr
<MetadataSearchResultVector
> result
;
286 SearchMetadata(base::MessageLoopProxy::current(),
287 resource_metadata_
.get(),
290 kDefaultAtMostNumMatches
,
291 google_apis::test_util::CreateCopyResultCallback(
293 base::RunLoop().RunUntilIdle();
294 EXPECT_EQ(FILE_ERROR_OK
, error
);
296 ASSERT_EQ(1U, result
->size());
298 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
299 result
->at(0).path
.AsUTF8Unsafe());
302 TEST_F(SearchMetadataTest
, SearchMetadata_ExcludeHostedDocument
) {
303 FileError error
= FILE_ERROR_FAILED
;
304 scoped_ptr
<MetadataSearchResultVector
> result
;
306 SearchMetadata(base::MessageLoopProxy::current(),
307 resource_metadata_
.get(),
309 SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS
,
310 kDefaultAtMostNumMatches
,
311 google_apis::test_util::CreateCopyResultCallback(
313 base::RunLoop().RunUntilIdle();
314 EXPECT_EQ(FILE_ERROR_OK
, error
);
316 ASSERT_EQ(0U, result
->size());
319 TEST_F(SearchMetadataTest
, SearchMetadata_SharedWithMe
) {
320 FileError error
= FILE_ERROR_FAILED
;
321 scoped_ptr
<MetadataSearchResultVector
> result
;
323 SearchMetadata(base::MessageLoopProxy::current(),
324 resource_metadata_
.get(),
326 SEARCH_METADATA_SHARED_WITH_ME
,
327 kDefaultAtMostNumMatches
,
328 google_apis::test_util::CreateCopyResultCallback(
330 base::RunLoop().RunUntilIdle();
331 EXPECT_EQ(FILE_ERROR_OK
, error
);
333 ASSERT_EQ(1U, result
->size());
334 EXPECT_EQ("drive/root/Directory 1/Shared To The Account Owner.txt",
335 result
->at(0).path
.AsUTF8Unsafe());
338 TEST_F(SearchMetadataTest
, SearchMetadata_FileAndDirectory
) {
339 FileError error
= FILE_ERROR_FAILED
;
340 scoped_ptr
<MetadataSearchResultVector
> result
;
342 SearchMetadata(base::MessageLoopProxy::current(),
343 resource_metadata_
.get(),
346 kDefaultAtMostNumMatches
,
347 google_apis::test_util::CreateCopyResultCallback(
350 base::RunLoop().RunUntilIdle();
351 EXPECT_EQ(FILE_ERROR_OK
, error
);
353 ASSERT_EQ(2U, result
->size());
355 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
356 result
->at(0).path
.AsUTF8Unsafe());
357 EXPECT_EQ("drive/root/Directory 2 excludeDir-test",
358 result
->at(1).path
.AsUTF8Unsafe());
361 TEST_F(SearchMetadataTest
, SearchMetadata_ExcludeDirectory
) {
362 FileError error
= FILE_ERROR_FAILED
;
363 scoped_ptr
<MetadataSearchResultVector
> result
;
365 SearchMetadata(base::MessageLoopProxy::current(),
366 resource_metadata_
.get(),
368 SEARCH_METADATA_EXCLUDE_DIRECTORIES
,
369 kDefaultAtMostNumMatches
,
370 google_apis::test_util::CreateCopyResultCallback(
373 base::RunLoop().RunUntilIdle();
374 EXPECT_EQ(FILE_ERROR_OK
, error
);
376 ASSERT_EQ(1U, result
->size());
378 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
379 result
->at(0).path
.AsUTF8Unsafe());
382 // "drive", "drive/root", "drive/other" should be excluded.
383 TEST_F(SearchMetadataTest
, SearchMetadata_ExcludeSpecialDirectories
) {
384 const char* kQueries
[] = { "drive", "root", "other" };
385 for (size_t i
= 0; i
< arraysize(kQueries
); ++i
) {
386 FileError error
= FILE_ERROR_FAILED
;
387 scoped_ptr
<MetadataSearchResultVector
> result
;
389 const std::string query
= kQueries
[i
];
390 SearchMetadata(base::MessageLoopProxy::current(),
391 resource_metadata_
.get(),
394 kDefaultAtMostNumMatches
,
395 google_apis::test_util::CreateCopyResultCallback(
398 base::RunLoop().RunUntilIdle();
399 EXPECT_EQ(FILE_ERROR_OK
, error
);
401 ASSERT_TRUE(result
->empty()) << ": " << query
<< " should not match";
405 TEST_F(SearchMetadataTest
, SearchMetadata_Offline
) {
406 FileError error
= FILE_ERROR_FAILED
;
407 scoped_ptr
<MetadataSearchResultVector
> result
;
409 SearchMetadata(base::MessageLoopProxy::current(),
410 resource_metadata_
.get(),
412 SEARCH_METADATA_OFFLINE
,
413 kDefaultAtMostNumMatches
,
414 google_apis::test_util::CreateCopyResultCallback(
416 base::RunLoop().RunUntilIdle();
417 EXPECT_EQ(FILE_ERROR_OK
, error
);
418 ASSERT_EQ(3U, result
->size());
420 // This is not included in the cache but is a hosted document.
421 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
422 result
->at(0).path
.AsUTF8Unsafe());
424 EXPECT_EQ("drive/root/File 2.txt",
425 result
->at(1).path
.AsUTF8Unsafe());
426 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
427 result
->at(2).path
.AsUTF8Unsafe());
430 TEST(SearchMetadataSimpleTest
, FindAndHighlight_ZeroMatches
) {
431 std::string highlighted_text
;
432 EXPECT_FALSE(FindAndHighlightWrapper("text", "query", &highlighted_text
));
435 TEST(SearchMetadataSimpleTest
, FindAndHighlight_EmptyText
) {
436 std::string highlighted_text
;
437 EXPECT_FALSE(FindAndHighlightWrapper("", "query", &highlighted_text
));
440 TEST(SearchMetadataSimpleTest
, FindAndHighlight_FullMatch
) {
441 std::string highlighted_text
;
442 EXPECT_TRUE(FindAndHighlightWrapper("hello", "hello", &highlighted_text
));
443 EXPECT_EQ("<b>hello</b>", highlighted_text
);
446 TEST(SearchMetadataSimpleTest
, FindAndHighlight_StartWith
) {
447 std::string highlighted_text
;
448 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "hello",
450 EXPECT_EQ("<b>hello</b>, world", highlighted_text
);
453 TEST(SearchMetadataSimpleTest
, FindAndHighlight_EndWith
) {
454 std::string highlighted_text
;
455 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "world",
457 EXPECT_EQ("hello, <b>world</b>", highlighted_text
);
460 TEST(SearchMetadataSimpleTest
, FindAndHighlight_InTheMiddle
) {
461 std::string highlighted_text
;
462 EXPECT_TRUE(FindAndHighlightWrapper("yo hello, world", "hello",
464 EXPECT_EQ("yo <b>hello</b>, world", highlighted_text
);
467 TEST(SearchMetadataSimpleTest
, FindAndHighlight_MultipeMatches
) {
468 std::string highlighted_text
;
469 EXPECT_TRUE(FindAndHighlightWrapper("yoyoyoyoy", "yoy", &highlighted_text
));
470 // Only the first match is highlighted.
471 EXPECT_EQ("<b>yoy</b>oyoyoy", highlighted_text
);
474 TEST(SearchMetadataSimpleTest
, FindAndHighlight_IgnoreCase
) {
475 std::string highlighted_text
;
476 EXPECT_TRUE(FindAndHighlightWrapper("HeLLo", "hello", &highlighted_text
));
477 EXPECT_EQ("<b>HeLLo</b>", highlighted_text
);
480 TEST(SearchMetadataSimpleTest
, FindAndHighlight_IgnoreCaseNonASCII
) {
481 std::string highlighted_text
;
483 // Case and accent ignorance in Greek. Find "socra" in "Socra'tes".
484 EXPECT_TRUE(FindAndHighlightWrapper(
485 "\xCE\xA3\xCF\x89\xCE\xBA\xCF\x81\xCE\xAC\xCF\x84\xCE\xB7\xCF\x82",
486 "\xCF\x83\xCF\x89\xCE\xBA\xCF\x81\xCE\xB1", &highlighted_text
));
488 "<b>\xCE\xA3\xCF\x89\xCE\xBA\xCF\x81\xCE\xAC</b>\xCF\x84\xCE\xB7\xCF\x82",
491 // In Japanese characters.
492 // Find Hiragana "pi" + "(small)ya" in Katakana "hi" + semi-voiced-mark + "ya"
493 EXPECT_TRUE(FindAndHighlightWrapper(
494 "\xE3\x81\xB2\xE3\x82\x9A\xE3\x82\x83\xE3\x83\xBC",
495 "\xE3\x83\x94\xE3\x83\xA4",
498 "<b>\xE3\x81\xB2\xE3\x82\x9A\xE3\x82\x83</b>\xE3\x83\xBC",
502 TEST(SearchMetadataSimpleTest
, MultiTextBySingleQuery
) {
503 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents
query(
504 base::UTF8ToUTF16("hello"));
506 std::string highlighted_text
;
507 EXPECT_TRUE(FindAndHighlight("hello", &query
, &highlighted_text
));
508 EXPECT_EQ("<b>hello</b>", highlighted_text
);
509 EXPECT_FALSE(FindAndHighlight("goodbye", &query
, &highlighted_text
));
510 EXPECT_TRUE(FindAndHighlight("1hello2", &query
, &highlighted_text
));
511 EXPECT_EQ("1<b>hello</b>2", highlighted_text
);
514 TEST(SearchMetadataSimpleTest
, FindAndHighlight_MetaChars
) {
515 std::string highlighted_text
;
516 EXPECT_TRUE(FindAndHighlightWrapper("<hello>", "hello", &highlighted_text
));
517 EXPECT_EQ("<<b>hello</b>>", highlighted_text
);
520 TEST(SearchMetadataSimpleTest
, FindAndHighlight_MoreMetaChars
) {
521 std::string highlighted_text
;
522 EXPECT_TRUE(FindAndHighlightWrapper("a&b&c&d", "b&c", &highlighted_text
));
523 EXPECT_EQ("a&<b>b&c</b>&d", highlighted_text
);
526 } // namespace internal