Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/command_line.h" | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/extensions/extension_service.h" | |
| 15 #include "chrome/browser/extensions/test_extension_system.h" | |
| 16 #include "chrome/browser/extensions/zipfile_installer.h" | |
| 17 #include "chrome/common/chrome_paths.h" | |
| 18 #include "chrome/test/base/testing_profile.h" | |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | |
| 20 #include "content/public/test/test_utils.h" | |
| 21 #include "extensions/browser/extension_registry.h" | |
| 22 #include "extensions/browser/extension_registry_observer.h" | |
| 23 #include "extensions/common/constants.h" | |
| 24 #include "extensions/common/extension.h" | |
| 25 #include "testing/gtest/include/gtest/gtest.h" | |
| 26 | |
| 27 namespace extensions { | |
| 28 | |
| 29 struct MockExtensionRegistryObserver | |
| 30 : public extensions::ExtensionRegistryObserver { | |
| 31 void WaitForInstall() { | |
| 32 scoped_refptr<content::MessageLoopRunner> runner = | |
| 33 new content::MessageLoopRunner; | |
| 34 quit_closure_ = runner->QuitClosure(); | |
| 35 runner->Run(); | |
| 36 } | |
| 37 | |
| 38 virtual void OnExtensionWillBeInstalled( | |
| 39 content::BrowserContext* browser_context, | |
| 40 const Extension* extension, | |
| 41 bool is_update, | |
| 42 bool from_ephemeral, | |
| 43 const std::string& old_name) OVERRIDE { | |
| 44 last_extension_installed = extension->id(); | |
| 45 quit_closure_.Run(); | |
| 46 } | |
| 47 | |
| 48 std::string last_extension_installed; | |
| 49 base::Closure quit_closure_; | |
| 50 }; | |
|
asargent_no_longer_on_chrome
2014/08/07 17:28:25
It might be helpful to put this class into an anon
elijahtaylor1
2014/08/07 22:41:25
Done.
| |
| 51 | |
| 52 class ZipFileInstallerTest : public testing::Test { | |
| 53 public: | |
| 54 virtual void SetUp() { | |
| 55 browser_threads_.reset(new content::TestBrowserThreadBundle( | |
| 56 content::TestBrowserThreadBundle::IO_MAINLOOP)); | |
| 57 in_process_utility_thread_helper_.reset( | |
| 58 new content::InProcessUtilityThreadHelper); | |
| 59 | |
| 60 // Create profile for extension service. | |
| 61 profile_.reset(new TestingProfile()); | |
| 62 TestExtensionSystem* system = | |
| 63 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_.get())); | |
| 64 extension_service_ = system->CreateExtensionService( | |
| 65 base::CommandLine::ForCurrentProcess(), base::FilePath(), false); | |
| 66 extensions::ExtensionRegistry* registry( | |
| 67 extensions::ExtensionRegistry::Get(profile_.get())); | |
| 68 registry->AddObserver(&observer_); | |
| 69 } | |
| 70 | |
| 71 virtual void TearDown() { | |
| 72 // Need to destruct ZipFileInstaller before the message loop since | |
| 73 // it posts a task to it. | |
| 74 zipfile_installer_ = NULL; | |
| 75 extensions::ExtensionRegistry* registry( | |
| 76 extensions::ExtensionRegistry::Get(profile_.get())); | |
| 77 registry->RemoveObserver(&observer_); | |
| 78 profile_.reset(); | |
| 79 base::RunLoop().RunUntilIdle(); | |
| 80 } | |
| 81 | |
| 82 void SetupInstaller(const std::string& zip_name) { | |
|
asargent_no_longer_on_chrome
2014/08/07 17:28:25
You might consider renaming this to "RunInstaller"
elijahtaylor1
2014/08/07 22:41:25
Done.
| |
| 83 base::FilePath original_path; | |
| 84 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path)); | |
| 85 original_path = original_path.AppendASCII("extensions") | |
| 86 .AppendASCII("zipfile_installer") | |
| 87 .AppendASCII(zip_name); | |
| 88 ASSERT_TRUE(base::PathExists(original_path)) << original_path.value(); | |
| 89 | |
| 90 zipfile_installer_ = ZipFileInstaller::Create(extension_service_); | |
| 91 | |
| 92 base::MessageLoopProxy::current()->PostTask( | |
| 93 FROM_HERE, | |
| 94 base::Bind(&ZipFileInstaller::LoadFromZipFile, | |
| 95 zipfile_installer_.get(), | |
| 96 original_path)); | |
| 97 observer_.WaitForInstall(); | |
| 98 } | |
| 99 | |
| 100 protected: | |
| 101 scoped_refptr<ZipFileInstaller> zipfile_installer_; | |
| 102 | |
| 103 scoped_ptr<TestingProfile> profile_; | |
| 104 ExtensionService* extension_service_; | |
| 105 | |
| 106 scoped_ptr<content::TestBrowserThreadBundle> browser_threads_; | |
| 107 scoped_ptr<content::InProcessUtilityThreadHelper> | |
| 108 in_process_utility_thread_helper_; | |
| 109 MockExtensionRegistryObserver observer_; | |
| 110 }; | |
| 111 | |
| 112 TEST_F(ZipFileInstallerTest, GoodZip) { | |
| 113 SetupInstaller("good.zip"); | |
| 114 } | |
| 115 | |
| 116 TEST_F(ZipFileInstallerTest, ZipWithPublicKey) { | |
| 117 SetupInstaller("public_key.zip"); | |
| 118 const char kIdForPublicKey[] = "ikppjpenhoddphklkpdfdfdabbakkpal"; | |
| 119 EXPECT_EQ(observer_.last_extension_installed, kIdForPublicKey); | |
| 120 } | |
| 121 | |
| 122 } // namespace extensions | |
| OLD | NEW |