OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 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/spellchecker/spellcheck_host_impl.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/run_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 12 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "components/spellcheck/common/spellcheck_result.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 #if BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 19 #error !BUILDFLAG(USE_BROWSER_SPELLCHECKER) is required for these unit tests. |
| 20 #endif |
| 21 |
| 22 class TestSpellCheckHostImpl { |
| 23 public: |
| 24 TestSpellCheckHostImpl() |
| 25 : spellcheck_(base::MakeUnique<SpellcheckService>(&testing_profile_)) {} |
| 26 |
| 27 SpellcheckCustomDictionary& GetCustomDictionary() const { |
| 28 EXPECT_NE(nullptr, spellcheck_.get()); |
| 29 SpellcheckCustomDictionary* custom_dictionary = |
| 30 spellcheck_->GetCustomDictionary(); |
| 31 return *custom_dictionary; |
| 32 } |
| 33 |
| 34 std::vector<SpellCheckResult> FilterCustomWordResults( |
| 35 const std::string& text, |
| 36 const std::vector<SpellCheckResult>& service_results) const { |
| 37 return SpellCheckHostImpl::FilterCustomWordResults( |
| 38 text, GetCustomDictionary(), service_results); |
| 39 } |
| 40 |
| 41 private: |
| 42 content::TestBrowserThreadBundle thread_bundle_; |
| 43 TestingProfile testing_profile_; |
| 44 std::unique_ptr<SpellcheckService> spellcheck_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(TestSpellCheckHostImpl); |
| 47 }; |
| 48 |
| 49 // Spelling corrections of custom dictionary words should be removed from the |
| 50 // results returned by the remote Spelling service. |
| 51 TEST(SpellCheckHostFilterTest, SpellingServiceCustomResults) { |
| 52 std::vector<SpellCheckResult> service_results; |
| 53 service_results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 0, 6, |
| 54 base::ASCIIToUTF16("Hello"))); |
| 55 service_results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 7, 5, |
| 56 base::ASCIIToUTF16("World"))); |
| 57 TestSpellCheckHostImpl spellcheck_host; |
| 58 spellcheck_host.GetCustomDictionary().AddWord("Helllo"); |
| 59 std::vector<SpellCheckResult> results = |
| 60 spellcheck_host.FilterCustomWordResults("Helllo Warld", service_results); |
| 61 EXPECT_EQ(1u, results.size()); |
| 62 |
| 63 EXPECT_EQ(service_results[1].decoration, results[0].decoration); |
| 64 EXPECT_EQ(service_results[1].location, results[0].location); |
| 65 EXPECT_EQ(service_results[1].length, results[0].length); |
| 66 EXPECT_EQ(service_results[1].replacement, results[0].replacement); |
| 67 } |
| 68 |
| 69 // Spelling corrections of words that are not in the custom dictionary should |
| 70 // be retained in the results returned by the remote Spelling service. |
| 71 TEST(SpellCheckHostFilterTest, SpellingServiceResults) { |
| 72 std::vector<SpellCheckResult> service_results; |
| 73 service_results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 0, 6, |
| 74 base::ASCIIToUTF16("Hello"))); |
| 75 service_results.push_back(SpellCheckResult(SpellCheckResult::SPELLING, 7, 5, |
| 76 base::ASCIIToUTF16("World"))); |
| 77 TestSpellCheckHostImpl spellcheck_host; |
| 78 spellcheck_host.GetCustomDictionary().AddWord("Halo"); |
| 79 std::vector<SpellCheckResult> results = |
| 80 spellcheck_host.FilterCustomWordResults("Helllo Warld", service_results); |
| 81 EXPECT_EQ(2u, results.size()); |
| 82 |
| 83 for (size_t i = 0; i < results.size(); ++i) { |
| 84 EXPECT_EQ(service_results[i].decoration, results[i].decoration); |
| 85 EXPECT_EQ(service_results[i].location, results[i].location); |
| 86 EXPECT_EQ(service_results[i].length, results[i].length); |
| 87 EXPECT_EQ(service_results[i].replacement, results[i].replacement); |
| 88 } |
| 89 } |
OLD | NEW |