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/browser/keyword_provider.h"
17 #include "components/omnibox/browser/mock_autocomplete_provider_client.h"
18 #include "components/search_engines/template_url_service.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_registry_observer.h"
21 #include "extensions/common/extension.h"
23 namespace extensions
{
27 class ScopedExtensionLoadObserver
: public ExtensionRegistryObserver
{
29 ScopedExtensionLoadObserver(ExtensionRegistry
* registry
,
30 const base::Closure
& quit_closure
);
31 ~ScopedExtensionLoadObserver() override
;
34 void OnExtensionInstalled(content::BrowserContext
* browser_context
,
35 const Extension
* extension
,
36 bool is_update
) override
;
38 ExtensionRegistry
* registry_
;
39 base::Closure quit_closure_
;
41 DISALLOW_COPY_AND_ASSIGN(ScopedExtensionLoadObserver
);
44 ScopedExtensionLoadObserver::ScopedExtensionLoadObserver(
45 ExtensionRegistry
* registry
,
46 const base::Closure
& quit_closure
)
47 : registry_(registry
),
48 quit_closure_(quit_closure
) {
49 registry_
->AddObserver(this);
52 ScopedExtensionLoadObserver::~ScopedExtensionLoadObserver() {
53 registry_
->RemoveObserver(this);
56 void ScopedExtensionLoadObserver::OnExtensionInstalled(
57 content::BrowserContext
* browser_context
,
58 const Extension
* extension
,
63 class KeywordExtensionsDelegateImplTest
: public ExtensionServiceTestBase
{
65 KeywordExtensionsDelegateImplTest() {}
66 ~KeywordExtensionsDelegateImplTest() override
{}
69 void SetUp() override
;
71 void RunTest(bool incognito
);
74 DISALLOW_COPY_AND_ASSIGN(KeywordExtensionsDelegateImplTest
);
77 void KeywordExtensionsDelegateImplTest::SetUp() {
78 ExtensionServiceTestBase::SetUp();
79 InitializeExtensionService(CreateDefaultInitParams());
82 void KeywordExtensionsDelegateImplTest::RunTest(bool incognito
) {
83 scoped_ptr
<TemplateURLService
> empty_model(new TemplateURLService(NULL
, 0));
84 MockAutocompleteProviderClient client
;
85 client
.set_template_url_service(empty_model
.Pass());
86 scoped_refptr
<KeywordProvider
> keyword_provider
=
87 new KeywordProvider(&client
, nullptr);
92 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &path
));
93 path
= path
.AppendASCII("extensions").AppendASCII("good_unpacked");
95 base::RunLoop run_loop
;
96 ScopedExtensionLoadObserver
load_observer(registry(),
97 run_loop
.QuitClosure());
99 scoped_refptr
<UnpackedInstaller
> installer(
100 UnpackedInstaller::Create(service()));
101 installer
->Load(path
);
106 ASSERT_EQ(1U, registry()->enabled_extensions().size());
107 scoped_refptr
<const Extension
> extension
=
108 *(registry()->enabled_extensions().begin());
109 ASSERT_FALSE(util::IsIncognitoEnabled(extension
->id(), profile()));
111 Profile
* profile_to_use
= incognito
?
112 profile()->GetOffTheRecordProfile() : profile();
113 KeywordExtensionsDelegateImpl
delegate_impl(profile_to_use
,
114 keyword_provider
.get());
115 KeywordExtensionsDelegate
* delegate
= &delegate_impl
;
116 EXPECT_NE(incognito
, delegate
->IsEnabledExtension(extension
->id()));
118 // Enable the extension in incognito mode, which requires a reload.
120 base::RunLoop run_loop
;
121 ScopedExtensionLoadObserver
load_observer(registry(),
122 run_loop
.QuitClosure());
124 util::SetIsIncognitoEnabled(extension
->id(), profile(), true);
129 ASSERT_EQ(1U, registry()->enabled_extensions().size());
130 extension
= *(registry()->enabled_extensions().begin());
131 ASSERT_TRUE(util::IsIncognitoEnabled(extension
->id(), profile()));
132 EXPECT_TRUE(delegate
->IsEnabledExtension(extension
->id()));
135 TEST_F(KeywordExtensionsDelegateImplTest
, IsEnabledExtension
) {
139 TEST_F(KeywordExtensionsDelegateImplTest
, IsEnabledExtensionIncognito
) {
145 } // namespace extensions