1 // Copyright 2014 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.
5 #include "chrome/browser/autocomplete/keyword_extensions_delegate_impl.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/path_service.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_service_test_base.h"
11 #include "chrome/browser/extensions/extension_util.h"
12 #include "chrome/browser/extensions/test_extension_system.h"
13 #include "chrome/browser/extensions/unpacked_installer.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "components/omnibox/keyword_provider.h"
17 #include "components/search_engines/template_url_service.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/browser/extension_registry_observer.h"
20 #include "extensions/common/extension.h"
22 namespace extensions {
26 class ScopedExtensionLoadObserver : public ExtensionRegistryObserver {
28 ScopedExtensionLoadObserver(ExtensionRegistry* registry,
29 const base::Closure& quit_closure);
30 virtual ~ScopedExtensionLoadObserver();
33 virtual void OnExtensionInstalled(content::BrowserContext* browser_context,
34 const Extension* extension,
35 bool is_update) OVERRIDE;
37 ExtensionRegistry* registry_;
38 base::Closure quit_closure_;
40 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionLoadObserver);
43 ScopedExtensionLoadObserver::ScopedExtensionLoadObserver(
44 ExtensionRegistry* registry,
45 const base::Closure& quit_closure)
46 : registry_(registry),
47 quit_closure_(quit_closure) {
48 registry_->AddObserver(this);
51 ScopedExtensionLoadObserver::~ScopedExtensionLoadObserver() {
52 registry_->RemoveObserver(this);
55 void ScopedExtensionLoadObserver::OnExtensionInstalled(
56 content::BrowserContext* browser_context,
57 const Extension* extension,
62 class KeywordExtensionsDelegateImplTest : public ExtensionServiceTestBase {
64 KeywordExtensionsDelegateImplTest() {}
65 virtual ~KeywordExtensionsDelegateImplTest() {}
68 virtual void SetUp() OVERRIDE;
70 void RunTest(bool incognito);
73 DISALLOW_COPY_AND_ASSIGN(KeywordExtensionsDelegateImplTest);
76 void KeywordExtensionsDelegateImplTest::SetUp() {
77 ExtensionServiceTestBase::SetUp();
78 InitializeExtensionService(CreateDefaultInitParams());
79 InitializeProcessManager();
82 void KeywordExtensionsDelegateImplTest::RunTest(bool incognito) {
83 TemplateURLService empty_model(NULL, 0);
84 scoped_refptr<KeywordProvider> keyword_provider =
85 new KeywordProvider(NULL, &empty_model);
90 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
91 path = path.AppendASCII("extensions").AppendASCII("good_unpacked");
93 base::RunLoop run_loop;
94 ScopedExtensionLoadObserver load_observer(registry(),
95 run_loop.QuitClosure());
97 scoped_refptr<UnpackedInstaller> installer(
98 UnpackedInstaller::Create(service()));
99 installer->Load(path);
104 ASSERT_EQ(1U, registry()->enabled_extensions().size());
105 scoped_refptr<const Extension> extension =
106 *(registry()->enabled_extensions().begin());
107 ASSERT_FALSE(util::IsIncognitoEnabled(extension->id(), profile()));
109 Profile* profile_to_use = incognito ?
110 profile()->GetOffTheRecordProfile() : profile();
111 KeywordExtensionsDelegateImpl delegate_impl(profile_to_use,
112 keyword_provider.get());
113 KeywordExtensionsDelegate* delegate = &delegate_impl;
114 EXPECT_NE(incognito, delegate->IsEnabledExtension(extension->id()));
116 // Enable the extension in incognito mode, which requires a reload.
118 base::RunLoop run_loop;
119 ScopedExtensionLoadObserver load_observer(registry(),
120 run_loop.QuitClosure());
122 util::SetIsIncognitoEnabled(extension->id(), profile(), true);
127 ASSERT_EQ(1U, registry()->enabled_extensions().size());
128 extension = *(registry()->enabled_extensions().begin());
129 ASSERT_TRUE(util::IsIncognitoEnabled(extension->id(), profile()));
130 EXPECT_TRUE(delegate->IsEnabledExtension(extension->id()));
133 TEST_F(KeywordExtensionsDelegateImplTest, IsEnabledExtension) {
137 TEST_F(KeywordExtensionsDelegateImplTest, IsEnabledExtensionIncognito) {
143 } // namespace extensions