diff options
Diffstat (limited to 'chrome/browser/extensions/extensions_service_unittest.cc')
-rw-r--r-- | chrome/browser/extensions/extensions_service_unittest.cc | 98 |
1 files changed, 92 insertions, 6 deletions
diff --git a/chrome/browser/extensions/extensions_service_unittest.cc b/chrome/browser/extensions/extensions_service_unittest.cc index bfc5664..03b0b24 100644 --- a/chrome/browser/extensions/extensions_service_unittest.cc +++ b/chrome/browser/extensions/extensions_service_unittest.cc @@ -32,6 +32,12 @@ struct ExtensionsOrder { class ExtensionsServiceTestFrontend : public ExtensionsServiceFrontendInterface { public: + + ExtensionsServiceTestFrontend() { + file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("ext_test"), + &install_dir_); + } + ~ExtensionsServiceTestFrontend() { for (ExtensionList::iterator iter = extensions_.begin(); iter != extensions_.end(); ++iter) { @@ -47,6 +53,14 @@ class ExtensionsServiceTestFrontend return &extensions_; } + std::vector<FilePath>* installed() { + return &installed_; + } + + FilePath install_dir() { + return install_dir_; + } + // ExtensionsServiceFrontendInterface virtual MessageLoop* GetMessageLoop() { return &message_loop_; @@ -65,10 +79,43 @@ class ExtensionsServiceTestFrontend std::stable_sort(extensions_.begin(), extensions_.end(), ExtensionsOrder()); } + virtual void InstallExtension(const FilePath& extension_path) { + } + + virtual void OnExtensionInstallError(const std::string& message) { + errors_.push_back(message); + } + + virtual void OnExtensionInstalled(FilePath path) { + installed_.push_back(path); + } + + void TestInstallExtension(const FilePath& path, + ExtensionsServiceBackend* backend, + bool should_succeed) { + ASSERT_TRUE(file_util::PathExists(path)); + EXPECT_EQ(should_succeed, + backend->InstallExtension(path, install_dir_, + scoped_refptr<ExtensionsServiceFrontendInterface>(this))); + message_loop_.RunAllPending(); + if (should_succeed) { + EXPECT_EQ(1u, installed_.size()); + EXPECT_EQ(0u, errors_.size()); + } else { + EXPECT_EQ(0u, installed_.size()); + EXPECT_EQ(1u, errors_.size()); + } + installed_.clear(); + errors_.clear(); + } + + private: MessageLoop message_loop_; ExtensionList extensions_; std::vector<std::string> errors_; + std::vector<FilePath> installed_; + FilePath install_dir_; }; // make the test a PlatformTest to setup autorelease pools properly on mac @@ -76,10 +123,9 @@ typedef PlatformTest ExtensionsServiceTest; // Test loading extensions from the profile directory. TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectory) { - std::wstring extensions_dir; - ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_dir)); - FilePath extensions_path = FilePath::FromWStringHack(extensions_dir).Append( - FILE_PATH_LITERAL("extensions")); + FilePath extensions_path; + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); + extensions_path = extensions_path.AppendASCII("extensions"); scoped_refptr<ExtensionsServiceBackend> backend(new ExtensionsServiceBackend); scoped_refptr<ExtensionsServiceTestFrontend> frontend( @@ -108,11 +154,11 @@ TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectory) { EXPECT_EQ(2u, scripts[0].matches.size()); EXPECT_EQ("http://*.google.com/*", scripts[0].matches[0]); EXPECT_EQ("https://*.google.com/*", scripts[0].matches[1]); - EXPECT_EQ(extension->path().Append(FILE_PATH_LITERAL("script1.js")).value(), + EXPECT_EQ(extension->path().AppendASCII("script1.js").value(), scripts[0].path.value()); EXPECT_EQ(1u, scripts[1].matches.size()); EXPECT_EQ("http://*.yahoo.com/*", scripts[1].matches[0]); - EXPECT_EQ(extension->path().Append(FILE_PATH_LITERAL("script2.js")).value(), + EXPECT_EQ(extension->path().AppendASCII("script2.js").value(), scripts[1].path.value()); EXPECT_EQ(std::string("com.google.myextension2"), @@ -123,3 +169,43 @@ TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectory) { frontend->extensions()->at(1)->description()); ASSERT_EQ(0u, frontend->extensions()->at(1)->user_scripts().size()); }; + +// Test installing extensions. +TEST_F(ExtensionsServiceTest, InstallExtension) { + FilePath extensions_path; + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); + extensions_path = extensions_path.AppendASCII("extensions"); + + scoped_refptr<ExtensionsServiceBackend> backend(new ExtensionsServiceBackend); + scoped_refptr<ExtensionsServiceTestFrontend> frontend( + new ExtensionsServiceTestFrontend); + + FilePath path = extensions_path.AppendASCII("good.crx"); + + // A simple extension that should install without error. + frontend->TestInstallExtension(path, backend, true); + // TODO(erikkay): verify the contents of the installed extension. + + // Installing the same extension twice should fail. + frontend->TestInstallExtension(path, backend, false); + + // 0-length extension file. + path = extensions_path.AppendASCII("not_an_extension.crx"); + frontend->TestInstallExtension(path, backend, false); + + // Bad magic number. + path = extensions_path.AppendASCII("bad_magic.crx"); + frontend->TestInstallExtension(path, backend, false); + + // Poorly formed JSON. + path = extensions_path.AppendASCII("bad_json.crx"); + frontend->TestInstallExtension(path, backend, false); + + // Incorrect zip hash. + path = extensions_path.AppendASCII("bad_hash.crx"); + frontend->TestInstallExtension(path, backend, false); + + // TODO(erikkay): add more tests for many of the failure cases. + // TODO(erikkay): add tests for upgrade cases. +} + |