| 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 #include <vector> |
| 9 | 9 |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 NAME_NOT_A_TREE, | 27 NAME_NOT_A_TREE, |
| 28 EMPTY_NAME_TREE, | 28 EMPTY_NAME_TREE, |
| 29 MISSING_MESSAGE, | 29 MISSING_MESSAGE, |
| 30 PLACEHOLDER_NOT_A_TREE, | 30 PLACEHOLDER_NOT_A_TREE, |
| 31 EMPTY_PLACEHOLDER_TREE, | 31 EMPTY_PLACEHOLDER_TREE, |
| 32 CONTENT_MISSING, | 32 CONTENT_MISSING, |
| 33 MESSAGE_PLACEHOLDER_DOESNT_MATCH, | 33 MESSAGE_PLACEHOLDER_DOESNT_MATCH, |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 // Helper method for dictionary building. | 36 // Helper method for dictionary building. |
| 37 void SetDictionary(const std::wstring name, | 37 void SetDictionary(const std::string& name, |
| 38 DictionaryValue* subtree, | 38 DictionaryValue* subtree, |
| 39 DictionaryValue* target) { | 39 DictionaryValue* target) { |
| 40 target->Set(name, static_cast<Value*>(subtree)); | 40 target->Set(name, static_cast<Value*>(subtree)); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void CreateContentTree(const std::wstring& name, | 43 void CreateContentTree(const std::string& name, |
| 44 const std::string content, | 44 const std::string& content, |
| 45 DictionaryValue* dict) { | 45 DictionaryValue* dict) { |
| 46 DictionaryValue* content_tree = new DictionaryValue; | 46 DictionaryValue* content_tree = new DictionaryValue; |
| 47 content_tree->SetString(ExtensionMessageBundle::kContentKey, content); | 47 content_tree->SetString(ExtensionMessageBundle::kContentKey, content); |
| 48 SetDictionary(name, content_tree, dict); | 48 SetDictionary(name, content_tree, dict); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void CreatePlaceholdersTree(DictionaryValue* dict) { | 51 void CreatePlaceholdersTree(DictionaryValue* dict) { |
| 52 DictionaryValue* placeholders_tree = new DictionaryValue; | 52 DictionaryValue* placeholders_tree = new DictionaryValue; |
| 53 CreateContentTree(L"a", "A", placeholders_tree); | 53 CreateContentTree("a", "A", placeholders_tree); |
| 54 CreateContentTree(L"b", "B", placeholders_tree); | 54 CreateContentTree("b", "B", placeholders_tree); |
| 55 CreateContentTree(L"c", "C", placeholders_tree); | 55 CreateContentTree("c", "C", placeholders_tree); |
| 56 SetDictionary(ExtensionMessageBundle::kPlaceholdersKey, | 56 SetDictionary(ExtensionMessageBundle::kPlaceholdersKey, |
| 57 placeholders_tree, | 57 placeholders_tree, |
| 58 dict); | 58 dict); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void CreateMessageTree(const std::wstring& name, | 61 void CreateMessageTree(const std::string& name, |
| 62 const std::string& message, | 62 const std::string& message, |
| 63 bool create_placeholder_subtree, | 63 bool create_placeholder_subtree, |
| 64 DictionaryValue* dict) { | 64 DictionaryValue* dict) { |
| 65 DictionaryValue* message_tree = new DictionaryValue; | 65 DictionaryValue* message_tree = new DictionaryValue; |
| 66 if (create_placeholder_subtree) | 66 if (create_placeholder_subtree) |
| 67 CreatePlaceholdersTree(message_tree); | 67 CreatePlaceholdersTree(message_tree); |
| 68 message_tree->SetString(ExtensionMessageBundle::kMessageKey, message); | 68 message_tree->SetString(ExtensionMessageBundle::kMessageKey, message); |
| 69 SetDictionary(name, message_tree, dict); | 69 SetDictionary(name, message_tree, dict); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // Caller owns the memory. | 72 // Caller owns the memory. |
| 73 DictionaryValue* CreateGoodDictionary() { | 73 DictionaryValue* CreateGoodDictionary() { |
| 74 DictionaryValue* dict = new DictionaryValue; | 74 DictionaryValue* dict = new DictionaryValue; |
| 75 CreateMessageTree(L"n1", "message1 $a$ $b$", true, dict); | 75 CreateMessageTree("n1", "message1 $a$ $b$", true, dict); |
| 76 CreateMessageTree(L"n2", "message2 $c$", true, dict); | 76 CreateMessageTree("n2", "message2 $c$", true, dict); |
| 77 CreateMessageTree(L"n3", "message3", false, dict); | 77 CreateMessageTree("n3", "message3", false, dict); |
| 78 return dict; | 78 return dict; |
| 79 } | 79 } |
| 80 | 80 |
| 81 // Caller owns the memory. | 81 // Caller owns the memory. |
| 82 DictionaryValue* CreateBadDictionary(enum BadDictionary what_is_bad) { | 82 DictionaryValue* CreateBadDictionary(enum BadDictionary what_is_bad) { |
| 83 DictionaryValue* dict = CreateGoodDictionary(); | 83 DictionaryValue* dict = CreateGoodDictionary(); |
| 84 // Now remove/break things. | 84 // Now remove/break things. |
| 85 switch (what_is_bad) { | 85 switch (what_is_bad) { |
| 86 case INVALID_NAME: | 86 case INVALID_NAME: |
| 87 CreateMessageTree(L"n 5", "nevermind", false, dict); | 87 CreateMessageTree("n 5", "nevermind", false, dict); |
| 88 break; | 88 break; |
| 89 case NAME_NOT_A_TREE: | 89 case NAME_NOT_A_TREE: |
| 90 dict->SetString(L"n4", "whatever"); | 90 dict->SetString("n4", "whatever"); |
| 91 break; | 91 break; |
| 92 case EMPTY_NAME_TREE: { | 92 case EMPTY_NAME_TREE: { |
| 93 DictionaryValue* empty_tree = new DictionaryValue; | 93 DictionaryValue* empty_tree = new DictionaryValue; |
| 94 SetDictionary(L"n4", empty_tree, dict); | 94 SetDictionary("n4", empty_tree, dict); |
| 95 } | 95 } |
| 96 break; | 96 break; |
| 97 case MISSING_MESSAGE: | 97 case MISSING_MESSAGE: |
| 98 dict->Remove(L"n1.message", NULL); | 98 dict->Remove("n1.message", NULL); |
| 99 break; | 99 break; |
| 100 case PLACEHOLDER_NOT_A_TREE: | 100 case PLACEHOLDER_NOT_A_TREE: |
| 101 dict->SetString(L"n1.placeholders", "whatever"); | 101 dict->SetString("n1.placeholders", "whatever"); |
| 102 break; | 102 break; |
| 103 case EMPTY_PLACEHOLDER_TREE: { | 103 case EMPTY_PLACEHOLDER_TREE: { |
| 104 DictionaryValue* empty_tree = new DictionaryValue; | 104 DictionaryValue* empty_tree = new DictionaryValue; |
| 105 SetDictionary(L"n1.placeholders", empty_tree, dict); | 105 SetDictionary("n1.placeholders", empty_tree, dict); |
| 106 } | 106 } |
| 107 break; | 107 break; |
| 108 case CONTENT_MISSING: | 108 case CONTENT_MISSING: |
| 109 dict->Remove(L"n1.placeholders.a.content", NULL); | 109 dict->Remove("n1.placeholders.a.content", NULL); |
| 110 break; | 110 break; |
| 111 case MESSAGE_PLACEHOLDER_DOESNT_MATCH: | 111 case MESSAGE_PLACEHOLDER_DOESNT_MATCH: |
| 112 DictionaryValue* value; | 112 DictionaryValue* value; |
| 113 dict->Remove(L"n1.placeholders.a", NULL); | 113 dict->Remove("n1.placeholders.a", NULL); |
| 114 dict->GetDictionary(L"n1.placeholders", &value); | 114 dict->GetDictionary("n1.placeholders", &value); |
| 115 CreateContentTree(L"x", "X", value); | 115 CreateContentTree("x", "X", value); |
| 116 break; | 116 break; |
| 117 } | 117 } |
| 118 | 118 |
| 119 return dict; | 119 return dict; |
| 120 } | 120 } |
| 121 | 121 |
| 122 unsigned int ReservedMessagesCount() { | 122 unsigned int ReservedMessagesCount() { |
| 123 // Update when adding new reserved messages. | 123 // Update when adding new reserved messages. |
| 124 return 5U; | 124 return 5U; |
| 125 } | 125 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 EXPECT_EQ("message3", handler_->GetL10nMessage("n3")); | 182 EXPECT_EQ("message3", handler_->GetL10nMessage("n3")); |
| 183 CheckReservedMessages(handler_.get()); | 183 CheckReservedMessages(handler_.get()); |
| 184 } | 184 } |
| 185 | 185 |
| 186 TEST_F(ExtensionMessageBundleTest, InitAppDictConsultedFirst) { | 186 TEST_F(ExtensionMessageBundleTest, InitAppDictConsultedFirst) { |
| 187 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); | 187 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
| 188 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); | 188 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
| 189 | 189 |
| 190 DictionaryValue* app_dict = catalogs_[0].get(); | 190 DictionaryValue* app_dict = catalogs_[0].get(); |
| 191 // Flip placeholders in message of n1 tree. | 191 // Flip placeholders in message of n1 tree. |
| 192 app_dict->SetString(L"n1.message", "message1 $b$ $a$"); | 192 app_dict->SetString("n1.message", "message1 $b$ $a$"); |
| 193 // Remove one message from app dict. | 193 // Remove one message from app dict. |
| 194 app_dict->Remove(L"n2", NULL); | 194 app_dict->Remove("n2", NULL); |
| 195 // Replace n3 with N3. | 195 // Replace n3 with N3. |
| 196 app_dict->Remove(L"n3", NULL); | 196 app_dict->Remove("n3", NULL); |
| 197 CreateMessageTree(L"N3", "message3_app_dict", false, app_dict); | 197 CreateMessageTree("N3", "message3_app_dict", false, app_dict); |
| 198 | 198 |
| 199 CreateMessageBundle(); | 199 CreateMessageBundle(); |
| 200 | 200 |
| 201 EXPECT_TRUE(handler_.get() != NULL); | 201 EXPECT_TRUE(handler_.get() != NULL); |
| 202 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size()); | 202 EXPECT_EQ(3U + ReservedMessagesCount(), handler_->size()); |
| 203 | 203 |
| 204 EXPECT_EQ("message1 B A", handler_->GetL10nMessage("n1")); | 204 EXPECT_EQ("message1 B A", handler_->GetL10nMessage("n1")); |
| 205 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2")); | 205 EXPECT_EQ("message2 C", handler_->GetL10nMessage("n2")); |
| 206 EXPECT_EQ("message3_app_dict", handler_->GetL10nMessage("n3")); | 206 EXPECT_EQ("message3_app_dict", handler_->GetL10nMessage("n3")); |
| 207 CheckReservedMessages(handler_.get()); | 207 CheckReservedMessages(handler_.get()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 catalogs_[0].reset(CreateBadDictionary(MESSAGE_PLACEHOLDER_DOESNT_MATCH)); | 251 catalogs_[0].reset(CreateBadDictionary(MESSAGE_PLACEHOLDER_DOESNT_MATCH)); |
| 252 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); | 252 handler_.reset(ExtensionMessageBundle::Create(catalogs_, &error)); |
| 253 EXPECT_TRUE(handler_.get() == NULL); | 253 EXPECT_TRUE(handler_.get() == NULL); |
| 254 EXPECT_EQ("Variable $a$ used but not defined.", error); | 254 EXPECT_EQ("Variable $a$ used but not defined.", error); |
| 255 } | 255 } |
| 256 | 256 |
| 257 TEST_F(ExtensionMessageBundleTest, ReservedMessagesOverrideDeveloperMessages) { | 257 TEST_F(ExtensionMessageBundleTest, ReservedMessagesOverrideDeveloperMessages) { |
| 258 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); | 258 catalogs_.push_back(linked_ptr<DictionaryValue>(CreateGoodDictionary())); |
| 259 | 259 |
| 260 DictionaryValue* dict = catalogs_[0].get(); | 260 DictionaryValue* dict = catalogs_[0].get(); |
| 261 CreateMessageTree( | 261 CreateMessageTree(ExtensionMessageBundle::kUILocaleKey, "x", false, dict); |
| 262 ASCIIToWide(ExtensionMessageBundle::kUILocaleKey), "x", false, dict); | |
| 263 | 262 |
| 264 std::string error = CreateMessageBundle(); | 263 std::string error = CreateMessageBundle(); |
| 265 | 264 |
| 266 EXPECT_TRUE(handler_.get() == NULL); | 265 EXPECT_TRUE(handler_.get() == NULL); |
| 267 std::string expected_error = ExtensionErrorUtils::FormatErrorMessage( | 266 std::string expected_error = ExtensionErrorUtils::FormatErrorMessage( |
| 268 errors::kReservedMessageFound, ExtensionMessageBundle::kUILocaleKey); | 267 errors::kReservedMessageFound, ExtensionMessageBundle::kUILocaleKey); |
| 269 EXPECT_EQ(expected_error, error); | 268 EXPECT_EQ(expected_error, error); |
| 270 } | 269 } |
| 271 | 270 |
| 272 TEST_F(ExtensionMessageBundleTest, AppendReservedMessagesForLTR) { | 271 TEST_F(ExtensionMessageBundleTest, AppendReservedMessagesForLTR) { |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 // Store a map for given id. | 416 // Store a map for given id. |
| 418 L10nMessagesMap messages; | 417 L10nMessagesMap messages; |
| 419 messages.insert(std::make_pair("message_name", "message_value")); | 418 messages.insert(std::make_pair("message_name", "message_value")); |
| 420 (*GetExtensionToL10nMessagesMap())[extension_id] = messages; | 419 (*GetExtensionToL10nMessagesMap())[extension_id] = messages; |
| 421 | 420 |
| 422 L10nMessagesMap* map = GetL10nMessagesMap(extension_id); | 421 L10nMessagesMap* map = GetL10nMessagesMap(extension_id); |
| 423 ASSERT_TRUE(NULL != map); | 422 ASSERT_TRUE(NULL != map); |
| 424 EXPECT_EQ(1U, map->size()); | 423 EXPECT_EQ(1U, map->size()); |
| 425 EXPECT_EQ("message_value", (*map)["message_name"]); | 424 EXPECT_EQ("message_value", (*map)["message_name"]); |
| 426 } | 425 } |
| OLD | NEW |