diff options
author | hbono@chromium.org <hbono@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-14 05:38:04 +0000 |
---|---|---|
committer | hbono@chromium.org <hbono@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-14 05:38:04 +0000 |
commit | 12cc36539c6bafab23be2399300beb8cbb17988a (patch) | |
tree | 81594c7e055359712f1e458e0efc1589309f21bf | |
parent | 891f89be969ed572740e76fbefa3df4ef57c7b36 (diff) | |
download | chromium_src-12cc36539c6bafab23be2399300beb8cbb17988a.zip chromium_src-12cc36539c6bafab23be2399300beb8cbb17988a.tar.gz chromium_src-12cc36539c6bafab23be2399300beb8cbb17988a.tar.bz2 |
Add SpellingServiceClient::IsAvailable().
This change adds a static function SpellingServiceClient::IsAvailable() and use it instead of checking conditions directly.
BUG=none
TEST=SpellingServiceClientTest.AvailableServices
Review URL: https://chromiumcodereview.appspot.com/10536141
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@142102 0039d316-1c4b-4281-b951-d872f2087c98
4 files changed, 77 insertions, 7 deletions
diff --git a/chrome/browser/spellchecker/spelling_service_client.cc b/chrome/browser/spellchecker/spelling_service_client.cc index 7889b66..e318a98 100644 --- a/chrome/browser/spellchecker/spelling_service_client.cc +++ b/chrome/browser/spellchecker/spelling_service_client.cc @@ -72,7 +72,7 @@ bool SpellingServiceClient::RequestTextCheck( uloc_getLanguage(id, language, arraysize(language), &error); country = uloc_getISO3Country(id); } - if (type == SPELLCHECK && base::strcasecmp(language, ULOC_ENGLISH)) + if (!IsAvailable(profile, SPELLCHECK)) return false; // Format the JSON request to be sent to the Spelling service. @@ -112,6 +112,22 @@ bool SpellingServiceClient::RequestTextCheck( return true; } +bool SpellingServiceClient::IsAvailable(Profile* profile, ServiceType type) { + const PrefService* pref = profile->GetPrefs(); + if (!pref->GetBoolean(prefs::kEnableSpellCheck) || + !pref->GetBoolean(prefs::kSpellCheckUseSpellingService)) + return false; + + // Enable the suggest service only on languages not supported by the + // spellcheck service. When this client calls the spellcheck service, it + // returns not only spellcheck results but also spelling suggestions provided + // by the suggest service. That is, it is not useful to use the suggest + // service when this client can use the spellcheck service. + std::string locale = pref->GetString(prefs::kSpellCheckDictionary); + bool spellcheck_available = locale.empty() || !locale.compare(0, 2, "en"); + return type == SUGGEST ? !spellcheck_available : spellcheck_available; +} + void SpellingServiceClient::OnURLFetchComplete( const net::URLFetcher* source) { scoped_ptr<net::URLFetcher> clean_up_fetcher(fetcher_.release()); diff --git a/chrome/browser/spellchecker/spelling_service_client.h b/chrome/browser/spellchecker/spelling_service_client.h index c531405..be9d33c 100644 --- a/chrome/browser/spellchecker/spelling_service_client.h +++ b/chrome/browser/spellchecker/spelling_service_client.h @@ -89,6 +89,9 @@ class SpellingServiceClient : public net::URLFetcherDelegate { const string16& text, const TextCheckCompleteCallback& callback); + // Returns whether the specified service is available for the given profile. + static bool IsAvailable(Profile* profile, ServiceType type); + private: // Creates a URLFetcher object used for sending a JSON-RPC request. This // function is overriden by unit tests to prevent them from actually sending diff --git a/chrome/browser/spellchecker/spelling_service_client_unittest.cc b/chrome/browser/spellchecker/spelling_service_client_unittest.cc index 7bb1b8a..85bd814 100644 --- a/chrome/browser/spellchecker/spelling_service_client_unittest.cc +++ b/chrome/browser/spellchecker/spelling_service_client_unittest.cc @@ -10,7 +10,9 @@ #include "base/stringprintf.h" #include "base/utf_string_conversions.h" #include "base/values.h" +#include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/spellchecker/spelling_service_client.h" +#include "chrome/common/pref_names.h" #include "chrome/common/spellcheck_result.h" #include "chrome/test/base/testing_profile.h" #include "content/public/test/test_url_fetcher_factory.h" @@ -258,6 +260,10 @@ TEST_F(SpellingServiceClientTest, RequestTextCheck) { }, }; + PrefService* pref = profile_.GetPrefs(); + pref->SetBoolean(prefs::kEnableSpellCheck, true); + pref->SetBoolean(prefs::kSpellCheckUseSpellingService, true); + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { client_.SetHTTPRequest(kTests[i].request_type, kTests[i].request_text); client_.SetHTTPResponse(kTests[i].response_status, kTests[i].response_data); @@ -273,3 +279,51 @@ TEST_F(SpellingServiceClientTest, RequestTextCheck) { client_.CallOnURLFetchComplete(); } } + +// Verify that SpellingServiceClient::IsAvailable() returns true only when it +// can send suggest requests or spellcheck requests. +TEST_F(SpellingServiceClientTest, AvailableServices) { + const SpellingServiceClient::ServiceType kSuggest = + SpellingServiceClient::SUGGEST; + const SpellingServiceClient::ServiceType kSpellcheck = + SpellingServiceClient::SPELLCHECK; + + // When a user disables spellchecking or prevent using the Spelling service, + // this function should return false both for suggestions and for spellcheck. + PrefService* pref = profile_.GetPrefs(); + pref->SetBoolean(prefs::kEnableSpellCheck, false); + pref->SetBoolean(prefs::kSpellCheckUseSpellingService, false); + EXPECT_FALSE(client_.IsAvailable(&profile_, kSuggest)); + EXPECT_FALSE(client_.IsAvailable(&profile_, kSpellcheck)); + + pref->SetBoolean(prefs::kEnableSpellCheck, true); + pref->SetBoolean(prefs::kSpellCheckUseSpellingService, true); + + // For locales supported by the SpellCheck service, this function returns + // false for suggestions and true for spellcheck. (The comment in + // SpellingServiceClient::IsAvailable() describes why this function returns + // false for suggestions.) + static const char* kSupported[] = { + "", "en-AU", "en-CA", "en-GB", "en-US", + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSupported); ++i) { + pref->SetString(prefs::kSpellCheckDictionary, kSupported[i]); + EXPECT_FALSE(client_.IsAvailable(&profile_, kSuggest)); + EXPECT_TRUE(client_.IsAvailable(&profile_, kSpellcheck)); + } + + // On the other hand, this function returns true for suggestions and false for + // spellcheck for unsupported locales. + static const char* kUnsupported[] = { + "af-ZA", "bg-BG", "ca-ES", "cs-CZ", "da-DK", "de-DE", "el-GR", "es-ES", + "et-EE", "fo-FO", "fr-FR", "he-IL", "hi-IN", "hr-HR", "hu-HU", "id-ID", + "it-IT", "lt-LT", "lv-LV", "nb-NO", "nl-NL", "pl-PL", "pt-BR", "pt-PT", + "ro-RO", "ru-RU", "sk-SK", "sl-SI", "sh", "sr", "sv-SE", "tr-TR", + "uk-UA", "vi-VN", + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kUnsupported); ++i) { + pref->SetString(prefs::kSpellCheckDictionary, kUnsupported[i]); + EXPECT_TRUE(client_.IsAvailable(&profile_, kSuggest)); + EXPECT_FALSE(client_.IsAvailable(&profile_, kSpellcheck)); + } +} diff --git a/chrome/browser/tab_contents/spelling_menu_observer.cc b/chrome/browser/tab_contents/spelling_menu_observer.cc index 29fe6e6..b7f1d44 100644 --- a/chrome/browser/tab_contents/spelling_menu_observer.cc +++ b/chrome/browser/tab_contents/spelling_menu_observer.cc @@ -59,12 +59,9 @@ void SpellingMenuObserver::InitMenu(const content::ContextMenuParams& params) { } // Append a placeholder item for the suggestion from the Spelling serivce and - // send a request to the service if we have enabled the spellchecking and - // integrated the Spelling service. - PrefService* pref = profile->GetPrefs(); - if (params.spellcheck_enabled && - pref->GetBoolean(prefs::kEnableSpellCheck) && - pref->GetBoolean(prefs::kSpellCheckUseSpellingService)) { + // send a request to the service if we can retrieve suggestions from it. + if (SpellingServiceClient::IsAvailable(profile, + SpellingServiceClient::SUGGEST)) { // Retrieve the misspelled word to be sent to the Spelling service. string16 text = params.misspelled_word; if (!text.empty()) { |