OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/extensions/extension_message_bundle.h" | 5 #include "chrome/common/extensions/extension_message_bundle.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
| 8 #include <vector> |
8 | 9 |
| 10 #include "base/linked_ptr.h" |
9 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
10 #include "base/string_util.h" | 12 #include "base/string_util.h" |
11 #include "base/values.h" | 13 #include "base/values.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 // Helper method for dictionary building. | 18 // Helper method for dictionary building. |
17 void SetDictionary(const std::wstring name, | 19 void SetDictionary(const std::wstring name, |
18 DictionaryValue* target, | 20 DictionaryValue* target, |
(...skipping 23 matching lines...) Expand all Loading... |
42 const std::string& message, | 44 const std::string& message, |
43 bool create_placeholder_subtree, | 45 bool create_placeholder_subtree, |
44 DictionaryValue* dict) { | 46 DictionaryValue* dict) { |
45 DictionaryValue* message_tree = new DictionaryValue; | 47 DictionaryValue* message_tree = new DictionaryValue; |
46 if (create_placeholder_subtree) | 48 if (create_placeholder_subtree) |
47 CreatePlaceholdersTree(message_tree); | 49 CreatePlaceholdersTree(message_tree); |
48 message_tree->SetString(ExtensionMessageBundle::kMessageKey, message); | 50 message_tree->SetString(ExtensionMessageBundle::kMessageKey, message); |
49 SetDictionary(name, dict, message_tree); | 51 SetDictionary(name, dict, message_tree); |
50 } | 52 } |
51 | 53 |
52 void CreateGoodDictionary(DictionaryValue* dict) { | 54 // Caller owns the memory. |
53 dict->Clear(); | 55 DictionaryValue* CreateGoodDictionary() { |
| 56 DictionaryValue* dict = new DictionaryValue; |
54 CreateMessageTree(L"n1", "message1 $a$ $b$", true, dict); | 57 CreateMessageTree(L"n1", "message1 $a$ $b$", true, dict); |
55 CreateMessageTree(L"n2", "message2 $c$", true, dict); | 58 CreateMessageTree(L"n2", "message2 $c$", true, dict); |
56 CreateMessageTree(L"n3", "message3", false, dict); | 59 CreateMessageTree(L"n3", "message3", false, dict); |
| 60 return dict; |
57 } | 61 } |
58 | 62 |
59 enum BadDictionary { | 63 enum BadDictionary { |
60 INVALID_NAME, | 64 INVALID_NAME, |
61 NAME_NOT_A_TREE, | 65 NAME_NOT_A_TREE, |
62 EMPTY_NAME_TREE, | 66 EMPTY_NAME_TREE, |
63 MISSING_MESSAGE, | 67 MISSING_MESSAGE, |
64 PLACEHOLDER_NOT_A_TREE, | 68 PLACEHOLDER_NOT_A_TREE, |
65 EMPTY_PLACEHOLDER_TREE, | 69 EMPTY_PLACEHOLDER_TREE, |
66 CONTENT_MISSING, | 70 CONTENT_MISSING, |
67 MESSAGE_PLACEHOLDER_DOESNT_MATCH, | 71 MESSAGE_PLACEHOLDER_DOESNT_MATCH, |
68 }; | 72 }; |
69 | 73 |
70 void CreateBadDictionary(DictionaryValue* dict, | 74 // Caller owns the memory. |
71 enum BadDictionary what_is_bad) { | 75 DictionaryValue* CreateBadDictionary(enum BadDictionary what_is_bad) { |
72 CreateGoodDictionary(dict); | 76 DictionaryValue* dict = CreateGoodDictionary(); |
73 // Now remove/break things. | 77 // Now remove/break things. |
74 switch (what_is_bad) { | 78 switch (what_is_bad) { |
75 case INVALID_NAME: | 79 case INVALID_NAME: |
76 CreateMessageTree(L"n 5", "nevermind", false, dict); | 80 CreateMessageTree(L"n 5", "nevermind", false, dict); |
77 break; | 81 break; |
78 case NAME_NOT_A_TREE: | 82 case NAME_NOT_A_TREE: |
79 dict->SetString(L"n4", "whatever"); | 83 dict->SetString(L"n4", "whatever"); |
80 break; | 84 break; |
81 case EMPTY_NAME_TREE: { | 85 case EMPTY_NAME_TREE: { |
82 DictionaryValue* empty_tree = new DictionaryValue; | 86 DictionaryValue* empty_tree = new DictionaryValue; |
(...skipping 14 matching lines...) Expand all Loading... |
97 case CONTENT_MISSING: | 101 case CONTENT_MISSING: |
98 dict->Remove(L"n1.placeholders.a.content", NULL); | 102 dict->Remove(L"n1.placeholders.a.content", NULL); |
99 break; | 103 break; |
100 case MESSAGE_PLACEHOLDER_DOESNT_MATCH: | 104 case MESSAGE_PLACEHOLDER_DOESNT_MATCH: |
101 DictionaryValue* value; | 105 DictionaryValue* value; |
102 dict->Remove(L"n1.placeholders.a", NULL); | 106 dict->Remove(L"n1.placeholders.a", NULL); |
103 dict->GetDictionary(L"n1.placeholders", &value); | 107 dict->GetDictionary(L"n1.placeholders", &value); |
104 CreateContentTree(L"x", "X", value); | 108 CreateContentTree(L"x", "X", value); |
105 break; | 109 break; |
106 } | 110 } |
| 111 |
| 112 return dict; |
107 } | 113 } |
108 | 114 |
109 TEST(ExtensionMessageBundle, InitEmptyDictionaries) { | 115 TEST(ExtensionMessageBundle, InitEmptyDictionaries) { |
110 DictionaryValue default_dict; | 116 std::vector<linked_ptr<DictionaryValue> > catalogs; |
111 DictionaryValue app_dict; | |
112 std::string error; | 117 std::string error; |
113 scoped_ptr<ExtensionMessageBundle> handler( | 118 scoped_ptr<ExtensionMessageBundle> handler( |
114 ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 119 ExtensionMessageBundle::Create(catalogs, &error)); |
115 | 120 |
116 EXPECT_TRUE(handler.get() != NULL); | 121 EXPECT_TRUE(handler.get() != NULL); |
117 EXPECT_EQ(0U, handler->size()); | 122 EXPECT_EQ(0U, handler->size()); |
118 } | 123 } |
119 | 124 |
120 TEST(ExtensionMessageBundle, InitGoodDefaultDictEmptyAppDict) { | 125 TEST(ExtensionMessageBundle, InitGoodDefaultDict) { |
121 DictionaryValue default_dict; | 126 std::vector<linked_ptr<DictionaryValue> > catalogs; |
122 DictionaryValue app_dict; | 127 catalogs.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
| 128 |
123 std::string error; | 129 std::string error; |
124 | |
125 CreateGoodDictionary(&default_dict); | |
126 scoped_ptr<ExtensionMessageBundle> handler( | 130 scoped_ptr<ExtensionMessageBundle> handler( |
127 ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 131 ExtensionMessageBundle::Create(catalogs, &error)); |
128 | 132 |
129 EXPECT_TRUE(handler.get() != NULL); | 133 EXPECT_TRUE(handler.get() != NULL); |
130 EXPECT_EQ(3U, handler->size()); | 134 EXPECT_EQ(3U, handler->size()); |
131 | 135 |
132 EXPECT_EQ("message1 A B", handler->GetL10nMessage("n1")); | 136 EXPECT_EQ("message1 A B", handler->GetL10nMessage("n1")); |
133 EXPECT_EQ("message2 C", handler->GetL10nMessage("n2")); | 137 EXPECT_EQ("message2 C", handler->GetL10nMessage("n2")); |
134 EXPECT_EQ("message3", handler->GetL10nMessage("n3")); | 138 EXPECT_EQ("message3", handler->GetL10nMessage("n3")); |
135 } | 139 } |
136 | 140 |
137 TEST(ExtensionMessageBundle, InitAppDictConsultedFirst) { | 141 TEST(ExtensionMessageBundle, InitAppDictConsultedFirst) { |
138 DictionaryValue default_dict; | 142 std::vector<linked_ptr<DictionaryValue> > catalogs; |
139 DictionaryValue app_dict; | 143 catalogs.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
| 144 catalogs.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
| 145 |
| 146 DictionaryValue* app_dict = catalogs[0].get(); |
| 147 // Flip placeholders in message of n1 tree. |
| 148 app_dict->SetString(L"n1.message", "message1 $b$ $a$"); |
| 149 // Remove one message from app dict. |
| 150 app_dict->Remove(L"n2", NULL); |
| 151 // Replace n3 with N3. |
| 152 app_dict->Remove(L"n3", NULL); |
| 153 CreateMessageTree(L"N3", "message3_app_dict", false, app_dict); |
| 154 |
140 std::string error; | 155 std::string error; |
141 | |
142 CreateGoodDictionary(&default_dict); | |
143 CreateGoodDictionary(&app_dict); | |
144 // Flip placeholders in message of n1 tree. | |
145 app_dict.SetString(L"n1.message", "message1 $b$ $a$"); | |
146 // Remove one message from app dict. | |
147 app_dict.Remove(L"n2", NULL); | |
148 // Replace n3 with N3. | |
149 app_dict.Remove(L"n3", NULL); | |
150 CreateMessageTree(L"N3", "message3_app_dict", false, &app_dict); | |
151 | |
152 scoped_ptr<ExtensionMessageBundle> handler( | 156 scoped_ptr<ExtensionMessageBundle> handler( |
153 ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 157 ExtensionMessageBundle::Create(catalogs, &error)); |
154 | 158 |
155 EXPECT_TRUE(handler.get() != NULL); | 159 EXPECT_TRUE(handler.get() != NULL); |
156 EXPECT_EQ(3U, handler->size()); | 160 EXPECT_EQ(3U, handler->size()); |
157 | 161 |
158 EXPECT_EQ("message1 B A", handler->GetL10nMessage("n1")); | 162 EXPECT_EQ("message1 B A", handler->GetL10nMessage("n1")); |
159 EXPECT_EQ("message2 C", handler->GetL10nMessage("n2")); | 163 EXPECT_EQ("message2 C", handler->GetL10nMessage("n2")); |
160 EXPECT_EQ("message3_app_dict", handler->GetL10nMessage("n3")); | 164 EXPECT_EQ("message3_app_dict", handler->GetL10nMessage("n3")); |
161 } | 165 } |
162 | 166 |
163 TEST(ExtensionMessageBundle, InitBadAppDict) { | 167 TEST(ExtensionMessageBundle, InitBadAppDict) { |
164 DictionaryValue default_dict; | 168 std::vector<linked_ptr<DictionaryValue> > catalogs; |
165 DictionaryValue app_dict; | 169 catalogs.push_back( |
| 170 linked_ptr<DictionaryValue>(CreateBadDictionary(INVALID_NAME))); |
| 171 catalogs.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
| 172 |
166 std::string error; | 173 std::string error; |
167 | |
168 CreateBadDictionary(&app_dict, INVALID_NAME); | |
169 scoped_ptr<ExtensionMessageBundle> handler( | 174 scoped_ptr<ExtensionMessageBundle> handler( |
170 ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 175 ExtensionMessageBundle::Create(catalogs, &error)); |
171 | 176 |
172 EXPECT_TRUE(handler.get() == NULL); | 177 EXPECT_TRUE(handler.get() == NULL); |
173 EXPECT_EQ("Name of a key \"n 5\" is invalid. Only ASCII [a-z], " | 178 EXPECT_EQ("Name of a key \"n 5\" is invalid. Only ASCII [a-z], " |
174 "[A-Z], [0-9] and \"_\" are allowed.", error); | 179 "[A-Z], [0-9] and \"_\" are allowed.", error); |
175 | 180 |
176 CreateBadDictionary(&app_dict, NAME_NOT_A_TREE); | 181 catalogs[0].reset(CreateBadDictionary(NAME_NOT_A_TREE)); |
177 handler.reset(ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 182 handler.reset(ExtensionMessageBundle::Create(catalogs, &error)); |
178 EXPECT_TRUE(handler.get() == NULL); | 183 EXPECT_TRUE(handler.get() == NULL); |
179 EXPECT_EQ("Not a valid tree for key n4.", error); | 184 EXPECT_EQ("Not a valid tree for key n4.", error); |
180 | 185 |
181 CreateBadDictionary(&app_dict, EMPTY_NAME_TREE); | 186 catalogs[0].reset(CreateBadDictionary(EMPTY_NAME_TREE)); |
182 handler.reset(ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 187 handler.reset(ExtensionMessageBundle::Create(catalogs, &error)); |
183 EXPECT_TRUE(handler.get() == NULL); | 188 EXPECT_TRUE(handler.get() == NULL); |
184 EXPECT_EQ("There is no \"message\" element for key n4.", error); | 189 EXPECT_EQ("There is no \"message\" element for key n4.", error); |
185 | 190 |
186 CreateBadDictionary(&app_dict, MISSING_MESSAGE); | 191 catalogs[0].reset(CreateBadDictionary(MISSING_MESSAGE)); |
187 handler.reset(ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 192 handler.reset(ExtensionMessageBundle::Create(catalogs, &error)); |
188 EXPECT_TRUE(handler.get() == NULL); | 193 EXPECT_TRUE(handler.get() == NULL); |
189 EXPECT_EQ("There is no \"message\" element for key n1.", error); | 194 EXPECT_EQ("There is no \"message\" element for key n1.", error); |
190 | 195 |
191 CreateBadDictionary(&app_dict, PLACEHOLDER_NOT_A_TREE); | 196 catalogs[0].reset(CreateBadDictionary(PLACEHOLDER_NOT_A_TREE)); |
192 handler.reset(ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 197 handler.reset(ExtensionMessageBundle::Create(catalogs, &error)); |
193 EXPECT_TRUE(handler.get() == NULL); | 198 EXPECT_TRUE(handler.get() == NULL); |
194 EXPECT_EQ("Not a valid \"placeholders\" element for key n1.", error); | 199 EXPECT_EQ("Not a valid \"placeholders\" element for key n1.", error); |
195 | 200 |
196 CreateBadDictionary(&app_dict, EMPTY_PLACEHOLDER_TREE); | 201 catalogs[0].reset(CreateBadDictionary(EMPTY_PLACEHOLDER_TREE)); |
197 handler.reset(ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 202 handler.reset(ExtensionMessageBundle::Create(catalogs, &error)); |
198 EXPECT_TRUE(handler.get() == NULL); | 203 EXPECT_TRUE(handler.get() == NULL); |
199 EXPECT_EQ("Variable $a$ used but not defined.", error); | 204 EXPECT_EQ("Variable $a$ used but not defined.", error); |
200 | 205 |
201 CreateBadDictionary(&app_dict, CONTENT_MISSING); | 206 catalogs[0].reset(CreateBadDictionary(CONTENT_MISSING)); |
202 handler.reset(ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 207 handler.reset(ExtensionMessageBundle::Create(catalogs, &error)); |
203 EXPECT_TRUE(handler.get() == NULL); | 208 EXPECT_TRUE(handler.get() == NULL); |
204 EXPECT_EQ("Invalid \"content\" element for key n1.", error); | 209 EXPECT_EQ("Invalid \"content\" element for key n1.", error); |
205 | 210 |
206 CreateBadDictionary(&app_dict, MESSAGE_PLACEHOLDER_DOESNT_MATCH); | 211 catalogs[0].reset(CreateBadDictionary(MESSAGE_PLACEHOLDER_DOESNT_MATCH)); |
207 handler.reset(ExtensionMessageBundle::Create(default_dict, app_dict, &error)); | 212 handler.reset(ExtensionMessageBundle::Create(catalogs, &error)); |
208 EXPECT_TRUE(handler.get() == NULL); | 213 EXPECT_TRUE(handler.get() == NULL); |
209 EXPECT_EQ("Variable $a$ used but not defined.", error); | 214 EXPECT_EQ("Variable $a$ used but not defined.", error); |
210 } | 215 } |
211 | 216 |
212 struct ReplaceVariables { | 217 struct ReplaceVariables { |
213 const char* original; | 218 const char* original; |
214 const char* result; | 219 const char* result; |
215 const char* error; | 220 const char* error; |
216 const char* begin_delimiter; | 221 const char* begin_delimiter; |
217 const char* end_delimiter; | 222 const char* end_delimiter; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 ExtensionMessageBundle::ReplaceVariables(messages, | 284 ExtensionMessageBundle::ReplaceVariables(messages, |
280 test_cases[i].begin_delimiter, | 285 test_cases[i].begin_delimiter, |
281 test_cases[i].end_delimiter, | 286 test_cases[i].end_delimiter, |
282 &text, | 287 &text, |
283 &error)); | 288 &error)); |
284 EXPECT_EQ(test_cases[i].result, text); | 289 EXPECT_EQ(test_cases[i].result, text); |
285 } | 290 } |
286 } | 291 } |
287 | 292 |
288 } // namespace | 293 } // namespace |
OLD | NEW |