1 // Copyright (c) 2011 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.
5 #include "chrome/browser/language_usage_metrics.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 TEST(LanguageUsageMetricsTest
, ParseAcceptLanguages
) {
10 std::set
<Language
> language_set
;
11 std::set
<Language
>::const_iterator it
;
13 // Basic single language case.
14 LanguageUsageMetrics::ParseAcceptLanguages("ja", &language_set
);
15 EXPECT_EQ(1U, language_set
.size());
16 EXPECT_EQ(JAPANESE
, *language_set
.begin());
19 LanguageUsageMetrics::ParseAcceptLanguages(std::string(), &language_set
);
20 EXPECT_EQ(0U, language_set
.size());
22 // Country code is ignored.
23 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP", &language_set
);
24 EXPECT_EQ(1U, language_set
.size());
25 EXPECT_EQ(JAPANESE
, *language_set
.begin());
28 LanguageUsageMetrics::ParseAcceptLanguages("Ja-jP", &language_set
);
29 EXPECT_EQ(1U, language_set
.size());
30 EXPECT_EQ(JAPANESE
, *language_set
.begin());
32 // Underscore as the separator.
33 LanguageUsageMetrics::ParseAcceptLanguages("ja_JP", &language_set
);
34 EXPECT_EQ(1U, language_set
.size());
35 EXPECT_EQ(JAPANESE
, *language_set
.begin());
37 // The result contains a same language code only once.
38 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP,ja", &language_set
);
39 EXPECT_EQ(1U, language_set
.size());
40 EXPECT_EQ(JAPANESE
, *language_set
.begin());
42 // Basic two languages case.
43 LanguageUsageMetrics::ParseAcceptLanguages("en,ja", &language_set
);
44 EXPECT_EQ(2U, language_set
.size());
45 it
= language_set
.begin();
46 EXPECT_EQ(ENGLISH
, *it
);
47 EXPECT_EQ(JAPANESE
, *++it
);
49 // Multiple languages.
50 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP,en,es,ja,en-US",
52 EXPECT_EQ(3U, language_set
.size());
53 it
= language_set
.begin();
54 EXPECT_EQ(ENGLISH
, *it
);
55 EXPECT_EQ(JAPANESE
, *++it
);
56 EXPECT_EQ(SPANISH
, *++it
);
58 // Two empty languages.
59 LanguageUsageMetrics::ParseAcceptLanguages(",", &language_set
);
60 EXPECT_EQ(0U, language_set
.size());
63 LanguageUsageMetrics::ParseAcceptLanguages("ja,", &language_set
);
64 EXPECT_EQ(1U, language_set
.size());
65 EXPECT_EQ(JAPANESE
, *language_set
.begin());
68 LanguageUsageMetrics::ParseAcceptLanguages(",es", &language_set
);
69 EXPECT_EQ(1U, language_set
.size());
70 EXPECT_EQ(SPANISH
, *language_set
.begin());
72 // Combination of invalid and valid.
73 LanguageUsageMetrics::ParseAcceptLanguages("zz,en", &language_set
);
74 EXPECT_EQ(1U, language_set
.size());
75 EXPECT_EQ(ENGLISH
, *language_set
.begin());
78 TEST(LanguageUsageMetricsTest
, ToLanguage
) {
80 EXPECT_EQ(JAPANESE
, LanguageUsageMetrics::ToLanguage("ja"));
83 EXPECT_EQ(SPANISH
, LanguageUsageMetrics::ToLanguage("Es"));
85 // Coutry code is ignored.
86 EXPECT_EQ(JAPANESE
, LanguageUsageMetrics::ToLanguage("ja-JP"));
88 // Invalid locales are considered as unknown language.
89 EXPECT_EQ(UNKNOWN_LANGUAGE
, LanguageUsageMetrics::ToLanguage(std::string()));
90 EXPECT_EQ(UNKNOWN_LANGUAGE
, LanguageUsageMetrics::ToLanguage("xx"));