Chromium Code Reviews| Index: chrome/browser/extensions/external_extension_provider_impl_unittest.cc |
| =================================================================== |
| --- chrome/browser/extensions/external_extension_provider_impl_unittest.cc (revision 0) |
| +++ chrome/browser/extensions/external_extension_provider_impl_unittest.cc (revision 0) |
| @@ -0,0 +1,191 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| +#include "base/message_loop.h" |
| +#include "base/values.h" |
| +#include "base/version.h" |
| +#include "chrome/browser/extensions/crx_installer.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/extensions/external_extension_provider_interface.h" |
| +#include "chrome/browser/extensions/external_extension_provider_impl.h" |
| +#include "chrome/browser/extensions/external_policy_extension_loader.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/test/base/testing_pref_service.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "content/browser/browser_thread.h" |
| +#include "content/common/notification_details.h" |
| +#include "content/common/notification_service.h" |
| +#include "content/common/notification_source.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +class MockExternalExtensionLoader : public ExternalExtensionLoader { |
| + public: |
| + MockExternalExtensionLoader() {} |
| + |
| + void SetPrefs(const base::DictionaryValue& prefs) { |
| + prefs_.reset(prefs.DeepCopy()); |
| + } |
| + |
| + private: |
| + virtual void StartLoading() { |
| + // No actual loading to be done, simple use the prefs given. |
|
Finnur
2011/10/19 10:01:29
nit: simple -> simply?
Roger Tawa OOO till Jul 10th
2011/10/19 20:48:25
Done.
|
| + LoadFinished(); |
| + } |
| + |
| + virtual const FilePath GetBaseCrxFilePath() { |
| + return FilePath(FILE_PATH_LITERAL("/tmp")); |
| + } |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockExternalExtensionLoader); |
| +}; |
| + |
| +class MockExternalPolicyExtensionProviderVisitor |
| + : public ExternalExtensionProviderInterface::VisitorInterface { |
| + public: |
| + MockExternalPolicyExtensionProviderVisitor() { |
| + profile_.reset(new TestingProfile); |
| + profile_->CreateExtensionService(CommandLine::ForCurrentProcess(), |
| + FilePath(FILE_PATH_LITERAL("/tmp")), false); |
| + |
| + // The provider owns the loader. |
| + loader_ = new MockExternalExtensionLoader(); |
| + provider_.reset(new DefaultAppsProvider(this, loader_, profile_.get())); |
| + } |
| + |
| + virtual void OnExternalExtensionFileFound(const std::string& id, |
| + const Version* version, |
| + const FilePath& path, |
| + Extension::Location unused) { |
| + } |
| + |
| + virtual void OnExternalExtensionUpdateUrlFound( |
| + const std::string& id, const GURL& update_url, |
| + Extension::Location location) { |
| + ADD_FAILURE() << "There should be no external extensions from URLs."; |
| + } |
| + |
| + virtual void OnExternalProviderReady() { |
| + EXPECT_TRUE(provider_->IsReady()); |
| + } |
| + |
| + TestingProfile* profile() const { return profile_.get(); } |
| + DefaultAppsProvider* provider() const { return provider_.get(); } |
| + MockExternalExtensionLoader* loader() const { return loader_; } |
| + |
| + private: |
| + scoped_ptr<TestingProfile> profile_; |
| + scoped_ptr<DefaultAppsProvider> provider_; |
| + MockExternalExtensionLoader* loader_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyExtensionProviderVisitor); |
| +}; |
| + |
| +class DefaultAppsProviderTest : public testing::Test { |
| + public: |
| + DefaultAppsProviderTest() |
| + : loop_(MessageLoop::TYPE_IO), |
| + ui_thread_(BrowserThread::UI, &loop_) { |
| + } |
| + |
| + virtual ~DefaultAppsProviderTest() {} |
| + |
| + MockExternalPolicyExtensionProviderVisitor* visitor() { |
| + return &visitor_; |
| + } |
| + |
| + int GetProviderState(); |
| + |
| + void AddExtensionToDictionary(base::DictionaryValue* prefs, |
| + const char* id, |
| + const char* name, |
| + const char* version); |
| + |
| + private: |
| + // We need these to satisfy BrowserThread::CurrentlyOn(BrowserThread::UI) |
| + // checks in ExternalExtensionProviderImpl. |
| + MessageLoop loop_; |
| + BrowserThread ui_thread_; |
| + MockExternalPolicyExtensionProviderVisitor visitor_; |
| +}; |
| + |
| +int DefaultAppsProviderTest::GetProviderState() { |
| + return visitor()->profile()->GetPrefs()->GetInteger( |
| + prefs::kDefaultAppsInstallState); |
| +} |
| + |
| +void DefaultAppsProviderTest::AddExtensionToDictionary( |
| + base::DictionaryValue* prefs, |
| + const char* id, |
| + const char* name, |
| + const char* version) { |
| + scoped_ptr<base::DictionaryValue> extension(new base::DictionaryValue); |
| + |
| + extension->SetString("external_crx", name); |
| + extension->SetString("external_version", version); |
| + |
| + prefs->Set(id, extension.release()); |
| +} |
| + |
| +} // anonymous namespace |
| + |
| +TEST_F(DefaultAppsProviderTest, NoExtensions) { |
| + // Setup the loader to provide nothing. |
| + base::DictionaryValue prefs; |
| + visitor()->loader()->SetPrefs(prefs); |
| + |
| + visitor()->provider()->VisitRegisteredExtension(); |
| + |
| + EXPECT_EQ(0u, visitor()->provider()->invalid_extensions().size()); |
| + EXPECT_EQ(0u, visitor()->provider()->install_error_extensions().size()); |
| + EXPECT_EQ(DefaultAppsProvider::kInstallDone, GetProviderState()); |
| +} |
| + |
| +TEST_F(DefaultAppsProviderTest, InvalidExtension) { |
| + // Setup the loader to one invalid extension. |
| + base::DictionaryValue prefs; |
| + prefs.SetInteger("bad_id", 0); |
| + visitor()->loader()->SetPrefs(prefs); |
| + |
| + // Run the provider. |
| + visitor()->provider()->VisitRegisteredExtension(); |
| + |
| + // Make sure state is "done". |
| + EXPECT_EQ(1u, visitor()->provider()->invalid_extensions().size()); |
| + EXPECT_EQ(0u, visitor()->provider()->install_error_extensions().size()); |
| + EXPECT_EQ(DefaultAppsProvider::kInstallDone, GetProviderState()); |
| +} |
| + |
| +TEST_F(DefaultAppsProviderTest, InstallErrorExtension) { |
| + // Setup the loader to one valid extension that fails to load. |
| + base::DictionaryValue prefs; |
| + AddExtensionToDictionary(&prefs, "blpcfgokakmgnkcojhhkbfbldkacnbeo", |
| + "dummy.crx", "1.0.0.0"); |
| + visitor()->loader()->SetPrefs(prefs); |
| + |
| + // Run the provider. |
| + visitor()->provider()->VisitRegisteredExtension(); |
| + |
| + EXPECT_EQ(0u, visitor()->provider()->invalid_extensions().size()); |
| + EXPECT_EQ(0u, visitor()->provider()->install_error_extensions().size()); |
| + EXPECT_EQ(DefaultAppsProvider::kInstalling, GetProviderState()); |
| + |
| + // Pretend the load failed. |
| + ExtensionService* service = visitor()->profile()->GetExtensionService(); |
| + scoped_refptr<CrxInstaller> crx_installer(service->MakeCrxInstaller(NULL)); |
| + crx_installer->set_expected_id("blpcfgokakmgnkcojhhkbfbldkacnbeo"); |
| + visitor()->provider()->Observe(chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| + Source<CrxInstaller>(crx_installer.get()), |
| + NotificationService::NoDetails()); |
| + |
| + EXPECT_EQ(0u, visitor()->provider()->invalid_extensions().size()); |
| + EXPECT_EQ(1u, visitor()->provider()->install_error_extensions().size()); |
| + EXPECT_EQ(DefaultAppsProvider::kInstallDone, GetProviderState()); |
| +} |
| Property changes on: chrome\browser\extensions\external_extension_provider_impl_unittest.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |