summaryrefslogtreecommitdiffstats
path: root/chrome/browser/language_usage_metrics_unittest.cc
blob: db6e987e919bee604e333293f55624e466cd7a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/language_usage_metrics.h"

#include "testing/gtest/include/gtest/gtest.h"

TEST(LanguageUsageMetricsTest, ParseAcceptLanguages) {
  std::set<Language> language_set;
  std::set<Language>::const_iterator it;

  // Basic single language case.
  LanguageUsageMetrics::ParseAcceptLanguages("ja", &language_set);
  EXPECT_EQ(1U, language_set.size());
  EXPECT_EQ(JAPANESE, *language_set.begin());

  // Empty language.
  LanguageUsageMetrics::ParseAcceptLanguages("", &language_set);
  EXPECT_EQ(0U, language_set.size());

  // Country code is ignored.
  LanguageUsageMetrics::ParseAcceptLanguages("ja-JP", &language_set);
  EXPECT_EQ(1U, language_set.size());
  EXPECT_EQ(JAPANESE, *language_set.begin());

  // Case is ignored.
  LanguageUsageMetrics::ParseAcceptLanguages("Ja-jP", &language_set);
  EXPECT_EQ(1U, language_set.size());
  EXPECT_EQ(JAPANESE, *language_set.begin());

  // Underscore as the separator.
  LanguageUsageMetrics::ParseAcceptLanguages("ja_JP", &language_set);
  EXPECT_EQ(1U, language_set.size());
  EXPECT_EQ(JAPANESE, *language_set.begin());

  // The result contains a same language code only once.
  LanguageUsageMetrics::ParseAcceptLanguages("ja-JP,ja", &language_set);
  EXPECT_EQ(1U, language_set.size());
  EXPECT_EQ(JAPANESE, *language_set.begin());

  // Basic two languages case.
  LanguageUsageMetrics::ParseAcceptLanguages("en,ja", &language_set);
  EXPECT_EQ(2U, language_set.size());
  it = language_set.begin();
  EXPECT_EQ(ENGLISH, *it);
  EXPECT_EQ(JAPANESE, *++it);

  // Multiple languages.
  LanguageUsageMetrics::ParseAcceptLanguages("ja-JP,en,es,ja,en-US",
                                             &language_set);
  EXPECT_EQ(3U, language_set.size());
  it = language_set.begin();
  EXPECT_EQ(ENGLISH, *it);
  EXPECT_EQ(JAPANESE, *++it);
  EXPECT_EQ(SPANISH, *++it);

  // Two empty languages.
  LanguageUsageMetrics::ParseAcceptLanguages(",", &language_set);
  EXPECT_EQ(0U, language_set.size());

  // Trailing comma.
  LanguageUsageMetrics::ParseAcceptLanguages("ja,", &language_set);
  EXPECT_EQ(1U, language_set.size());
  EXPECT_EQ(JAPANESE, *language_set.begin());

  // Leading comma.
  LanguageUsageMetrics::ParseAcceptLanguages(",es", &language_set);
  EXPECT_EQ(1U, language_set.size());
  EXPECT_EQ(SPANISH, *language_set.begin());

  // Combination of invalid and valid.
  LanguageUsageMetrics::ParseAcceptLanguages("zz,en", &language_set);
  EXPECT_EQ(1U, language_set.size());
  EXPECT_EQ(ENGLISH, *language_set.begin());
}

TEST(LanguageUsageMetricsTest, ToLanguage) {
  // Basic case.
  EXPECT_EQ(JAPANESE, LanguageUsageMetrics::ToLanguage("ja"));

  // Case is ignored.
  EXPECT_EQ(SPANISH, LanguageUsageMetrics::ToLanguage("Es"));

  // Coutry code is ignored.
  EXPECT_EQ(JAPANESE, LanguageUsageMetrics::ToLanguage("ja-JP"));

  // Invalid locales are considered as unknown language.
  EXPECT_EQ(UNKNOWN_LANGUAGE, LanguageUsageMetrics::ToLanguage(""));
  EXPECT_EQ(UNKNOWN_LANGUAGE, LanguageUsageMetrics::ToLanguage("xx"));
}