Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 ExtensionLoadObserver : public ExtensionRegistryObserver { | |
|
Yoyo Zhou
2014/09/05 16:21:33
Consider making this a ScopedExtensionLoadObserver
Lei Zhang
2014/09/05 19:35:08
Done.
| |
| 27 public: | |
| 28 explicit ExtensionLoadObserver(const base::Closure& quit_closure); | |
| 29 virtual ~ExtensionLoadObserver() {} | |
| 30 | |
| 31 private: | |
| 32 virtual void OnExtensionInstalled(content::BrowserContext* browser_context, | |
| 33 const Extension* extension, | |
| 34 bool is_update) OVERRIDE; | |
| 35 | |
| 36 base::Closure quit_closure_; | |
| 37 | |
| 38 DISALLOW_COPY_AND_ASSIGN(ExtensionLoadObserver); | |
| 39 }; | |
| 40 | |
| 41 ExtensionLoadObserver::ExtensionLoadObserver(const base::Closure& quit_closure) | |
| 42 : quit_closure_(quit_closure) { | |
| 43 } | |
| 44 | |
| 45 void ExtensionLoadObserver::OnExtensionInstalled( | |
| 46 content::BrowserContext* browser_context, | |
| 47 const Extension* extension, | |
| 48 bool is_update) { | |
| 49 quit_closure_.Run(); | |
| 50 } | |
| 51 | |
| 52 class KeywordExtensionsDelegateImplTest : public ExtensionServiceTestBase { | |
| 53 public: | |
| 54 KeywordExtensionsDelegateImplTest() {} | |
| 55 virtual ~KeywordExtensionsDelegateImplTest() {} | |
| 56 | |
| 57 protected: | |
| 58 virtual void SetUp() OVERRIDE; | |
| 59 virtual void TearDown() OVERRIDE; | |
| 60 | |
| 61 void RunTest(bool incognito); | |
| 62 | |
| 63 private: | |
| 64 DISALLOW_COPY_AND_ASSIGN(KeywordExtensionsDelegateImplTest); | |
| 65 }; | |
| 66 | |
| 67 void KeywordExtensionsDelegateImplTest::SetUp() { | |
| 68 ExtensionServiceTestBase::SetUp(); | |
| 69 InitializeExtensionService(CreateDefaultInitParams()); | |
| 70 | |
| 71 TestExtensionSystem* extension_system = | |
| 72 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile())); | |
| 73 extension_system->CreateProcessManager(); | |
|
Yoyo Zhou
2014/09/05 16:21:33
This is all just InitializeProcessManager()
Lei Zhang
2014/09/05 19:35:08
Done.
| |
| 74 | |
| 75 TestExtensionSystem* incognito_extension_system = | |
| 76 static_cast<TestExtensionSystem*>( | |
| 77 ExtensionSystem::Get(profile()->GetOffTheRecordProfile())); | |
| 78 incognito_extension_system->SetExtensionService( | |
| 79 service(), false /* does not take ownership */); | |
| 80 } | |
| 81 | |
| 82 void KeywordExtensionsDelegateImplTest::TearDown() { | |
| 83 TestExtensionSystem* incognito_extension_system = | |
| 84 static_cast<TestExtensionSystem*>( | |
| 85 ExtensionSystem::Get(profile()->GetOffTheRecordProfile())); | |
| 86 incognito_extension_system->SetExtensionService( | |
| 87 NULL, false /* does not take ownership */); | |
| 88 } | |
| 89 | |
| 90 void KeywordExtensionsDelegateImplTest::RunTest(bool incognito) { | |
| 91 TemplateURLService empty_model(NULL, 0); | |
| 92 scoped_refptr<KeywordProvider> keyword_provider = | |
| 93 new KeywordProvider(NULL, &empty_model); | |
| 94 | |
| 95 // Load an extension. | |
| 96 { | |
| 97 base::FilePath path; | |
| 98 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | |
| 99 path = path.AppendASCII("extensions").AppendASCII("good_unpacked"); | |
| 100 | |
| 101 base::RunLoop run_loop; | |
| 102 ExtensionLoadObserver load_observer(run_loop.QuitClosure()); | |
| 103 registry()->AddObserver(&load_observer); | |
| 104 | |
| 105 scoped_refptr<UnpackedInstaller> installer( | |
| 106 UnpackedInstaller::Create(service())); | |
| 107 installer->Load(path); | |
| 108 | |
| 109 run_loop.Run(); | |
| 110 registry()->RemoveObserver(&load_observer); | |
| 111 } | |
| 112 | |
| 113 ASSERT_EQ(1U, registry()->enabled_extensions().size()); | |
| 114 scoped_refptr<const Extension> extension = | |
| 115 *(registry()->enabled_extensions().begin()); | |
| 116 ASSERT_FALSE(util::IsIncognitoEnabled(extension->id(), profile())); | |
| 117 | |
| 118 Profile* profile_to_use = incognito ? | |
| 119 profile()->GetOffTheRecordProfile() : profile(); | |
| 120 KeywordExtensionsDelegateImpl delegate_impl(profile_to_use, | |
|
Yoyo Zhou
2014/09/05 16:21:33
Here's a suggestion. We could use dependency injec
Lei Zhang
2014/09/05 19:35:08
Better yet, I'm just going to take this portion of
| |
| 121 keyword_provider.get()); | |
| 122 KeywordExtensionsDelegate* delegate = &delegate_impl; | |
| 123 EXPECT_NE(incognito, delegate->IsEnabledExtension(extension->id())); | |
| 124 | |
| 125 // Enable the extension in incognito mode, which requires a reload. | |
| 126 { | |
| 127 base::RunLoop run_loop; | |
| 128 ExtensionLoadObserver load_observer(run_loop.QuitClosure()); | |
| 129 registry()->AddObserver(&load_observer); | |
| 130 | |
| 131 util::SetIsIncognitoEnabled(extension->id(), profile(), true); | |
| 132 | |
| 133 run_loop.Run(); | |
| 134 registry()->RemoveObserver(&load_observer); | |
| 135 } | |
| 136 | |
| 137 ASSERT_EQ(1U, registry()->enabled_extensions().size()); | |
| 138 extension = *(registry()->enabled_extensions().begin()); | |
| 139 ASSERT_TRUE(util::IsIncognitoEnabled(extension->id(), profile())); | |
| 140 EXPECT_TRUE(delegate->IsEnabledExtension(extension->id())); | |
| 141 } | |
| 142 | |
| 143 TEST_F(KeywordExtensionsDelegateImplTest, IsEnabledExtension) { | |
| 144 RunTest(false); | |
| 145 } | |
| 146 | |
| 147 TEST_F(KeywordExtensionsDelegateImplTest, IsEnabledExtensionIncognito) { | |
| 148 RunTest(true); | |
| 149 } | |
| 150 | |
| 151 } // namespace | |
| 152 | |
| 153 } // namespace extensions | |
| OLD | NEW |