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
<int> language_set
;
11 std::set
<int>::const_iterator it
;
13 const int ENGLISH
= 25966;
14 const int SPANISH
= 25971;
15 const int JAPANESE
= 27233;
17 // Basic single language case.
18 LanguageUsageMetrics::ParseAcceptLanguages("ja", &language_set
);
19 EXPECT_EQ(1U, language_set
.size());
20 EXPECT_EQ(JAPANESE
, *language_set
.begin());
23 LanguageUsageMetrics::ParseAcceptLanguages(std::string(), &language_set
);
24 EXPECT_EQ(0U, language_set
.size());
26 // Country code is ignored.
27 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP", &language_set
);
28 EXPECT_EQ(1U, language_set
.size());
29 EXPECT_EQ(JAPANESE
, *language_set
.begin());
32 LanguageUsageMetrics::ParseAcceptLanguages("Ja-jP", &language_set
);
33 EXPECT_EQ(1U, language_set
.size());
34 EXPECT_EQ(JAPANESE
, *language_set
.begin());
36 // Underscore as the separator.
37 LanguageUsageMetrics::ParseAcceptLanguages("ja_JP", &language_set
);
38 EXPECT_EQ(1U, language_set
.size());
39 EXPECT_EQ(JAPANESE
, *language_set
.begin());
41 // The result contains a same language code only once.
42 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP,ja", &language_set
);
43 EXPECT_EQ(1U, language_set
.size());
44 EXPECT_EQ(JAPANESE
, *language_set
.begin());
46 // Basic two languages case.
47 LanguageUsageMetrics::ParseAcceptLanguages("en,ja", &language_set
);
48 EXPECT_EQ(2U, language_set
.size());
49 it
= language_set
.begin();
50 EXPECT_EQ(ENGLISH
, *it
);
51 EXPECT_EQ(JAPANESE
, *++it
);
53 // Multiple languages.
54 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP,en,es,ja,en-US",
56 EXPECT_EQ(3U, language_set
.size());
57 it
= language_set
.begin();
58 EXPECT_EQ(ENGLISH
, *it
);
59 EXPECT_EQ(SPANISH
, *++it
);
60 EXPECT_EQ(JAPANESE
, *++it
);
62 // Two empty languages.
63 LanguageUsageMetrics::ParseAcceptLanguages(",", &language_set
);
64 EXPECT_EQ(0U, language_set
.size());
67 LanguageUsageMetrics::ParseAcceptLanguages("ja,", &language_set
);
68 EXPECT_EQ(1U, language_set
.size());
69 EXPECT_EQ(JAPANESE
, *language_set
.begin());
72 LanguageUsageMetrics::ParseAcceptLanguages(",es", &language_set
);
73 EXPECT_EQ(1U, language_set
.size());
74 EXPECT_EQ(SPANISH
, *language_set
.begin());
76 // Combination of invalid and valid.
77 LanguageUsageMetrics::ParseAcceptLanguages("1234,en", &language_set
);
78 EXPECT_EQ(1U, language_set
.size());
79 it
= language_set
.begin();
80 EXPECT_EQ(ENGLISH
, *it
);
83 TEST(LanguageUsageMetricsTest
, ToLanguageCode
) {
84 const int SPANISH
= 25971;
85 const int JAPANESE
= 27233;
88 EXPECT_EQ(JAPANESE
, LanguageUsageMetrics::ToLanguageCode("ja"));
91 EXPECT_EQ(SPANISH
, LanguageUsageMetrics::ToLanguageCode("Es"));
93 // Coutry code is ignored.
94 EXPECT_EQ(JAPANESE
, LanguageUsageMetrics::ToLanguageCode("ja-JP"));
96 // Invalid locales are considered as unknown language.
97 EXPECT_EQ(0, LanguageUsageMetrics::ToLanguageCode(std::string()));
98 EXPECT_EQ(0, LanguageUsageMetrics::ToLanguageCode("1234"));
100 // "xx" is not acceptable because it doesn't exist in ISO 639-1 table.
101 // However, LanguageUsageMetrics doesn't tell what code is valid.
102 EXPECT_EQ(30840, LanguageUsageMetrics::ToLanguageCode("xx"));