Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / spellchecker / spellcheck_service_browsertest.cc
blob1d6e1e9b90c799e841b801e8bda416feb0e2314f
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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/path_service.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/synchronization/waitable_event.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/spellchecker/spellcheck_factory.h"
11 #include "chrome/browser/spellchecker/spellcheck_service.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/spellcheck_common.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "components/user_prefs/user_prefs.h"
19 #include "content/public/test/test_utils.h"
20 #include "url/gurl.h"
22 using content::BrowserContext;
24 namespace {
26 // A corrupted BDICT data used in DeleteCorruptedBDICT. Please do not use this
27 // BDICT data for other tests.
28 const uint8 kCorruptedBDICT[] = {
29 0x42, 0x44, 0x69, 0x63, 0x02, 0x00, 0x01, 0x00,
30 0x20, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00,
31 0x65, 0x72, 0xe0, 0xac, 0x27, 0xc7, 0xda, 0x66,
32 0x6d, 0x1e, 0xa6, 0x35, 0xd1, 0xf6, 0xb7, 0x35,
33 0x32, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
34 0x39, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00,
35 0x0a, 0x0a, 0x41, 0x46, 0x20, 0x30, 0x00, 0x00,
36 0x00, 0x00, 0x00, 0xe6, 0x49, 0x00, 0x68, 0x02,
37 0x73, 0x06, 0x74, 0x0b, 0x77, 0x11, 0x79, 0x15,
40 } // namespace
42 class SpellcheckServiceBrowserTest : public InProcessBrowserTest {
43 public:
44 BrowserContext* GetContext() {
45 return static_cast<BrowserContext*>(browser()->profile());
49 // Tests that we can delete a corrupted BDICT file used by hunspell. We do not
50 // run this test on Mac because Mac does not use hunspell by default.
51 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) {
52 // Write the corrupted BDICT data to create a corrupted BDICT file.
53 base::FilePath dict_dir;
54 ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir));
55 base::FilePath bdict_path =
56 chrome::spellcheck_common::GetVersionedFileName("en-US", dict_dir);
58 size_t actual = base::WriteFile(bdict_path,
59 reinterpret_cast<const char*>(kCorruptedBDICT),
60 arraysize(kCorruptedBDICT));
61 EXPECT_EQ(arraysize(kCorruptedBDICT), actual);
63 // Attach an event to the SpellcheckService object so we can receive its
64 // status updates.
65 base::WaitableEvent event(true, false);
66 SpellcheckService::AttachStatusEvent(&event);
68 BrowserContext* context = GetContext();
70 // Ensure that the SpellcheckService object does not already exist. Otherwise
71 // the next line will not force creation of the SpellcheckService and the
72 // test will fail.
73 SpellcheckService* service = static_cast<SpellcheckService*>(
74 SpellcheckServiceFactory::GetInstance()->GetServiceForBrowserContext(
75 context,
76 false));
77 ASSERT_EQ(NULL, service);
79 // Getting the spellcheck_service will initialize the SpellcheckService
80 // object with the corrupted BDICT file created above since the hunspell
81 // dictionary is loaded in the SpellcheckService constructor right now.
82 // The SpellCheckHost object will send a BDICT_CORRUPTED event.
83 SpellcheckServiceFactory::GetForContext(context);
85 // Check the received event. Also we check if Chrome has successfully deleted
86 // the corrupted dictionary. We delete the corrupted dictionary to avoid
87 // leaking it when this test fails.
88 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
89 content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
90 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED,
91 SpellcheckService::GetStatusEvent());
92 if (base::PathExists(bdict_path)) {
93 ADD_FAILURE();
94 EXPECT_TRUE(base::DeleteFile(bdict_path, true));
98 // Checks that preferences migrate correctly.
99 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, PreferencesMigrated) {
100 PrefService* prefs = user_prefs::UserPrefs::Get(GetContext());
101 prefs->Set(prefs::kSpellCheckDictionaries, base::ListValue());
102 prefs->SetString(prefs::kSpellCheckDictionary, "en-US");
104 // Create a SpellcheckService which will migrate the preferences.
105 SpellcheckServiceFactory::GetForContext(GetContext());
107 // Make sure the preferences have been migrated.
108 std::string new_pref;
109 EXPECT_TRUE(
110 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &new_pref));
111 EXPECT_EQ("en-US", new_pref);
112 EXPECT_TRUE(prefs->GetString(prefs::kSpellCheckDictionary).empty());
115 // Checks that preferences are not migrated when they shouldn't be.
116 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, PreferencesNotMigrated) {
117 PrefService* prefs = user_prefs::UserPrefs::Get(GetContext());
118 base::ListValue dictionaries;
119 dictionaries.AppendString("en-US");
120 prefs->Set(prefs::kSpellCheckDictionaries, dictionaries);
121 prefs->SetString(prefs::kSpellCheckDictionary, "fr");
123 // Create a SpellcheckService which will migrate the preferences.
124 SpellcheckServiceFactory::GetForContext(GetContext());
126 // Make sure the preferences have not been migrated.
127 std::string new_pref;
128 EXPECT_TRUE(
129 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &new_pref));
130 EXPECT_EQ("en-US", new_pref);
131 EXPECT_TRUE(prefs->GetString(prefs::kSpellCheckDictionary).empty());
134 // Checks that if a user starts multilingual mode with spellchecking disabled
135 // that all languages get deselected and spellchecking gets enabled.
136 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest,
137 SpellcheckingDisabledPreferenceMigration) {
138 base::CommandLine::ForCurrentProcess()->AppendSwitch(
139 switches::kEnableMultilingualSpellChecker);
141 PrefService* prefs = user_prefs::UserPrefs::Get(GetContext());
142 base::ListValue dictionaries;
143 dictionaries.AppendString("en-US");
144 prefs->Set(prefs::kSpellCheckDictionaries, dictionaries);
145 prefs->SetBoolean(prefs::kEnableContinuousSpellcheck, false);
147 // Migrate the preferences.
148 SpellcheckServiceFactory::GetForContext(GetContext());
150 EXPECT_TRUE(prefs->GetBoolean(prefs::kEnableContinuousSpellcheck));
151 EXPECT_EQ(0U, prefs->GetList(prefs::kSpellCheckDictionaries)->GetSize());
154 // Make sure that there is only one language in the preference when not using
155 // multilingual spellchecking.
156 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest,
157 MultilingualToSingleLanguagePreferenceMigration) {
158 PrefService* prefs = user_prefs::UserPrefs::Get(GetContext());
159 base::ListValue dictionaries;
160 dictionaries.AppendString("en-US");
161 dictionaries.AppendString("fr");
162 prefs->Set(prefs::kSpellCheckDictionaries, dictionaries);
164 // Migrate the preference.
165 SpellcheckServiceFactory::GetForContext(GetContext());
167 EXPECT_EQ(1U, prefs->GetList(prefs::kSpellCheckDictionaries)->GetSize());
168 std::string new_pref;
169 ASSERT_TRUE(
170 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &new_pref));
171 EXPECT_EQ("en-US", new_pref);
174 // If using multilingual spellchecking with spellchecking enabled, make sure the
175 // preference stays the same and spellchecking stays enabled.
176 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest,
177 MultilingualPreferenceNotMigrated) {
178 base::CommandLine::ForCurrentProcess()->AppendSwitch(
179 switches::kEnableMultilingualSpellChecker);
181 PrefService* prefs = user_prefs::UserPrefs::Get(GetContext());
182 base::ListValue dictionaries;
183 dictionaries.AppendString("en-US");
184 dictionaries.AppendString("fr");
185 prefs->Set(prefs::kSpellCheckDictionaries, dictionaries);
186 prefs->SetBoolean(prefs::kEnableContinuousSpellcheck, true);
188 // Should not migrate any preferences.
189 SpellcheckServiceFactory::GetForContext(GetContext());
191 EXPECT_TRUE(prefs->GetBoolean(prefs::kEnableContinuousSpellcheck));
192 EXPECT_EQ(2U, prefs->GetList(prefs::kSpellCheckDictionaries)->GetSize());
193 std::string pref;
194 ASSERT_TRUE(
195 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &pref));
196 EXPECT_EQ("en-US", pref);
197 ASSERT_TRUE(
198 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(1, &pref));
199 EXPECT_EQ("fr", pref);
202 // If not using multilingual spellchecking and only one language is selected,
203 // the preference should not change.
204 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest,
205 SingleLanguagePreferenceNotMigrated) {
206 PrefService* prefs = user_prefs::UserPrefs::Get(GetContext());
207 base::ListValue dictionaries;
208 dictionaries.AppendString("en-US");
209 prefs->Set(prefs::kSpellCheckDictionaries, dictionaries);
211 // Should not migrate any preferences.
212 SpellcheckServiceFactory::GetForContext(GetContext());
214 EXPECT_EQ(1U, prefs->GetList(prefs::kSpellCheckDictionaries)->GetSize());
215 std::string pref;
216 ASSERT_TRUE(
217 prefs->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &pref));
218 EXPECT_EQ("en-US", pref);