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 ~ScopedExtensionLoadObserver() override
;
33 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 ~KeywordExtensionsDelegateImplTest() override
{}
68 void SetUp() override
;
70 void RunTest(bool incognito
);
73 DISALLOW_COPY_AND_ASSIGN(KeywordExtensionsDelegateImplTest
);
76 void KeywordExtensionsDelegateImplTest::SetUp() {
77 ExtensionServiceTestBase::SetUp();
78 InitializeExtensionService(CreateDefaultInitParams());
81 void KeywordExtensionsDelegateImplTest::RunTest(bool incognito
) {
82 TemplateURLService
empty_model(NULL
, 0);
83 scoped_refptr
<KeywordProvider
> keyword_provider
=
84 new KeywordProvider(NULL
, &empty_model
);
89 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &path
));
90 path
= path
.AppendASCII("extensions").AppendASCII("good_unpacked");
92 base::RunLoop run_loop
;
93 ScopedExtensionLoadObserver
load_observer(registry(),
94 run_loop
.QuitClosure());
96 scoped_refptr
<UnpackedInstaller
> installer(
97 UnpackedInstaller::Create(service()));
98 installer
->Load(path
);
103 ASSERT_EQ(1U, registry()->enabled_extensions().size());
104 scoped_refptr
<const Extension
> extension
=
105 *(registry()->enabled_extensions().begin());
106 ASSERT_FALSE(util::IsIncognitoEnabled(extension
->id(), profile()));
108 Profile
* profile_to_use
= incognito
?
109 profile()->GetOffTheRecordProfile() : profile();
110 KeywordExtensionsDelegateImpl
delegate_impl(profile_to_use
,
111 keyword_provider
.get());
112 KeywordExtensionsDelegate
* delegate
= &delegate_impl
;
113 EXPECT_NE(incognito
, delegate
->IsEnabledExtension(extension
->id()));
115 // Enable the extension in incognito mode, which requires a reload.
117 base::RunLoop run_loop
;
118 ScopedExtensionLoadObserver
load_observer(registry(),
119 run_loop
.QuitClosure());
121 util::SetIsIncognitoEnabled(extension
->id(), profile(), true);
126 ASSERT_EQ(1U, registry()->enabled_extensions().size());
127 extension
= *(registry()->enabled_extensions().begin());
128 ASSERT_TRUE(util::IsIncognitoEnabled(extension
->id(), profile()));
129 EXPECT_TRUE(delegate
->IsEnabledExtension(extension
->id()));
132 TEST_F(KeywordExtensionsDelegateImplTest
, IsEnabledExtension
) {
136 TEST_F(KeywordExtensionsDelegateImplTest
, IsEnabledExtensionIncognito
) {
142 } // namespace extensions