Home | History | Annotate | Download | only in spellchecker
      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.
      4 
      5 #include <vector>
      6 
      7 #include "base/files/scoped_temp_dir.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
     10 #include "chrome/browser/spellchecker/spellcheck_factory.h"
     11 #include "chrome/browser/spellchecker/spellcheck_service.h"
     12 #include "chrome/common/spellcheck_common.h"
     13 #include "chrome/test/base/testing_profile.h"
     14 #include "content/public/test/test_browser_thread_bundle.h"
     15 #include "testing/gmock/include/gmock/gmock.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 using chrome::spellcheck_common::WordList;
     19 
     20 static BrowserContextKeyedService* BuildSpellcheckService(
     21     content::BrowserContext* profile) {
     22   return new SpellcheckService(static_cast<Profile*>(profile));
     23 }
     24 
     25 class SpellcheckServiceTest : public testing::Test {
     26  protected:
     27   virtual void SetUp() OVERRIDE {
     28     // Use SetTestingFactoryAndUse to force creation and initialization.
     29     SpellcheckServiceFactory::GetInstance()->SetTestingFactoryAndUse(
     30         &profile_, &BuildSpellcheckService);
     31   }
     32 
     33  private:
     34   content::TestBrowserThreadBundle thread_bundle_;
     35   TestingProfile profile_;
     36 };
     37 
     38 TEST_F(SpellcheckServiceTest, GetSpellCheckLanguages1) {
     39   std::vector<std::string> accept_languages;
     40   accept_languages.push_back("en");
     41   accept_languages.push_back("en-US");
     42   std::vector<std::string> languages;
     43 
     44   SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages(
     45       accept_languages, "en-US", &languages);
     46 
     47   EXPECT_EQ(1U, languages.size());
     48   EXPECT_EQ("en-US", languages[0]);
     49 }
     50 
     51 TEST_F(SpellcheckServiceTest, GetSpellCheckLanguages2) {
     52   std::vector<std::string> accept_languages;
     53   accept_languages.push_back("en-US");
     54   accept_languages.push_back("en");
     55   std::vector<std::string> languages;
     56 
     57   SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages(
     58       accept_languages, "en-US", &languages);
     59 
     60   EXPECT_EQ(1U, languages.size());
     61   EXPECT_EQ("en-US", languages[0]);
     62 }
     63 
     64 TEST_F(SpellcheckServiceTest, GetSpellCheckLanguages3) {
     65   std::vector<std::string> accept_languages;
     66   accept_languages.push_back("en");
     67   accept_languages.push_back("en-US");
     68   accept_languages.push_back("en-AU");
     69   std::vector<std::string> languages;
     70 
     71   SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages(
     72       accept_languages, "en-US", &languages);
     73 
     74   EXPECT_EQ(2U, languages.size());
     75 
     76   std::sort(languages.begin(), languages.end());
     77   EXPECT_EQ("en-AU", languages[0]);
     78   EXPECT_EQ("en-US", languages[1]);
     79 }
     80 
     81 TEST_F(SpellcheckServiceTest, GetSpellCheckLanguages4) {
     82   std::vector<std::string> accept_languages;
     83   accept_languages.push_back("en");
     84   accept_languages.push_back("en-US");
     85   accept_languages.push_back("fr");
     86   std::vector<std::string> languages;
     87 
     88   SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages(
     89       accept_languages, "en-US", &languages);
     90 
     91   EXPECT_EQ(2U, languages.size());
     92 
     93   std::sort(languages.begin(), languages.end());
     94   EXPECT_EQ("en-US", languages[0]);
     95   EXPECT_EQ("fr", languages[1]);
     96 }
     97 
     98 TEST_F(SpellcheckServiceTest, GetSpellCheckLanguages5) {
     99   std::vector<std::string> accept_languages;
    100   accept_languages.push_back("en");
    101   accept_languages.push_back("en-JP");  // Will not exist.
    102   accept_languages.push_back("fr");
    103   accept_languages.push_back("aa");  // Will not exist.
    104   std::vector<std::string> languages;
    105 
    106   SpellcheckService::GetSpellCheckLanguagesFromAcceptLanguages(
    107       accept_languages, "fr", &languages);
    108 
    109   EXPECT_EQ(1U, languages.size());
    110   EXPECT_EQ("fr", languages[0]);
    111 }
    112