OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/spellchecker/spellcheck_service.h" | 5 #include "chrome/browser/spellchecker/spellcheck_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/string_split.h" |
| 17 #include "base/strings/string_util.h" |
16 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
17 #include "base/tuple.h" | 19 #include "base/tuple.h" |
18 #include "base/values.h" | 20 #include "base/values.h" |
19 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 22 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
21 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
22 #include "chrome/common/chrome_paths.h" | 24 #include "chrome/common/chrome_paths.h" |
23 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
25 #include "chrome/common/spellcheck_common.h" | 27 #include "chrome/common/spellcheck_common.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 BrowserContext* GetContext() { | 81 BrowserContext* GetContext() { |
80 return static_cast<BrowserContext*>(browser()->profile()); | 82 return static_cast<BrowserContext*>(browser()->profile()); |
81 } | 83 } |
82 | 84 |
83 PrefService* GetPrefs() { | 85 PrefService* GetPrefs() { |
84 return prefs_; | 86 return prefs_; |
85 } | 87 } |
86 | 88 |
87 void InitSpellcheck(bool enable_spellcheck, | 89 void InitSpellcheck(bool enable_spellcheck, |
88 const std::string& single_dictionary, | 90 const std::string& single_dictionary, |
89 const std::vector<std::string>& multiple_dictionaries) { | 91 const std::string& multiple_dictionaries) { |
90 prefs_->SetBoolean(prefs::kEnableContinuousSpellcheck, enable_spellcheck); | 92 prefs_->SetBoolean(prefs::kEnableContinuousSpellcheck, enable_spellcheck); |
91 prefs_->SetString(prefs::kSpellCheckDictionary, single_dictionary); | 93 prefs_->SetString(prefs::kSpellCheckDictionary, single_dictionary); |
92 base::ListValue dictionaries_value; | 94 base::ListValue dictionaries_value; |
93 dictionaries_value.AppendStrings(multiple_dictionaries); | 95 dictionaries_value.AppendStrings( |
| 96 base::SplitString(multiple_dictionaries, ",", base::TRIM_WHITESPACE, |
| 97 base::SPLIT_WANT_NONEMPTY)); |
94 prefs_->Set(prefs::kSpellCheckDictionaries, dictionaries_value); | 98 prefs_->Set(prefs::kSpellCheckDictionaries, dictionaries_value); |
95 SpellcheckService* spellcheck = | 99 SpellcheckService* spellcheck = |
96 SpellcheckServiceFactory::GetForRenderProcessId(renderer_->GetID()); | 100 SpellcheckServiceFactory::GetForRenderProcessId(renderer_->GetID()); |
97 ASSERT_NE(nullptr, spellcheck); | 101 ASSERT_NE(nullptr, spellcheck); |
98 spellcheck->InitForRenderer(renderer_.get()); | 102 spellcheck->InitForRenderer(renderer_.get()); |
99 } | 103 } |
100 | 104 |
101 void EnableSpellcheck(bool enable_spellcheck) { | 105 void EnableSpellcheck(bool enable_spellcheck) { |
102 ScopedPreferenceChange scope(&renderer_->sink()); | 106 ScopedPreferenceChange scope(&renderer_->sink()); |
103 prefs_->SetBoolean(prefs::kEnableContinuousSpellcheck, enable_spellcheck); | 107 prefs_->SetBoolean(prefs::kEnableContinuousSpellcheck, enable_spellcheck); |
104 } | 108 } |
105 | 109 |
106 void SetSingleLanguageDictionary(const std::string& single_dictionary) { | 110 void SetSingleLanguageDictionary(const std::string& single_dictionary) { |
107 ScopedPreferenceChange scope(&renderer_->sink()); | 111 ScopedPreferenceChange scope(&renderer_->sink()); |
108 prefs_->SetString(prefs::kSpellCheckDictionary, single_dictionary); | 112 prefs_->SetString(prefs::kSpellCheckDictionary, single_dictionary); |
109 } | 113 } |
110 | 114 |
111 void SetMultiLingualDictionaries( | 115 void SetMultiLingualDictionaries(const std::string& multiple_dictionaries) { |
112 const std::vector<std::string>& multiple_dictionaries) { | |
113 ScopedPreferenceChange scope(&renderer_->sink()); | 116 ScopedPreferenceChange scope(&renderer_->sink()); |
114 base::ListValue dictionaries_value; | 117 base::ListValue dictionaries_value; |
115 dictionaries_value.AppendStrings(multiple_dictionaries); | 118 dictionaries_value.AppendStrings( |
| 119 base::SplitString(multiple_dictionaries, ",", base::TRIM_WHITESPACE, |
| 120 base::SPLIT_WANT_NONEMPTY)); |
116 prefs_->Set(prefs::kSpellCheckDictionaries, dictionaries_value); | 121 prefs_->Set(prefs::kSpellCheckDictionaries, dictionaries_value); |
117 } | 122 } |
118 | 123 |
| 124 std::string GetMultilingualDictionaries() { |
| 125 const base::ListValue* list_value = |
| 126 prefs_->GetList(prefs::kSpellCheckDictionaries); |
| 127 std::vector<std::string> dictionaries; |
| 128 for (const auto& item_value : *list_value) { |
| 129 std::string dictionary; |
| 130 EXPECT_TRUE(item_value->GetAsString(&dictionary)); |
| 131 dictionaries.push_back(dictionary); |
| 132 } |
| 133 return base::JoinString(dictionaries, ","); |
| 134 } |
| 135 |
| 136 void SetAcceptLanguages(const std::string& accept_languages) { |
| 137 ScopedPreferenceChange scope(&renderer_->sink()); |
| 138 prefs_->SetString(prefs::kAcceptLanguages, accept_languages); |
| 139 } |
| 140 |
119 // Returns the boolean parameter sent in the first | 141 // Returns the boolean parameter sent in the first |
120 // SpellCheckMsg_EnableSpellCheck message. For example, if spellcheck service | 142 // SpellCheckMsg_EnableSpellCheck message. For example, if spellcheck service |
121 // sent the SpellCheckMsg_EnableSpellCheck(true) message, then this method | 143 // sent the SpellCheckMsg_EnableSpellCheck(true) message, then this method |
122 // returns true. | 144 // returns true. |
123 bool GetFirstEnableSpellcheckMessageParam() { | 145 bool GetFirstEnableSpellcheckMessageParam() { |
124 const IPC::Message* message = renderer_->sink().GetFirstMessageMatching( | 146 const IPC::Message* message = renderer_->sink().GetFirstMessageMatching( |
125 SpellCheckMsg_EnableSpellCheck::ID); | 147 SpellCheckMsg_EnableSpellCheck::ID); |
126 EXPECT_NE(nullptr, message); | 148 EXPECT_NE(nullptr, message); |
127 if (!message) | 149 if (!message) |
128 return false; | 150 return false; |
129 | 151 |
130 SpellCheckMsg_EnableSpellCheck::Param param; | 152 SpellCheckMsg_EnableSpellCheck::Param param; |
131 bool ok = SpellCheckMsg_EnableSpellCheck::Read(message, ¶m); | 153 bool ok = SpellCheckMsg_EnableSpellCheck::Read(message, ¶m); |
132 EXPECT_TRUE(ok); | 154 EXPECT_TRUE(ok); |
133 if (!ok) | 155 if (!ok) |
134 return false; | 156 return false; |
135 | 157 |
136 return base::get<0>(param); | 158 return base::get<0>(param); |
137 } | 159 } |
138 | 160 |
139 private: | 161 private: |
140 scoped_ptr<content::MockRenderProcessHost> renderer_; | 162 scoped_ptr<content::MockRenderProcessHost> renderer_; |
141 | 163 |
142 // Not owned preferences service. | 164 // Not owned preferences service. |
143 PrefService* prefs_; | 165 PrefService* prefs_; |
144 }; | 166 }; |
145 | 167 |
| 168 // Removing a spellcheck language from accept languages should remove it from |
| 169 // spellcheck languages list as well. |
| 170 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, |
| 171 RemoveSpellcheckLanguageFromAcceptLanguages) { |
| 172 InitSpellcheck(true, "", "en-US,fr"); |
| 173 SetAcceptLanguages("en-US,es,ru"); |
| 174 EXPECT_EQ("en-US", GetMultilingualDictionaries()); |
| 175 } |
| 176 |
| 177 // Keeping spellcheck languages in accept languages should not alter spellcheck |
| 178 // languages list. |
| 179 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, |
| 180 KeepSpellcheckLanguagesInAcceptLanguages) { |
| 181 InitSpellcheck(true, "", "en-US,fr"); |
| 182 SetAcceptLanguages("en-US,fr,es"); |
| 183 EXPECT_EQ("en-US,fr", GetMultilingualDictionaries()); |
| 184 } |
| 185 |
146 // Starting with spellcheck enabled should send the 'enable spellcheck' message | 186 // Starting with spellcheck enabled should send the 'enable spellcheck' message |
147 // to the renderer. Consequently disabling spellcheck should send the 'disable | 187 // to the renderer. Consequently disabling spellcheck should send the 'disable |
148 // spellcheck' message to the renderer. | 188 // spellcheck' message to the renderer. |
149 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, StartWithSpellcheck) { | 189 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, StartWithSpellcheck) { |
150 std::vector<std::string> dictionaries; | 190 InitSpellcheck(true, "", "en-US,fr"); |
151 dictionaries.push_back("en-US"); | |
152 dictionaries.push_back("fr"); | |
153 InitSpellcheck(true, "", dictionaries); | |
154 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); | 191 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); |
155 | 192 |
156 EnableSpellcheck(false); | 193 EnableSpellcheck(false); |
157 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); | 194 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); |
158 } | 195 } |
159 | 196 |
160 // Starting with only a single-language spellcheck setting should send the | 197 // Starting with only a single-language spellcheck setting should send the |
161 // 'enable spellcheck' message to the renderer. Consequently removing spellcheck | 198 // 'enable spellcheck' message to the renderer. Consequently removing spellcheck |
162 // languages should disable spellcheck. | 199 // languages should disable spellcheck. |
163 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, | 200 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, |
164 StartWithSingularLanguagePreference) { | 201 StartWithSingularLanguagePreference) { |
165 InitSpellcheck(true, "en-US", std::vector<std::string>()); | 202 InitSpellcheck(true, "en-US", ""); |
166 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); | 203 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); |
167 | 204 |
168 SetMultiLingualDictionaries(std::vector<std::string>()); | 205 SetMultiLingualDictionaries(""); |
169 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); | 206 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); |
170 } | 207 } |
171 | 208 |
172 // Starting with a multi-language spellcheck setting should send the 'enable | 209 // Starting with a multi-language spellcheck setting should send the 'enable |
173 // spellcheck' message to the renderer. Consequently removing spellcheck | 210 // spellcheck' message to the renderer. Consequently removing spellcheck |
174 // languages should disable spellcheck. | 211 // languages should disable spellcheck. |
175 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, | 212 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, |
176 StartWithMultiLanguagePreference) { | 213 StartWithMultiLanguagePreference) { |
177 std::vector<std::string> dictionaries; | 214 InitSpellcheck(true, "", "en-US,fr"); |
178 dictionaries.push_back("en-US"); | |
179 dictionaries.push_back("fr"); | |
180 InitSpellcheck(true, "", dictionaries); | |
181 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); | 215 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); |
182 | 216 |
183 SetMultiLingualDictionaries(std::vector<std::string>()); | 217 SetMultiLingualDictionaries(""); |
184 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); | 218 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); |
185 } | 219 } |
186 | 220 |
187 // Starting with both single-language and multi-language spellcheck settings | 221 // Starting with both single-language and multi-language spellcheck settings |
188 // should send the 'enable spellcheck' message to the renderer. Consequently | 222 // should send the 'enable spellcheck' message to the renderer. Consequently |
189 // removing spellcheck languages should disable spellcheck. | 223 // removing spellcheck languages should disable spellcheck. |
190 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, | 224 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, |
191 StartWithBothLanguagePreferences) { | 225 StartWithBothLanguagePreferences) { |
192 std::vector<std::string> dictionaries; | 226 InitSpellcheck(true, "en-US", "en-US,fr"); |
193 dictionaries.push_back("en-US"); | |
194 dictionaries.push_back("fr"); | |
195 InitSpellcheck(true, "en-US", dictionaries); | |
196 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); | 227 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); |
197 | 228 |
198 SetMultiLingualDictionaries(std::vector<std::string>()); | 229 SetMultiLingualDictionaries(""); |
199 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); | 230 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); |
200 } | 231 } |
201 | 232 |
202 // Starting without spellcheck languages should send the 'disable spellcheck' | 233 // Starting without spellcheck languages should send the 'disable spellcheck' |
203 // message to the renderer. Consequently adding spellchecking languages should | 234 // message to the renderer. Consequently adding spellchecking languages should |
204 // enable spellcheck. | 235 // enable spellcheck. |
205 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, StartWithoutLanguages) { | 236 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, StartWithoutLanguages) { |
206 InitSpellcheck(true, "", std::vector<std::string>()); | 237 InitSpellcheck(true, "", ""); |
207 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); | 238 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); |
208 | 239 |
209 SetMultiLingualDictionaries(std::vector<std::string>(1, "en-US")); | 240 SetMultiLingualDictionaries("en-US"); |
210 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); | 241 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); |
211 } | 242 } |
212 | 243 |
213 // Starting with spellcheck disabled should send the 'disable spellcheck' | 244 // Starting with spellcheck disabled should send the 'disable spellcheck' |
214 // message to the renderer. Consequently enabling spellcheck should send the | 245 // message to the renderer. Consequently enabling spellcheck should send the |
215 // 'enable spellcheck' message to the renderer. | 246 // 'enable spellcheck' message to the renderer. |
216 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, StartWithoutSpellcheck) { | 247 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, StartWithoutSpellcheck) { |
217 std::vector<std::string> dictionaries; | 248 InitSpellcheck(false, "", "en-US,fr"); |
218 dictionaries.push_back("en-US"); | |
219 dictionaries.push_back("fr"); | |
220 InitSpellcheck(false, "", dictionaries); | |
221 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); | 249 EXPECT_FALSE(GetFirstEnableSpellcheckMessageParam()); |
222 | 250 |
223 EnableSpellcheck(true); | 251 EnableSpellcheck(true); |
224 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); | 252 EXPECT_TRUE(GetFirstEnableSpellcheckMessageParam()); |
225 } | 253 } |
226 | 254 |
227 // Tests that we can delete a corrupted BDICT file used by hunspell. We do not | 255 // Tests that we can delete a corrupted BDICT file used by hunspell. We do not |
228 // run this test on Mac because Mac does not use hunspell by default. | 256 // run this test on Mac because Mac does not use hunspell by default. |
229 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) { | 257 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) { |
230 // Write the corrupted BDICT data to create a corrupted BDICT file. | 258 // Write the corrupted BDICT data to create a corrupted BDICT file. |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 EXPECT_TRUE(GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | 368 EXPECT_TRUE(GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); |
341 EXPECT_EQ(2U, GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetSize()); | 369 EXPECT_EQ(2U, GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetSize()); |
342 std::string pref; | 370 std::string pref; |
343 ASSERT_TRUE( | 371 ASSERT_TRUE( |
344 GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &pref)); | 372 GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &pref)); |
345 EXPECT_EQ("en-US", pref); | 373 EXPECT_EQ("en-US", pref); |
346 ASSERT_TRUE( | 374 ASSERT_TRUE( |
347 GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetString(1, &pref)); | 375 GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetString(1, &pref)); |
348 EXPECT_EQ("fr", pref); | 376 EXPECT_EQ("fr", pref); |
349 } | 377 } |
OLD | NEW |