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

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

Issue 1201563004: Revert of Handle typographical apostrophe with spelling service client (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 16 matching lines...) Expand all
27 // verifies JSON-RPC requests when the SpellingServiceClient class sends them to 27 // verifies JSON-RPC requests when the SpellingServiceClient class sends them to
28 // the Spelling service. This class also verifies the SpellingServiceClient 28 // the Spelling service. This class also verifies the SpellingServiceClient
29 // class does not either send cookies to the Spelling service or accept cookies 29 // class does not either send cookies to the Spelling service or accept cookies
30 // from it. 30 // from it.
31 class TestSpellingURLFetcher : public net::TestURLFetcher { 31 class TestSpellingURLFetcher : public net::TestURLFetcher {
32 public: 32 public:
33 TestSpellingURLFetcher(int id, 33 TestSpellingURLFetcher(int id,
34 const GURL& url, 34 const GURL& url,
35 net::URLFetcherDelegate* d, 35 net::URLFetcherDelegate* d,
36 int version, 36 int version,
37 const std::string& sanitized_text, 37 const std::string& text,
38 const std::string& language, 38 const std::string& language,
39 int status, 39 int status,
40 const std::string& response) 40 const std::string& response)
41 : net::TestURLFetcher(0, url, d), 41 : net::TestURLFetcher(0, url, d),
42 version_(base::StringPrintf("v%d", version)), 42 version_(base::StringPrintf("v%d", version)),
43 language_(language.empty() ? std::string("en") : language), 43 language_(language.empty() ? std::string("en") : language),
44 sanitized_text_(sanitized_text) { 44 text_(text) {
45 set_response_code(status); 45 set_response_code(status);
46 SetResponseString(response); 46 SetResponseString(response);
47 } 47 }
48 ~TestSpellingURLFetcher() override {} 48 ~TestSpellingURLFetcher() override {}
49 49
50 void SetUploadData(const std::string& upload_content_type, 50 void SetUploadData(const std::string& upload_content_type,
51 const std::string& upload_content) override { 51 const std::string& upload_content) override {
52 // Verify the given content type is JSON. (The Spelling service returns an 52 // Verify the given content type is JSON. (The Spelling service returns an
53 // internal server error when this content type is not JSON.) 53 // internal server error when this content type is not JSON.)
54 EXPECT_EQ("application/json", upload_content_type); 54 EXPECT_EQ("application/json", upload_content_type);
55 55
56 // Parse the JSON to be sent to the service, and verify its parameters. 56 // Parse the JSON to be sent to the service, and verify its parameters.
57 scoped_ptr<base::DictionaryValue> value( 57 scoped_ptr<base::DictionaryValue> value(
58 static_cast<base::DictionaryValue*>(base::JSONReader::DeprecatedRead( 58 static_cast<base::DictionaryValue*>(base::JSONReader::DeprecatedRead(
59 upload_content, base::JSON_ALLOW_TRAILING_COMMAS))); 59 upload_content, base::JSON_ALLOW_TRAILING_COMMAS)));
60 ASSERT_TRUE(value.get()); 60 ASSERT_TRUE(value.get());
61 std::string method; 61 std::string method;
62 EXPECT_TRUE(value->GetString("method", &method)); 62 EXPECT_TRUE(value->GetString("method", &method));
63 EXPECT_EQ("spelling.check", method); 63 EXPECT_EQ("spelling.check", method);
64 std::string version; 64 std::string version;
65 EXPECT_TRUE(value->GetString("apiVersion", &version)); 65 EXPECT_TRUE(value->GetString("apiVersion", &version));
66 EXPECT_EQ(version_, version); 66 EXPECT_EQ(version_, version);
67 std::string sanitized_text; 67 std::string text;
68 EXPECT_TRUE(value->GetString("params.text", &sanitized_text)); 68 EXPECT_TRUE(value->GetString("params.text", &text));
69 EXPECT_EQ(sanitized_text_, sanitized_text); 69 EXPECT_EQ(text_, text);
70 std::string language; 70 std::string language;
71 EXPECT_TRUE(value->GetString("params.language", &language)); 71 EXPECT_TRUE(value->GetString("params.language", &language));
72 EXPECT_EQ(language_, language); 72 EXPECT_EQ(language_, language);
73 ASSERT_TRUE(GetExpectedCountry(language, &country_)); 73 ASSERT_TRUE(GetExpectedCountry(language, &country_));
74 std::string country; 74 std::string country;
75 EXPECT_TRUE(value->GetString("params.originCountry", &country)); 75 EXPECT_TRUE(value->GetString("params.originCountry", &country));
76 EXPECT_EQ(country_, country); 76 EXPECT_EQ(country_, country);
77 77
78 net::TestURLFetcher::SetUploadData(upload_content_type, upload_content); 78 net::TestURLFetcher::SetUploadData(upload_content_type, upload_content);
79 } 79 }
(...skipping 19 matching lines...) Expand all
99 country->assign(kCountries[i].country); 99 country->assign(kCountries[i].country);
100 return true; 100 return true;
101 } 101 }
102 } 102 }
103 return false; 103 return false;
104 } 104 }
105 105
106 std::string version_; 106 std::string version_;
107 std::string language_; 107 std::string language_;
108 std::string country_; 108 std::string country_;
109 std::string sanitized_text_; 109 std::string text_;
110 }; 110 };
111 111
112 // A class derived from the SpellingServiceClient class used by the 112 // A class derived from the SpellingServiceClient class used by the
113 // SpellingServiceClientTest class. This class overrides CreateURLFetcher so 113 // SpellingServiceClientTest class. This class overrides CreateURLFetcher so
114 // this test can use TestSpellingURLFetcher. This class also lets tests access 114 // this test can use TestSpellingURLFetcher. This class also lets tests access
115 // the ParseResponse method. 115 // the ParseResponse method.
116 class TestingSpellingServiceClient : public SpellingServiceClient { 116 class TestingSpellingServiceClient : public SpellingServiceClient {
117 public: 117 public:
118 TestingSpellingServiceClient() 118 TestingSpellingServiceClient()
119 : request_type_(0), 119 : request_type_(0),
120 response_status_(0), 120 response_status_(0),
121 success_(false), 121 success_(false),
122 fetcher_(NULL) { 122 fetcher_(NULL) {
123 } 123 }
124 ~TestingSpellingServiceClient() override {} 124 ~TestingSpellingServiceClient() override {}
125 125
126 void SetHTTPRequest(int type, 126 void SetHTTPRequest(int type,
127 const std::string& sanitized_text, 127 const std::string& text,
128 const std::string& language) { 128 const std::string& language) {
129 request_type_ = type; 129 request_type_ = type;
130 sanitized_request_text_ = sanitized_text; 130 request_text_ = text;
131 request_language_ = language; 131 request_language_ = language;
132 } 132 }
133 133
134 void SetHTTPResponse(int status, const char* data) { 134 void SetHTTPResponse(int status, const char* data) {
135 response_status_ = status; 135 response_status_ = status;
136 response_data_.assign(data); 136 response_data_.assign(data);
137 } 137 }
138 138
139 void SetExpectedTextCheckResult(bool success, const char* text) { 139 void SetExpectedTextCheckResult(bool success, const char* text) {
140 success_ = success; 140 success_ = success;
141 corrected_text_.assign(base::UTF8ToUTF16(text)); 141 corrected_text_.assign(base::UTF8ToUTF16(text));
142 } 142 }
143 143
144 void CallOnURLFetchComplete() { 144 void CallOnURLFetchComplete() {
145 ASSERT_TRUE(fetcher_); 145 ASSERT_TRUE(fetcher_);
146 fetcher_->delegate()->OnURLFetchComplete(fetcher_); 146 fetcher_->delegate()->OnURLFetchComplete(fetcher_);
147 fetcher_ = NULL; 147 fetcher_ = NULL;
148 } 148 }
149 149
150 void VerifyResponse(bool success, 150 void VerifyResponse(bool success,
151 const base::string16& request_text, 151 const base::string16& request_text,
152 const std::vector<SpellCheckResult>& results) { 152 const std::vector<SpellCheckResult>& results) {
153 EXPECT_EQ(success_, success); 153 EXPECT_EQ(success_, success);
154 base::string16 text(base::UTF8ToUTF16(sanitized_request_text_)); 154 base::string16 text(base::UTF8ToUTF16(request_text_));
155 EXPECT_EQ(text, request_text);
155 for (std::vector<SpellCheckResult>::const_iterator it = results.begin(); 156 for (std::vector<SpellCheckResult>::const_iterator it = results.begin();
156 it != results.end(); ++it) { 157 it != results.end(); ++it) {
157 text.replace(it->location, it->length, it->replacement); 158 text.replace(it->location, it->length, it->replacement);
158 } 159 }
159 EXPECT_EQ(corrected_text_, text); 160 EXPECT_EQ(corrected_text_, text);
160 } 161 }
161 162
162 bool ParseResponseSuccess(const std::string& data) { 163 bool ParseResponseSuccess(const std::string& data) {
163 std::vector<SpellCheckResult> results; 164 std::vector<SpellCheckResult> results;
164 return ParseResponse(data, &results); 165 return ParseResponse(data, &results);
165 } 166 }
166 167
167 private: 168 private:
168 scoped_ptr<net::URLFetcher> CreateURLFetcher(const GURL& url) override { 169 scoped_ptr<net::URLFetcher> CreateURLFetcher(const GURL& url) override {
169 EXPECT_EQ("https://www.googleapis.com/rpc", url.spec()); 170 EXPECT_EQ("https://www.googleapis.com/rpc", url.spec());
170 fetcher_ = new TestSpellingURLFetcher( 171 fetcher_ = new TestSpellingURLFetcher(0, url, this,
171 0, url, this, request_type_, sanitized_request_text_, request_language_, 172 request_type_, request_text_,
172 response_status_, response_data_); 173 request_language_,
174 response_status_, response_data_);
173 return scoped_ptr<net::URLFetcher>(fetcher_); 175 return scoped_ptr<net::URLFetcher>(fetcher_);
174 } 176 }
175 177
176 int request_type_; 178 int request_type_;
177 std::string sanitized_request_text_; 179 std::string request_text_;
178 std::string request_language_; 180 std::string request_language_;
179 int response_status_; 181 int response_status_;
180 std::string response_data_; 182 std::string response_data_;
181 bool success_; 183 bool success_;
182 base::string16 corrected_text_; 184 base::string16 corrected_text_;
183 TestSpellingURLFetcher* fetcher_; // weak 185 TestSpellingURLFetcher* fetcher_; // weak
184 }; 186 };
185 187
186 // A test class used for testing the SpellingServiceClient class. This class 188 // A test class used for testing the SpellingServiceClient class. This class
187 // implements a callback function used by the SpellingServiceClient class to 189 // implements a callback function used by the SpellingServiceClient class to
(...skipping 20 matching lines...) Expand all
208 // that it parses a JSON response from the service and calls the callback 210 // that it parses a JSON response from the service and calls the callback
209 // function. To avoid sending JSON-RPC requests to the service, this test uses a 211 // function. To avoid sending JSON-RPC requests to the service, this test uses a
210 // custom TestURLFecher class (TestSpellingURLFetcher) which calls 212 // custom TestURLFecher class (TestSpellingURLFetcher) which calls
211 // SpellingServiceClient::OnURLFetchComplete() with the parameters set by this 213 // SpellingServiceClient::OnURLFetchComplete() with the parameters set by this
212 // test. This test also uses a custom callback function that replaces all 214 // test. This test also uses a custom callback function that replaces all
213 // misspelled words with ones suggested by the service so this test can compare 215 // misspelled words with ones suggested by the service so this test can compare
214 // the corrected text with the expected results. (If there are not any 216 // the corrected text with the expected results. (If there are not any
215 // misspelled words, |corrected_text| should be equal to |request_text|.) 217 // misspelled words, |corrected_text| should be equal to |request_text|.)
216 TEST_F(SpellingServiceClientTest, RequestTextCheck) { 218 TEST_F(SpellingServiceClientTest, RequestTextCheck) {
217 static const struct { 219 static const struct {
218 const wchar_t* request_text; 220 const char* request_text;
219 const char* sanitized_request_text;
220 SpellingServiceClient::ServiceType request_type; 221 SpellingServiceClient::ServiceType request_type;
221 int response_status; 222 int response_status;
222 const char* response_data; 223 const char* response_data;
223 bool success; 224 bool success;
224 const char* corrected_text; 225 const char* corrected_text;
225 const char* language; 226 const char* language;
226 } kTests[] = { 227 } kTests[] = {
227 { 228 {
228 L"",
229 "", 229 "",
230 SpellingServiceClient::SUGGEST, 230 SpellingServiceClient::SUGGEST,
231 500, 231 500,
232 "", 232 "",
233 false, 233 false,
234 "", 234 "",
235 "af", 235 "af",
236 }, { 236 }, {
237 L"chromebook",
238 "chromebook", 237 "chromebook",
239 SpellingServiceClient::SUGGEST, 238 SpellingServiceClient::SUGGEST,
240 200, 239 200,
241 "{}", 240 "{}",
242 true, 241 true,
243 "chromebook", 242 "chromebook",
244 "af", 243 "af",
245 }, { 244 }, {
246 L"chrombook",
247 "chrombook", 245 "chrombook",
248 SpellingServiceClient::SUGGEST, 246 SpellingServiceClient::SUGGEST,
249 200, 247 200,
250 "{\n" 248 "{\n"
251 " \"result\": {\n" 249 " \"result\": {\n"
252 " \"spellingCheckResponse\": {\n" 250 " \"spellingCheckResponse\": {\n"
253 " \"misspellings\": [{\n" 251 " \"misspellings\": [{\n"
254 " \"charStart\": 0,\n" 252 " \"charStart\": 0,\n"
255 " \"charLength\": 9,\n" 253 " \"charLength\": 9,\n"
256 " \"suggestions\": [{ \"suggestion\": \"chromebook\" }],\n" 254 " \"suggestions\": [{ \"suggestion\": \"chromebook\" }],\n"
257 " \"canAutoCorrect\": false\n" 255 " \"canAutoCorrect\": false\n"
258 " }]\n" 256 " }]\n"
259 " }\n" 257 " }\n"
260 " }\n" 258 " }\n"
261 "}", 259 "}",
262 true, 260 true,
263 "chromebook", 261 "chromebook",
264 "af", 262 "af",
265 }, { 263 }, {
266 L"",
267 "", 264 "",
268 SpellingServiceClient::SPELLCHECK, 265 SpellingServiceClient::SPELLCHECK,
269 500, 266 500,
270 "", 267 "",
271 false, 268 false,
272 "", 269 "",
273 "en", 270 "en",
274 }, { 271 }, {
275 L"I have been to USA.",
276 "I have been to USA.", 272 "I have been to USA.",
277 SpellingServiceClient::SPELLCHECK, 273 SpellingServiceClient::SPELLCHECK,
278 200, 274 200,
279 "{}", 275 "{}",
280 true, 276 true,
281 "I have been to USA.", 277 "I have been to USA.",
282 "en", 278 "en",
283 }, { 279 }, {
284 L"I have bean to USA.",
285 "I have bean to USA.", 280 "I have bean to USA.",
286 SpellingServiceClient::SPELLCHECK, 281 SpellingServiceClient::SPELLCHECK,
287 200, 282 200,
288 "{\n" 283 "{\n"
289 " \"result\": {\n" 284 " \"result\": {\n"
290 " \"spellingCheckResponse\": {\n" 285 " \"spellingCheckResponse\": {\n"
291 " \"misspellings\": [{\n" 286 " \"misspellings\": [{\n"
292 " \"charStart\": 7,\n" 287 " \"charStart\": 7,\n"
293 " \"charLength\": 4,\n" 288 " \"charLength\": 4,\n"
294 " \"suggestions\": [{ \"suggestion\": \"been\" }],\n" 289 " \"suggestions\": [{ \"suggestion\": \"been\" }],\n"
295 " \"canAutoCorrect\": false\n" 290 " \"canAutoCorrect\": false\n"
296 " }]\n" 291 " }]\n"
297 " }\n" 292 " }\n"
298 " }\n" 293 " }\n"
299 "}", 294 "}",
300 true, 295 true,
301 "I have been to USA.", 296 "I have been to USA.",
302 "en", 297 "en",
303 }, {
304 L"I\x2019mattheIn'n'Out.",
305 "I'mattheIn'n'Out.",
306 SpellingServiceClient::SPELLCHECK,
307 200,
308 "{\n"
309 " \"result\": {\n"
310 " \"spellingCheckResponse\": {\n"
311 " \"misspellings\": [{\n"
312 " \"charStart\": 0,\n"
313 " \"charLength\": 16,\n"
314 " \"suggestions\":"
315 " [{ \"suggestion\": \"I'm at the In'N'Out\" }],\n"
316 " \"canAutoCorrect\": false\n"
317 " }]\n"
318 " }\n"
319 " }\n"
320 "}",
321 true,
322 "I'm at the In'N'Out.",
323 "en",
324 }, 298 },
325 }; 299 };
326 300
327 PrefService* pref = profile_.GetPrefs(); 301 PrefService* pref = profile_.GetPrefs();
328 pref->SetBoolean(prefs::kEnableContinuousSpellcheck, true); 302 pref->SetBoolean(prefs::kEnableContinuousSpellcheck, true);
329 pref->SetBoolean(prefs::kSpellCheckUseSpellingService, true); 303 pref->SetBoolean(prefs::kSpellCheckUseSpellingService, true);
330 304
331 for (size_t i = 0; i < arraysize(kTests); ++i) { 305 for (size_t i = 0; i < arraysize(kTests); ++i) {
332 client_.SetHTTPRequest(kTests[i].request_type, 306 client_.SetHTTPRequest(kTests[i].request_type, kTests[i].request_text,
333 kTests[i].sanitized_request_text,
334 kTests[i].language); 307 kTests[i].language);
335 client_.SetHTTPResponse(kTests[i].response_status, kTests[i].response_data); 308 client_.SetHTTPResponse(kTests[i].response_status, kTests[i].response_data);
336 client_.SetExpectedTextCheckResult(kTests[i].success, 309 client_.SetExpectedTextCheckResult(kTests[i].success,
337 kTests[i].corrected_text); 310 kTests[i].corrected_text);
338 pref->SetString(prefs::kSpellCheckDictionary, kTests[i].language); 311 pref->SetString(prefs::kSpellCheckDictionary, kTests[i].language);
339 client_.RequestTextCheck( 312 client_.RequestTextCheck(
340 &profile_, 313 &profile_,
341 kTests[i].request_type, 314 kTests[i].request_type,
342 base::WideToUTF16(kTests[i].request_text), 315 base::ASCIIToUTF16(kTests[i].request_text),
343 base::Bind(&SpellingServiceClientTest::OnTextCheckComplete, 316 base::Bind(&SpellingServiceClientTest::OnTextCheckComplete,
344 base::Unretained(this), 0)); 317 base::Unretained(this), 0));
345 client_.CallOnURLFetchComplete(); 318 client_.CallOnURLFetchComplete();
346 } 319 }
347 } 320 }
348 321
349 // Verify that SpellingServiceClient::IsAvailable() returns true only when it 322 // Verify that SpellingServiceClient::IsAvailable() returns true only when it
350 // can send suggest requests or spellcheck requests. 323 // can send suggest requests or spellcheck requests.
351 TEST_F(SpellingServiceClientTest, AvailableServices) { 324 TEST_F(SpellingServiceClientTest, AvailableServices) {
352 const SpellingServiceClient::ServiceType kSuggest = 325 const SpellingServiceClient::ServiceType kSuggest =
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 } 375 }
403 #endif // !defined(OS_MACOSX) 376 #endif // !defined(OS_MACOSX)
404 } 377 }
405 378
406 // Verify that an error in JSON response from spelling service will result in 379 // Verify that an error in JSON response from spelling service will result in
407 // ParseResponse returning false. 380 // ParseResponse returning false.
408 TEST_F(SpellingServiceClientTest, ResponseErrorTest) { 381 TEST_F(SpellingServiceClientTest, ResponseErrorTest) {
409 EXPECT_TRUE(client_.ParseResponseSuccess("{\"result\": {}}")); 382 EXPECT_TRUE(client_.ParseResponseSuccess("{\"result\": {}}"));
410 EXPECT_FALSE(client_.ParseResponseSuccess("{\"error\": {}}")); 383 EXPECT_FALSE(client_.ParseResponseSuccess("{\"error\": {}}"));
411 } 384 }
OLDNEW
« no previous file with comments | « chrome/browser/spellchecker/spelling_service_client.cc ('k') | chrome/renderer/spellchecker/spellcheck.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698