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" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/command_line.h" | |
5 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/prefs/pref_service.h" | |
13 #include "base/strings/string_piece.h" | |
6 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
7 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/spellchecker/spellcheck_factory.h" | 16 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
9 #include "chrome/browser/spellchecker/spellcheck_service.h" | |
10 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
19 #include "chrome/common/chrome_switches.h" | |
20 #include "chrome/common/pref_names.h" | |
12 #include "chrome/common/spellcheck_common.h" | 21 #include "chrome/common/spellcheck_common.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 22 #include "chrome/test/base/in_process_browser_test.h" |
23 #include "components/user_prefs/user_prefs.h" | |
14 #include "content/public/test/test_utils.h" | 24 #include "content/public/test/test_utils.h" |
15 #include "url/gurl.h" | |
16 | 25 |
17 using content::BrowserContext; | 26 using content::BrowserContext; |
18 | 27 |
19 namespace { | 28 namespace { |
20 | 29 |
21 // A corrupted BDICT data used in DeleteCorruptedBDICT. Please do not use this | 30 // A corrupted BDICT data used in DeleteCorruptedBDICT. Please do not use this |
22 // BDICT data for other tests. | 31 // BDICT data for other tests. |
23 const uint8 kCorruptedBDICT[] = { | 32 const uint8 kCorruptedBDICT[] = { |
24 0x42, 0x44, 0x69, 0x63, 0x02, 0x00, 0x01, 0x00, | 33 0x42, 0x44, 0x69, 0x63, 0x02, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, |
25 0x20, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00, | 34 0x3b, 0x00, 0x00, 0x00, 0x65, 0x72, 0xe0, 0xac, 0x27, 0xc7, 0xda, 0x66, |
26 0x65, 0x72, 0xe0, 0xac, 0x27, 0xc7, 0xda, 0x66, | 35 0x6d, 0x1e, 0xa6, 0x35, 0xd1, 0xf6, 0xb7, 0x35, 0x32, 0x00, 0x00, 0x00, |
27 0x6d, 0x1e, 0xa6, 0x35, 0xd1, 0xf6, 0xb7, 0x35, | 36 0x38, 0x00, 0x00, 0x00, 0x39, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00, |
28 0x32, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, | 37 0x0a, 0x0a, 0x41, 0x46, 0x20, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe6, |
29 0x39, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00, | 38 0x49, 0x00, 0x68, 0x02, 0x73, 0x06, 0x74, 0x0b, 0x77, 0x11, 0x79, 0x15, |
30 0x0a, 0x0a, 0x41, 0x46, 0x20, 0x30, 0x00, 0x00, | |
31 0x00, 0x00, 0x00, 0xe6, 0x49, 0x00, 0x68, 0x02, | |
32 0x73, 0x06, 0x74, 0x0b, 0x77, 0x11, 0x79, 0x15, | |
33 }; | 39 }; |
34 | 40 |
35 } // namespace | 41 } // namespace |
36 | 42 |
37 class SpellcheckServiceBrowserTest : public InProcessBrowserTest { | 43 struct SpellcheckLanguageTestCase { |
44 SpellcheckLanguageTestCase(const std::string& spellcheck_dictionary, | |
45 const std::string& accept_languages, | |
46 size_t expected_enabled_spellcheck_languages, | |
47 base::StringPiece first, | |
48 base::StringPiece second, | |
49 base::StringPiece third) | |
50 : spellcheck_dictionary(spellcheck_dictionary), | |
51 accept_languages(accept_languages), | |
please use gerrit instead
2015/06/17 21:15:07
+6 spaces of indent here and the one line below.
Julius
2015/06/17 21:45:39
Done.
| |
52 expected_enabled_spellcheck_languages(expected_enabled_spellcheck_languages) | |
53 { | |
54 if (!first.empty()) | |
55 expected_spellcheck_languages.push_back(first.as_string()); | |
56 | |
57 if (!second.empty()) | |
58 expected_spellcheck_languages.push_back(second.as_string()); | |
59 | |
60 if (!third.empty()) | |
61 expected_spellcheck_languages.push_back(third.as_string()); | |
62 } | |
63 ~SpellcheckLanguageTestCase() {} | |
64 | |
65 const std::string spellcheck_dictionary; | |
66 const std::string accept_languages; | |
67 size_t expected_enabled_spellcheck_languages; | |
68 std::vector<std::string> expected_spellcheck_languages; | |
69 }; | |
70 | |
71 class SpellcheckServiceBrowserTest | |
72 : public InProcessBrowserTest, | |
73 public testing::WithParamInterface<SpellcheckLanguageTestCase> { | |
38 public: | 74 public: |
39 Profile* GetProfile() { | 75 SpellcheckServiceBrowserTest() {} |
40 return browser()->profile(); | 76 ~SpellcheckServiceBrowserTest() override {} |
77 | |
78 BrowserContext* GetContext() { | |
79 return static_cast<BrowserContext*>(browser()->profile()); | |
41 } | 80 } |
81 | |
82 private: | |
83 DISALLOW_COPY_AND_ASSIGN(SpellcheckServiceBrowserTest); | |
42 }; | 84 }; |
43 | 85 |
86 INSTANTIATE_TEST_CASE_P( | |
87 SpellcheckLanguageTestCases, | |
88 SpellcheckServiceBrowserTest, | |
89 testing::Values( | |
90 SpellcheckLanguageTestCase("en-US", | |
91 "en,en-US", | |
92 1UL, | |
93 base::StringPiece("en-US"), | |
please use gerrit instead
2015/06/17 21:15:07
No need for base::StringPiece() it, Just "en-US" s
Julius
2015/06/17 21:45:39
clang_format as well. Done.
| |
94 nullptr, | |
95 nullptr), | |
96 SpellcheckLanguageTestCase("en-US", | |
97 "en-US,en", | |
98 1UL, | |
99 base::StringPiece("en-US"), | |
100 nullptr, | |
101 nullptr), | |
102 SpellcheckLanguageTestCase("en-US", | |
103 "en,fr,en-US,en-AU", | |
104 1UL, | |
105 base::StringPiece("en-US"), | |
106 base::StringPiece("fr"), | |
107 base::StringPiece("en-AU")), | |
108 SpellcheckLanguageTestCase("fr", | |
109 "en,en-JP,fr,zz,en-US", | |
110 1UL, | |
111 base::StringPiece("fr"), | |
112 base::StringPiece("en-US"), | |
113 nullptr))); | |
114 | |
115 IN_PROC_BROWSER_TEST_P(SpellcheckServiceBrowserTest, GetSpellcheckLanguages) { | |
116 BrowserContext* context = GetContext(); | |
117 PrefService* prefs = user_prefs::UserPrefs::Get(context); | |
118 prefs->SetString(prefs::kSpellCheckDictionary, | |
119 GetParam().spellcheck_dictionary); | |
120 prefs->SetString(prefs::kAcceptLanguages, GetParam().accept_languages); | |
121 | |
122 std::vector<std::string> spellcheck_languages; | |
123 size_t enabled_spellcheck_languages = | |
124 SpellcheckService::GetSpellCheckLanguages(context, &spellcheck_languages); | |
125 | |
126 EXPECT_EQ(GetParam().expected_enabled_spellcheck_languages, | |
127 enabled_spellcheck_languages); | |
128 EXPECT_EQ(GetParam().expected_spellcheck_languages, spellcheck_languages); | |
129 } | |
130 | |
44 // Tests that we can delete a corrupted BDICT file used by hunspell. We do not | 131 // Tests that we can delete a corrupted BDICT file used by hunspell. We do not |
45 // run this test on Mac because Mac does not use hunspell by default. | 132 // run this test on Mac because Mac does not use hunspell by default. |
46 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) { | 133 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) { |
47 // Write the corrupted BDICT data to create a corrupted BDICT file. | 134 // Write the corrupted BDICT data to create a corrupted BDICT file. |
48 base::FilePath dict_dir; | 135 base::FilePath dict_dir; |
49 ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir)); | 136 ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir)); |
50 base::FilePath bdict_path = | 137 base::FilePath bdict_path = |
51 chrome::spellcheck_common::GetVersionedFileName("en-US", dict_dir); | 138 chrome::spellcheck_common::GetVersionedFileName("en-US", dict_dir); |
52 | 139 |
53 size_t actual = base::WriteFile(bdict_path, | 140 size_t actual = base::WriteFile( |
54 reinterpret_cast<const char*>(kCorruptedBDICT), | 141 bdict_path, reinterpret_cast<const char*>(kCorruptedBDICT), |
55 arraysize(kCorruptedBDICT)); | 142 arraysize(kCorruptedBDICT)); |
56 EXPECT_EQ(arraysize(kCorruptedBDICT), actual); | 143 EXPECT_EQ(arraysize(kCorruptedBDICT), actual); |
57 | 144 |
58 // Attach an event to the SpellcheckService object so we can receive its | 145 // Attach an event to the SpellcheckService object so we can receive its |
59 // status updates. | 146 // status updates. |
60 base::WaitableEvent event(true, false); | 147 base::WaitableEvent event(true, false); |
61 SpellcheckService::AttachStatusEvent(&event); | 148 SpellcheckService::AttachStatusEvent(&event); |
62 | 149 |
63 BrowserContext * context = static_cast<BrowserContext*>(GetProfile()); | 150 BrowserContext* context = GetContext(); |
64 | 151 |
65 // Ensure that the SpellcheckService object does not already exist. Otherwise | 152 // Ensure that the SpellcheckService object does not already exist. Otherwise |
66 // the next line will not force creation of the SpellcheckService and the | 153 // the next line will not force creation of the SpellcheckService and the |
67 // test will fail. | 154 // test will fail. |
68 SpellcheckService* service = static_cast<SpellcheckService*>( | 155 SpellcheckService* service = static_cast<SpellcheckService*>( |
69 SpellcheckServiceFactory::GetInstance()->GetServiceForBrowserContext( | 156 SpellcheckServiceFactory::GetInstance()->GetServiceForBrowserContext( |
70 context, | 157 context, false)); |
71 false)); | |
72 ASSERT_EQ(NULL, service); | 158 ASSERT_EQ(NULL, service); |
73 | 159 |
74 // Getting the spellcheck_service will initialize the SpellcheckService | 160 // Getting the spellcheck_service will initialize the SpellcheckService |
75 // object with the corrupted BDICT file created above since the hunspell | 161 // object with the corrupted BDICT file created above since the hunspell |
76 // dictionary is loaded in the SpellcheckService constructor right now. | 162 // dictionary is loaded in the SpellcheckService constructor right now. |
77 // The SpellCheckHost object will send a BDICT_CORRUPTED event. | 163 // The SpellCheckHost object will send a BDICT_CORRUPTED event. |
78 SpellcheckServiceFactory::GetForContext(context); | 164 SpellcheckServiceFactory::GetForContext(context); |
79 | 165 |
80 // Check the received event. Also we check if Chrome has successfully deleted | 166 // Check the received event. Also we check if Chrome has successfully deleted |
81 // the corrupted dictionary. We delete the corrupted dictionary to avoid | 167 // the corrupted dictionary. We delete the corrupted dictionary to avoid |
82 // leaking it when this test fails. | 168 // leaking it when this test fails. |
83 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 169 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
84 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 170 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
85 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED, | 171 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED, |
86 SpellcheckService::GetStatusEvent()); | 172 SpellcheckService::GetStatusEvent()); |
87 if (base::PathExists(bdict_path)) { | 173 if (base::PathExists(bdict_path)) { |
88 ADD_FAILURE(); | 174 ADD_FAILURE(); |
89 EXPECT_TRUE(base::DeleteFile(bdict_path, true)); | 175 EXPECT_TRUE(base::DeleteFile(bdict_path, true)); |
90 } | 176 } |
91 } | 177 } |
178 | |
179 class MultilingualSpellcheckServiceBrowserTest | |
180 : public InProcessBrowserTest, | |
181 public testing::WithParamInterface<SpellcheckLanguageTestCase> { | |
182 public: | |
183 MultilingualSpellcheckServiceBrowserTest() {} | |
184 ~MultilingualSpellcheckServiceBrowserTest() override {} | |
185 | |
186 BrowserContext* GetContext() { | |
187 return static_cast<BrowserContext*>(browser()->profile()); | |
188 } | |
189 | |
190 void SetUpCommandLine(base::CommandLine* command_line) override { | |
191 InProcessBrowserTest::SetUpCommandLine(command_line); | |
192 command_line->AppendSwitch(switches::kEnableMultilingualSpellChecker); | |
193 } | |
194 | |
195 private: | |
196 DISALLOW_COPY_AND_ASSIGN(MultilingualSpellcheckServiceBrowserTest); | |
197 }; | |
198 | |
199 INSTANTIATE_TEST_CASE_P( | |
200 MultilingualSpellcheckLanguageTestCases, | |
201 MultilingualSpellcheckServiceBrowserTest, | |
202 testing::Values( | |
203 SpellcheckLanguageTestCase("en-US", | |
204 "en,en-US", | |
205 1UL, | |
206 base::StringPiece("en-US"), | |
207 nullptr, | |
208 nullptr), | |
209 SpellcheckLanguageTestCase("en-US", | |
210 "en-US,en", | |
211 1UL, | |
212 base::StringPiece("en-US"), | |
213 nullptr, | |
214 nullptr), | |
215 SpellcheckLanguageTestCase("en-US,fr", | |
216 "en,fr,en-US,en-AU", | |
217 2UL, | |
218 base::StringPiece("en-US"), | |
219 base::StringPiece("fr"), | |
220 base::StringPiece("en-AU")), | |
221 SpellcheckLanguageTestCase("fr", | |
222 "en,en-JP,fr,zz,en-US", | |
223 1UL, | |
224 base::StringPiece("fr"), | |
225 base::StringPiece("en-US"), | |
226 nullptr))); | |
227 | |
228 IN_PROC_BROWSER_TEST_P(MultilingualSpellcheckServiceBrowserTest, | |
229 GetSpellcheckLanguages) { | |
230 BrowserContext* context = GetContext(); | |
231 PrefService* prefs = user_prefs::UserPrefs::Get(context); | |
232 prefs->SetString(prefs::kSpellCheckDictionaries, | |
233 GetParam().spellcheck_dictionary); | |
234 prefs->SetString(prefs::kAcceptLanguages, GetParam().accept_languages); | |
235 | |
236 std::vector<std::string> spellcheck_languages; | |
237 size_t enabled_spellcheck_languages = | |
238 SpellcheckService::GetSpellCheckLanguages(context, | |
239 &spellcheck_languages); | |
240 | |
241 EXPECT_EQ(GetParam().expected_enabled_spellcheck_languages, | |
242 enabled_spellcheck_languages); | |
243 EXPECT_EQ(GetParam().expected_spellcheck_languages, spellcheck_languages); | |
244 } | |
OLD | NEW |