| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/drive/search_metadata.h" | |
| 6 | |
| 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" | |
| 21 | |
| 22 namespace drive { | |
| 23 namespace internal { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const int kDefaultAtMostNumMatches = 10; | |
| 28 | |
| 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); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 class SearchMetadataTest : public testing::Test { | |
| 43 protected: | |
| 44 void SetUp() override { | |
| 45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 46 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); | |
| 47 | |
| 48 metadata_storage_.reset(new ResourceMetadataStorage( | |
| 49 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get().get())); | |
| 50 ASSERT_TRUE(metadata_storage_->Initialize()); | |
| 51 | |
| 52 cache_.reset(new FileCache(metadata_storage_.get(), | |
| 53 temp_dir_.path(), | |
| 54 base::ThreadTaskRunnerHandle::Get().get(), | |
| 55 fake_free_disk_space_getter_.get())); | |
| 56 ASSERT_TRUE(cache_->Initialize()); | |
| 57 | |
| 58 resource_metadata_.reset( | |
| 59 new ResourceMetadata(metadata_storage_.get(), | |
| 60 cache_.get(), | |
| 61 base::ThreadTaskRunnerHandle::Get().get())); | |
| 62 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->Initialize()); | |
| 63 | |
| 64 AddEntriesToMetadata(); | |
| 65 } | |
| 66 | |
| 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"; | |
| 71 | |
| 72 ResourceEntry entry; | |
| 73 std::string local_id; | |
| 74 | |
| 75 // drive/root | |
| 76 EXPECT_EQ(FILE_ERROR_OK, resource_metadata_->GetIdByPath( | |
| 77 util::GetDriveMyDriveRootPath(), &local_id)); | |
| 78 const std::string root_local_id = local_id; | |
| 79 | |
| 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; | |
| 84 | |
| 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)); | |
| 90 | |
| 91 // drive/root/Directory 1/Shared To The Account Owner.txt | |
| 92 entry = GetFileEntry( | |
| 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)); | |
| 96 | |
| 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)); | |
| 100 | |
| 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; | |
| 106 | |
| 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)); | |
| 110 | |
| 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)); | |
| 116 | |
| 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)); | |
| 125 } | |
| 126 | |
| 127 ResourceEntry GetFileEntry(const std::string& name, | |
| 128 const std::string& resource_id, | |
| 129 int64 last_accessed, | |
| 130 const std::string& parent_local_id) { | |
| 131 ResourceEntry entry; | |
| 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); | |
| 136 return entry; | |
| 137 } | |
| 138 | |
| 139 ResourceEntry GetDirectoryEntry(const std::string& name, | |
| 140 const std::string& resource_id, | |
| 141 int64 last_accessed, | |
| 142 const std::string& parent_local_id) { | |
| 143 ResourceEntry entry; | |
| 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); | |
| 149 return entry; | |
| 150 } | |
| 151 | |
| 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> | |
| 158 resource_metadata_; | |
| 159 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; | |
| 160 }; | |
| 161 | |
| 162 TEST_F(SearchMetadataTest, SearchMetadata_ZeroMatches) { | |
| 163 FileError error = FILE_ERROR_FAILED; | |
| 164 scoped_ptr<MetadataSearchResultVector> result; | |
| 165 | |
| 166 SearchMetadata( | |
| 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); | |
| 173 ASSERT_TRUE(result); | |
| 174 ASSERT_EQ(0U, result->size()); | |
| 175 } | |
| 176 | |
| 177 TEST_F(SearchMetadataTest, SearchMetadata_RegularFile) { | |
| 178 FileError error = FILE_ERROR_FAILED; | |
| 179 scoped_ptr<MetadataSearchResultVector> result; | |
| 180 | |
| 181 SearchMetadata( | |
| 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); | |
| 188 ASSERT_TRUE(result); | |
| 189 ASSERT_EQ(1U, result->size()); | |
| 190 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt", | |
| 191 result->at(0).path.AsUTF8Unsafe()); | |
| 192 } | |
| 193 | |
| 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; | |
| 199 | |
| 200 // The query is all in lower case. | |
| 201 SearchMetadata( | |
| 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); | |
| 208 ASSERT_TRUE(result); | |
| 209 ASSERT_EQ(1U, result->size()); | |
| 210 EXPECT_EQ("drive/root/Directory 1/SubDirectory File 1.txt", | |
| 211 result->at(0).path.AsUTF8Unsafe()); | |
| 212 } | |
| 213 | |
| 214 TEST_F(SearchMetadataTest, SearchMetadata_RegularFiles) { | |
| 215 FileError error = FILE_ERROR_FAILED; | |
| 216 scoped_ptr<MetadataSearchResultVector> result; | |
| 217 | |
| 218 SearchMetadata( | |
| 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); | |
| 224 ASSERT_TRUE(result); | |
| 225 ASSERT_EQ(2U, result->size()); | |
| 226 | |
| 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()); | |
| 233 } | |
| 234 | |
| 235 TEST_F(SearchMetadataTest, SearchMetadata_AtMostOneFile) { | |
| 236 FileError error = FILE_ERROR_FAILED; | |
| 237 scoped_ptr<MetadataSearchResultVector> result; | |
| 238 | |
| 239 // There are two files matching "SubDir" but only one file should be | |
| 240 // returned. | |
| 241 SearchMetadata( | |
| 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); | |
| 248 ASSERT_TRUE(result); | |
| 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()); | |
| 252 } | |
| 253 | |
| 254 TEST_F(SearchMetadataTest, SearchMetadata_Directory) { | |
| 255 FileError error = FILE_ERROR_FAILED; | |
| 256 scoped_ptr<MetadataSearchResultVector> result; | |
| 257 | |
| 258 SearchMetadata( | |
| 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); | |
| 265 ASSERT_TRUE(result); | |
| 266 ASSERT_EQ(1U, result->size()); | |
| 267 EXPECT_EQ("drive/root/Directory 1", result->at(0).path.AsUTF8Unsafe()); | |
| 268 } | |
| 269 | |
| 270 TEST_F(SearchMetadataTest, SearchMetadata_HostedDocument) { | |
| 271 FileError error = FILE_ERROR_FAILED; | |
| 272 scoped_ptr<MetadataSearchResultVector> result; | |
| 273 | |
| 274 SearchMetadata( | |
| 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); | |
| 280 ASSERT_TRUE(result); | |
| 281 ASSERT_EQ(1U, result->size()); | |
| 282 | |
| 283 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc", | |
| 284 result->at(0).path.AsUTF8Unsafe()); | |
| 285 } | |
| 286 | |
| 287 TEST_F(SearchMetadataTest, SearchMetadata_ExcludeHostedDocument) { | |
| 288 FileError error = FILE_ERROR_FAILED; | |
| 289 scoped_ptr<MetadataSearchResultVector> result; | |
| 290 | |
| 291 SearchMetadata( | |
| 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); | |
| 298 ASSERT_TRUE(result); | |
| 299 ASSERT_EQ(0U, result->size()); | |
| 300 } | |
| 301 | |
| 302 TEST_F(SearchMetadataTest, SearchMetadata_SharedWithMe) { | |
| 303 FileError error = FILE_ERROR_FAILED; | |
| 304 scoped_ptr<MetadataSearchResultVector> result; | |
| 305 | |
| 306 SearchMetadata( | |
| 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); | |
| 313 ASSERT_TRUE(result); | |
| 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()); | |
| 317 } | |
| 318 | |
| 319 TEST_F(SearchMetadataTest, SearchMetadata_FileAndDirectory) { | |
| 320 FileError error = FILE_ERROR_FAILED; | |
| 321 scoped_ptr<MetadataSearchResultVector> result; | |
| 322 | |
| 323 SearchMetadata( | |
| 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)); | |
| 328 | |
| 329 base::RunLoop().RunUntilIdle(); | |
| 330 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 331 ASSERT_TRUE(result); | |
| 332 ASSERT_EQ(2U, result->size()); | |
| 333 | |
| 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()); | |
| 338 } | |
| 339 | |
| 340 TEST_F(SearchMetadataTest, SearchMetadata_ExcludeDirectory) { | |
| 341 FileError error = FILE_ERROR_FAILED; | |
| 342 scoped_ptr<MetadataSearchResultVector> result; | |
| 343 | |
| 344 SearchMetadata( | |
| 345 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(), | |
| 346 "excludeDir-test", | |
| 347 base::Bind(&MatchesType, SEARCH_METADATA_EXCLUDE_DIRECTORIES), | |
| 348 kDefaultAtMostNumMatches, | |
| 349 google_apis::test_util::CreateCopyResultCallback(&error, &result)); | |
| 350 | |
| 351 base::RunLoop().RunUntilIdle(); | |
| 352 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 353 ASSERT_TRUE(result); | |
| 354 ASSERT_EQ(1U, result->size()); | |
| 355 | |
| 356 EXPECT_EQ("drive/root/Document 1 excludeDir-test.gdoc", | |
| 357 result->at(0).path.AsUTF8Unsafe()); | |
| 358 } | |
| 359 | |
| 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; | |
| 366 | |
| 367 const std::string query = kQueries[i]; | |
| 368 SearchMetadata( | |
| 369 base::ThreadTaskRunnerHandle::Get(), resource_metadata_.get(), query, | |
| 370 base::Bind(&MatchesType, SEARCH_METADATA_ALL), kDefaultAtMostNumMatches, | |
| 371 google_apis::test_util::CreateCopyResultCallback(&error, &result)); | |
| 372 | |
| 373 base::RunLoop().RunUntilIdle(); | |
| 374 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 375 ASSERT_TRUE(result); | |
| 376 ASSERT_TRUE(result->empty()) << ": " << query << " should not match"; | |
| 377 } | |
| 378 } | |
| 379 | |
| 380 TEST_F(SearchMetadataTest, SearchMetadata_Offline) { | |
| 381 FileError error = FILE_ERROR_FAILED; | |
| 382 scoped_ptr<MetadataSearchResultVector> result; | |
| 383 | |
| 384 SearchMetadata( | |
| 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()); | |
| 392 | |
| 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()); | |
| 396 | |
| 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()); | |
| 401 } | |
| 402 | |
| 403 TEST(SearchMetadataSimpleTest, FindAndHighlight_ZeroMatches) { | |
| 404 std::string highlighted_text; | |
| 405 EXPECT_FALSE(FindAndHighlightWrapper("text", "query", &highlighted_text)); | |
| 406 } | |
| 407 | |
| 408 TEST(SearchMetadataSimpleTest, FindAndHighlight_EmptyText) { | |
| 409 std::string highlighted_text; | |
| 410 EXPECT_FALSE(FindAndHighlightWrapper("", "query", &highlighted_text)); | |
| 411 } | |
| 412 | |
| 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); | |
| 417 } | |
| 418 | |
| 419 TEST(SearchMetadataSimpleTest, FindAndHighlight_StartWith) { | |
| 420 std::string highlighted_text; | |
| 421 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "hello", | |
| 422 &highlighted_text)); | |
| 423 EXPECT_EQ("<b>hello</b>, world", highlighted_text); | |
| 424 } | |
| 425 | |
| 426 TEST(SearchMetadataSimpleTest, FindAndHighlight_EndWith) { | |
| 427 std::string highlighted_text; | |
| 428 EXPECT_TRUE(FindAndHighlightWrapper("hello, world", "world", | |
| 429 &highlighted_text)); | |
| 430 EXPECT_EQ("hello, <b>world</b>", highlighted_text); | |
| 431 } | |
| 432 | |
| 433 TEST(SearchMetadataSimpleTest, FindAndHighlight_InTheMiddle) { | |
| 434 std::string highlighted_text; | |
| 435 EXPECT_TRUE(FindAndHighlightWrapper("yo hello, world", "hello", | |
| 436 &highlighted_text)); | |
| 437 EXPECT_EQ("yo <b>hello</b>, world", highlighted_text); | |
| 438 } | |
| 439 | |
| 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); | |
| 445 } | |
| 446 | |
| 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); | |
| 451 } | |
| 452 | |
| 453 TEST(SearchMetadataSimpleTest, FindAndHighlight_IgnoreCaseNonASCII) { | |
| 454 std::string highlighted_text; | |
| 455 | |
| 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)); | |
| 460 EXPECT_EQ( | |
| 461 "<b>\xCE\xA3\xCF\x89\xCE\xBA\xCF\x81\xCE\xAC</b>\xCF\x84\xCE\xB7\xCF\x82", | |
| 462 highlighted_text); | |
| 463 | |
| 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", | |
| 469 &highlighted_text)); | |
| 470 EXPECT_EQ( | |
| 471 "<b>\xE3\x81\xB2\xE3\x82\x9A\xE3\x82\x83</b>\xE3\x83\xBC", | |
| 472 highlighted_text); | |
| 473 } | |
| 474 | |
| 475 TEST(SearchMetadataSimpleTest, MultiTextBySingleQuery) { | |
| 476 base::i18n::FixedPatternStringSearchIgnoringCaseAndAccents query( | |
| 477 base::UTF8ToUTF16("hello")); | |
| 478 | |
| 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); | |
| 485 } | |
| 486 | |
| 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); | |
| 491 } | |
| 492 | |
| 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); | |
| 497 } | |
| 498 | |
| 499 } // namespace internal | |
| 500 } // namespace drive | |
| OLD | NEW |