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"
10 #include "testing/gtest/include/gtest/gtest.h"
13 namespace language_prefs
{
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*>.
20 bool Compare(T a
, T b
) {
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
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) {
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
) {
54 // Returns false if |prefs| has duplicated |ibus_config_value| or
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) {
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.
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.
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
) {
91 if (pref
.default_pref_value
> pref
.max_pref_value
) {
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