Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / chromeos / language_preferences_unittest.cc
blob36862b37368d09069a13c0b10ea5cf5b8ef1d759
1 // Copyright (c) 2010 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/chromeos/language_preferences.h"
7 #include <cstring>
8 #include <set>
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace chromeos {
13 namespace language_prefs {
15 namespace {
17 // Compares |a| and |b| and returns true if a is equal to b. The second one is a
18 // specialized function for LanguageMultipleChoicePreference<const char*>.
19 template <typename T>
20 bool Compare(T a, T b) {
21 return a == b;
23 template <>
24 bool Compare<const char*>(const char* a, const char* b) {
25 return !std::strcmp(a, b);
28 // Returns false if one or more prefs have a bad |default_pref_value|.
29 // C++ template is used here since LanguageMultipleChoicePreference is a class
30 // template.
31 template <typename T>
32 bool CheckDefaultValueOfMultipleChoicePrefs(
33 const LanguageMultipleChoicePreference<T>* prefs, size_t prefs_len) {
34 const size_t kMaxItems = LanguageMultipleChoicePreference<T>::kMaxItems;
35 for (size_t i = 0; i < prefs_len; ++i) {
36 bool default_value_is_valid = false;
37 for (size_t j = 0; j < kMaxItems; ++j) {
38 const LanguageMultipleChoicePreference<T>& pref = prefs[i];
39 if (pref.values_and_ids[j].item_message_id == 0) {
40 break;
42 if (Compare(pref.default_pref_value,
43 pref.values_and_ids[j].ibus_config_value)) {
44 default_value_is_valid = true;
47 if (!default_value_is_valid) {
48 return false;
51 return true;
54 // Returns false if |prefs| has duplicated |ibus_config_value| or
55 // |item_message_id|.
56 template <typename T>
57 bool CheckDuplicationOfMultipleChoicePrefs(
58 const LanguageMultipleChoicePreference<T>* prefs, size_t prefs_len) {
59 const size_t kMaxItems = LanguageMultipleChoicePreference<T>::kMaxItems;
60 for (size_t i = 0; i < prefs_len; ++i) {
61 std::set<T> ibus_config_value_set;
62 std::set<int> item_message_id_set;
63 for (size_t j = 0; j < kMaxItems; ++j) {
64 const LanguageMultipleChoicePreference<T>& pref = prefs[i];
65 if (pref.values_and_ids[j].item_message_id == 0) {
66 break;
68 const T ibus_config_value = pref.values_and_ids[j].ibus_config_value;
69 if (!ibus_config_value_set.insert(ibus_config_value).second) {
70 // |ibus_config_value| is already in the set.
71 return false;
73 const int item_message_id = pref.values_and_ids[j].item_message_id;
74 if (!item_message_id_set.insert(item_message_id).second) {
75 // |item_message_id| is already in the set.
76 return false;
80 return true;
83 // Returns false if one or more prefs have an out-of-range |default_pref_value|.
84 bool CheckDefaultValueOfIntegerRangePrefs(
85 const LanguageIntegerRangePreference* prefs, size_t prefs_len) {
86 for (size_t i = 0; i < prefs_len; ++i) {
87 const LanguageIntegerRangePreference& pref = prefs[i];
88 if (pref.default_pref_value < pref.min_pref_value) {
89 return false;
91 if (pref.default_pref_value > pref.max_pref_value) {
92 return false;
95 return true;
98 } // namespace
100 // Checks |default_pref_value| in LanguageMultipleChoicePreference prefs.
101 TEST(LanguagePreferencesTest, TestDefaultValuesOfMultipleChoicePrefs) {
102 EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs(
103 kChewingMultipleChoicePrefs, kNumChewingMultipleChoicePrefs));
104 EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs(
105 &kChewingHsuSelKeyType, 1));
106 EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs(
107 &kPinyinDoublePinyinSchema, 1));
108 EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs(
109 kMozcMultipleChoicePrefs, kNumMozcMultipleChoicePrefs));
112 // Checks |ibus_config_value| and |item_message_id| duplications in
113 // LanguageMultipleChoicePreference prefs.
114 TEST(LanguagePreferencesTest, TestDuplicationOfMultipleChoicePrefs) {
115 EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs(
116 kChewingMultipleChoicePrefs, kNumChewingMultipleChoicePrefs));
117 EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs(
118 &kChewingHsuSelKeyType, 1));
119 EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs(
120 &kPinyinDoublePinyinSchema, 1));
121 EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs(
122 kMozcMultipleChoicePrefs, kNumMozcMultipleChoicePrefs));
125 // Checks |default_pref_value| in LanguageIntegerRangePreference prefs.
126 TEST(LanguagePreferencesTest, TestDefaultValuesOfIntegerRangePrefs) {
127 EXPECT_TRUE(CheckDefaultValueOfIntegerRangePrefs(
128 kChewingIntegerPrefs, kNumChewingIntegerPrefs));
129 EXPECT_TRUE(CheckDefaultValueOfIntegerRangePrefs(
130 kMozcIntegerPrefs, kNumMozcIntegerPrefs));
133 } // namespace language_prefs
134 } // namespace chromeos