diff options
author | cira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-19 20:45:14 +0000 |
---|---|---|
committer | cira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-19 20:45:14 +0000 |
commit | 300cc58db6bc8d78e279dc07c46f15d9702148c6 (patch) | |
tree | 01eb9d8585bc43b01850c226bc4d5f2a3ec00cea /chrome/browser/extensions/extension_file_util_unittest.cc | |
parent | cceaf1fbf3f4963c59a49268217f1e3edfb372ef (diff) | |
download | chromium_src-300cc58db6bc8d78e279dc07c46f15d9702148c6.zip chromium_src-300cc58db6bc8d78e279dc07c46f15d9702148c6.tar.gz chromium_src-300cc58db6bc8d78e279dc07c46f15d9702148c6.tar.bz2 |
This change enables Chrome to load locale information for the extension. It detects default locale, and filters out all locales not supported
by Chrome or with invalid names/missing messages.
It also checks for folders that start with _ and are not in the reserved list.
We don't validate messages file with this CL.
Added support for loading supplied locale information to the extension_file_util, and detecting default locale.
Added new constants to extension class (_locales directory name, messages filename).
Added new error messages to _constants.
Added new unittests.
BUG=12131
TEST=There should be no visible changes, except in case of error when loading extension (e.g. create empty _locales folder and try loading).
Review URL: http://codereview.chromium.org/170015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23739 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_file_util_unittest.cc')
-rw-r--r-- | chrome/browser/extensions/extension_file_util_unittest.cc | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_file_util_unittest.cc b/chrome/browser/extensions/extension_file_util_unittest.cc index 6e9b2dc..73dfb46 100644 --- a/chrome/browser/extensions/extension_file_util_unittest.cc +++ b/chrome/browser/extensions/extension_file_util_unittest.cc @@ -8,8 +8,13 @@ #include "base/scoped_temp_dir.h" #include "base/path_service.h" #include "chrome/common/chrome_paths.h" +#include "chrome/common/extensions/extension.h" +#include "chrome/common/extensions/extension_constants.h" +#include "chrome/common/json_value_serializer.h" #include "testing/gtest/include/gtest/gtest.h" +namespace keys = extension_manifest_keys; + TEST(ExtensionFileUtil, MoveDirSafely) { // Create a test directory structure with some data in it. ScopedTempDir temp; @@ -162,6 +167,110 @@ TEST(ExtensionFileUtil, CompareToInstalledVersion) { ASSERT_EQ("", version_out); } +// Creates minimal manifest, with or without default_locale section. +bool CreateMinimalManifest(const std::string& locale, + const FilePath& manifest_path) { + DictionaryValue manifest; + + manifest.SetString(keys::kVersion, "1.0.0.0"); + manifest.SetString(keys::kName, "my extension"); + if (!locale.empty()) { + manifest.SetString(keys::kDefaultLocale, locale); + } + + JSONFileValueSerializer serializer(manifest_path); + return serializer.Serialize(manifest); +} + +TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + ASSERT_TRUE(CreateMinimalManifest( + "en_US", temp.path().AppendASCII(Extension::kManifestFilename))); + + FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + + FilePath locale_1 = src_path.AppendASCII("sr"); + ASSERT_TRUE(file_util::CreateDirectory(locale_1)); + + std::string data = "foobar"; + ASSERT_TRUE( + file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename), + data.c_str(), data.length())); + + FilePath locale_2 = src_path.AppendASCII("en_US"); + ASSERT_TRUE(file_util::CreateDirectory(locale_2)); + + ASSERT_TRUE( + file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename), + data.c_str(), data.length())); + + std::string error; + scoped_ptr<Extension> extension( + extension_file_util::LoadExtension(temp.path(), false, &error)); + ASSERT_FALSE(extension == NULL); + EXPECT_EQ(static_cast<unsigned int>(2), + extension->supported_locales().size()); + EXPECT_EQ("en-US", extension->default_locale()); +} + +TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + ASSERT_TRUE(CreateMinimalManifest( + "", temp.path().AppendASCII(Extension::kManifestFilename))); + + std::string error; + scoped_ptr<Extension> extension( + extension_file_util::LoadExtension(temp.path(), false, &error)); + ASSERT_FALSE(extension == NULL); + EXPECT_TRUE(extension->supported_locales().empty()); + EXPECT_TRUE(extension->default_locale().empty()); +} + +TEST(ExtensionFileUtil, CheckIllegalFilenamesNoUnderscores) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + + FilePath src_path = temp.path().AppendASCII("some_dir"); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + + std::string data = "foobar"; + ASSERT_TRUE(file_util::WriteFile(src_path.AppendASCII("some_file.txt"), + data.c_str(), data.length())); + std::string error; + EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), + &error)); +} + +TEST(ExtensionFileUtil, CheckIllegalFilenamesOnlyReserved) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + + FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + + std::string error; + EXPECT_TRUE(extension_file_util::CheckForIllegalFilenames(temp.path(), + &error)); +} + +TEST(ExtensionFileUtil, CheckIllegalFilenamesReservedAndIllegal) { + ScopedTempDir temp; + ASSERT_TRUE(temp.CreateUniqueTempDir()); + + FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + + src_path = temp.path().AppendASCII("_some_dir"); + ASSERT_TRUE(file_util::CreateDirectory(src_path)); + + std::string error; + EXPECT_FALSE(extension_file_util::CheckForIllegalFilenames(temp.path(), + &error)); +} + // TODO(aa): More tests as motivation allows. Maybe steal some from // ExtensionsService? Many of them could probably be tested here without the // MessageLoop shenanigans. |