Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: extensions/common/message_bundle_unittest.cc

Issue 598173003: Run clang-modernize -use-nullptr over src/extensions/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/common/message_bundle.h" 5 #include "extensions/common/message_bundle.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/i18n/rtl.h" 10 #include "base/i18n/rtl.h"
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 break; 90 break;
91 case NAME_NOT_A_TREE: 91 case NAME_NOT_A_TREE:
92 dict->SetString("n4", "whatever"); 92 dict->SetString("n4", "whatever");
93 break; 93 break;
94 case EMPTY_NAME_TREE: { 94 case EMPTY_NAME_TREE: {
95 base::DictionaryValue* empty_tree = new base::DictionaryValue; 95 base::DictionaryValue* empty_tree = new base::DictionaryValue;
96 SetDictionary("n4", empty_tree, dict); 96 SetDictionary("n4", empty_tree, dict);
97 } 97 }
98 break; 98 break;
99 case MISSING_MESSAGE: 99 case MISSING_MESSAGE:
100 dict->Remove("n1.message", NULL); 100 dict->Remove("n1.message", nullptr);
101 break; 101 break;
102 case PLACEHOLDER_NOT_A_TREE: 102 case PLACEHOLDER_NOT_A_TREE:
103 dict->SetString("n1.placeholders", "whatever"); 103 dict->SetString("n1.placeholders", "whatever");
104 break; 104 break;
105 case EMPTY_PLACEHOLDER_TREE: { 105 case EMPTY_PLACEHOLDER_TREE: {
106 base::DictionaryValue* empty_tree = new base::DictionaryValue; 106 base::DictionaryValue* empty_tree = new base::DictionaryValue;
107 SetDictionary("n1.placeholders", empty_tree, dict); 107 SetDictionary("n1.placeholders", empty_tree, dict);
108 } 108 }
109 break; 109 break;
110 case CONTENT_MISSING: 110 case CONTENT_MISSING:
111 dict->Remove("n1.placeholders.a.content", NULL); 111 dict->Remove("n1.placeholders.a.content", nullptr);
112 break; 112 break;
113 case MESSAGE_PLACEHOLDER_DOESNT_MATCH: 113 case MESSAGE_PLACEHOLDER_DOESNT_MATCH:
114 base::DictionaryValue* value; 114 base::DictionaryValue* value;
115 dict->Remove("n1.placeholders.a", NULL); 115 dict->Remove("n1.placeholders.a", nullptr);
116 dict->GetDictionary("n1.placeholders", &value); 116 dict->GetDictionary("n1.placeholders", &value);
117 CreateContentTree("x", "X", value); 117 CreateContentTree("x", "X", value);
118 break; 118 break;
119 } 119 }
120 120
121 return dict; 121 return dict;
122 } 122 }
123 123
124 unsigned int ReservedMessagesCount() { 124 unsigned int ReservedMessagesCount() {
125 // Update when adding new reserved messages. 125 // Update when adding new reserved messages.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 scoped_ptr<MessageBundle> handler_; 160 scoped_ptr<MessageBundle> handler_;
161 std::vector<linked_ptr<base::DictionaryValue> > catalogs_; 161 std::vector<linked_ptr<base::DictionaryValue> > catalogs_;
162 }; 162 };
163 163
164 TEST_F(MessageBundleTest, ReservedMessagesCount) { 164 TEST_F(MessageBundleTest, ReservedMessagesCount) {
165 ASSERT_EQ(5U, ReservedMessagesCount()); 165 ASSERT_EQ(5U, ReservedMessagesCount());
166 } 166 }
167 167
168 TEST_F(MessageBundleTest, InitEmptyDictionaries) { 168 TEST_F(MessageBundleTest, InitEmptyDictionaries) {
169 CreateMessageBundle(); 169 CreateMessageBundle();
170 EXPECT_TRUE(handler_.get() != NULL); 170 EXPECT_TRUE(handler_.get() != nullptr);
171 EXPECT_EQ(0U + ReservedMessagesCount(), handler_->size()); 171 EXPECT_EQ(0U + ReservedMessagesCount(), handler_->size());
172 CheckReservedMessages(handler_.get()); 172 CheckReservedMessages(handler_.get());
173 } 173 }
174 174
175 TEST_F(MessageBundleTest, InitGoodDefaultDict) { 175 TEST_F(MessageBundleTest, InitGoodDefaultDict) {
176 catalogs_.push_back( 176 catalogs_.push_back(
177 linked_ptr<base::DictionaryValue>(CreateGoodDictionary())); 177 linked_ptr<base::DictionaryValue>(CreateGoodDictionary()));
178 CreateMessageBundle(); 178 CreateMessageBundle();
179 179
180 EXPECT_TRUE(handler_.get() != NULL); 180 EXPECT_TRUE(handler_.get() != nullptr);
181 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size()); 181 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size());
182 182
183 EXPECT_EQ("message1 A B", handler_->GetL10nMessage("n1")); 183 EXPECT_EQ("message1 A B", handler_->GetL10nMessage("n1"));
184 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2")); 184 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2"));
185 EXPECT_EQ("message3", handler_->GetL10nMessage("n3")); 185 EXPECT_EQ("message3", handler_->GetL10nMessage("n3"));
186 CheckReservedMessages(handler_.get()); 186 CheckReservedMessages(handler_.get());
187 } 187 }
188 188
189 TEST_F(MessageBundleTest, InitAppDictConsultedFirst) { 189 TEST_F(MessageBundleTest, InitAppDictConsultedFirst) {
190 catalogs_.push_back( 190 catalogs_.push_back(
191 linked_ptr<base::DictionaryValue>(CreateGoodDictionary())); 191 linked_ptr<base::DictionaryValue>(CreateGoodDictionary()));
192 catalogs_.push_back( 192 catalogs_.push_back(
193 linked_ptr<base::DictionaryValue>(CreateGoodDictionary())); 193 linked_ptr<base::DictionaryValue>(CreateGoodDictionary()));
194 194
195 base::DictionaryValue* app_dict = catalogs_[0].get(); 195 base::DictionaryValue* app_dict = catalogs_[0].get();
196 // Flip placeholders in message of n1 tree. 196 // Flip placeholders in message of n1 tree.
197 app_dict->SetString("n1.message", "message1 $b$ $a$"); 197 app_dict->SetString("n1.message", "message1 $b$ $a$");
198 // Remove one message from app dict. 198 // Remove one message from app dict.
199 app_dict->Remove("n2", NULL); 199 app_dict->Remove("n2", nullptr);
200 // Replace n3 with N3. 200 // Replace n3 with N3.
201 app_dict->Remove("n3", NULL); 201 app_dict->Remove("n3", nullptr);
202 CreateMessageTree("N3", "message3_app_dict", false, app_dict); 202 CreateMessageTree("N3", "message3_app_dict", false, app_dict);
203 203
204 CreateMessageBundle(); 204 CreateMessageBundle();
205 205
206 EXPECT_TRUE(handler_.get() != NULL); 206 EXPECT_TRUE(handler_.get() != nullptr);
207 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size()); 207 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size());
208 208
209 EXPECT_EQ("message1 B A", handler_->GetL10nMessage("n1")); 209 EXPECT_EQ("message1 B A", handler_->GetL10nMessage("n1"));
210 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2")); 210 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2"));
211 EXPECT_EQ("message3_app_dict", handler_->GetL10nMessage("n3")); 211 EXPECT_EQ("message3_app_dict", handler_->GetL10nMessage("n3"));
212 CheckReservedMessages(handler_.get()); 212 CheckReservedMessages(handler_.get());
213 } 213 }
214 214
215 TEST_F(MessageBundleTest, InitBadAppDict) { 215 TEST_F(MessageBundleTest, InitBadAppDict) {
216 catalogs_.push_back( 216 catalogs_.push_back(
217 linked_ptr<base::DictionaryValue>(CreateBadDictionary(INVALID_NAME))); 217 linked_ptr<base::DictionaryValue>(CreateBadDictionary(INVALID_NAME)));
218 catalogs_.push_back( 218 catalogs_.push_back(
219 linked_ptr<base::DictionaryValue>(CreateGoodDictionary())); 219 linked_ptr<base::DictionaryValue>(CreateGoodDictionary()));
220 220
221 std::string error = CreateMessageBundle(); 221 std::string error = CreateMessageBundle();
222 222
223 EXPECT_TRUE(handler_.get() == NULL); 223 EXPECT_TRUE(handler_.get() == nullptr);
224 EXPECT_EQ("Name of a key \"n 5\" is invalid. Only ASCII [a-z], " 224 EXPECT_EQ("Name of a key \"n 5\" is invalid. Only ASCII [a-z], "
225 "[A-Z], [0-9] and \"_\" are allowed.", error); 225 "[A-Z], [0-9] and \"_\" are allowed.", error);
226 226
227 catalogs_[0].reset(CreateBadDictionary(NAME_NOT_A_TREE)); 227 catalogs_[0].reset(CreateBadDictionary(NAME_NOT_A_TREE));
228 handler_.reset(MessageBundle::Create(catalogs_, &error)); 228 handler_.reset(MessageBundle::Create(catalogs_, &error));
229 EXPECT_TRUE(handler_.get() == NULL); 229 EXPECT_TRUE(handler_.get() == nullptr);
230 EXPECT_EQ("Not a valid tree for key n4.", error); 230 EXPECT_EQ("Not a valid tree for key n4.", error);
231 231
232 catalogs_[0].reset(CreateBadDictionary(EMPTY_NAME_TREE)); 232 catalogs_[0].reset(CreateBadDictionary(EMPTY_NAME_TREE));
233 handler_.reset(MessageBundle::Create(catalogs_, &error)); 233 handler_.reset(MessageBundle::Create(catalogs_, &error));
234 EXPECT_TRUE(handler_.get() == NULL); 234 EXPECT_TRUE(handler_.get() == nullptr);
235 EXPECT_EQ("There is no \"message\" element for key n4.", error); 235 EXPECT_EQ("There is no \"message\" element for key n4.", error);
236 236
237 catalogs_[0].reset(CreateBadDictionary(MISSING_MESSAGE)); 237 catalogs_[0].reset(CreateBadDictionary(MISSING_MESSAGE));
238 handler_.reset(MessageBundle::Create(catalogs_, &error)); 238 handler_.reset(MessageBundle::Create(catalogs_, &error));
239 EXPECT_TRUE(handler_.get() == NULL); 239 EXPECT_TRUE(handler_.get() == nullptr);
240 EXPECT_EQ("There is no \"message\" element for key n1.", error); 240 EXPECT_EQ("There is no \"message\" element for key n1.", error);
241 241
242 catalogs_[0].reset(CreateBadDictionary(PLACEHOLDER_NOT_A_TREE)); 242 catalogs_[0].reset(CreateBadDictionary(PLACEHOLDER_NOT_A_TREE));
243 handler_.reset(MessageBundle::Create(catalogs_, &error)); 243 handler_.reset(MessageBundle::Create(catalogs_, &error));
244 EXPECT_TRUE(handler_.get() == NULL); 244 EXPECT_TRUE(handler_.get() == nullptr);
245 EXPECT_EQ("Not a valid \"placeholders\" element for key n1.", error); 245 EXPECT_EQ("Not a valid \"placeholders\" element for key n1.", error);
246 246
247 catalogs_[0].reset(CreateBadDictionary(EMPTY_PLACEHOLDER_TREE)); 247 catalogs_[0].reset(CreateBadDictionary(EMPTY_PLACEHOLDER_TREE));
248 handler_.reset(MessageBundle::Create(catalogs_, &error)); 248 handler_.reset(MessageBundle::Create(catalogs_, &error));
249 EXPECT_TRUE(handler_.get() == NULL); 249 EXPECT_TRUE(handler_.get() == nullptr);
250 EXPECT_EQ("Variable $a$ used but not defined.", error); 250 EXPECT_EQ("Variable $a$ used but not defined.", error);
251 251
252 catalogs_[0].reset(CreateBadDictionary(CONTENT_MISSING)); 252 catalogs_[0].reset(CreateBadDictionary(CONTENT_MISSING));
253 handler_.reset(MessageBundle::Create(catalogs_, &error)); 253 handler_.reset(MessageBundle::Create(catalogs_, &error));
254 EXPECT_TRUE(handler_.get() == NULL); 254 EXPECT_TRUE(handler_.get() == nullptr);
255 EXPECT_EQ("Invalid \"content\" element for key n1.", error); 255 EXPECT_EQ("Invalid \"content\" element for key n1.", error);
256 256
257 catalogs_[0].reset(CreateBadDictionary(MESSAGE_PLACEHOLDER_DOESNT_MATCH)); 257 catalogs_[0].reset(CreateBadDictionary(MESSAGE_PLACEHOLDER_DOESNT_MATCH));
258 handler_.reset(MessageBundle::Create(catalogs_, &error)); 258 handler_.reset(MessageBundle::Create(catalogs_, &error));
259 EXPECT_TRUE(handler_.get() == NULL); 259 EXPECT_TRUE(handler_.get() == nullptr);
260 EXPECT_EQ("Variable $a$ used but not defined.", error); 260 EXPECT_EQ("Variable $a$ used but not defined.", error);
261 } 261 }
262 262
263 TEST_F(MessageBundleTest, ReservedMessagesOverrideDeveloperMessages) { 263 TEST_F(MessageBundleTest, ReservedMessagesOverrideDeveloperMessages) {
264 catalogs_.push_back( 264 catalogs_.push_back(
265 linked_ptr<base::DictionaryValue>(CreateGoodDictionary())); 265 linked_ptr<base::DictionaryValue>(CreateGoodDictionary()));
266 266
267 base::DictionaryValue* dict = catalogs_[0].get(); 267 base::DictionaryValue* dict = catalogs_[0].get();
268 CreateMessageTree(MessageBundle::kUILocaleKey, "x", false, dict); 268 CreateMessageTree(MessageBundle::kUILocaleKey, "x", false, dict);
269 269
270 std::string error = CreateMessageBundle(); 270 std::string error = CreateMessageBundle();
271 271
272 EXPECT_TRUE(handler_.get() == NULL); 272 EXPECT_TRUE(handler_.get() == nullptr);
273 std::string expected_error = ErrorUtils::FormatErrorMessage( 273 std::string expected_error = ErrorUtils::FormatErrorMessage(
274 errors::kReservedMessageFound, MessageBundle::kUILocaleKey); 274 errors::kReservedMessageFound, MessageBundle::kUILocaleKey);
275 EXPECT_EQ(expected_error, error); 275 EXPECT_EQ(expected_error, error);
276 } 276 }
277 277
278 TEST_F(MessageBundleTest, AppendReservedMessagesForLTR) { 278 TEST_F(MessageBundleTest, AppendReservedMessagesForLTR) {
279 CreateMessageBundle(); 279 CreateMessageBundle();
280 280
281 ASSERT_TRUE(handler_.get() != NULL); 281 ASSERT_TRUE(handler_.get() != nullptr);
282 ClearDictionary(); 282 ClearDictionary();
283 ASSERT_TRUE(AppendReservedMessages("en_US")); 283 ASSERT_TRUE(AppendReservedMessages("en_US"));
284 284
285 EXPECT_EQ("en_US", 285 EXPECT_EQ("en_US",
286 handler_->GetL10nMessage(MessageBundle::kUILocaleKey)); 286 handler_->GetL10nMessage(MessageBundle::kUILocaleKey));
287 EXPECT_EQ("ltr", handler_->GetL10nMessage( 287 EXPECT_EQ("ltr", handler_->GetL10nMessage(
288 MessageBundle::kBidiDirectionKey)); 288 MessageBundle::kBidiDirectionKey));
289 EXPECT_EQ("rtl", handler_->GetL10nMessage( 289 EXPECT_EQ("rtl", handler_->GetL10nMessage(
290 MessageBundle::kBidiReversedDirectionKey)); 290 MessageBundle::kBidiReversedDirectionKey));
291 EXPECT_EQ("left", handler_->GetL10nMessage( 291 EXPECT_EQ("left", handler_->GetL10nMessage(
292 MessageBundle::kBidiStartEdgeKey)); 292 MessageBundle::kBidiStartEdgeKey));
293 EXPECT_EQ("right", handler_->GetL10nMessage( 293 EXPECT_EQ("right", handler_->GetL10nMessage(
294 MessageBundle::kBidiEndEdgeKey)); 294 MessageBundle::kBidiEndEdgeKey));
295 } 295 }
296 296
297 TEST_F(MessageBundleTest, AppendReservedMessagesForRTL) { 297 TEST_F(MessageBundleTest, AppendReservedMessagesForRTL) {
298 CreateMessageBundle(); 298 CreateMessageBundle();
299 299
300 ASSERT_TRUE(handler_.get() != NULL); 300 ASSERT_TRUE(handler_.get() != nullptr);
301 ClearDictionary(); 301 ClearDictionary();
302 ASSERT_TRUE(AppendReservedMessages("he")); 302 ASSERT_TRUE(AppendReservedMessages("he"));
303 303
304 EXPECT_EQ("he", 304 EXPECT_EQ("he",
305 handler_->GetL10nMessage(MessageBundle::kUILocaleKey)); 305 handler_->GetL10nMessage(MessageBundle::kUILocaleKey));
306 EXPECT_EQ("rtl", handler_->GetL10nMessage( 306 EXPECT_EQ("rtl", handler_->GetL10nMessage(
307 MessageBundle::kBidiDirectionKey)); 307 MessageBundle::kBidiDirectionKey));
308 EXPECT_EQ("ltr", handler_->GetL10nMessage( 308 EXPECT_EQ("ltr", handler_->GetL10nMessage(
309 MessageBundle::kBidiReversedDirectionKey)); 309 MessageBundle::kBidiReversedDirectionKey));
310 EXPECT_EQ("right", handler_->GetL10nMessage( 310 EXPECT_EQ("right", handler_->GetL10nMessage(
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 } 399 }
400 400
401 /////////////////////////////////////////////////////////////////////////////// 401 ///////////////////////////////////////////////////////////////////////////////
402 // 402 //
403 // Renderer helper functions test. 403 // Renderer helper functions test.
404 // 404 //
405 /////////////////////////////////////////////////////////////////////////////// 405 ///////////////////////////////////////////////////////////////////////////////
406 406
407 TEST(GetExtensionToL10nMessagesMapTest, ReturnsTheSameObject) { 407 TEST(GetExtensionToL10nMessagesMapTest, ReturnsTheSameObject) {
408 ExtensionToL10nMessagesMap* map1 = GetExtensionToL10nMessagesMap(); 408 ExtensionToL10nMessagesMap* map1 = GetExtensionToL10nMessagesMap();
409 ASSERT_TRUE(NULL != map1); 409 ASSERT_TRUE(nullptr != map1);
410 410
411 ExtensionToL10nMessagesMap* map2 = GetExtensionToL10nMessagesMap(); 411 ExtensionToL10nMessagesMap* map2 = GetExtensionToL10nMessagesMap();
412 ASSERT_EQ(map1, map2); 412 ASSERT_EQ(map1, map2);
413 } 413 }
414 414
415 TEST(GetExtensionToL10nMessagesMapTest, ReturnsNullForUnknownExtensionId) { 415 TEST(GetExtensionToL10nMessagesMapTest, ReturnsNullForUnknownExtensionId) {
416 const std::string extension_id("some_unique_12334212314234_id"); 416 const std::string extension_id("some_unique_12334212314234_id");
417 L10nMessagesMap* map = GetL10nMessagesMap(extension_id); 417 L10nMessagesMap* map = GetL10nMessagesMap(extension_id);
418 EXPECT_TRUE(NULL == map); 418 EXPECT_TRUE(nullptr == map);
419 } 419 }
420 420
421 TEST(GetExtensionToL10nMessagesMapTest, ReturnsMapForKnownExtensionId) { 421 TEST(GetExtensionToL10nMessagesMapTest, ReturnsMapForKnownExtensionId) {
422 const std::string extension_id("some_unique_121212121212121_id"); 422 const std::string extension_id("some_unique_121212121212121_id");
423 // Store a map for given id. 423 // Store a map for given id.
424 L10nMessagesMap messages; 424 L10nMessagesMap messages;
425 messages.insert(std::make_pair("message_name", "message_value")); 425 messages.insert(std::make_pair("message_name", "message_value"));
426 (*GetExtensionToL10nMessagesMap())[extension_id] = messages; 426 (*GetExtensionToL10nMessagesMap())[extension_id] = messages;
427 427
428 L10nMessagesMap* map = GetL10nMessagesMap(extension_id); 428 L10nMessagesMap* map = GetL10nMessagesMap(extension_id);
429 ASSERT_TRUE(NULL != map); 429 ASSERT_TRUE(nullptr != map);
430 EXPECT_EQ(1U, map->size()); 430 EXPECT_EQ(1U, map->size());
431 EXPECT_EQ("message_value", (*map)["message_name"]); 431 EXPECT_EQ("message_value", (*map)["message_name"]);
432 } 432 }
433 433
434 } // namespace extensions 434 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698