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