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.
9 #include "base/json/json_reader.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/spellchecker/spelling_service_client.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/spellcheck_result.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "net/base/load_flags.h"
21 #include "net/url_request/test_url_fetcher_factory.h"
22 #include "testing/gtest/include/gtest/gtest.h"
26 // A mock URL fetcher used in the TestingSpellingServiceClient class. This class
27 // verifies JSON-RPC requests when the SpellingServiceClient class sends them to
28 // the Spelling service. This class also verifies the SpellingServiceClient
29 // class does not either send cookies to the Spelling service or accept cookies
31 class TestSpellingURLFetcher
: public net::TestURLFetcher
{
33 TestSpellingURLFetcher(int id
,
35 net::URLFetcherDelegate
* d
,
37 const std::string
& text
,
38 const std::string
& language
,
40 const std::string
& response
)
41 : net::TestURLFetcher(0, url
, d
),
42 version_(base::StringPrintf("v%d", version
)),
43 language_(language
.empty() ? std::string("en") : language
),
45 set_response_code(status
);
46 SetResponseString(response
);
48 ~TestSpellingURLFetcher() override
{}
50 void SetUploadData(const std::string
& upload_content_type
,
51 const std::string
& upload_content
) override
{
52 // Verify the given content type is JSON. (The Spelling service returns an
53 // internal server error when this content type is not JSON.)
54 EXPECT_EQ("application/json", upload_content_type
);
56 // Parse the JSON to be sent to the service, and verify its parameters.
57 scoped_ptr
<base::DictionaryValue
> value(static_cast<base::DictionaryValue
*>(
58 base::JSONReader::Read(upload_content
,
59 base::JSON_ALLOW_TRAILING_COMMAS
)));
60 ASSERT_TRUE(!!value
.get());
62 EXPECT_TRUE(value
->GetString("method", &method
));
63 EXPECT_EQ("spelling.check", method
);
65 EXPECT_TRUE(value
->GetString("apiVersion", &version
));
66 EXPECT_EQ(version_
, version
);
68 EXPECT_TRUE(value
->GetString("params.text", &text
));
69 EXPECT_EQ(text_
, text
);
71 EXPECT_TRUE(value
->GetString("params.language", &language
));
72 EXPECT_EQ(language_
, language
);
73 ASSERT_TRUE(GetExpectedCountry(language
, &country_
));
75 EXPECT_TRUE(value
->GetString("params.originCountry", &country
));
76 EXPECT_EQ(country_
, country
);
78 net::TestURLFetcher::SetUploadData(upload_content_type
, upload_content
);
81 void Start() override
{
82 // Verify that this client does not either send cookies to the Spelling
83 // service or accept cookies from it.
84 EXPECT_EQ(net::LOAD_DO_NOT_SEND_COOKIES
| net::LOAD_DO_NOT_SAVE_COOKIES
,
89 bool GetExpectedCountry(const std::string
& language
, std::string
* country
) {
97 for (size_t i
= 0; i
< arraysize(kCountries
); ++i
) {
98 if (!language
.compare(kCountries
[i
].language
)) {
99 country
->assign(kCountries
[i
].country
);
106 std::string version_
;
107 std::string language_
;
108 std::string country_
;
112 // A class derived from the SpellingServiceClient class used by the
113 // SpellingServiceClientTest class. This class overrides CreateURLFetcher so
114 // this test can use TestSpellingURLFetcher. This class also lets tests access
115 // the ParseResponse method.
116 class TestingSpellingServiceClient
: public SpellingServiceClient
{
118 TestingSpellingServiceClient()
124 ~TestingSpellingServiceClient() override
{}
126 void SetHTTPRequest(int type
,
127 const std::string
& text
,
128 const std::string
& language
) {
129 request_type_
= type
;
130 request_text_
= text
;
131 request_language_
= language
;
134 void SetHTTPResponse(int status
, const char* data
) {
135 response_status_
= status
;
136 response_data_
.assign(data
);
139 void SetExpectedTextCheckResult(bool success
, const char* text
) {
141 corrected_text_
.assign(base::UTF8ToUTF16(text
));
144 void CallOnURLFetchComplete() {
145 ASSERT_TRUE(!!fetcher_
);
146 fetcher_
->delegate()->OnURLFetchComplete(fetcher_
);
150 void VerifyResponse(bool success
,
151 const base::string16
& request_text
,
152 const std::vector
<SpellCheckResult
>& results
) {
153 EXPECT_EQ(success_
, success
);
154 base::string16
text(base::UTF8ToUTF16(request_text_
));
155 EXPECT_EQ(text
, request_text
);
156 for (std::vector
<SpellCheckResult
>::const_iterator it
= results
.begin();
157 it
!= results
.end(); ++it
) {
158 text
.replace(it
->location
, it
->length
, it
->replacement
);
160 EXPECT_EQ(corrected_text_
, text
);
163 bool ParseResponseSuccess(const std::string
& data
) {
164 std::vector
<SpellCheckResult
> results
;
165 return ParseResponse(data
, &results
);
169 net::URLFetcher
* CreateURLFetcher(const GURL
& url
) override
{
170 EXPECT_EQ("https://www.googleapis.com/rpc", url
.spec());
171 fetcher_
= new TestSpellingURLFetcher(0, url
, this,
172 request_type_
, request_text_
,
174 response_status_
, response_data_
);
179 std::string request_text_
;
180 std::string request_language_
;
181 int response_status_
;
182 std::string response_data_
;
184 base::string16 corrected_text_
;
185 TestSpellingURLFetcher
* fetcher_
; // weak
188 // A test class used for testing the SpellingServiceClient class. This class
189 // implements a callback function used by the SpellingServiceClient class to
190 // monitor the class calls the callback with expected results.
191 class SpellingServiceClientTest
: public testing::Test
{
193 void OnTextCheckComplete(int tag
,
195 const base::string16
& text
,
196 const std::vector
<SpellCheckResult
>& results
) {
197 client_
.VerifyResponse(success
, text
, results
);
201 content::TestBrowserThreadBundle thread_bundle_
;
202 TestingSpellingServiceClient client_
;
203 TestingProfile profile_
;
208 // Verifies that SpellingServiceClient::RequestTextCheck() creates a JSON
209 // request sent to the Spelling service as we expect. This test also verifies
210 // that it parses a JSON response from the service and calls the callback
211 // function. To avoid sending JSON-RPC requests to the service, this test uses a
212 // custom TestURLFecher class (TestSpellingURLFetcher) which calls
213 // SpellingServiceClient::OnURLFetchComplete() with the parameters set by this
214 // test. This test also uses a custom callback function that replaces all
215 // misspelled words with ones suggested by the service so this test can compare
216 // the corrected text with the expected results. (If there are not any
217 // misspelled words, |corrected_text| should be equal to |request_text|.)
218 TEST_F(SpellingServiceClientTest
, RequestTextCheck
) {
219 static const struct {
220 const char* request_text
;
221 SpellingServiceClient::ServiceType request_type
;
223 const char* response_data
;
225 const char* corrected_text
;
226 const char* language
;
230 SpellingServiceClient::SUGGEST
,
238 SpellingServiceClient::SUGGEST
,
246 SpellingServiceClient::SUGGEST
,
250 " \"spellingCheckResponse\": {\n"
251 " \"misspellings\": [{\n"
252 " \"charStart\": 0,\n"
253 " \"charLength\": 9,\n"
254 " \"suggestions\": [{ \"suggestion\": \"chromebook\" }],\n"
255 " \"canAutoCorrect\": false\n"
265 SpellingServiceClient::SPELLCHECK
,
272 "I have been to USA.",
273 SpellingServiceClient::SPELLCHECK
,
277 "I have been to USA.",
280 "I have bean to USA.",
281 SpellingServiceClient::SPELLCHECK
,
285 " \"spellingCheckResponse\": {\n"
286 " \"misspellings\": [{\n"
287 " \"charStart\": 7,\n"
288 " \"charLength\": 4,\n"
289 " \"suggestions\": [{ \"suggestion\": \"been\" }],\n"
290 " \"canAutoCorrect\": false\n"
296 "I have been to USA.",
301 PrefService
* pref
= profile_
.GetPrefs();
302 pref
->SetBoolean(prefs::kEnableContinuousSpellcheck
, true);
303 pref
->SetBoolean(prefs::kSpellCheckUseSpellingService
, true);
305 for (size_t i
= 0; i
< arraysize(kTests
); ++i
) {
306 client_
.SetHTTPRequest(kTests
[i
].request_type
, kTests
[i
].request_text
,
308 client_
.SetHTTPResponse(kTests
[i
].response_status
, kTests
[i
].response_data
);
309 client_
.SetExpectedTextCheckResult(kTests
[i
].success
,
310 kTests
[i
].corrected_text
);
311 pref
->SetString(prefs::kSpellCheckDictionary
, kTests
[i
].language
);
312 client_
.RequestTextCheck(
314 kTests
[i
].request_type
,
315 base::ASCIIToUTF16(kTests
[i
].request_text
),
316 base::Bind(&SpellingServiceClientTest::OnTextCheckComplete
,
317 base::Unretained(this), 0));
318 client_
.CallOnURLFetchComplete();
322 // Verify that SpellingServiceClient::IsAvailable() returns true only when it
323 // can send suggest requests or spellcheck requests.
324 TEST_F(SpellingServiceClientTest
, AvailableServices
) {
325 const SpellingServiceClient::ServiceType kSuggest
=
326 SpellingServiceClient::SUGGEST
;
327 const SpellingServiceClient::ServiceType kSpellcheck
=
328 SpellingServiceClient::SPELLCHECK
;
330 // When a user disables spellchecking or prevent using the Spelling service,
331 // this function should return false both for suggestions and for spellcheck.
332 PrefService
* pref
= profile_
.GetPrefs();
333 pref
->SetBoolean(prefs::kEnableContinuousSpellcheck
, false);
334 pref
->SetBoolean(prefs::kSpellCheckUseSpellingService
, false);
335 EXPECT_FALSE(client_
.IsAvailable(&profile_
, kSuggest
));
336 EXPECT_FALSE(client_
.IsAvailable(&profile_
, kSpellcheck
));
338 pref
->SetBoolean(prefs::kEnableContinuousSpellcheck
, true);
339 pref
->SetBoolean(prefs::kSpellCheckUseSpellingService
, true);
341 // For locales supported by the SpellCheck service, this function returns
342 // false for suggestions and true for spellcheck. (The comment in
343 // SpellingServiceClient::IsAvailable() describes why this function returns
344 // false for suggestions.) If there is no language set, then we
345 // do not allow any remote.
346 pref
->SetString(prefs::kSpellCheckDictionary
, std::string());
347 EXPECT_FALSE(client_
.IsAvailable(&profile_
, kSuggest
));
348 EXPECT_FALSE(client_
.IsAvailable(&profile_
, kSpellcheck
));
350 #if !defined(OS_MACOSX)
351 static const char* kSupported
[] = {
352 "en-AU", "en-CA", "en-GB", "en-US",
354 // If spellcheck is allowed, then suggest is not since spellcheck is a
355 // superset of suggest.
356 for (size_t i
= 0; i
< arraysize(kSupported
); ++i
) {
357 pref
->SetString(prefs::kSpellCheckDictionary
, kSupported
[i
]);
358 EXPECT_FALSE(client_
.IsAvailable(&profile_
, kSuggest
));
359 EXPECT_TRUE(client_
.IsAvailable(&profile_
, kSpellcheck
));
362 // This function returns true for suggestions for all and false for
363 // spellcheck for unsupported locales.
364 static const char* kUnsupported
[] = {
365 "af-ZA", "bg-BG", "ca-ES", "cs-CZ", "da-DK", "de-DE", "el-GR", "es-ES",
366 "et-EE", "fo-FO", "fr-FR", "he-IL", "hi-IN", "hr-HR", "hu-HU", "id-ID",
367 "it-IT", "lt-LT", "lv-LV", "nb-NO", "nl-NL", "pl-PL", "pt-BR", "pt-PT",
368 "ro-RO", "ru-RU", "sk-SK", "sl-SI", "sh", "sr", "sv-SE", "tr-TR",
371 for (size_t i
= 0; i
< arraysize(kUnsupported
); ++i
) {
372 pref
->SetString(prefs::kSpellCheckDictionary
, kUnsupported
[i
]);
373 EXPECT_TRUE(client_
.IsAvailable(&profile_
, kSuggest
));
374 EXPECT_FALSE(client_
.IsAvailable(&profile_
, kSpellcheck
));
376 #endif // !defined(OS_MACOSX)
379 // Verify that an error in JSON response from spelling service will result in
380 // ParseResponse returning false.
381 TEST_F(SpellingServiceClientTest
, ResponseErrorTest
) {
382 EXPECT_TRUE(client_
.ParseResponseSuccess("{\"result\": {}}"));
383 EXPECT_FALSE(client_
.ParseResponseSuccess("{\"error\": {}}"));