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

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

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

Powered by Google App Engine
This is Rietveld 408576698