Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / language_usage_metrics / language_usage_metrics_unittest.cc
blob74344eb329453f3e091c22abd2e8f7281b76635b
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());
24 // Empty language.
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());
33 // Case is ignored.
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",
57 &language_set);
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());
68 // Trailing comma.
69 LanguageUsageMetrics::ParseAcceptLanguages("ja,", &language_set);
70 EXPECT_EQ(1U, language_set.size());
71 EXPECT_EQ(JAPANESE, *language_set.begin());
73 // Leading comma.
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;
89 // Basic case.
90 EXPECT_EQ(JAPANESE, LanguageUsageMetrics::ToLanguageCode("ja"));
92 // Case is ignored.
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