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/browser/extensions/extension_file_util.h" | 5 #include "chrome/browser/extensions/extension_file_util.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 extension_file_util::CompareToInstalledVersion( | 110 extension_file_util::CompareToInstalledVersion( |
111 temp.path(), kBadId, kMissingVersion, "1.0.0", &version_dir)); | 111 temp.path(), kBadId, kMissingVersion, "1.0.0", &version_dir)); |
112 } | 112 } |
113 | 113 |
114 // Creates minimal manifest, with or without default_locale section. | 114 // Creates minimal manifest, with or without default_locale section. |
115 bool CreateMinimalManifest(const std::string& locale, | 115 bool CreateMinimalManifest(const std::string& locale, |
116 const FilePath& manifest_path) { | 116 const FilePath& manifest_path) { |
117 DictionaryValue manifest; | 117 DictionaryValue manifest; |
118 | 118 |
119 manifest.SetString(keys::kVersion, "1.0.0.0"); | 119 manifest.SetString(keys::kVersion, "1.0.0.0"); |
120 manifest.SetString(keys::kName, "my extension"); | 120 manifest.SetString(keys::kName, "__MSG_name__"); |
| 121 manifest.SetString(keys::kDescription, "__MSG_description__"); |
121 if (!locale.empty()) { | 122 if (!locale.empty()) { |
122 manifest.SetString(keys::kDefaultLocale, locale); | 123 manifest.SetString(keys::kDefaultLocale, locale); |
123 } | 124 } |
124 | 125 |
125 JSONFileValueSerializer serializer(manifest_path); | 126 JSONFileValueSerializer serializer(manifest_path); |
126 return serializer.Serialize(manifest); | 127 return serializer.Serialize(manifest); |
127 } | 128 } |
128 | 129 |
129 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { | 130 TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { |
130 ScopedTempDir temp; | 131 ScopedTempDir temp; |
131 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 132 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
132 ASSERT_TRUE(CreateMinimalManifest( | 133 ASSERT_TRUE(CreateMinimalManifest( |
133 "en_US", temp.path().AppendASCII(Extension::kManifestFilename))); | 134 "en_US", temp.path().AppendASCII(Extension::kManifestFilename))); |
134 | 135 |
135 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); | 136 FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); |
136 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 137 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
137 | 138 |
138 FilePath locale_1 = src_path.AppendASCII("sr"); | 139 FilePath locale_1 = src_path.AppendASCII("sr"); |
139 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); | 140 ASSERT_TRUE(file_util::CreateDirectory(locale_1)); |
140 | 141 |
141 std::string data = "foobar"; | 142 std::string data = "{ \"name\": { \"message\": \"foobar\" }," |
| 143 "\"description\": { \"message\": \"nice going\" } }"; |
142 ASSERT_TRUE( | 144 ASSERT_TRUE( |
143 file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename), | 145 file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename), |
144 data.c_str(), data.length())); | 146 data.c_str(), data.length())); |
145 | 147 |
146 FilePath locale_2 = src_path.AppendASCII("en_US"); | 148 FilePath locale_2 = src_path.AppendASCII("en_US"); |
147 ASSERT_TRUE(file_util::CreateDirectory(locale_2)); | 149 ASSERT_TRUE(file_util::CreateDirectory(locale_2)); |
148 | 150 |
149 ASSERT_TRUE( | 151 ASSERT_TRUE( |
150 file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename), | 152 file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename), |
151 data.c_str(), data.length())); | 153 data.c_str(), data.length())); |
152 | 154 |
153 std::string error; | 155 std::string error; |
154 scoped_ptr<Extension> extension( | 156 scoped_ptr<Extension> extension( |
155 extension_file_util::LoadExtension(temp.path(), false, &error)); | 157 extension_file_util::LoadExtension(temp.path(), false, &error)); |
156 ASSERT_FALSE(extension == NULL); | 158 ASSERT_FALSE(extension == NULL); |
157 EXPECT_EQ(static_cast<unsigned int>(2), | 159 EXPECT_EQ(2U, extension->supported_locales().size()); |
158 extension->supported_locales().size()); | |
159 EXPECT_EQ("en-US", extension->default_locale()); | 160 EXPECT_EQ("en-US", extension->default_locale()); |
| 161 EXPECT_EQ("foobar", extension->name()); |
| 162 EXPECT_EQ("nice going", extension->description()); |
160 } | 163 } |
161 | 164 |
162 TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { | 165 TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { |
163 ScopedTempDir temp; | 166 ScopedTempDir temp; |
164 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 167 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
165 ASSERT_TRUE(CreateMinimalManifest( | 168 ASSERT_TRUE(CreateMinimalManifest( |
166 "", temp.path().AppendASCII(Extension::kManifestFilename))); | 169 "", temp.path().AppendASCII(Extension::kManifestFilename))); |
167 | 170 |
168 std::string error; | 171 std::string error; |
169 scoped_ptr<Extension> extension( | 172 scoped_ptr<Extension> extension( |
170 extension_file_util::LoadExtension(temp.path(), false, &error)); | 173 extension_file_util::LoadExtension(temp.path(), false, &error)); |
171 ASSERT_FALSE(extension == NULL); | 174 ASSERT_FALSE(extension == NULL); |
172 EXPECT_TRUE(extension->supported_locales().empty()); | 175 EXPECT_TRUE(extension->supported_locales().empty()); |
173 EXPECT_TRUE(extension->default_locale().empty()); | 176 EXPECT_TRUE(extension->default_locale().empty()); |
174 } | 177 } |
175 | 178 |
176 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { | 179 TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { |
177 ScopedTempDir temp; | 180 ScopedTempDir temp; |
178 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 181 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
179 | 182 |
180 FilePath src_path = temp.path().AppendASCII("some_dir"); | 183 FilePath src_path = temp.path().AppendASCII("some_dir"); |
181 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 184 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
182 | 185 |
183 std::string data = "foobar"; | 186 std::string data = "{ \"name\": { \"message\": \"foobar\" } }"; |
184 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), | 187 ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), |
185 data.c_str(), data.length())); | 188 data.c_str(), data.length())); |
186 std::string error; | 189 std::string error; |
187 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 190 EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
188 &error)); | 191 &error)); |
189 } | 192 } |
190 | 193 |
191 TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) { | 194 TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) { |
192 ScopedTempDir temp; | 195 ScopedTempDir temp; |
193 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 196 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
(...skipping 17 matching lines...) Expand all Loading... |
211 ASSERT_TRUE(file_util::CreateDirectory(src_path)); | 214 ASSERT_TRUE(file_util::CreateDirectory(src_path)); |
212 | 215 |
213 std::string error; | 216 std::string error; |
214 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), | 217 EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), |
215 &error)); | 218 &error)); |
216 } | 219 } |
217 | 220 |
218 // TODO(aa): More tests as motivation allows. Maybe steal some from | 221 // TODO(aa): More tests as motivation allows. Maybe steal some from |
219 // ExtensionsService? Many of them could probably be tested here without the | 222 // ExtensionsService? Many of them could probably be tested here without the |
220 // MessageLoop shenanigans. | 223 // MessageLoop shenanigans. |
OLD | NEW |