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), |
| 52 expected_enabled_spellcheck_languages( |
| 53 expected_enabled_spellcheck_languages) { |
| 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(SpellcheckLanguageTestCase("en-US", |
| 90 "en,en-US", |
| 91 1UL, |
| 92 "en-US", |
| 93 nullptr, |
| 94 nullptr), |
| 95 SpellcheckLanguageTestCase("en-US", |
| 96 "en-US,en", |
| 97 1UL, |
| 98 "en-US", |
| 99 nullptr, |
| 100 nullptr), |
| 101 SpellcheckLanguageTestCase("en-US", |
| 102 "en,fr,en-US,en-AU", |
| 103 1UL, |
| 104 "en-US", |
| 105 "fr", |
| 106 "en-AU"), |
| 107 SpellcheckLanguageTestCase("fr", |
| 108 "en,en-JP,fr,zz,en-US", |
| 109 1UL, |
| 110 "fr", |
| 111 "en-US", |
| 112 nullptr))); |
| 113 |
| 114 IN_PROC_BROWSER_TEST_P(SpellcheckServiceBrowserTest, GetSpellcheckLanguages) { |
| 115 BrowserContext* context = GetContext(); |
| 116 PrefService* prefs = user_prefs::UserPrefs::Get(context); |
| 117 prefs->SetString(prefs::kSpellCheckDictionary, |
| 118 GetParam().spellcheck_dictionary); |
| 119 prefs->SetString(prefs::kAcceptLanguages, GetParam().accept_languages); |
| 120 |
| 121 std::vector<std::string> spellcheck_languages; |
| 122 size_t enabled_spellcheck_languages = |
| 123 SpellcheckService::GetSpellCheckLanguages(context, &spellcheck_languages); |
| 124 |
| 125 EXPECT_EQ(GetParam().expected_enabled_spellcheck_languages, |
| 126 enabled_spellcheck_languages); |
| 127 EXPECT_EQ(GetParam().expected_spellcheck_languages, spellcheck_languages); |
| 128 } |
| 129 |
44 // Tests that we can delete a corrupted BDICT file used by hunspell. We do not | 130 // 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. | 131 // run this test on Mac because Mac does not use hunspell by default. |
46 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) { | 132 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) { |
47 // Write the corrupted BDICT data to create a corrupted BDICT file. | 133 // Write the corrupted BDICT data to create a corrupted BDICT file. |
48 base::FilePath dict_dir; | 134 base::FilePath dict_dir; |
49 ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir)); | 135 ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir)); |
50 base::FilePath bdict_path = | 136 base::FilePath bdict_path = |
51 chrome::spellcheck_common::GetVersionedFileName("en-US", dict_dir); | 137 chrome::spellcheck_common::GetVersionedFileName("en-US", dict_dir); |
52 | 138 |
53 size_t actual = base::WriteFile(bdict_path, | 139 size_t actual = base::WriteFile( |
54 reinterpret_cast<const char*>(kCorruptedBDICT), | 140 bdict_path, reinterpret_cast<const char*>(kCorruptedBDICT), |
55 arraysize(kCorruptedBDICT)); | 141 arraysize(kCorruptedBDICT)); |
56 EXPECT_EQ(arraysize(kCorruptedBDICT), actual); | 142 EXPECT_EQ(arraysize(kCorruptedBDICT), actual); |
57 | 143 |
58 // Attach an event to the SpellcheckService object so we can receive its | 144 // Attach an event to the SpellcheckService object so we can receive its |
59 // status updates. | 145 // status updates. |
60 base::WaitableEvent event(true, false); | 146 base::WaitableEvent event(true, false); |
61 SpellcheckService::AttachStatusEvent(&event); | 147 SpellcheckService::AttachStatusEvent(&event); |
62 | 148 |
63 BrowserContext * context = static_cast<BrowserContext*>(GetProfile()); | 149 BrowserContext* context = GetContext(); |
64 | 150 |
65 // Ensure that the SpellcheckService object does not already exist. Otherwise | 151 // Ensure that the SpellcheckService object does not already exist. Otherwise |
66 // the next line will not force creation of the SpellcheckService and the | 152 // the next line will not force creation of the SpellcheckService and the |
67 // test will fail. | 153 // test will fail. |
68 SpellcheckService* service = static_cast<SpellcheckService*>( | 154 SpellcheckService* service = static_cast<SpellcheckService*>( |
69 SpellcheckServiceFactory::GetInstance()->GetServiceForBrowserContext( | 155 SpellcheckServiceFactory::GetInstance()->GetServiceForBrowserContext( |
70 context, | 156 context, false)); |
71 false)); | |
72 ASSERT_EQ(NULL, service); | 157 ASSERT_EQ(NULL, service); |
73 | 158 |
74 // Getting the spellcheck_service will initialize the SpellcheckService | 159 // Getting the spellcheck_service will initialize the SpellcheckService |
75 // object with the corrupted BDICT file created above since the hunspell | 160 // object with the corrupted BDICT file created above since the hunspell |
76 // dictionary is loaded in the SpellcheckService constructor right now. | 161 // dictionary is loaded in the SpellcheckService constructor right now. |
77 // The SpellCheckHost object will send a BDICT_CORRUPTED event. | 162 // The SpellCheckHost object will send a BDICT_CORRUPTED event. |
78 SpellcheckServiceFactory::GetForContext(context); | 163 SpellcheckServiceFactory::GetForContext(context); |
79 | 164 |
80 // Check the received event. Also we check if Chrome has successfully deleted | 165 // Check the received event. Also we check if Chrome has successfully deleted |
81 // the corrupted dictionary. We delete the corrupted dictionary to avoid | 166 // the corrupted dictionary. We delete the corrupted dictionary to avoid |
82 // leaking it when this test fails. | 167 // leaking it when this test fails. |
83 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | 168 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); |
84 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 169 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
85 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED, | 170 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED, |
86 SpellcheckService::GetStatusEvent()); | 171 SpellcheckService::GetStatusEvent()); |
87 if (base::PathExists(bdict_path)) { | 172 if (base::PathExists(bdict_path)) { |
88 ADD_FAILURE(); | 173 ADD_FAILURE(); |
89 EXPECT_TRUE(base::DeleteFile(bdict_path, true)); | 174 EXPECT_TRUE(base::DeleteFile(bdict_path, true)); |
90 } | 175 } |
91 } | 176 } |
| 177 |
| 178 class MultilingualSpellcheckServiceBrowserTest |
| 179 : public InProcessBrowserTest, |
| 180 public testing::WithParamInterface<SpellcheckLanguageTestCase> { |
| 181 public: |
| 182 MultilingualSpellcheckServiceBrowserTest() {} |
| 183 ~MultilingualSpellcheckServiceBrowserTest() override {} |
| 184 |
| 185 BrowserContext* GetContext() { |
| 186 return static_cast<BrowserContext*>(browser()->profile()); |
| 187 } |
| 188 |
| 189 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 190 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 191 command_line->AppendSwitch(switches::kEnableMultilingualSpellChecker); |
| 192 } |
| 193 |
| 194 private: |
| 195 DISALLOW_COPY_AND_ASSIGN(MultilingualSpellcheckServiceBrowserTest); |
| 196 }; |
| 197 |
| 198 INSTANTIATE_TEST_CASE_P( |
| 199 MultilingualSpellcheckLanguageTestCases, |
| 200 MultilingualSpellcheckServiceBrowserTest, |
| 201 testing::Values(SpellcheckLanguageTestCase("en-US", |
| 202 "en,en-US", |
| 203 1UL, |
| 204 "en-US", |
| 205 nullptr, |
| 206 nullptr), |
| 207 SpellcheckLanguageTestCase("en-US", |
| 208 "en-US,en", |
| 209 1UL, |
| 210 "en-US", |
| 211 nullptr, |
| 212 nullptr), |
| 213 SpellcheckLanguageTestCase("en-US,fr", |
| 214 "en,fr,en-US,en-AU", |
| 215 2UL, |
| 216 "en-US", |
| 217 "fr", |
| 218 "en-AU"), |
| 219 SpellcheckLanguageTestCase("fr", |
| 220 "en,en-JP,fr,zz,en-US", |
| 221 1UL, |
| 222 "fr", |
| 223 "en-US", |
| 224 nullptr))); |
| 225 |
| 226 IN_PROC_BROWSER_TEST_P(MultilingualSpellcheckServiceBrowserTest, |
| 227 GetSpellcheckLanguages) { |
| 228 BrowserContext* context = GetContext(); |
| 229 PrefService* prefs = user_prefs::UserPrefs::Get(context); |
| 230 prefs->SetString(prefs::kSpellCheckDictionaries, |
| 231 GetParam().spellcheck_dictionary); |
| 232 prefs->SetString(prefs::kAcceptLanguages, GetParam().accept_languages); |
| 233 |
| 234 std::vector<std::string> spellcheck_languages; |
| 235 size_t enabled_spellcheck_languages = |
| 236 SpellcheckService::GetSpellCheckLanguages(context, &spellcheck_languages); |
| 237 |
| 238 EXPECT_EQ(GetParam().expected_enabled_spellcheck_languages, |
| 239 enabled_spellcheck_languages); |
| 240 EXPECT_EQ(GetParam().expected_spellcheck_languages, spellcheck_languages); |
| 241 } |
OLD | NEW |