Home | History | Annotate | Download | only in translate
      1 // Copyright 2013 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 "chrome/browser/translate/translate_prefs.h"
      6 
      7 #include <algorithm>
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "chrome/test/base/scoped_browser_locale.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 TEST(TranslatePrefsTest, CreateBlockedLanguages) {
     15   std::vector<std::string> blacklisted_languages;
     16   blacklisted_languages.push_back("en");
     17   blacklisted_languages.push_back("fr");
     18   // Hebrew: synonym to 'he'
     19   blacklisted_languages.push_back("iw");
     20   // Haitian is not used as Accept-Language
     21   blacklisted_languages.push_back("ht");
     22 
     23   std::vector<std::string> accept_languages;
     24   accept_languages.push_back("en");
     25   // The subcode (IT) will be ignored when merging, except for Chinese.
     26   accept_languages.push_back("it-IT");
     27   accept_languages.push_back("ja");
     28   // Filippino: synonym to 'tl'
     29   accept_languages.push_back("fil");
     30   // General Chinese is not used as Translate language, but not filtered
     31   // when merging.
     32   accept_languages.push_back("zh");
     33   // Chinese with a sub code is acceptable for the blocked-language list.
     34   accept_languages.push_back("zh-TW");
     35 
     36   std::vector<std::string> blocked_languages;
     37 
     38   TranslatePrefs::CreateBlockedLanguages(&blocked_languages,
     39                                          blacklisted_languages,
     40                                          accept_languages);
     41 
     42   // The order of the elements cannot be determined.
     43   std::vector<std::string> expected;
     44   expected.push_back("en");
     45   expected.push_back("fr");
     46   expected.push_back("iw");
     47   expected.push_back("ht");
     48   expected.push_back("it");
     49   expected.push_back("ja");
     50   expected.push_back("tl");
     51   expected.push_back("zh");
     52   expected.push_back("zh-TW");
     53 
     54   EXPECT_EQ(expected.size(), blocked_languages.size());
     55   for (std::vector<std::string>::const_iterator it = expected.begin();
     56        it != expected.end(); ++it) {
     57     EXPECT_NE(blocked_languages.end(),
     58               std::find(blocked_languages.begin(),
     59                         blocked_languages.end(),
     60                         *it));
     61   }
     62 }
     63 
     64 TEST(TranslatePrefsTest, CreateBlockedLanguagesNonEnglishUI) {
     65   std::vector<std::string> blacklisted_languages;
     66   blacklisted_languages.push_back("fr");
     67 
     68   std::vector<std::string> accept_languages;
     69   accept_languages.push_back("en");
     70   accept_languages.push_back("ja");
     71   accept_languages.push_back("zh");
     72 
     73   // Run in an English locale.
     74   {
     75     ScopedBrowserLocale scoped_browser_locale("en");
     76     std::vector<std::string> blocked_languages;
     77     TranslatePrefs::CreateBlockedLanguages(&blocked_languages,
     78                                            blacklisted_languages,
     79                                            accept_languages);
     80     std::vector<std::string> expected;
     81     expected.push_back("en");
     82     expected.push_back("fr");
     83     expected.push_back("ja");
     84     expected.push_back("zh");
     85 
     86     EXPECT_EQ(expected.size(), blocked_languages.size());
     87     for (std::vector<std::string>::const_iterator it = expected.begin();
     88          it != expected.end(); ++it) {
     89       EXPECT_NE(blocked_languages.end(),
     90                 std::find(blocked_languages.begin(),
     91                           blocked_languages.end(),
     92                           *it));
     93     }
     94   }
     95 
     96   // Run in a Japanese locale.
     97   // English should not be included in the result even though Accept Languages
     98   // has English because the UI is not English.
     99   {
    100     ScopedBrowserLocale scoped_browser_locale("ja");
    101     std::vector<std::string> blocked_languages;
    102     TranslatePrefs::CreateBlockedLanguages(&blocked_languages,
    103                                            blacklisted_languages,
    104                                            accept_languages);
    105     std::vector<std::string> expected;
    106     expected.push_back("fr");
    107     expected.push_back("ja");
    108     expected.push_back("zh");
    109 
    110     EXPECT_EQ(expected.size(), blocked_languages.size());
    111     for (std::vector<std::string>::const_iterator it = expected.begin();
    112          it != expected.end(); ++it) {
    113       EXPECT_NE(blocked_languages.end(),
    114                 std::find(blocked_languages.begin(),
    115                           blocked_languages.end(),
    116                           *it));
    117     }
    118   }
    119 }
    120