keyword_extensions_delegate_impl_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5#include "chrome/browser/autocomplete/keyword_extensions_delegate_impl.h"
6
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"
21
22namespace extensions {
23
24namespace {
25
26class ScopedExtensionLoadObserver : public ExtensionRegistryObserver {
27 public:
28  ScopedExtensionLoadObserver(ExtensionRegistry* registry,
29                              const base::Closure& quit_closure);
30  virtual ~ScopedExtensionLoadObserver();
31
32 private:
33  virtual void OnExtensionInstalled(content::BrowserContext* browser_context,
34                                    const Extension* extension,
35                                    bool is_update) OVERRIDE;
36
37  ExtensionRegistry* registry_;
38  base::Closure quit_closure_;
39
40  DISALLOW_COPY_AND_ASSIGN(ScopedExtensionLoadObserver);
41};
42
43ScopedExtensionLoadObserver::ScopedExtensionLoadObserver(
44    ExtensionRegistry* registry,
45    const base::Closure& quit_closure)
46    : registry_(registry),
47      quit_closure_(quit_closure) {
48  registry_->AddObserver(this);
49}
50
51ScopedExtensionLoadObserver::~ScopedExtensionLoadObserver() {
52  registry_->RemoveObserver(this);
53}
54
55void ScopedExtensionLoadObserver::OnExtensionInstalled(
56    content::BrowserContext* browser_context,
57    const Extension* extension,
58    bool is_update) {
59  quit_closure_.Run();
60}
61
62class KeywordExtensionsDelegateImplTest : public ExtensionServiceTestBase {
63 public:
64  KeywordExtensionsDelegateImplTest() {}
65  virtual ~KeywordExtensionsDelegateImplTest() {}
66
67 protected:
68  virtual void SetUp() OVERRIDE;
69
70  void RunTest(bool incognito);
71
72 private:
73  DISALLOW_COPY_AND_ASSIGN(KeywordExtensionsDelegateImplTest);
74};
75
76void KeywordExtensionsDelegateImplTest::SetUp() {
77  ExtensionServiceTestBase::SetUp();
78  InitializeExtensionService(CreateDefaultInitParams());
79  InitializeProcessManager();
80}
81
82void KeywordExtensionsDelegateImplTest::RunTest(bool incognito) {
83  TemplateURLService empty_model(NULL, 0);
84  scoped_refptr<KeywordProvider> keyword_provider =
85      new KeywordProvider(NULL, &empty_model);
86
87  // Load an extension.
88  {
89    base::FilePath path;
90    ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
91    path = path.AppendASCII("extensions").AppendASCII("good_unpacked");
92
93    base::RunLoop run_loop;
94    ScopedExtensionLoadObserver load_observer(registry(),
95                                              run_loop.QuitClosure());
96
97    scoped_refptr<UnpackedInstaller> installer(
98        UnpackedInstaller::Create(service()));
99    installer->Load(path);
100
101    run_loop.Run();
102  }
103
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()));
108
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()));
115
116  // Enable the extension in incognito mode, which requires a reload.
117  {
118    base::RunLoop run_loop;
119    ScopedExtensionLoadObserver load_observer(registry(),
120                                              run_loop.QuitClosure());
121
122    util::SetIsIncognitoEnabled(extension->id(), profile(), true);
123
124    run_loop.Run();
125  }
126
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()));
131}
132
133TEST_F(KeywordExtensionsDelegateImplTest, IsEnabledExtension) {
134  RunTest(false);
135}
136
137TEST_F(KeywordExtensionsDelegateImplTest, IsEnabledExtensionIncognito) {
138  RunTest(true);
139}
140
141}  // namespace
142
143}  // namespace extensions
144