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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
// Copyright (c) 2010 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/chromeos/language_preferences.h"
#include <cstring>
#include <set>
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace language_prefs {
namespace {
// Compares |a| and |b| and returns true if a is equal to b. The second one is a
// specialized function for LanguageMultipleChoicePreference<const char*>.
template <typename T>
bool Compare(T a, T b) {
return a == b;
}
template <>
bool Compare<const char*>(const char* a, const char* b) {
return !std::strcmp(a, b);
}
// Returns false if one or more prefs have a bad |default_pref_value|.
// C++ template is used here since LanguageMultipleChoicePreference is a class
// template.
template <typename T>
bool CheckDefaultValueOfMultipleChoicePrefs(
const LanguageMultipleChoicePreference<T>* prefs, size_t prefs_len) {
const size_t kMaxItems = LanguageMultipleChoicePreference<T>::kMaxItems;
for (size_t i = 0; i < prefs_len; ++i) {
bool default_value_is_valid = false;
for (size_t j = 0; j < kMaxItems; ++j) {
const LanguageMultipleChoicePreference<T>& pref = prefs[i];
if (pref.values_and_ids[j].item_message_id == 0) {
break;
}
if (Compare(pref.default_pref_value,
pref.values_and_ids[j].ibus_config_value)) {
default_value_is_valid = true;
}
}
if (!default_value_is_valid) {
return false;
}
}
return true;
}
// Returns false if |prefs| has duplicated |ibus_config_value| or
// |item_message_id|.
template <typename T>
bool CheckDuplicationOfMultipleChoicePrefs(
const LanguageMultipleChoicePreference<T>* prefs, size_t prefs_len) {
const size_t kMaxItems = LanguageMultipleChoicePreference<T>::kMaxItems;
for (size_t i = 0; i < prefs_len; ++i) {
std::set<T> ibus_config_value_set;
std::set<int> item_message_id_set;
for (size_t j = 0; j < kMaxItems; ++j) {
const LanguageMultipleChoicePreference<T>& pref = prefs[i];
if (pref.values_and_ids[j].item_message_id == 0) {
break;
}
const T ibus_config_value = pref.values_and_ids[j].ibus_config_value;
if (!ibus_config_value_set.insert(ibus_config_value).second) {
// |ibus_config_value| is already in the set.
return false;
}
const int item_message_id = pref.values_and_ids[j].item_message_id;
if (!item_message_id_set.insert(item_message_id).second) {
// |item_message_id| is already in the set.
return false;
}
}
}
return true;
}
// Returns false if one or more prefs have an out-of-range |default_pref_value|.
bool CheckDefaultValueOfIntegerRangePrefs(
const LanguageIntegerRangePreference* prefs, size_t prefs_len) {
for (size_t i = 0; i < prefs_len; ++i) {
const LanguageIntegerRangePreference& pref = prefs[i];
if (pref.default_pref_value < pref.min_pref_value) {
return false;
}
if (pref.default_pref_value > pref.max_pref_value) {
return false;
}
}
return true;
}
} // namespace
// Checks |default_pref_value| in LanguageMultipleChoicePreference prefs.
TEST(LanguagePreferencesTest, TestDefaultValuesOfMultipleChoicePrefs) {
EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs(
kChewingMultipleChoicePrefs, kNumChewingMultipleChoicePrefs));
EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs(
&kChewingHsuSelKeyType, 1));
EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs(
&kPinyinDoublePinyinSchema, 1));
EXPECT_TRUE(CheckDefaultValueOfMultipleChoicePrefs(
kMozcMultipleChoicePrefs, kNumMozcMultipleChoicePrefs));
}
// Checks |ibus_config_value| and |item_message_id| duplications in
// LanguageMultipleChoicePreference prefs.
TEST(LanguagePreferencesTest, TestDuplicationOfMultipleChoicePrefs) {
EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs(
kChewingMultipleChoicePrefs, kNumChewingMultipleChoicePrefs));
EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs(
&kChewingHsuSelKeyType, 1));
EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs(
&kPinyinDoublePinyinSchema, 1));
EXPECT_TRUE(CheckDuplicationOfMultipleChoicePrefs(
kMozcMultipleChoicePrefs, kNumMozcMultipleChoicePrefs));
}
// Checks |default_pref_value| in LanguageIntegerRangePreference prefs.
TEST(LanguagePreferencesTest, TestDefaultValuesOfIntegerRangePrefs) {
EXPECT_TRUE(CheckDefaultValueOfIntegerRangePrefs(
kChewingIntegerPrefs, kNumChewingIntegerPrefs));
EXPECT_TRUE(CheckDefaultValueOfIntegerRangePrefs(
kMozcIntegerPrefs, kNumMozcIntegerPrefs));
}
} // namespace language_prefs
} // namespace chromeos
|