Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: chrome/browser/spellchecker/spellcheck_service_browsertest.cc

Issue 1156473007: Enables the user to select multiple languages for spellchecking (UI) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactored some tests and addressed comments. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_member.h"
please use gerrit instead 2015/06/17 17:47:16 Why this include?
Julius 2015/06/17 20:14:45 Deleted.
13 #include "base/prefs/pref_service.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" 25 #include "url/gurl.h"
16 26
17 using content::BrowserContext; 27 using content::BrowserContext;
18 28
19 namespace { 29 namespace {
20 30
21 // A corrupted BDICT data used in DeleteCorruptedBDICT. Please do not use this 31 // A corrupted BDICT data used in DeleteCorruptedBDICT. Please do not use this
22 // BDICT data for other tests. 32 // BDICT data for other tests.
23 const uint8 kCorruptedBDICT[] = { 33 const uint8 kCorruptedBDICT[] = {
Julius 2015/06/17 00:59:53 clang-format reformatted this.
please use gerrit instead 2015/06/17 17:47:15 Acknowledged.
24 0x42, 0x44, 0x69, 0x63, 0x02, 0x00, 0x01, 0x00, 34 0x42, 0x44, 0x69, 0x63, 0x02, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00,
25 0x20, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00, 35 0x3b, 0x00, 0x00, 0x00, 0x65, 0x72, 0xe0, 0xac, 0x27, 0xc7, 0xda, 0x66,
26 0x65, 0x72, 0xe0, 0xac, 0x27, 0xc7, 0xda, 0x66, 36 0x6d, 0x1e, 0xa6, 0x35, 0xd1, 0xf6, 0xb7, 0x35, 0x32, 0x00, 0x00, 0x00,
27 0x6d, 0x1e, 0xa6, 0x35, 0xd1, 0xf6, 0xb7, 0x35, 37 0x38, 0x00, 0x00, 0x00, 0x39, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00,
28 0x32, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 38 0x0a, 0x0a, 0x41, 0x46, 0x20, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe6,
29 0x39, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00, 39 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 }; 40 };
34 41
35 } // namespace 42 } // namespace
36 43
37 class SpellcheckServiceBrowserTest : public InProcessBrowserTest { 44 struct SpellcheckLanguageTestCase {
45 SpellcheckLanguageTestCase(
46 const std::string& spellcheck_dictionary,
47 const std::string& accept_languages,
48 const unsigned long& expected_enabled_spellcheck_languages,
please use gerrit instead 2015/06/17 17:47:15 size_t, no const, no &. "const var_type& var_name
Julius 2015/06/17 20:14:46 Done.
49 const std::vector<std::string> expected_spellcheck_languages)
50 : spellcheck_dictionary(spellcheck_dictionary),
51 accept_languages(accept_languages),
52 expected_enabled_spellcheck_languages(
53 expected_enabled_spellcheck_languages),
54 expected_spellcheck_languages(expected_spellcheck_languages) {}
55 ~SpellcheckLanguageTestCase() {}
56
57 const std::string spellcheck_dictionary;
58 const std::string accept_languages;
59 const unsigned long expected_enabled_spellcheck_languages;
please use gerrit instead 2015/06/17 17:47:15 size_t
Julius 2015/06/17 20:14:45 Done.
60 const std::vector<std::string> expected_spellcheck_languages;
61 };
62
63 class SpellcheckServiceBrowserTest
64 : public InProcessBrowserTest,
65 public testing::WithParamInterface<SpellcheckLanguageTestCase> {
38 public: 66 public:
39 Profile* GetProfile() { 67 Profile* GetProfile() { return browser()->profile(); }
please use gerrit instead 2015/06/17 17:47:16 class SpellcheckServiceBrowserTest : public In
Julius 2015/06/17 20:14:46 Done.
40 return browser()->profile();
41 }
42 }; 68 };
43 69
70 INSTANTIATE_TEST_CASE_P(
71 SpellcheckLanguageTestCases,
72 SpellcheckServiceBrowserTest,
73 testing::Values(
74 SpellcheckLanguageTestCase("en-US",
75 "en,en-US",
76 1UL,
77 std::vector<std::string>{"en-US"}),
78 SpellcheckLanguageTestCase("en-US",
79 "en-US,en",
80 1UL,
81 std::vector<std::string>{"en-US"}),
82 SpellcheckLanguageTestCase("en-US",
83 "en,fr,en-US,en-AU",
84 1UL,
85 std::vector<std::string>{"en-US", "fr",
please use gerrit instead 2015/06/17 17:47:15 This is "(Uniform) Initialization Syntax", which i
Julius 2015/06/17 20:14:46 Done.
86 "en-AU"}),
87 SpellcheckLanguageTestCase("fr",
88 "en,en-JP,fr,zz,en-US",
89 1UL,
90 std::vector<std::string>{"fr", "en-US"})));
91
92 IN_PROC_BROWSER_TEST_P(SpellcheckServiceBrowserTest, GetSpellcheckLanguages) {
93 BrowserContext* context = static_cast<BrowserContext*>(GetProfile());
94 PrefService* prefs = user_prefs::UserPrefs::Get(context);
95 prefs->SetString(prefs::kSpellCheckDictionary,
96 GetParam().spellcheck_dictionary);
97 prefs->SetString(prefs::kAcceptLanguages, GetParam().accept_languages);
98
99 std::vector<std::string> spellcheck_languages;
100 size_t enabled_spellcheck_languages =
101 SpellcheckService::GetSpellCheckLanguages(context, &spellcheck_languages);
102
103 EXPECT_EQ(GetParam().expected_enabled_spellcheck_languages,
104 enabled_spellcheck_languages);
105 EXPECT_EQ(GetParam().expected_spellcheck_languages, spellcheck_languages);
106 }
107
44 // Tests that we can delete a corrupted BDICT file used by hunspell. We do not 108 // 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. 109 // run this test on Mac because Mac does not use hunspell by default.
46 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) { 110 IN_PROC_BROWSER_TEST_F(SpellcheckServiceBrowserTest, DeleteCorruptedBDICT) {
47 // Write the corrupted BDICT data to create a corrupted BDICT file. 111 // Write the corrupted BDICT data to create a corrupted BDICT file.
48 base::FilePath dict_dir; 112 base::FilePath dict_dir;
49 ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir)); 113 ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir));
50 base::FilePath bdict_path = 114 base::FilePath bdict_path =
51 chrome::spellcheck_common::GetVersionedFileName("en-US", dict_dir); 115 chrome::spellcheck_common::GetVersionedFileName("en-US", dict_dir);
52 116
53 size_t actual = base::WriteFile(bdict_path, 117 size_t actual = base::WriteFile(
54 reinterpret_cast<const char*>(kCorruptedBDICT), 118 bdict_path, reinterpret_cast<const char*>(kCorruptedBDICT),
Julius 2015/06/17 00:59:53 clang-format reformatted this and the lines below.
please use gerrit instead 2015/06/17 17:47:15 Acknowledged.
55 arraysize(kCorruptedBDICT)); 119 arraysize(kCorruptedBDICT));
56 EXPECT_EQ(arraysize(kCorruptedBDICT), actual); 120 EXPECT_EQ(arraysize(kCorruptedBDICT), actual);
57 121
58 // Attach an event to the SpellcheckService object so we can receive its 122 // Attach an event to the SpellcheckService object so we can receive its
59 // status updates. 123 // status updates.
60 base::WaitableEvent event(true, false); 124 base::WaitableEvent event(true, false);
61 SpellcheckService::AttachStatusEvent(&event); 125 SpellcheckService::AttachStatusEvent(&event);
62 126
63 BrowserContext * context = static_cast<BrowserContext*>(GetProfile()); 127 BrowserContext* context = static_cast<BrowserContext*>(GetProfile());
64 128
65 // Ensure that the SpellcheckService object does not already exist. Otherwise 129 // Ensure that the SpellcheckService object does not already exist. Otherwise
66 // the next line will not force creation of the SpellcheckService and the 130 // the next line will not force creation of the SpellcheckService and the
67 // test will fail. 131 // test will fail.
68 SpellcheckService* service = static_cast<SpellcheckService*>( 132 SpellcheckService* service = static_cast<SpellcheckService*>(
69 SpellcheckServiceFactory::GetInstance()->GetServiceForBrowserContext( 133 SpellcheckServiceFactory::GetInstance()->GetServiceForBrowserContext(
70 context, 134 context, false));
71 false));
72 ASSERT_EQ(NULL, service); 135 ASSERT_EQ(NULL, service);
73 136
74 // Getting the spellcheck_service will initialize the SpellcheckService 137 // Getting the spellcheck_service will initialize the SpellcheckService
75 // object with the corrupted BDICT file created above since the hunspell 138 // object with the corrupted BDICT file created above since the hunspell
76 // dictionary is loaded in the SpellcheckService constructor right now. 139 // dictionary is loaded in the SpellcheckService constructor right now.
77 // The SpellCheckHost object will send a BDICT_CORRUPTED event. 140 // The SpellCheckHost object will send a BDICT_CORRUPTED event.
78 SpellcheckServiceFactory::GetForContext(context); 141 SpellcheckServiceFactory::GetForContext(context);
79 142
80 // Check the received event. Also we check if Chrome has successfully deleted 143 // Check the received event. Also we check if Chrome has successfully deleted
81 // the corrupted dictionary. We delete the corrupted dictionary to avoid 144 // the corrupted dictionary. We delete the corrupted dictionary to avoid
82 // leaking it when this test fails. 145 // leaking it when this test fails.
83 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); 146 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE);
84 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); 147 content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
85 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED, 148 EXPECT_EQ(SpellcheckService::BDICT_CORRUPTED,
86 SpellcheckService::GetStatusEvent()); 149 SpellcheckService::GetStatusEvent());
87 if (base::PathExists(bdict_path)) { 150 if (base::PathExists(bdict_path)) {
88 ADD_FAILURE(); 151 ADD_FAILURE();
89 EXPECT_TRUE(base::DeleteFile(bdict_path, true)); 152 EXPECT_TRUE(base::DeleteFile(bdict_path, true));
90 } 153 }
91 } 154 }
155
156 class MultilingualSpellcheckServiceBrowserTest
157 : public InProcessBrowserTest,
158 public testing::WithParamInterface<SpellcheckLanguageTestCase> {
159 public:
160 Profile* GetProfile() { return browser()->profile(); }
161 void SetUpCommandLine(base::CommandLine* command_line) override {
please use gerrit instead 2015/06/17 17:47:15 Please also call "InProcBrowserTest::SetUpCommandL
Julius 2015/06/17 20:14:46 Done.
162 command_line->AppendSwitch(switches::kEnableMultilingualSpellChecker);
163 }
164 };
165
166 INSTANTIATE_TEST_CASE_P(
167 MultilingualSpellcheckLanguageTestCases,
168 MultilingualSpellcheckServiceBrowserTest,
169 testing::Values(
170 SpellcheckLanguageTestCase("en-US",
171 "en,en-US",
172 1UL,
173 std::vector<std::string>{"en-US"}),
174 SpellcheckLanguageTestCase("en-US",
175 "en-US,en",
176 1UL,
177 std::vector<std::string>{"en-US"}),
178 SpellcheckLanguageTestCase("en-US,fr",
179 "en,fr,en-US,en-AU",
180 2UL,
181 std::vector<std::string>{"en-US", "fr",
182 "en-AU"}),
183 SpellcheckLanguageTestCase("fr",
184 "en,en-JP,fr,zz,en-US",
185 1UL,
186 std::vector<std::string>{"fr", "en-US"})));
187
188 IN_PROC_BROWSER_TEST_P(MultilingualSpellcheckServiceBrowserTest,
189 GetSpellcheckLanguages) {
190 BrowserContext* context = static_cast<BrowserContext*>(GetProfile());
191 PrefService* prefs = user_prefs::UserPrefs::Get(context);
192 prefs->SetString(prefs::kSpellCheckDictionaries,
193 GetParam().spellcheck_dictionary);
194 prefs->SetString(prefs::kAcceptLanguages, GetParam().accept_languages);
195
196 std::vector<std::string> spellcheck_languages;
197 size_t enabled_spellcheck_languages =
198 SpellcheckService::GetSpellCheckLanguages(context,
199 &spellcheck_languages);
200
201 EXPECT_EQ(GetParam().expected_enabled_spellcheck_languages,
202 enabled_spellcheck_languages);
203 EXPECT_EQ(GetParam().expected_spellcheck_languages, spellcheck_languages);
204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698