Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / drive / search_metadata_unittest.cc
blob538d154c3fb9dc43608244ecfdfc0aa334877dd3
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 "components/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/memory/scoped_vector.h"
11 #include "base/run_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "components/drive/drive_api_util.h"
16 #include "components/drive/drive_test_util.h"
17 #include "components/drive/fake_free_disk_space_getter.h"
18 #include "components/drive/file_cache.h"
19 #include "components/drive/file_system_core_util.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace drive {
24 namespace internal {
26 namespace {
28 const int kDefaultAtMostNumMatches = 10;
30 // A simple wrapper for testing FindAndHighlightWrapper(). It just converts the
31 // query text parameter to FixedPatternStringSearchIgnoringCaseAndAccents.
32 bool FindAndHighlightWrapper(
33 const std::string& text,
34 const std::string& query_text,
35 std::string* highlighted_text) {
36 ScopedVector<base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents>
37 queries;
38 queries.push_back(
39 new base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents(
40 base::UTF8ToUTF16(query_text)));
41 return FindAndHighlight(text, queries, highlighted_text);
44 } // namespace
46 class SearchMetadataTest : public testing::Test {
47 protected:
48 void SetUp() override {
49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
50 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter);
52 metadata_storage_.reset(new ResourceMetadataStorage(
53 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get().get()));
54 ASSERT_TRUE(metadata_storage_->Initialize());
56 cache_.reset(new FileCache(metadata_storage_.get(),
57 temp_dir_.path(),
58 base::ThreadTaskRunnerHandle::Get().get(),
59 fake_free_disk_space_getter_.get()));
60 ASSERT_TRUE(cache_->Initialize());
62 resource_metadata_.reset(
63 new ResourceMetadata(metadata_storage_.get(),
64 cache_.get(),
65 base::ThreadTaskRunnerHandle::Get().get()));
66 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->Initialize());
68 AddEntriesToMetadata();
71 void AddEntriesToMetadata() {
72 base::FilePath temp_file;
73 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file));
74 const std::string temp_file_md5 = "md5";
76 ResourceEntry entry;
77 std::string local_id;
79 // drive/root
80 EXPECT_EQ(FILE_ERROR_OK, resource_metadata_->GetIdByPath(
81 util::GetDriveMyDriveRootPath(), &local_id));
82 const std::string root_local_id = local_id;
84 // drive/root/Directory-1
85 EXPECT_EQ(FILE_ERROR_OK,
86 resource_metadata_->AddEntry(
87 GetDirectoryEntry("Directory-1", "dir1", 1, root_local_id),
88 &local_id));
89 const std::string dir1_local_id = local_id;
91 // drive/root/Directory-1/SubDirectory File 1.txt
92 EXPECT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(GetFileEntry(
93 "SubDirectory File 1.txt", "file1a", 2, dir1_local_id), &local_id));
94 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
95 local_id, temp_file_md5, temp_file, FileCache::FILE_OPERATION_COPY));
97 // drive/root/Directory-1/Shared To The Account Owner.txt
98 entry = GetFileEntry(
99 "Shared To The Account Owner.txt", "file1b", 3, dir1_local_id);
100 entry.set_shared_with_me(true);
101 EXPECT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(entry, &local_id));
103 // drive/root/Directory 2 excludeDir-test
104 EXPECT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(GetDirectoryEntry(
105 "Directory 2 excludeDir-test", "dir2", 4, root_local_id), &local_id));
107 // drive/root/Slash \xE2\x88\x95 in directory
108 EXPECT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(
109 GetDirectoryEntry("Slash \xE2\x88\x95 in directory", "dir3", 5,
110 root_local_id), &local_id));
111 const std::string dir3_local_id = local_id;
113 // drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt
114 EXPECT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(GetFileEntry(
115 "Slash SubDir File.txt", "file3a", 6, dir3_local_id), &local_id));
117 // drive/root/File 2.txt
118 EXPECT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(GetFileEntry(
119 "File 2.txt", "file2", 7, root_local_id), &local_id));
120 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
121 local_id, temp_file_md5, temp_file, FileCache::FILE_OPERATION_COPY));
123 // drive/root/Document 1 excludeDir-test
124 entry = GetFileEntry(
125 "Document 1 excludeDir-test", "doc1", 8, root_local_id);
126 entry.mutable_file_specific_info()->set_is_hosted_document(true);
127 entry.mutable_file_specific_info()->set_document_extension(".gdoc");
128 entry.mutable_file_specific_info()->set_content_mime_type(
129 drive::util::kGoogleDocumentMimeType);
130 EXPECT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(entry, &local_id));
133 ResourceEntry GetFileEntry(const std::string& name,
134 const std::string& resource_id,
135 int64 last_accessed,
136 const std::string& parent_local_id) {
137 ResourceEntry entry;
138 entry.set_title(name);
139 entry.set_resource_id(resource_id);
140 entry.set_parent_local_id(parent_local_id);
141 entry.mutable_file_info()->set_last_accessed(last_accessed);
142 return entry;
145 ResourceEntry GetDirectoryEntry(const std::string& name,
146 const std::string& resource_id,
147 int64 last_accessed,
148 const std::string& parent_local_id) {
149 ResourceEntry entry;
150 entry.set_title(name);
151 entry.set_resource_id(resource_id);
152 entry.set_parent_local_id(parent_local_id);
153 entry.mutable_file_info()->set_last_accessed(last_accessed);
154 entry.mutable_file_info()->set_is_directory(true);
155 return entry;
158 content::TestBrowserThreadBundle thread_bundle_;
159 base::ScopedTempDir temp_dir_;
160 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_;
161 scoped_ptr<ResourceMetadataStorage,
162 test_util::DestroyHelperForTests> metadata_storage_;
163 scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests>
164 resource_metadata_;
165 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
168 TEST_F(SearchMetadataTest, SearchMetadata_ZeroMatches) {
169 FileError error = FILE_ERROR_FAILED;
170 scoped_ptr<MetadataSearchResultVector> result;
172 SearchMetadata(
173 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(),
174 "NonExistent", base::Bind(&MatchesType, SEARCH_METADATA_ALL),
175 kDefaultAtMostNumMatches,
176 google_apis::test_util::CreateCopyResultCallback(&error, &result));
177 base::RunLoop().RunUntilIdle();
178 EXPECT_EQ(FILE_ERROR_OK, error);
179 ASSERT_TRUE(result);
180 ASSERT_EQ(0U, result->size());
183 TEST_F(SearchMetadataTest, SearchMetadata_RegularFile) {
184 FileError error = FILE_ERROR_FAILED;
185 scoped_ptr<MetadataSearchResultVector> result;
187 SearchMetadata(
188 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(),
189 "SubDirectory File 1.txt", base::Bind(&MatchesType, SEARCH_METADATA_ALL),
190 kDefaultAtMostNumMatches,
191 google_apis::test_util::CreateCopyResultCallback(&error, &result));
192 base::RunLoop().RunUntilIdle();
193 EXPECT_EQ(FILE_ERROR_OK, error);
194 ASSERT_TRUE(result);
195 ASSERT_EQ(1U, result->size());
196 EXPECT_EQ("drive/root/Directory-1/SubDirectory File 1.txt",
197 result->at(0).path.AsUTF8Unsafe());
200 // This test checks if |FindAndHighlightWrapper| does case-insensitive search.
201 // Tricker test cases for |FindAndHighlightWrapper| can be found below.
202 TEST_F(SearchMetadataTest, SearchMetadata_CaseInsensitiveSearch) {
203 FileError error = FILE_ERROR_FAILED;
204 scoped_ptr<MetadataSearchResultVector> result;
206 // The query is all in lower case.
207 SearchMetadata(
208 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(),
209 "subdirectory file 1.txt", base::Bind(&MatchesType, SEARCH_METADATA_ALL),
210 kDefaultAtMostNumMatches,
211 google_apis::test_util::CreateCopyResultCallback(&error, &result));
212 base::RunLoop().RunUntilIdle();
213 EXPECT_EQ(FILE_ERROR_OK, error);
214 ASSERT_TRUE(result);
215 ASSERT_EQ(1U, result->size());
216 EXPECT_EQ("drive/root/Directory-1/SubDirectory File 1.txt",
217 result->at(0).path.AsUTF8Unsafe());
220 TEST_F(SearchMetadataTest, SearchMetadata_RegularFiles) {
221 FileError error = FILE_ERROR_FAILED;
222 scoped_ptr<MetadataSearchResultVector> result;
224 SearchMetadata(
225 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(), "SubDir",
226 base::Bind(&MatchesType, SEARCH_METADATA_ALL), kDefaultAtMostNumMatches,
227 google_apis::test_util::CreateCopyResultCallback(&error, &result));
228 base::RunLoop().RunUntilIdle();
229 EXPECT_EQ(FILE_ERROR_OK, error);
230 ASSERT_TRUE(result);
231 ASSERT_EQ(2U, result->size());
233 // All base names should contain "File". The results should be sorted by the
234 // last accessed time in descending order.
235 EXPECT_EQ("drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt",
236 result->at(0).path.AsUTF8Unsafe());
237 EXPECT_EQ("drive/root/Directory-1/SubDirectory File 1.txt",
238 result->at(1).path.AsUTF8Unsafe());
241 TEST_F(SearchMetadataTest, SearchMetadata_AtMostOneFile) {
242 FileError error = FILE_ERROR_FAILED;
243 scoped_ptr<MetadataSearchResultVector> result;
245 // There are two files matching "SubDir" but only one file should be
246 // returned.
247 SearchMetadata(
248 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(), "SubDir",
249 base::Bind(&MatchesType, SEARCH_METADATA_ALL),
250 1, // at_most_num_matches
251 google_apis::test_util::CreateCopyResultCallback(&error, &result));
252 base::RunLoop().RunUntilIdle();
253 EXPECT_EQ(FILE_ERROR_OK, error);
254 ASSERT_TRUE(result);
255 ASSERT_EQ(1U, result->size());
256 EXPECT_EQ("drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt",
257 result->at(0).path.AsUTF8Unsafe());
260 TEST_F(SearchMetadataTest, SearchMetadata_Directory) {
261 FileError error = FILE_ERROR_FAILED;
262 scoped_ptr<MetadataSearchResultVector> result;
264 SearchMetadata(
265 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(),
266 "Directory-1", base::Bind(&MatchesType, SEARCH_METADATA_ALL),
267 kDefaultAtMostNumMatches,
268 google_apis::test_util::CreateCopyResultCallback(&error, &result));
269 base::RunLoop().RunUntilIdle();
270 EXPECT_EQ(FILE_ERROR_OK, error);
271 ASSERT_TRUE(result);
272 ASSERT_EQ(1U, result->size());
273 EXPECT_EQ("drive/root/Directory-1", result->at(0).path.AsUTF8Unsafe());
276 TEST_F(SearchMetadataTest, SearchMetadata_HostedDocument) {
277 FileError error = FILE_ERROR_FAILED;
278 scoped_ptr<MetadataSearchResultVector> result;
280 SearchMetadata(
281 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(), "Document",
282 base::Bind(&MatchesType, SEARCH_METADATA_ALL), kDefaultAtMostNumMatches,
283 google_apis::test_util::CreateCopyResultCallback(&error, &result));
284 base::RunLoop().RunUntilIdle();
285 EXPECT_EQ(FILE_ERROR_OK, error);
286 ASSERT_TRUE(result);
287 ASSERT_EQ(1U, result->size());
289 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
290 result->at(0).path.AsUTF8Unsafe());
293 TEST_F(SearchMetadataTest, SearchMetadata_ExcludeHostedDocument) {
294 FileError error = FILE_ERROR_FAILED;
295 scoped_ptr<MetadataSearchResultVector> result;
297 SearchMetadata(
298 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(), "Document",
299 base::Bind(&MatchesType, SEARCH_METADATA_EXCLUDE_HOSTED_DOCUMENTS),
300 kDefaultAtMostNumMatches,
301 google_apis::test_util::CreateCopyResultCallback(&error, &result));
302 base::RunLoop().RunUntilIdle();
303 EXPECT_EQ(FILE_ERROR_OK, error);
304 ASSERT_TRUE(result);
305 ASSERT_EQ(0U, result->size());
308 TEST_F(SearchMetadataTest, SearchMetadata_SharedWithMe) {
309 FileError error = FILE_ERROR_FAILED;
310 scoped_ptr<MetadataSearchResultVector> result;
312 SearchMetadata(
313 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(), "",
314 base::Bind(&MatchesType, SEARCH_METADATA_SHARED_WITH_ME),
315 kDefaultAtMostNumMatches,
316 google_apis::test_util::CreateCopyResultCallback(&error, &result));
317 base::RunLoop().RunUntilIdle();
318 EXPECT_EQ(FILE_ERROR_OK, error);
319 ASSERT_TRUE(result);
320 ASSERT_EQ(1U, result->size());
321 EXPECT_EQ("drive/root/Directory-1/Shared To The Account Owner.txt",
322 result->at(0).path.AsUTF8Unsafe());
325 TEST_F(SearchMetadataTest, SearchMetadata_FileAndDirectory) {
326 FileError error = FILE_ERROR_FAILED;
327 scoped_ptr<MetadataSearchResultVector> result;
329 SearchMetadata(
330 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(),
331 "excludeDir-test", base::Bind(&MatchesType, SEARCH_METADATA_ALL),
332 kDefaultAtMostNumMatches,
333 google_apis::test_util::CreateCopyResultCallback(&error, &result));
335 base::RunLoop().RunUntilIdle();
336 EXPECT_EQ(FILE_ERROR_OK, error);
337 ASSERT_TRUE(result);
338 ASSERT_EQ(2U, result->size());
340 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
341 result->at(0).path.AsUTF8Unsafe());
342 EXPECT_EQ("drive/root/Directory 2 excludeDir-test",
343 result->at(1).path.AsUTF8Unsafe());
346 TEST_F(SearchMetadataTest, SearchMetadata_ExcludeDirectory) {
347 FileError error = FILE_ERROR_FAILED;
348 scoped_ptr<MetadataSearchResultVector> result;
350 SearchMetadata(
351 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(),
352 "excludeDir-test",
353 base::Bind(&MatchesType, SEARCH_METADATA_EXCLUDE_DIRECTORIES),
354 kDefaultAtMostNumMatches,
355 google_apis::test_util::CreateCopyResultCallback(&error, &result));
357 base::RunLoop().RunUntilIdle();
358 EXPECT_EQ(FILE_ERROR_OK, error);
359 ASSERT_TRUE(result);
360 ASSERT_EQ(1U, result->size());
362 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
363 result->at(0).path.AsUTF8Unsafe());
366 // "drive", "drive/root", "drive/other" should be excluded.
367 TEST_F(SearchMetadataTest, SearchMetadata_ExcludeSpecialDirectories) {
368 const char* const kQueries[] = { "drive", "root", "other" };
369 for (size_t i = 0; i < arraysize(kQueries); ++i) {
370 FileError error = FILE_ERROR_FAILED;
371 scoped_ptr<MetadataSearchResultVector> result;
373 const std::string query = kQueries[i];
374 SearchMetadata(
375 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(), query,
376 base::Bind(&MatchesType, SEARCH_METADATA_ALL), kDefaultAtMostNumMatches,
377 google_apis::test_util::CreateCopyResultCallback(&error, &result));
379 base::RunLoop().RunUntilIdle();
380 EXPECT_EQ(FILE_ERROR_OK, error);
381 ASSERT_TRUE(result);
382 ASSERT_TRUE(result->empty()) << ": " << query << " should not match";
386 TEST_F(SearchMetadataTest, SearchMetadata_Offline) {
387 FileError error = FILE_ERROR_FAILED;
388 scoped_ptr<MetadataSearchResultVector> result;
390 SearchMetadata(
391 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(), "",
392 base::Bind(&MatchesType, SEARCH_METADATA_OFFLINE),
393 kDefaultAtMostNumMatches,
394 google_apis::test_util::CreateCopyResultCallback(&error, &result));
395 base::RunLoop().RunUntilIdle();
396 EXPECT_EQ(FILE_ERROR_OK, error);
397 ASSERT_EQ(3U, result->size());
399 // This is not included in the cache but is a hosted document.
400 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc",
401 result->at(0).path.AsUTF8Unsafe());
403 EXPECT_EQ("drive/root/File 2.txt",
404 result->at(1).path.AsUTF8Unsafe());
405 EXPECT_EQ("drive/root/Directory-1/SubDirectory File 1.txt",
406 result->at(2).path.AsUTF8Unsafe());
409 TEST_F(SearchMetadataTest, SearchMetadata_MultipleKeywords) {
410 FileError error = FILE_ERROR_FAILED;
411 scoped_ptr<MetadataSearchResultVector> result;
413 SearchMetadata(
414 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(),
415 "Directory 1", base::Bind(&MatchesType, SEARCH_METADATA_ALL),
416 kDefaultAtMostNumMatches,
417 google_apis::test_util::CreateCopyResultCallback(&error, &result));
419 base::RunLoop().RunUntilIdle();
420 EXPECT_EQ(FILE_ERROR_OK, error);
421 ASSERT_TRUE(result);
422 ASSERT_EQ(2U, result->size());
424 EXPECT_EQ("drive/root/Directory-1/SubDirectory File 1.txt",
425 result->at(0).path.AsUTF8Unsafe());
426 EXPECT_EQ("drive/root/Directory-1", result->at(1).path.AsUTF8Unsafe());
429 TEST_F(SearchMetadataTest,
430 SearchMetadata_KeywordsSeparatedWithIdeographicSpace) {
431 FileError error = FILE_ERROR_FAILED;
432 scoped_ptr<MetadataSearchResultVector> result;
434 // \xE3\x80\x80 is ideographic space.
435 SearchMetadata(
436 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(),
437 "Directory\xE3\x80\x80"
438 "1",
439 base::Bind(&MatchesType, SEARCH_METADATA_ALL), kDefaultAtMostNumMatches,
440 google_apis::test_util::CreateCopyResultCallback(&error, &result));
442 base::RunLoop().RunUntilIdle();
443 EXPECT_EQ(FILE_ERROR_OK, error);
444 ASSERT_TRUE(result);
445 ASSERT_EQ(2U, result->size());
447 EXPECT_EQ("drive/root/Directory-1/SubDirectory File 1.txt",
448 result->at(0).path.AsUTF8Unsafe());
449 EXPECT_EQ("drive/root/Directory-1", result->at(1).path.AsUTF8Unsafe());
452 TEST(SearchMetadataSimpleTest, FindAndHighlight_ZeroMatches) {
453 std::string highlighted_text;
454 EXPECT_FALSE(FindAndHighlightWrapper("text", "query", &highlighted_text));
457 TEST(SearchMetadataSimpleTest, FindAndHighlight_EmptyText) {
458 std::string highlighted_text;
459 EXPECT_FALSE(FindAndHighlightWrapper("", "query", &highlighted_text));
462 TEST(SearchMetadataSimpleTest, FindAndHighlight_EmptyQuery) {
463 ScopedVector<base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents>
464 queries;
466 std::string highlighted_text;
467 EXPECT_TRUE(FindAndHighlight("hello", queries, &highlighted_text));
468 EXPECT_EQ("hello", highlighted_text);
471 TEST(SearchMetadataSimpleTest, FindAndHighlight_FullMatch) {
472 std::string highlighted_text;
473 EXPECT_TRUE(FindAndHighlightWrapper("hello", "hello", &highlighted_text));
474 EXPECT_EQ("<b>hello</b>", highlighted_text);
477 TEST(SearchMetadataSimpleTest, FindAndHighlight_StartWith) {
478 std::string highlighted_text;
479 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "hello",
480 &highlighted_text));
481 EXPECT_EQ("<b>hello</b>, world", highlighted_text);
484 TEST(SearchMetadataSimpleTest, FindAndHighlight_EndWith) {
485 std::string highlighted_text;
486 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "world",
487 &highlighted_text));
488 EXPECT_EQ("hello, <b>world</b>", highlighted_text);
491 TEST(SearchMetadataSimpleTest, FindAndHighlight_InTheMiddle) {
492 std::string highlighted_text;
493 EXPECT_TRUE(FindAndHighlightWrapper("yo hello, world", "hello",
494 &highlighted_text));
495 EXPECT_EQ("yo <b>hello</b>, world", highlighted_text);
498 TEST(SearchMetadataSimpleTest, FindAndHighlight_MultipeMatches) {
499 std::string highlighted_text;
500 EXPECT_TRUE(FindAndHighlightWrapper("yoyoyoyoy", "yoy", &highlighted_text));
501 // Only the first match is highlighted.
502 EXPECT_EQ("<b>yoy</b>oyoyoy", highlighted_text);
505 TEST(SearchMetadataSimpleTest, FindAndHighlight_IgnoreCase) {
506 std::string highlighted_text;
507 EXPECT_TRUE(FindAndHighlightWrapper("HeLLo", "hello", &highlighted_text));
508 EXPECT_EQ("<b>HeLLo</b>", highlighted_text);
511 TEST(SearchMetadataSimpleTest, FindAndHighlight_IgnoreCaseNonASCII) {
512 std::string highlighted_text;
514 // Case and accent ignorance in Greek. Find "socra" in "Socra'tes".
515 EXPECT_TRUE(FindAndHighlightWrapper(
516 "\xCE\xA3\xCF\x89\xCE\xBA\xCF\x81\xCE\xAC\xCF\x84\xCE\xB7\xCF\x82",
517 "\xCF\x83\xCF\x89\xCE\xBA\xCF\x81\xCE\xB1", &highlighted_text));
518 EXPECT_EQ(
519 "<b>\xCE\xA3\xCF\x89\xCE\xBA\xCF\x81\xCE\xAC</b>\xCF\x84\xCE\xB7\xCF\x82",
520 highlighted_text);
522 // In Japanese characters.
523 // Find Hiragana "pi" + "(small)ya" in Katakana "hi" + semi-voiced-mark + "ya"
524 EXPECT_TRUE(FindAndHighlightWrapper(
525 "\xE3\x81\xB2\xE3\x82\x9A\xE3\x82\x83\xE3\x83\xBC",
526 "\xE3\x83\x94\xE3\x83\xA4",
527 &highlighted_text));
528 EXPECT_EQ(
529 "<b>\xE3\x81\xB2\xE3\x82\x9A\xE3\x82\x83</b>\xE3\x83\xBC",
530 highlighted_text);
533 TEST(SearchMetadataSimpleTest, MultiTextBySingleQuery) {
534 ScopedVector<base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents>
535 queries;
536 queries.push_back(
537 new base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents(
538 base::UTF8ToUTF16("hello")));
540 std::string highlighted_text;
541 EXPECT_TRUE(FindAndHighlight("hello", queries, &highlighted_text));
542 EXPECT_EQ("<b>hello</b>", highlighted_text);
543 EXPECT_FALSE(FindAndHighlight("goodbye", queries, &highlighted_text));
544 EXPECT_TRUE(FindAndHighlight("1hello2", queries, &highlighted_text));
545 EXPECT_EQ("1<b>hello</b>2", highlighted_text);
548 TEST(SearchMetadataSimpleTest, FindAndHighlight_MetaChars) {
549 std::string highlighted_text;
550 EXPECT_TRUE(FindAndHighlightWrapper("<hello>", "hello", &highlighted_text));
551 EXPECT_EQ("&lt;<b>hello</b>&gt;", highlighted_text);
554 TEST(SearchMetadataSimpleTest, FindAndHighlight_MoreMetaChars) {
555 std::string highlighted_text;
556 EXPECT_TRUE(FindAndHighlightWrapper("a&b&c&d", "b&c", &highlighted_text));
557 EXPECT_EQ("a&amp;<b>b&amp;c</b>&amp;d", highlighted_text);
560 TEST(SearchMetadataSimpleTest, FindAndHighlight_SurrogatePair) {
561 std::string highlighted_text;
562 // \xF0\x9F\x98\x81 (U+1F601) is a surrogate pair for smile icon of emoji.
563 EXPECT_TRUE(FindAndHighlightWrapper("hi\xF0\x9F\x98\x81hello",
564 "i\xF0\x9F\x98\x81", &highlighted_text));
565 EXPECT_EQ("h<b>i\xF0\x9F\x98\x81</b>hello", highlighted_text);
568 TEST(SearchMetadataSimpleTest, FindAndHighlight_MultipleQueries) {
569 ScopedVector<base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents>
570 queries;
571 queries.push_back(
572 new base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents(
573 base::UTF8ToUTF16("hello")));
574 queries.push_back(
575 new base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents(
576 base::UTF8ToUTF16("good")));
578 std::string highlighted_text;
579 EXPECT_TRUE(
580 FindAndHighlight("good morning, hello", queries, &highlighted_text));
581 EXPECT_EQ("<b>good</b> morning, <b>hello</b>", highlighted_text);
584 TEST(SearchMetadataSimpleTest, FindAndHighlight_OverlappingHighlights) {
585 ScopedVector<base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents>
586 queries;
587 queries.push_back(
588 new base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents(
589 base::UTF8ToUTF16("morning")));
590 queries.push_back(
591 new base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents(
592 base::UTF8ToUTF16("ing,")));
594 std::string highlighted_text;
595 EXPECT_TRUE(
596 FindAndHighlight("good morning, hello", queries, &highlighted_text));
597 EXPECT_EQ("good <b>morning,</b> hello", highlighted_text);
600 } // namespace internal
601 } // namespace drive