Home | History | Annotate | Download | only in util
      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 <string>
      6 #include <vector>
      7 
      8 #include "chrome/installer/util/language_selector.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace {
     12 
     13 const wchar_t* const kExactMatchCandidates[] = {
     14   L"am", L"ar", L"bg", L"bn", L"ca", L"cs", L"da", L"de", L"el", L"en-gb",
     15   L"en-us", L"es", L"es-419", L"et", L"fa", L"fi", L"fil", L"fr", L"gu", L"hi",
     16   L"hr", L"hu", L"id", L"it", L"iw", L"ja", L"kn", L"ko", L"lt", L"lv", L"ml",
     17   L"mr", L"nl", L"no", L"pl", L"pt-br", L"pt-pt", L"ro", L"ru", L"sk", L"sl",
     18   L"sr", L"sv", L"sw", L"ta", L"te", L"th", L"tr", L"uk", L"vi", L"zh-cn",
     19   L"zh-tw"
     20 };
     21 
     22 const wchar_t* const kAliasMatchCandidates[] = {
     23   L"he", L"nb", L"tl", L"zh-chs",  L"zh-cht", L"zh-hans", L"zh-hant", L"zh-hk",
     24   L"zh-mo"
     25 };
     26 
     27 const wchar_t* const kWildcardMatchCandidates[] = {
     28   L"en-AU",
     29   L"es-CO", L"pt-AB", L"zh-SG"
     30 };
     31 
     32 }  // namespace
     33 
     34 // Test that a language is selected from the system.
     35 TEST(LanguageSelectorTest, DefaultSelection) {
     36   installer::LanguageSelector instance;
     37   EXPECT_FALSE(instance.matched_candidate().empty());
     38 }
     39 
     40 // Test some hypothetical candidate sets.
     41 TEST(LanguageSelectorTest, AssortedSelections) {
     42   {
     43     std::wstring candidates[] = {
     44       L"fr-BE", L"fr", L"en"
     45     };
     46     installer::LanguageSelector instance(
     47         std::vector<std::wstring>(&candidates[0],
     48                                   &candidates[arraysize(candidates)]));
     49     // Expect the exact match to win.
     50     EXPECT_EQ(L"fr", instance.matched_candidate());
     51   }
     52   {
     53     std::wstring candidates[] = {
     54       L"xx-YY", L"cc-Ssss-RR"
     55     };
     56     installer::LanguageSelector instance(
     57       std::vector<std::wstring>(&candidates[0],
     58       &candidates[arraysize(candidates)]));
     59     // Expect the fallback to win.
     60     EXPECT_EQ(L"en-us", instance.matched_candidate());
     61   }
     62   {
     63     std::wstring candidates[] = {
     64       L"zh-SG", L"en-GB"
     65     };
     66     installer::LanguageSelector instance(
     67       std::vector<std::wstring>(&candidates[0],
     68       &candidates[arraysize(candidates)]));
     69     // Expect the alias match to win.
     70     EXPECT_EQ(L"zh-SG", instance.matched_candidate());
     71   }
     72 }
     73 
     74 // A fixture for testing sets of single-candidate selections.
     75 class LanguageSelectorMatchCandidateTest
     76     : public ::testing::TestWithParam<const wchar_t*> {
     77 };
     78 
     79 TEST_P(LanguageSelectorMatchCandidateTest, TestMatchCandidate) {
     80   installer::LanguageSelector instance(
     81     std::vector<std::wstring>(1, std::wstring(GetParam())));
     82   EXPECT_EQ(GetParam(), instance.matched_candidate());
     83 }
     84 
     85 // Test that all existing translations can be found by exact match.
     86 INSTANTIATE_TEST_CASE_P(
     87     TestExactMatches,
     88     LanguageSelectorMatchCandidateTest,
     89     ::testing::ValuesIn(
     90         &kExactMatchCandidates[0],
     91         &kExactMatchCandidates[arraysize(kExactMatchCandidates)]));
     92 
     93 // Test the alias matches.
     94 INSTANTIATE_TEST_CASE_P(
     95     TestAliasMatches,
     96     LanguageSelectorMatchCandidateTest,
     97     ::testing::ValuesIn(
     98         &kAliasMatchCandidates[0],
     99         &kAliasMatchCandidates[arraysize(kAliasMatchCandidates)]));
    100 
    101 // Test a few wildcard matches.
    102 INSTANTIATE_TEST_CASE_P(
    103     TestWildcardMatches,
    104     LanguageSelectorMatchCandidateTest,
    105     ::testing::ValuesIn(
    106         &kWildcardMatchCandidates[0],
    107         &kWildcardMatchCandidates[arraysize(kWildcardMatchCandidates)]));
    108 
    109 // A fixture for testing aliases that match to an expected translation.  The
    110 // first member of the tuple is the expected translation, the second is a
    111 // candidate that should be aliased to the expectation.
    112 class LanguageSelectorAliasTest
    113     : public ::testing::TestWithParam< std::tr1::tuple<const wchar_t*,
    114                                                        const wchar_t*> > {
    115 };
    116 
    117 // Test that the candidate language maps to the aliased translation.
    118 TEST_P(LanguageSelectorAliasTest, AliasesMatch) {
    119   installer::LanguageSelector instance(
    120       std::vector<std::wstring>(1, std::tr1::get<1>(GetParam())));
    121   EXPECT_EQ(std::tr1::get<0>(GetParam()), instance.selected_translation());
    122 }
    123 
    124 INSTANTIATE_TEST_CASE_P(
    125     EnGbAliases,
    126     LanguageSelectorAliasTest,
    127     ::testing::Combine(
    128         ::testing::Values(L"en-gb"),
    129         ::testing::Values(L"en-au", L"en-ca", L"en-nz", L"en-za")));
    130 
    131 INSTANTIATE_TEST_CASE_P(
    132     IwAliases,
    133     LanguageSelectorAliasTest,
    134     ::testing::Combine(
    135         ::testing::Values(L"iw"),
    136         ::testing::Values(L"he")));
    137 
    138 INSTANTIATE_TEST_CASE_P(
    139     NoAliases,
    140     LanguageSelectorAliasTest,
    141     ::testing::Combine(
    142         ::testing::Values(L"no"),
    143         ::testing::Values(L"nb")));
    144 
    145 INSTANTIATE_TEST_CASE_P(
    146     FilAliases,
    147     LanguageSelectorAliasTest,
    148     ::testing::Combine(
    149         ::testing::Values(L"fil"),
    150         ::testing::Values(L"tl")));
    151 
    152 INSTANTIATE_TEST_CASE_P(
    153     ZhCnAliases,
    154     LanguageSelectorAliasTest,
    155     ::testing::Combine(
    156         ::testing::Values(L"zh-cn"),
    157         ::testing::Values(L"zh-chs", L"zh-hans", L"zh-sg")));
    158 
    159 INSTANTIATE_TEST_CASE_P(
    160     ZhTwAliases,
    161     LanguageSelectorAliasTest,
    162     ::testing::Combine(
    163         ::testing::Values(L"zh-tw"),
    164         ::testing::Values(L"zh-cht", L"zh-hant", L"zh-hk", L"zh-mo")));
    165 
    166 // Test that we can get the name of the default language.
    167 TEST(LanguageSelectorTest, DefaultLanguageName) {
    168   installer::LanguageSelector instance;
    169   EXPECT_FALSE(instance.GetLanguageName(instance.offset()).empty());
    170 }
    171 
    172