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 <tuple> | 10 #include <tuple> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
19 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 22 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
23 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
24 #include "chrome/common/chrome_paths.h" | 24 #include "chrome/common/chrome_paths.h" |
25 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
27 #include "chrome/test/base/in_process_browser_test.h" | 27 #include "chrome/test/base/in_process_browser_test.h" |
28 #include "components/prefs/pref_service.h" | 28 #include "components/prefs/pref_service.h" |
| 29 #include "components/spellcheck/browser/pref_names.h" |
29 #include "components/spellcheck/common/spellcheck_common.h" | 30 #include "components/spellcheck/common/spellcheck_common.h" |
30 #include "components/spellcheck/common/spellcheck_messages.h" | 31 #include "components/spellcheck/common/spellcheck_messages.h" |
31 #include "components/user_prefs/user_prefs.h" | 32 #include "components/user_prefs/user_prefs.h" |
32 #include "content/public/test/mock_render_process_host.h" | 33 #include "content/public/test/mock_render_process_host.h" |
33 #include "content/public/test/test_utils.h" | 34 #include "content/public/test/test_utils.h" |
34 #include "url/gurl.h" | 35 #include "url/gurl.h" |
35 | 36 |
36 using content::BrowserContext; | 37 using content::BrowserContext; |
37 | 38 |
38 namespace { | 39 namespace { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 return static_cast<BrowserContext*>(browser()->profile()); | 83 return static_cast<BrowserContext*>(browser()->profile()); |
83 } | 84 } |
84 | 85 |
85 PrefService* GetPrefs() { | 86 PrefService* GetPrefs() { |
86 return prefs_; | 87 return prefs_; |
87 } | 88 } |
88 | 89 |
89 void InitSpellcheck(bool enable_spellcheck, | 90 void InitSpellcheck(bool enable_spellcheck, |
90 const std::string& single_dictionary, | 91 const std::string& single_dictionary, |
91 const std::string& multiple_dictionaries) { | 92 const std::string& multiple_dictionaries) { |
92 prefs_->SetBoolean(prefs::kEnableContinuousSpellcheck, enable_spellcheck); | 93 prefs_->SetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck, |
93 prefs_->SetString(prefs::kSpellCheckDictionary, single_dictionary); | 94 enable_spellcheck); |
| 95 prefs_->SetString(spellcheck::prefs::kSpellCheckDictionary, |
| 96 single_dictionary); |
94 base::ListValue dictionaries_value; | 97 base::ListValue dictionaries_value; |
95 dictionaries_value.AppendStrings( | 98 dictionaries_value.AppendStrings( |
96 base::SplitString(multiple_dictionaries, ",", base::TRIM_WHITESPACE, | 99 base::SplitString(multiple_dictionaries, ",", base::TRIM_WHITESPACE, |
97 base::SPLIT_WANT_NONEMPTY)); | 100 base::SPLIT_WANT_NONEMPTY)); |
98 prefs_->Set(prefs::kSpellCheckDictionaries, dictionaries_value); | 101 prefs_->Set(spellcheck::prefs::kSpellCheckDictionaries, dictionaries_value); |
99 SpellcheckService* spellcheck = | 102 SpellcheckService* spellcheck = |
100 SpellcheckServiceFactory::GetForRenderProcessId(renderer_->GetID()); | 103 SpellcheckServiceFactory::GetForRenderProcessId(renderer_->GetID()); |
101 ASSERT_NE(nullptr, spellcheck); | 104 ASSERT_NE(nullptr, spellcheck); |
102 spellcheck->InitForRenderer(renderer_.get()); | 105 spellcheck->InitForRenderer(renderer_.get()); |
103 } | 106 } |
104 | 107 |
105 void EnableSpellcheck(bool enable_spellcheck) { | 108 void EnableSpellcheck(bool enable_spellcheck) { |
106 ScopedPreferenceChange scope(&renderer_->sink()); | 109 ScopedPreferenceChange scope(&renderer_->sink()); |
107 prefs_->SetBoolean(prefs::kEnableContinuousSpellcheck, enable_spellcheck); | 110 prefs_->SetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck, |
| 111 enable_spellcheck); |
108 } | 112 } |
109 | 113 |
110 void SetSingleLanguageDictionary(const std::string& single_dictionary) { | 114 void SetSingleLanguageDictionary(const std::string& single_dictionary) { |
111 ScopedPreferenceChange scope(&renderer_->sink()); | 115 ScopedPreferenceChange scope(&renderer_->sink()); |
112 prefs_->SetString(prefs::kSpellCheckDictionary, single_dictionary); | 116 prefs_->SetString(spellcheck::prefs::kSpellCheckDictionary, |
| 117 single_dictionary); |
113 } | 118 } |
114 | 119 |
115 void SetMultiLingualDictionaries(const std::string& multiple_dictionaries) { | 120 void SetMultiLingualDictionaries(const std::string& multiple_dictionaries) { |
116 ScopedPreferenceChange scope(&renderer_->sink()); | 121 ScopedPreferenceChange scope(&renderer_->sink()); |
117 base::ListValue dictionaries_value; | 122 base::ListValue dictionaries_value; |
118 dictionaries_value.AppendStrings( | 123 dictionaries_value.AppendStrings( |
119 base::SplitString(multiple_dictionaries, ",", base::TRIM_WHITESPACE, | 124 base::SplitString(multiple_dictionaries, ",", base::TRIM_WHITESPACE, |
120 base::SPLIT_WANT_NONEMPTY)); | 125 base::SPLIT_WANT_NONEMPTY)); |
121 prefs_->Set(prefs::kSpellCheckDictionaries, dictionaries_value); | 126 prefs_->Set(spellcheck::prefs::kSpellCheckDictionaries, dictionaries_value); |
122 } | 127 } |
123 | 128 |
124 std::string GetMultilingualDictionaries() { | 129 std::string GetMultilingualDictionaries() { |
125 const base::ListValue* list_value = | 130 const base::ListValue* list_value = |
126 prefs_->GetList(prefs::kSpellCheckDictionaries); | 131 prefs_->GetList(spellcheck::prefs::kSpellCheckDictionaries); |
127 std::vector<std::string> dictionaries; | 132 std::vector<std::string> dictionaries; |
128 for (const auto& item_value : *list_value) { | 133 for (const auto& item_value : *list_value) { |
129 std::string dictionary; | 134 std::string dictionary; |
130 EXPECT_TRUE(item_value->GetAsString(&dictionary)); | 135 EXPECT_TRUE(item_value->GetAsString(&dictionary)); |
131 dictionaries.push_back(dictionary); | 136 dictionaries.push_back(dictionary); |
132 } | 137 } |
133 return base::JoinString(dictionaries, ","); | 138 return base::JoinString(dictionaries, ","); |
134 } | 139 } |
135 | 140 |
136 void SetAcceptLanguages(const std::string& accept_languages) { | 141 void SetAcceptLanguages(const std::string& accept_languages) { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED, | 309 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED, |
305 SpellcheckService::GetStatusEvent()); | 310 SpellcheckService::GetStatusEvent()); |
306 if (base::PathExists(bdict_path)) { | 311 if (base::PathExists(bdict_path)) { |
307 ADD_FAILURE(); | 312 ADD_FAILURE(); |
308 EXPECT_TRUE(base::DeleteFile(bdict_path, true)); | 313 EXPECT_TRUE(base::DeleteFile(bdict_path, true)); |
309 } | 314 } |
310 } | 315 } |
311 | 316 |
312 // Checks that preferences migrate correctly. | 317 // Checks that preferences migrate correctly. |
313 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, PreferencesMigrated) { | 318 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, PreferencesMigrated) { |
314 GetPrefs()->Set(prefs::kSpellCheckDictionaries, base::ListValue()); | 319 GetPrefs()->Set(spellcheck::prefs::kSpellCheckDictionaries, |
315 GetPrefs()->SetString(prefs::kSpellCheckDictionary, "en-US"); | 320 base::ListValue()); |
| 321 GetPrefs()->SetString(spellcheck::prefs::kSpellCheckDictionary, "en-US"); |
316 | 322 |
317 // Create a SpellcheckService which will migrate the preferences. | 323 // Create a SpellcheckService which will migrate the preferences. |
318 SpellcheckServiceFactory::GetForContext(GetContext()); | 324 SpellcheckServiceFactory::GetForContext(GetContext()); |
319 | 325 |
320 // Make sure the preferences have been migrated. | 326 // Make sure the preferences have been migrated. |
321 std::string new_pref; | 327 std::string new_pref; |
322 EXPECT_TRUE(GetPrefs() | 328 EXPECT_TRUE(GetPrefs() |
323 ->GetList(prefs::kSpellCheckDictionaries) | 329 ->GetList(spellcheck::prefs::kSpellCheckDictionaries) |
324 ->GetString(0, &new_pref)); | 330 ->GetString(0, &new_pref)); |
325 EXPECT_EQ("en-US", new_pref); | 331 EXPECT_EQ("en-US", new_pref); |
326 EXPECT_TRUE(GetPrefs()->GetString(prefs::kSpellCheckDictionary).empty()); | 332 EXPECT_TRUE( |
| 333 GetPrefs()->GetString(spellcheck::prefs::kSpellCheckDictionary).empty()); |
327 } | 334 } |
328 | 335 |
329 // Checks that preferences are not migrated when they shouldn't be. | 336 // Checks that preferences are not migrated when they shouldn't be. |
330 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, PreferencesNotMigrated) { | 337 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, PreferencesNotMigrated) { |
331 base::ListValue dictionaries; | 338 base::ListValue dictionaries; |
332 dictionaries.AppendString("en-US"); | 339 dictionaries.AppendString("en-US"); |
333 GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionaries); | 340 GetPrefs()->Set(spellcheck::prefs::kSpellCheckDictionaries, dictionaries); |
334 GetPrefs()->SetString(prefs::kSpellCheckDictionary, "fr"); | 341 GetPrefs()->SetString(spellcheck::prefs::kSpellCheckDictionary, "fr"); |
335 | 342 |
336 // Create a SpellcheckService which will migrate the preferences. | 343 // Create a SpellcheckService which will migrate the preferences. |
337 SpellcheckServiceFactory::GetForContext(GetContext()); | 344 SpellcheckServiceFactory::GetForContext(GetContext()); |
338 | 345 |
339 // Make sure the preferences have not been migrated. | 346 // Make sure the preferences have not been migrated. |
340 std::string new_pref; | 347 std::string new_pref; |
341 EXPECT_TRUE(GetPrefs() | 348 EXPECT_TRUE(GetPrefs() |
342 ->GetList(prefs::kSpellCheckDictionaries) | 349 ->GetList(spellcheck::prefs::kSpellCheckDictionaries) |
343 ->GetString(0, &new_pref)); | 350 ->GetString(0, &new_pref)); |
344 EXPECT_EQ("en-US", new_pref); | 351 EXPECT_EQ("en-US", new_pref); |
345 EXPECT_TRUE(GetPrefs()->GetString(prefs::kSpellCheckDictionary).empty()); | 352 EXPECT_TRUE( |
| 353 GetPrefs()->GetString(spellcheck::prefs::kSpellCheckDictionary).empty()); |
346 } | 354 } |
347 | 355 |
348 // Checks that, if a user has spellchecking disabled, nothing changes | 356 // Checks that, if a user has spellchecking disabled, nothing changes |
349 // during migration. | 357 // during migration. |
350 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, | 358 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, |
351 SpellcheckingDisabledPreferenceMigration) { | 359 SpellcheckingDisabledPreferenceMigration) { |
352 base::ListValue dictionaries; | 360 base::ListValue dictionaries; |
353 dictionaries.AppendString("en-US"); | 361 dictionaries.AppendString("en-US"); |
354 GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionaries); | 362 GetPrefs()->Set(spellcheck::prefs::kSpellCheckDictionaries, dictionaries); |
355 GetPrefs()->SetBoolean(prefs::kEnableContinuousSpellcheck, false); | 363 GetPrefs()->SetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck, false); |
356 | 364 |
357 // Migrate the preferences. | 365 // Migrate the preferences. |
358 SpellcheckServiceFactory::GetForContext(GetContext()); | 366 SpellcheckServiceFactory::GetForContext(GetContext()); |
359 | 367 |
360 EXPECT_FALSE(GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | 368 EXPECT_FALSE( |
361 EXPECT_EQ(1U, GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetSize()); | 369 GetPrefs()->GetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck)); |
| 370 EXPECT_EQ(1U, GetPrefs() |
| 371 ->GetList(spellcheck::prefs::kSpellCheckDictionaries) |
| 372 ->GetSize()); |
362 } | 373 } |
363 | 374 |
364 // Make sure preferences get preserved and spellchecking stays enabled. | 375 // Make sure preferences get preserved and spellchecking stays enabled. |
365 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, | 376 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, |
366 MultilingualPreferenceNotMigrated) { | 377 MultilingualPreferenceNotMigrated) { |
367 base::ListValue dictionaries; | 378 base::ListValue dictionaries; |
368 dictionaries.AppendString("en-US"); | 379 dictionaries.AppendString("en-US"); |
369 dictionaries.AppendString("fr"); | 380 dictionaries.AppendString("fr"); |
370 GetPrefs()->Set(prefs::kSpellCheckDictionaries, dictionaries); | 381 GetPrefs()->Set(spellcheck::prefs::kSpellCheckDictionaries, dictionaries); |
371 GetPrefs()->SetBoolean(prefs::kEnableContinuousSpellcheck, true); | 382 GetPrefs()->SetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck, true); |
372 | 383 |
373 // Should not migrate any preferences. | 384 // Should not migrate any preferences. |
374 SpellcheckServiceFactory::GetForContext(GetContext()); | 385 SpellcheckServiceFactory::GetForContext(GetContext()); |
375 | 386 |
376 EXPECT_TRUE(GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | 387 EXPECT_TRUE( |
377 EXPECT_EQ(2U, GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetSize()); | 388 GetPrefs()->GetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck)); |
| 389 EXPECT_EQ(2U, GetPrefs() |
| 390 ->GetList(spellcheck::prefs::kSpellCheckDictionaries) |
| 391 ->GetSize()); |
378 std::string pref; | 392 std::string pref; |
379 ASSERT_TRUE( | 393 ASSERT_TRUE(GetPrefs() |
380 GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &pref)); | 394 ->GetList(spellcheck::prefs::kSpellCheckDictionaries) |
| 395 ->GetString(0, &pref)); |
381 EXPECT_EQ("en-US", pref); | 396 EXPECT_EQ("en-US", pref); |
382 ASSERT_TRUE( | 397 ASSERT_TRUE(GetPrefs() |
383 GetPrefs()->GetList(prefs::kSpellCheckDictionaries)->GetString(1, &pref)); | 398 ->GetList(spellcheck::prefs::kSpellCheckDictionaries) |
| 399 ->GetString(1, &pref)); |
384 EXPECT_EQ("fr", pref); | 400 EXPECT_EQ("fr", pref); |
385 } | 401 } |
OLD | NEW |