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. 4 5 #include "chrome/browser/chromeos/language_preferences.h" 6 7 #include <cstring> 8 #include <set> 9 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace chromeos { 13 namespace language_prefs { 14 15 namespace { 16 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; 22 } 23 template <> 24 bool Compare<const char*>(const char* a, const char* b) { 25 return !std::strcmp(a, b); 26 } 27 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; 41 } 42 if (Compare(pref.default_pref_value, 43 pref.values_and_ids[j].ibus_config_value)) { 44 default_value_is_valid = true; 45 } 46 } 47 if (!default_value_is_valid) { 48 return false; 49 } 50 } 51 return true; 52 } 53 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; 67 } 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; 72 } 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; 77 } 78 } 79 } 80 return true; 81 } 82 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; 90 } 91 if (pref.default_pref_value > pref.max_pref_value) { 92 return false; 93 } 94 } 95 return true; 96 } 97 98 } // namespace 99 100 // Checks |default_pref_value| in LanguageMultipleChoicePreference prefs. 101 TEST(LanguagePreferencesTest, TestDefaultValuesOfMultipleChoicePrefs) { 102 EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs( 103 kChewingMultipleChoicePrefs, kNumChewingMultipleChoicePrefs)); 104 EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs( 105 &kXkbModifierMultipleChoicePrefs, 1)); 106 EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs( 107 &kChewingHsuSelKeyType, 1)); 108 EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs( 109 &kPinyinDoublePinyinSchema, 1)); 110 EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs( 111 kMozcMultipleChoicePrefs, kNumMozcMultipleChoicePrefs)); 112 } 113 114 // Checks |ibus_config_value| and |item_message_id| duplications in 115 // LanguageMultipleChoicePreference prefs. 116 TEST(LanguagePreferencesTest, TestDuplicationOfMultipleChoicePrefs) { 117 EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs( 118 kChewingMultipleChoicePrefs, kNumChewingMultipleChoicePrefs)); 119 EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs( 120 &kXkbModifierMultipleChoicePrefs, 1)); 121 EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs( 122 &kChewingHsuSelKeyType, 1)); 123 EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs( 124 &kPinyinDoublePinyinSchema, 1)); 125 EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs( 126 kMozcMultipleChoicePrefs, kNumMozcMultipleChoicePrefs)); 127 } 128 129 // Checks |default_pref_value| in LanguageIntegerRangePreference prefs. 130 TEST(LanguagePreferencesTest, TestDefaultValuesOfIntegerRangePrefs) { 131 EXPECT_TRUE(CheckDefaultValueOfIntegerRangePrefs( 132 kChewingIntegerPrefs, kNumChewingIntegerPrefs)); 133 EXPECT_TRUE(CheckDefaultValueOfIntegerRangePrefs( 134 kMozcIntegerPrefs, kNumMozcIntegerPrefs)); 135 } 136 137 } // namespace language_prefs 138 } // namespace chromeos 139