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/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 "chrome/browser/drive/drive_api_util.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
26 const int kDefaultAtMostNumMatches
= 10;
28 // A simple wrapper for testing FindAndHighlightWrapper(). It just converts the
29 // query text parameter to FixedPatternStringSearchIgnoringCaseAndAccents.
30 bool FindAndHighlightWrapper(
31 const std::string
& text
,
32 const std::string
& query_text
,
33 std::string
* highlighted_text
) {
34 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents
query(
35 base::UTF8ToUTF16(query_text
));
36 return FindAndHighlight(text
, &query
, highlighted_text
);
41 class SearchMetadataTest
: public testing::Test
{
43 void SetUp() override
{
44 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
45 fake_free_disk_space_getter_
.reset(new FakeFreeDiskSpaceGetter
);
47 metadata_storage_
.reset(new ResourceMetadataStorage(
48 temp_dir_
.path(), base::MessageLoopProxy::current().get()));
49 ASSERT_TRUE(metadata_storage_
->Initialize());
51 cache_
.reset(new FileCache(metadata_storage_
.get(),
53 base::MessageLoopProxy::current().get(),
54 fake_free_disk_space_getter_
.get()));
55 ASSERT_TRUE(cache_
->Initialize());
57 resource_metadata_
.reset(
58 new ResourceMetadata(metadata_storage_
.get(),
60 base::MessageLoopProxy::current()));
61 ASSERT_EQ(FILE_ERROR_OK
, resource_metadata_
->Initialize());
63 AddEntriesToMetadata();
66 void AddEntriesToMetadata() {
67 base::FilePath temp_file
;
68 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &temp_file
));
69 const std::string temp_file_md5
= "md5";
75 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->GetIdByPath(
76 util::GetDriveMyDriveRootPath(), &local_id
));
77 const std::string root_local_id
= local_id
;
79 // drive/root/Directory 1
80 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetDirectoryEntry(
81 "Directory 1", "dir1", 1, root_local_id
), &local_id
));
82 const std::string dir1_local_id
= local_id
;
84 // drive/root/Directory 1/SubDirectory File 1.txt
85 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetFileEntry(
86 "SubDirectory File 1.txt", "file1a", 2, dir1_local_id
), &local_id
));
87 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Store(
88 local_id
, temp_file_md5
, temp_file
, FileCache::FILE_OPERATION_COPY
));
90 // drive/root/Directory 1/Shared To The Account Owner.txt
92 "Shared To The Account Owner.txt", "file1b", 3, dir1_local_id
);
93 entry
.set_shared_with_me(true);
94 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
96 // drive/root/Directory 2 excludeDir-test
97 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetDirectoryEntry(
98 "Directory 2 excludeDir-test", "dir2", 4, root_local_id
), &local_id
));
100 // drive/root/Slash \xE2\x88\x95 in directory
101 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(
102 GetDirectoryEntry("Slash \xE2\x88\x95 in directory", "dir3", 5,
103 root_local_id
), &local_id
));
104 const std::string dir3_local_id
= local_id
;
106 // drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt
107 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetFileEntry(
108 "Slash SubDir File.txt", "file3a", 6, dir3_local_id
), &local_id
));
110 // drive/root/File 2.txt
111 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(GetFileEntry(
112 "File 2.txt", "file2", 7, root_local_id
), &local_id
));
113 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Store(
114 local_id
, temp_file_md5
, temp_file
, FileCache::FILE_OPERATION_COPY
));
116 // drive/root/Document 1 excludeDir-test
117 entry
= GetFileEntry(
118 "Document 1 excludeDir-test", "doc1", 8, root_local_id
);
119 entry
.mutable_file_specific_info()->set_is_hosted_document(true);
120 entry
.mutable_file_specific_info()->set_document_extension(".gdoc");
121 entry
.mutable_file_specific_info()->set_content_mime_type(
122 drive::util::kGoogleDocumentMimeType
);
123 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
126 ResourceEntry
GetFileEntry(const std::string
& name
,
127 const std::string
& resource_id
,
129 const std::string
& parent_local_id
) {
131 entry
.set_title(name
);
132 entry
.set_resource_id(resource_id
);
133 entry
.set_parent_local_id(parent_local_id
);
134 entry
.mutable_file_info()->set_last_accessed(last_accessed
);
138 ResourceEntry
GetDirectoryEntry(const std::string
& name
,
139 const std::string
& resource_id
,
141 const std::string
& parent_local_id
) {
143 entry
.set_title(name
);
144 entry
.set_resource_id(resource_id
);
145 entry
.set_parent_local_id(parent_local_id
);
146 entry
.mutable_file_info()->set_last_accessed(last_accessed
);
147 entry
.mutable_file_info()->set_is_directory(true);
151 content::TestBrowserThreadBundle thread_bundle_
;
152 base::ScopedTempDir temp_dir_
;
153 scoped_ptr
<FakeFreeDiskSpaceGetter
> fake_free_disk_space_getter_
;
154 scoped_ptr
<ResourceMetadataStorage
,
155 test_util::DestroyHelperForTests
> metadata_storage_
;
156 scoped_ptr
<ResourceMetadata
, test_util::DestroyHelperForTests
>
158 scoped_ptr
<FileCache
, test_util::DestroyHelperForTests
> cache_
;
161 TEST_F(SearchMetadataTest
, SearchMetadata_ZeroMatches
) {
162 FileError error
= FILE_ERROR_FAILED
;
163 scoped_ptr
<MetadataSearchResultVector
> result
;
166 base::MessageLoopProxy::current(), resource_metadata_
.get(),
167 "NonExistent", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
168 kDefaultAtMostNumMatches
,
169 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
170 base::RunLoop().RunUntilIdle();
171 EXPECT_EQ(FILE_ERROR_OK
, error
);
173 ASSERT_EQ(0U, result
->size());
176 TEST_F(SearchMetadataTest
, SearchMetadata_RegularFile
) {
177 FileError error
= FILE_ERROR_FAILED
;
178 scoped_ptr
<MetadataSearchResultVector
> result
;
181 base::MessageLoopProxy::current(), resource_metadata_
.get(),
182 "SubDirectory File 1.txt", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
183 kDefaultAtMostNumMatches
,
184 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
185 base::RunLoop().RunUntilIdle();
186 EXPECT_EQ(FILE_ERROR_OK
, error
);
188 ASSERT_EQ(1U, result
->size());
189 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
190 result
->at(0).path
.AsUTF8Unsafe());
193 // This test checks if |FindAndHighlightWrapper| does case-insensitive search.
194 // Tricker test cases for |FindAndHighlightWrapper| can be found below.
195 TEST_F(SearchMetadataTest
, SearchMetadata_CaseInsensitiveSearch
) {
196 FileError error
= FILE_ERROR_FAILED
;
197 scoped_ptr
<MetadataSearchResultVector
> result
;
199 // The query is all in lower case.
201 base::MessageLoopProxy::current(), resource_metadata_
.get(),
202 "subdirectory file 1.txt", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
203 kDefaultAtMostNumMatches
,
204 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
205 base::RunLoop().RunUntilIdle();
206 EXPECT_EQ(FILE_ERROR_OK
, error
);
208 ASSERT_EQ(1U, result
->size());
209 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
210 result
->at(0).path
.AsUTF8Unsafe());
213 TEST_F(SearchMetadataTest
, SearchMetadata_RegularFiles
) {
214 FileError error
= FILE_ERROR_FAILED
;
215 scoped_ptr
<MetadataSearchResultVector
> result
;
218 base::MessageLoopProxy::current(), resource_metadata_
.get(), "SubDir",
219 base::Bind(&MatchesType
, SEARCH_METADATA_ALL
), kDefaultAtMostNumMatches
,
220 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
221 base::RunLoop().RunUntilIdle();
222 EXPECT_EQ(FILE_ERROR_OK
, error
);
224 ASSERT_EQ(2U, result
->size());
226 // All base names should contain "File". The results should be sorted by the
227 // last accessed time in descending order.
228 EXPECT_EQ("drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt",
229 result
->at(0).path
.AsUTF8Unsafe());
230 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
231 result
->at(1).path
.AsUTF8Unsafe());
234 TEST_F(SearchMetadataTest
, SearchMetadata_AtMostOneFile
) {
235 FileError error
= FILE_ERROR_FAILED
;
236 scoped_ptr
<MetadataSearchResultVector
> result
;
238 // There are two files matching "SubDir" but only one file should be
241 base::MessageLoopProxy::current(), resource_metadata_
.get(), "SubDir",
242 base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
243 1, // at_most_num_matches
244 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
245 base::RunLoop().RunUntilIdle();
246 EXPECT_EQ(FILE_ERROR_OK
, error
);
248 ASSERT_EQ(1U, result
->size());
249 EXPECT_EQ("drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt",
250 result
->at(0).path
.AsUTF8Unsafe());
253 TEST_F(SearchMetadataTest
, SearchMetadata_Directory
) {
254 FileError error
= FILE_ERROR_FAILED
;
255 scoped_ptr
<MetadataSearchResultVector
> result
;
258 base::MessageLoopProxy::current(), resource_metadata_
.get(),
259 "Directory 1", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
260 kDefaultAtMostNumMatches
,
261 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
262 base::RunLoop().RunUntilIdle();
263 EXPECT_EQ(FILE_ERROR_OK
, error
);
265 ASSERT_EQ(1U, result
->size());
266 EXPECT_EQ("drive/root/Directory 1", result
->at(0).path
.AsUTF8Unsafe());
269 TEST_F(SearchMetadataTest
, SearchMetadata_HostedDocument
) {
270 FileError error
= FILE_ERROR_FAILED
;
271 scoped_ptr
<MetadataSearchResultVector
> result
;
274 base::MessageLoopProxy::current(), resource_metadata_
.get(), "Document",
275 base::Bind(&MatchesType
, SEARCH_METADATA_ALL
), kDefaultAtMostNumMatches
,
276 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
277 base::RunLoop().RunUntilIdle();
278 EXPECT_EQ(FILE_ERROR_OK
, error
);
280 ASSERT_EQ(1U, result
->size());
282 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
283 result
->at(0).path
.AsUTF8Unsafe());
286 TEST_F(SearchMetadataTest
, SearchMetadata_ExcludeHostedDocument
) {
287 FileError error
= FILE_ERROR_FAILED
;
288 scoped_ptr
<MetadataSearchResultVector
> result
;
291 base::MessageLoopProxy::current(), resource_metadata_
.get(), "Document",
292 base::Bind(&MatchesType
, SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS
),
293 kDefaultAtMostNumMatches
,
294 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
295 base::RunLoop().RunUntilIdle();
296 EXPECT_EQ(FILE_ERROR_OK
, error
);
298 ASSERT_EQ(0U, result
->size());
301 TEST_F(SearchMetadataTest
, SearchMetadata_SharedWithMe
) {
302 FileError error
= FILE_ERROR_FAILED
;
303 scoped_ptr
<MetadataSearchResultVector
> result
;
306 base::MessageLoopProxy::current(), resource_metadata_
.get(), "",
307 base::Bind(&MatchesType
, SEARCH_METADATA_SHARED_WITH_ME
),
308 kDefaultAtMostNumMatches
,
309 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
310 base::RunLoop().RunUntilIdle();
311 EXPECT_EQ(FILE_ERROR_OK
, error
);
313 ASSERT_EQ(1U, result
->size());
314 EXPECT_EQ("drive/root/Directory 1/Shared To The Account Owner.txt",
315 result
->at(0).path
.AsUTF8Unsafe());
318 TEST_F(SearchMetadataTest
, SearchMetadata_FileAndDirectory
) {
319 FileError error
= FILE_ERROR_FAILED
;
320 scoped_ptr
<MetadataSearchResultVector
> result
;
323 base::MessageLoopProxy::current(), resource_metadata_
.get(),
324 "excludeDir-test", base::Bind(&MatchesType
, SEARCH_METADATA_ALL
),
325 kDefaultAtMostNumMatches
,
326 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
328 base::RunLoop().RunUntilIdle();
329 EXPECT_EQ(FILE_ERROR_OK
, error
);
331 ASSERT_EQ(2U, result
->size());
333 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
334 result
->at(0).path
.AsUTF8Unsafe());
335 EXPECT_EQ("drive/root/Directory 2 excludeDir-test",
336 result
->at(1).path
.AsUTF8Unsafe());
339 TEST_F(SearchMetadataTest
, SearchMetadata_ExcludeDirectory
) {
340 FileError error
= FILE_ERROR_FAILED
;
341 scoped_ptr
<MetadataSearchResultVector
> result
;
344 base::MessageLoopProxy::current(), resource_metadata_
.get(),
346 base::Bind(&MatchesType
, SEARCH_METADATA_EXCLUDE_DIRECTORIES
),
347 kDefaultAtMostNumMatches
,
348 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
350 base::RunLoop().RunUntilIdle();
351 EXPECT_EQ(FILE_ERROR_OK
, error
);
353 ASSERT_EQ(1U, result
->size());
355 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
356 result
->at(0).path
.AsUTF8Unsafe());
359 // "drive", "drive/root", "drive/other" should be excluded.
360 TEST_F(SearchMetadataTest
, SearchMetadata_ExcludeSpecialDirectories
) {
361 const char* const kQueries
[] = { "drive", "root", "other" };
362 for (size_t i
= 0; i
< arraysize(kQueries
); ++i
) {
363 FileError error
= FILE_ERROR_FAILED
;
364 scoped_ptr
<MetadataSearchResultVector
> result
;
366 const std::string query
= kQueries
[i
];
368 base::MessageLoopProxy::current(), resource_metadata_
.get(), query
,
369 base::Bind(&MatchesType
, SEARCH_METADATA_ALL
), kDefaultAtMostNumMatches
,
370 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
372 base::RunLoop().RunUntilIdle();
373 EXPECT_EQ(FILE_ERROR_OK
, error
);
375 ASSERT_TRUE(result
->empty()) << ": " << query
<< " should not match";
379 TEST_F(SearchMetadataTest
, SearchMetadata_Offline
) {
380 FileError error
= FILE_ERROR_FAILED
;
381 scoped_ptr
<MetadataSearchResultVector
> result
;
384 base::MessageLoopProxy::current(), resource_metadata_
.get(), "",
385 base::Bind(&MatchesType
, SEARCH_METADATA_OFFLINE
),
386 kDefaultAtMostNumMatches
,
387 google_apis::test_util::CreateCopyResultCallback(&error
, &result
));
388 base::RunLoop().RunUntilIdle();
389 EXPECT_EQ(FILE_ERROR_OK
, error
);
390 ASSERT_EQ(3U, result
->size());
392 // This is not included in the cache but is a hosted document.
393 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
394 result
->at(0).path
.AsUTF8Unsafe());
396 EXPECT_EQ("drive/root/File 2.txt",
397 result
->at(1).path
.AsUTF8Unsafe());
398 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt",
399 result
->at(2).path
.AsUTF8Unsafe());
402 TEST(SearchMetadataSimpleTest
, FindAndHighlight_ZeroMatches
) {
403 std::string highlighted_text
;
404 EXPECT_FALSE(FindAndHighlightWrapper("text", "query", &highlighted_text
));
407 TEST(SearchMetadataSimpleTest
, FindAndHighlight_EmptyText
) {
408 std::string highlighted_text
;
409 EXPECT_FALSE(FindAndHighlightWrapper("", "query", &highlighted_text
));
412 TEST(SearchMetadataSimpleTest
, FindAndHighlight_FullMatch
) {
413 std::string highlighted_text
;
414 EXPECT_TRUE(FindAndHighlightWrapper("hello", "hello", &highlighted_text
));
415 EXPECT_EQ("<b>hello</b>", highlighted_text
);
418 TEST(SearchMetadataSimpleTest
, FindAndHighlight_StartWith
) {
419 std::string highlighted_text
;
420 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "hello",
422 EXPECT_EQ("<b>hello</b>, world", highlighted_text
);
425 TEST(SearchMetadataSimpleTest
, FindAndHighlight_EndWith
) {
426 std::string highlighted_text
;
427 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "world",
429 EXPECT_EQ("hello, <b>world</b>", highlighted_text
);
432 TEST(SearchMetadataSimpleTest
, FindAndHighlight_InTheMiddle
) {
433 std::string highlighted_text
;
434 EXPECT_TRUE(FindAndHighlightWrapper("yo hello, world", "hello",
436 EXPECT_EQ("yo <b>hello</b>, world", highlighted_text
);
439 TEST(SearchMetadataSimpleTest
, FindAndHighlight_MultipeMatches
) {
440 std::string highlighted_text
;
441 EXPECT_TRUE(FindAndHighlightWrapper("yoyoyoyoy", "yoy", &highlighted_text
));
442 // Only the first match is highlighted.
443 EXPECT_EQ("<b>yoy</b>oyoyoy", highlighted_text
);
446 TEST(SearchMetadataSimpleTest
, FindAndHighlight_IgnoreCase
) {
447 std::string highlighted_text
;
448 EXPECT_TRUE(FindAndHighlightWrapper("HeLLo", "hello", &highlighted_text
));
449 EXPECT_EQ("<b>HeLLo</b>", highlighted_text
);
452 TEST(SearchMetadataSimpleTest
, FindAndHighlight_IgnoreCaseNonASCII
) {
453 std::string highlighted_text
;
455 // Case and accent ignorance in Greek. Find "socra" in "Socra'tes".
456 EXPECT_TRUE(FindAndHighlightWrapper(
457 "\xCE\xA3\xCF\x89\xCE\xBA\xCF\x81\xCE\xAC\xCF\x84\xCE\xB7\xCF\x82",
458 "\xCF\x83\xCF\x89\xCE\xBA\xCF\x81\xCE\xB1", &highlighted_text
));
460 "<b>\xCE\xA3\xCF\x89\xCE\xBA\xCF\x81\xCE\xAC</b>\xCF\x84\xCE\xB7\xCF\x82",
463 // In Japanese characters.
464 // Find Hiragana "pi" + "(small)ya" in Katakana "hi" + semi-voiced-mark + "ya"
465 EXPECT_TRUE(FindAndHighlightWrapper(
466 "\xE3\x81\xB2\xE3\x82\x9A\xE3\x82\x83\xE3\x83\xBC",
467 "\xE3\x83\x94\xE3\x83\xA4",
470 "<b>\xE3\x81\xB2\xE3\x82\x9A\xE3\x82\x83</b>\xE3\x83\xBC",
474 TEST(SearchMetadataSimpleTest
, MultiTextBySingleQuery
) {
475 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents
query(
476 base::UTF8ToUTF16("hello"));
478 std::string highlighted_text
;
479 EXPECT_TRUE(FindAndHighlight("hello", &query
, &highlighted_text
));
480 EXPECT_EQ("<b>hello</b>", highlighted_text
);
481 EXPECT_FALSE(FindAndHighlight("goodbye", &query
, &highlighted_text
));
482 EXPECT_TRUE(FindAndHighlight("1hello2", &query
, &highlighted_text
));
483 EXPECT_EQ("1<b>hello</b>2", highlighted_text
);
486 TEST(SearchMetadataSimpleTest
, FindAndHighlight_MetaChars
) {
487 std::string highlighted_text
;
488 EXPECT_TRUE(FindAndHighlightWrapper("<hello>", "hello", &highlighted_text
));
489 EXPECT_EQ("<<b>hello</b>>", highlighted_text
);
492 TEST(SearchMetadataSimpleTest
, FindAndHighlight_MoreMetaChars
) {
493 std::string highlighted_text
;
494 EXPECT_TRUE(FindAndHighlightWrapper("a&b&c&d", "b&c", &highlighted_text
));
495 EXPECT_EQ("a&<b>b&c</b>&d", highlighted_text
);
498 } // namespace internal