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. 4 5 #include "chrome/browser/language_usage_metrics.h" 6 7 #include "testing/gtest/include/gtest/gtest.h" 8 9 TEST(LanguageUsageMetricsTest, ParseAcceptLanguages) { 10 std::set<int> language_set; 11 std::set<int>::const_iterator it; 12 13 const int ENGLISH = 25966; 14 const int SPANISH = 25971; 15 const int JAPANESE = 27233; 16 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()); 21 22 // Empty language. 23 LanguageUsageMetrics::ParseAcceptLanguages(std::string(), &language_set); 24 EXPECT_EQ(0U, language_set.size()); 25 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()); 30 31 // Case is ignored. 32 LanguageUsageMetrics::ParseAcceptLanguages("Ja-jP", &language_set); 33 EXPECT_EQ(1U, language_set.size()); 34 EXPECT_EQ(JAPANESE, *language_set.begin()); 35 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()); 40 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()); 45 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); 52 53 // Multiple languages. 54 LanguageUsageMetrics::ParseAcceptLanguages("ja-JP,en,es,ja,en-US", 55 &language_set); 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); 61 62 // Two empty languages. 63 LanguageUsageMetrics::ParseAcceptLanguages(",", &language_set); 64 EXPECT_EQ(0U, language_set.size()); 65 66 // Trailing comma. 67 LanguageUsageMetrics::ParseAcceptLanguages("ja,", &language_set); 68 EXPECT_EQ(1U, language_set.size()); 69 EXPECT_EQ(JAPANESE, *language_set.begin()); 70 71 // Leading comma. 72 LanguageUsageMetrics::ParseAcceptLanguages(",es", &language_set); 73 EXPECT_EQ(1U, language_set.size()); 74 EXPECT_EQ(SPANISH, *language_set.begin()); 75 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); 81 } 82 83 TEST(LanguageUsageMetricsTest, ToLanguageCode) { 84 const int SPANISH = 25971; 85 const int JAPANESE = 27233; 86 87 // Basic case. 88 EXPECT_EQ(JAPANESE, LanguageUsageMetrics::ToLanguageCode("ja")); 89 90 // Case is ignored. 91 EXPECT_EQ(SPANISH, LanguageUsageMetrics::ToLanguageCode("Es")); 92 93 // Coutry code is ignored. 94 EXPECT_EQ(JAPANESE, LanguageUsageMetrics::ToLanguageCode("ja-JP")); 95 96 // Invalid locales are considered as unknown language. 97 EXPECT_EQ(0, LanguageUsageMetrics::ToLanguageCode(std::string())); 98 EXPECT_EQ(0, LanguageUsageMetrics::ToLanguageCode("1234")); 99 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")); 103 } 104