1 // Copyright 2014 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 "components/language_usage_metrics/language_usage_metrics.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace language_usage_metrics
{
11 TEST(LanguageUsageMetricsTest
, ParseAcceptLanguages
) {
12 std::set
<int> language_set
;
13 std::set
<int>::const_iterator it
;
15 const int ENGLISH
= 25966;
16 const int SPANISH
= 25971;
17 const int JAPANESE
= 27233;
19 // Basic single language case.
20 LanguageUsageMetrics::ParseAcceptLanguages("ja", &language_set
);
21 EXPECT_EQ(1U, language_set
.size());
22 EXPECT_EQ(JAPANESE
, *language_set
.begin());
25 LanguageUsageMetrics::ParseAcceptLanguages(std::string(), &language_set
);
26 EXPECT_EQ(0U, language_set
.size());
28 // Country code is ignored.
29 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP", &language_set
);
30 EXPECT_EQ(1U, language_set
.size());
31 EXPECT_EQ(JAPANESE
, *language_set
.begin());
34 LanguageUsageMetrics::ParseAcceptLanguages("Ja-jP", &language_set
);
35 EXPECT_EQ(1U, language_set
.size());
36 EXPECT_EQ(JAPANESE
, *language_set
.begin());
38 // Underscore as the separator.
39 LanguageUsageMetrics::ParseAcceptLanguages("ja_JP", &language_set
);
40 EXPECT_EQ(1U, language_set
.size());
41 EXPECT_EQ(JAPANESE
, *language_set
.begin());
43 // The result contains a same language code only once.
44 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP,ja", &language_set
);
45 EXPECT_EQ(1U, language_set
.size());
46 EXPECT_EQ(JAPANESE
, *language_set
.begin());
48 // Basic two languages case.
49 LanguageUsageMetrics::ParseAcceptLanguages("en,ja", &language_set
);
50 EXPECT_EQ(2U, language_set
.size());
51 it
= language_set
.begin();
52 EXPECT_EQ(ENGLISH
, *it
);
53 EXPECT_EQ(JAPANESE
, *++it
);
55 // Multiple languages.
56 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP,en,es,ja,en-US",
58 EXPECT_EQ(3U, language_set
.size());
59 it
= language_set
.begin();
60 EXPECT_EQ(ENGLISH
, *it
);
61 EXPECT_EQ(SPANISH
, *++it
);
62 EXPECT_EQ(JAPANESE
, *++it
);
64 // Two empty languages.
65 LanguageUsageMetrics::ParseAcceptLanguages(",", &language_set
);
66 EXPECT_EQ(0U, language_set
.size());
69 LanguageUsageMetrics::ParseAcceptLanguages("ja,", &language_set
);
70 EXPECT_EQ(1U, language_set
.size());
71 EXPECT_EQ(JAPANESE
, *language_set
.begin());
74 LanguageUsageMetrics::ParseAcceptLanguages(",es", &language_set
);
75 EXPECT_EQ(1U, language_set
.size());
76 EXPECT_EQ(SPANISH
, *language_set
.begin());
78 // Combination of invalid and valid.
79 LanguageUsageMetrics::ParseAcceptLanguages("1234,en", &language_set
);
80 EXPECT_EQ(1U, language_set
.size());
81 it
= language_set
.begin();
82 EXPECT_EQ(ENGLISH
, *it
);
85 TEST(LanguageUsageMetricsTest
, ToLanguageCode
) {
86 const int SPANISH
= 25971;
87 const int JAPANESE
= 27233;
90 EXPECT_EQ(JAPANESE
, LanguageUsageMetrics::ToLanguageCode("ja"));
93 EXPECT_EQ(SPANISH
, LanguageUsageMetrics::ToLanguageCode("Es"));
95 // Coutry code is ignored.
96 EXPECT_EQ(JAPANESE
, LanguageUsageMetrics::ToLanguageCode("ja-JP"));
98 // Invalid locales are considered as unknown language.
99 EXPECT_EQ(0, LanguageUsageMetrics::ToLanguageCode(std::string()));
100 EXPECT_EQ(0, LanguageUsageMetrics::ToLanguageCode("1234"));
102 // "xx" is not acceptable because it doesn't exist in ISO 639-1 table.
103 // However, LanguageUsageMetrics doesn't tell what code is valid.
104 EXPECT_EQ(30840, LanguageUsageMetrics::ToLanguageCode("xx"));
107 } // namespace language_usage_metrics