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