Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: extensions/browser/extensions_test.cc

Issue 2798503002: Extensions: Pull duplicated functionality into ExtensionsTest fixture. (Closed)
Patch Set: Add TODO for comments. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/extensions_test.h" 5 #include "extensions/browser/extensions_test.h"
6 6
7 #include "components/keyed_service/content/browser_context_dependency_manager.h" 7 #include "components/keyed_service/content/browser_context_dependency_manager.h"
8 #include "components/pref_registry/pref_registry_syncable.h"
9 #include "components/prefs/pref_service_factory.h"
10 #include "components/prefs/testing_pref_store.h"
8 #include "content/public/browser/content_browser_client.h" 11 #include "content/public/browser/content_browser_client.h"
9 #include "content/public/common/content_client.h" 12 #include "content/public/common/content_client.h"
10 #include "content/public/test/test_browser_context.h" 13 #include "content/public/test/test_browser_context.h"
14 #include "extensions/browser/extension_pref_value_map.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/extension_prefs_factory.h"
11 #include "extensions/browser/test_extensions_browser_client.h" 17 #include "extensions/browser/test_extensions_browser_client.h"
12 #include "extensions/test/test_content_browser_client.h" 18 #include "extensions/test/test_content_browser_client.h"
13 #include "extensions/test/test_content_utility_client.h" 19 #include "extensions/test/test_content_utility_client.h"
14 20
15 namespace extensions { 21 namespace extensions {
16 22
17 // This class does work in the constructor instead of SetUp() to give subclasses 23 // This class does work in the constructor instead of SetUp() to give subclasses
18 // a valid BrowserContext to use while initializing their members. For example: 24 // a valid BrowserContext to use while initializing their members. For example:
19 // 25 //
20 // class MyExtensionsTest : public ExtensionsTest { 26 // class MyExtensionsTest : public ExtensionsTest {
21 // MyExtensionsTest() 27 // MyExtensionsTest()
22 // : my_object_(browser_context())) { 28 // : my_object_(browser_context())) {
23 // } 29 // }
24 // }; 30 // };
31 // TODO(crbug.com/708256): All these instances are setup in the constructor, but
32 // destroyed in TearDown(), which may cause problems. Move this initialization
33 // to SetUp().
25 ExtensionsTest::ExtensionsTest() 34 ExtensionsTest::ExtensionsTest()
26 : content_browser_client_(new TestContentBrowserClient), 35 : content_browser_client_(new TestContentBrowserClient),
27 content_utility_client_(new TestContentUtilityClient), 36 content_utility_client_(new TestContentUtilityClient),
28 browser_context_(new content::TestBrowserContext), 37 browser_context_(new content::TestBrowserContext),
38 incognito_context_(new content::TestBrowserContextIncognito),
29 extensions_browser_client_( 39 extensions_browser_client_(
30 new TestExtensionsBrowserClient(browser_context_.get())) { 40 new TestExtensionsBrowserClient(browser_context_.get())) {
31 content::SetBrowserClientForTesting(content_browser_client_.get()); 41 content::SetBrowserClientForTesting(content_browser_client_.get());
32 content::SetUtilityClientForTesting(content_utility_client_.get()); 42 content::SetUtilityClientForTesting(content_utility_client_.get());
33 ExtensionsBrowserClient::Set(extensions_browser_client_.get()); 43 ExtensionsBrowserClient::Set(extensions_browser_client_.get());
34 extensions_browser_client_->set_extension_system_factory( 44 extensions_browser_client_->set_extension_system_factory(
35 &extension_system_factory_); 45 &extension_system_factory_);
46 extensions_browser_client_->SetIncognitoContext(incognito_context_.get());
47
48 // Set up all the dependencies of ExtensionPrefs.
49 extension_pref_value_map_.reset(new ExtensionPrefValueMap());
50 PrefServiceFactory factory;
51 factory.set_user_prefs(new TestingPrefStore());
52 factory.set_extension_prefs(new TestingPrefStore());
53 user_prefs::PrefRegistrySyncable* pref_registry =
54 new user_prefs::PrefRegistrySyncable();
55 // Prefs should be registered before the PrefService is created.
56 ExtensionPrefs::RegisterProfilePrefs(pref_registry);
57 pref_service_ = factory.Create(pref_registry);
58
59 std::unique_ptr<ExtensionPrefs> extension_prefs(ExtensionPrefs::Create(
60 browser_context(), pref_service_.get(),
61 browser_context()->GetPath().AppendASCII("Extensions"),
62 extension_pref_value_map_.get(), false /* extensions_disabled */,
63 std::vector<ExtensionPrefsObserver*>()));
64
65 ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(
66 browser_context(), std::move(extension_prefs));
36 } 67 }
37 68
38 ExtensionsTest::~ExtensionsTest() { 69 ExtensionsTest::~ExtensionsTest() {
39 ExtensionsBrowserClient::Set(nullptr); 70 ExtensionsBrowserClient::Set(nullptr);
40 content::SetBrowserClientForTesting(nullptr); 71 content::SetBrowserClientForTesting(nullptr);
41 content::SetUtilityClientForTesting(nullptr); 72 content::SetUtilityClientForTesting(nullptr);
42 } 73 }
43 74
44 void ExtensionsTest::SetUp() { 75 void ExtensionsTest::SetUp() {
45 // Crashing here? Don't use this class in Chrome's unit_tests. See header. 76 // Crashing here? Don't use this class in Chrome's unit_tests. See header.
46 BrowserContextDependencyManager::GetInstance() 77 BrowserContextDependencyManager::GetInstance()
47 ->CreateBrowserContextServicesForTest(browser_context_.get()); 78 ->CreateBrowserContextServicesForTest(browser_context_.get());
48 } 79 }
49 80
50 void ExtensionsTest::TearDown() { 81 void ExtensionsTest::TearDown() {
51 // Allows individual tests to have BrowserContextKeyedServiceFactory objects 82 // Allows individual tests to have BrowserContextKeyedServiceFactory objects
52 // as member variables instead of singletons. The individual services will be 83 // as member variables instead of singletons. The individual services will be
53 // cleaned up before the factories are destroyed. 84 // cleaned up before the factories are destroyed.
54 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices( 85 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
55 browser_context_.get()); 86 browser_context_.get());
87
88 // TODO(crbug.com/708256): |extension_browser_client_| is reset here but not
89 // unset as the singleton until the destructor. This can lead to use after
90 // free errors.
56 extensions_browser_client_.reset(); 91 extensions_browser_client_.reset();
57 browser_context_.reset(); 92 browser_context_.reset();
93 incognito_context_.reset();
58 } 94 }
59 95
60 } // namespace extensions 96 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698