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

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

Issue 2159283003: [WIP][DO NOT LAND] Componentize spellcheck Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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/spelling_service_client.h" 5 #include "components/spellcheck/browser/spelling_service_client.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/json/json_reader.h" 14 #include "base/json/json_reader.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "chrome/common/pref_names.h" 19 //#include "chrome/common/pref_names.h"
20 #include "chrome/common/spellcheck_result.h" 20 #include "components/spellcheck/browser/pref_names.h"
21 #include "components/spellcheck/common/spellcheck_result.h"
21 #include "chrome/test/base/testing_profile.h" 22 #include "chrome/test/base/testing_profile.h"
22 #include "components/prefs/pref_service.h" 23 #include "components/prefs/pref_service.h"
23 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "net/base/load_flags.h" 25 #include "net/base/load_flags.h"
25 #include "net/url_request/test_url_fetcher_factory.h" 26 #include "net/url_request/test_url_fetcher_factory.h"
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
27 28
28 namespace { 29 namespace {
29 30
30 // A mock URL fetcher used in the TestingSpellingServiceClient class. This class 31 // A mock URL fetcher used in the TestingSpellingServiceClient class. This class
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 " }\n" 325 " }\n"
325 " }\n" 326 " }\n"
326 "}", 327 "}",
327 true, 328 true,
328 "I'm at the In'N'Out.", 329 "I'm at the In'N'Out.",
329 "en", 330 "en",
330 }, 331 },
331 }; 332 };
332 333
333 PrefService* pref = profile_.GetPrefs(); 334 PrefService* pref = profile_.GetPrefs();
334 pref->SetBoolean(prefs::kEnableContinuousSpellcheck, true); 335 pref->SetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck, true);
335 pref->SetBoolean(prefs::kSpellCheckUseSpellingService, true); 336 pref->SetBoolean(spellcheck::prefs::kSpellCheckUseSpellingService, true);
336 337
337 for (size_t i = 0; i < arraysize(kTests); ++i) { 338 for (size_t i = 0; i < arraysize(kTests); ++i) {
338 client_.SetHTTPRequest(kTests[i].request_type, 339 client_.SetHTTPRequest(kTests[i].request_type,
339 kTests[i].sanitized_request_text, 340 kTests[i].sanitized_request_text,
340 kTests[i].language); 341 kTests[i].language);
341 client_.SetHTTPResponse(kTests[i].response_status, kTests[i].response_data); 342 client_.SetHTTPResponse(kTests[i].response_status, kTests[i].response_data);
342 client_.SetExpectedTextCheckResult(kTests[i].success, 343 client_.SetExpectedTextCheckResult(kTests[i].success,
343 kTests[i].corrected_text); 344 kTests[i].corrected_text);
344 base::ListValue dictionary; 345 base::ListValue dictionary;
345 dictionary.AppendString(kTests[i].language); 346 dictionary.AppendString(kTests[i].language);
346 pref->Set(prefs::kSpellCheckDictionaries, dictionary); 347 pref->Set(spellcheck::prefs::kSpellCheckDictionaries, dictionary);
347 348
348 client_.RequestTextCheck( 349 client_.RequestTextCheck(
349 &profile_, 350 &profile_,
350 kTests[i].request_type, 351 kTests[i].request_type,
351 base::WideToUTF16(kTests[i].request_text), 352 base::WideToUTF16(kTests[i].request_text),
352 base::Bind(&SpellingServiceClientTest::OnTextCheckComplete, 353 base::Bind(&SpellingServiceClientTest::OnTextCheckComplete,
353 base::Unretained(this), 0)); 354 base::Unretained(this), 0));
354 client_.CallOnURLFetchComplete(); 355 client_.CallOnURLFetchComplete();
355 } 356 }
356 } 357 }
357 358
358 // Verify that SpellingServiceClient::IsAvailable() returns true only when it 359 // Verify that SpellingServiceClient::IsAvailable() returns true only when it
359 // can send suggest requests or spellcheck requests. 360 // can send suggest requests or spellcheck requests.
360 TEST_F(SpellingServiceClientTest, AvailableServices) { 361 TEST_F(SpellingServiceClientTest, AvailableServices) {
361 const SpellingServiceClient::ServiceType kSuggest = 362 const SpellingServiceClient::ServiceType kSuggest =
362 SpellingServiceClient::SUGGEST; 363 SpellingServiceClient::SUGGEST;
363 const SpellingServiceClient::ServiceType kSpellcheck = 364 const SpellingServiceClient::ServiceType kSpellcheck =
364 SpellingServiceClient::SPELLCHECK; 365 SpellingServiceClient::SPELLCHECK;
365 366
366 // When a user disables spellchecking or prevent using the Spelling service, 367 // When a user disables spellchecking or prevent using the Spelling service,
367 // this function should return false both for suggestions and for spellcheck. 368 // this function should return false both for suggestions and for spellcheck.
368 PrefService* pref = profile_.GetPrefs(); 369 PrefService* pref = profile_.GetPrefs();
369 pref->SetBoolean(prefs::kEnableContinuousSpellcheck, false); 370 pref->SetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck, false);
370 pref->SetBoolean(prefs::kSpellCheckUseSpellingService, false); 371 pref->SetBoolean(spellcheck::prefs::kSpellCheckUseSpellingService, false);
371 EXPECT_FALSE(client_.IsAvailable(&profile_, kSuggest)); 372 EXPECT_FALSE(client_.IsAvailable(&profile_, kSuggest));
372 EXPECT_FALSE(client_.IsAvailable(&profile_, kSpellcheck)); 373 EXPECT_FALSE(client_.IsAvailable(&profile_, kSpellcheck));
373 374
374 pref->SetBoolean(prefs::kEnableContinuousSpellcheck, true); 375 pref->SetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck, true);
375 pref->SetBoolean(prefs::kSpellCheckUseSpellingService, true); 376 pref->SetBoolean(spellcheck::prefs::kSpellCheckUseSpellingService, true);
376 377
377 // For locales supported by the SpellCheck service, this function returns 378 // For locales supported by the SpellCheck service, this function returns
378 // false for suggestions and true for spellcheck. (The comment in 379 // false for suggestions and true for spellcheck. (The comment in
379 // SpellingServiceClient::IsAvailable() describes why this function returns 380 // SpellingServiceClient::IsAvailable() describes why this function returns
380 // false for suggestions.) If there is no language set, then we 381 // false for suggestions.) If there is no language set, then we
381 // do not allow any remote. 382 // do not allow any remote.
382 pref->Set(prefs::kSpellCheckDictionaries, base::ListValue()); 383 pref->Set(spellcheck::prefs::kSpellCheckDictionaries, base::ListValue());
383 384
384 EXPECT_FALSE(client_.IsAvailable(&profile_, kSuggest)); 385 EXPECT_FALSE(client_.IsAvailable(&profile_, kSuggest));
385 EXPECT_FALSE(client_.IsAvailable(&profile_, kSpellcheck)); 386 EXPECT_FALSE(client_.IsAvailable(&profile_, kSpellcheck));
386 387
387 static const char* kSupported[] = { 388 static const char* kSupported[] = {
388 "en-AU", "en-CA", "en-GB", "en-US", "da-DK", "es-ES", 389 "en-AU", "en-CA", "en-GB", "en-US", "da-DK", "es-ES",
389 }; 390 };
390 // If spellcheck is allowed, then suggest is not since spellcheck is a 391 // If spellcheck is allowed, then suggest is not since spellcheck is a
391 // superset of suggest. 392 // superset of suggest.
392 for (size_t i = 0; i < arraysize(kSupported); ++i) { 393 for (size_t i = 0; i < arraysize(kSupported); ++i) {
393 base::ListValue dictionary; 394 base::ListValue dictionary;
394 dictionary.AppendString(kSupported[i]); 395 dictionary.AppendString(kSupported[i]);
395 pref->Set(prefs::kSpellCheckDictionaries, dictionary); 396 pref->Set(spellcheck::prefs::kSpellCheckDictionaries, dictionary);
396 397
397 EXPECT_FALSE(client_.IsAvailable(&profile_, kSuggest)); 398 EXPECT_FALSE(client_.IsAvailable(&profile_, kSuggest));
398 EXPECT_TRUE(client_.IsAvailable(&profile_, kSpellcheck)); 399 EXPECT_TRUE(client_.IsAvailable(&profile_, kSpellcheck));
399 } 400 }
400 401
401 // This function returns true for suggestions for all and false for 402 // This function returns true for suggestions for all and false for
402 // spellcheck for unsupported locales. 403 // spellcheck for unsupported locales.
403 static const char* kUnsupported[] = { 404 static const char* kUnsupported[] = {
404 "af-ZA", "bg-BG", "ca-ES", "cs-CZ", "de-DE", "el-GR", "et-EE", "fo-FO", 405 "af-ZA", "bg-BG", "ca-ES", "cs-CZ", "de-DE", "el-GR", "et-EE", "fo-FO",
405 "fr-FR", "he-IL", "hi-IN", "hr-HR", "hu-HU", "id-ID", "it-IT", "lt-LT", 406 "fr-FR", "he-IL", "hi-IN", "hr-HR", "hu-HU", "id-ID", "it-IT", "lt-LT",
406 "lv-LV", "nb-NO", "nl-NL", "pl-PL", "pt-BR", "pt-PT", "ro-RO", "ru-RU", 407 "lv-LV", "nb-NO", "nl-NL", "pl-PL", "pt-BR", "pt-PT", "ro-RO", "ru-RU",
407 "sk-SK", "sl-SI", "sh", "sr", "sv-SE", "tr-TR", "uk-UA", "vi-VN", 408 "sk-SK", "sl-SI", "sh", "sr", "sv-SE", "tr-TR", "uk-UA", "vi-VN",
408 }; 409 };
409 for (size_t i = 0; i < arraysize(kUnsupported); ++i) { 410 for (size_t i = 0; i < arraysize(kUnsupported); ++i) {
410 SCOPED_TRACE(std::string("Expected language ") + kUnsupported[i]); 411 SCOPED_TRACE(std::string("Expected language ") + kUnsupported[i]);
411 base::ListValue dictionary; 412 base::ListValue dictionary;
412 dictionary.AppendString(kUnsupported[i]); 413 dictionary.AppendString(kUnsupported[i]);
413 pref->Set(prefs::kSpellCheckDictionaries, dictionary); 414 pref->Set(spellcheck::prefs::kSpellCheckDictionaries, dictionary);
414 415
415 EXPECT_TRUE(client_.IsAvailable(&profile_, kSuggest)); 416 EXPECT_TRUE(client_.IsAvailable(&profile_, kSuggest));
416 EXPECT_FALSE(client_.IsAvailable(&profile_, kSpellcheck)); 417 EXPECT_FALSE(client_.IsAvailable(&profile_, kSpellcheck));
417 } 418 }
418 } 419 }
419 420
420 // Verify that an error in JSON response from spelling service will result in 421 // Verify that an error in JSON response from spelling service will result in
421 // ParseResponse returning false. 422 // ParseResponse returning false.
422 TEST_F(SpellingServiceClientTest, ResponseErrorTest) { 423 TEST_F(SpellingServiceClientTest, ResponseErrorTest) {
423 EXPECT_TRUE(client_.ParseResponseSuccess("{\"result\": {}}")); 424 EXPECT_TRUE(client_.ParseResponseSuccess("{\"result\": {}}"));
424 EXPECT_FALSE(client_.ParseResponseSuccess("{\"error\": {}}")); 425 EXPECT_FALSE(client_.ParseResponseSuccess("{\"error\": {}}"));
425 } 426 }
OLDNEW
« no previous file with comments | « chrome/browser/spellchecker/spelling_service_client.cc ('k') | chrome/browser/spellchecker/word_trimmer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698