blob: d3a6dacb0a18c76b2c25c41cca923554f2626feb [file] [log] [blame]
[email protected]a80de9112014-07-31 03:15:081// 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#ifndef EXTENSIONS_BROWSER_EXTENSIONS_TEST_H_
6#define EXTENSIONS_BROWSER_EXTENSIONS_TEST_H_
7
dchengf5d241082016-04-21 03:43:118#include <memory>
9
[email protected]9fc5bdc82014-08-03 23:49:2610#include "base/compiler_specific.h"
[email protected]a80de9112014-07-31 03:15:0811#include "base/macros.h"
rockot8cba0362016-08-09 21:43:4312#include "content/public/test/test_content_client_initializer.h"
sudarsana.nagineni745ff1db2015-01-31 00:26:1913#include "content/public/test/test_renderer_host.h"
thestig34f7eca2015-02-03 18:42:4014#include "extensions/browser/mock_extension_system.h"
[email protected]a80de9112014-07-31 03:15:0815#include "testing/gtest/include/gtest/gtest.h"
16
karandeepb0daf48a2017-04-06 04:38:3417class ExtensionPrefValueMap;
18class PrefService;
19
[email protected]a80de9112014-07-31 03:15:0820namespace content {
jamescook30a1ccd2014-09-09 15:59:2121class BrowserContext;
asargent275faaa2015-01-27 23:43:2922class ContentUtilityClient;
sudarsana.nagineni745ff1db2015-01-31 00:26:1923class RenderViewHostTestEnabler;
[email protected]a80de9112014-07-31 03:15:0824}
25
26namespace extensions {
27class TestExtensionsBrowserClient;
28
29// Base class for extensions module unit tests of browser process code. Sets up
30// the content module and extensions module client interfaces. Initializes
karandeepb0daf48a2017-04-06 04:38:3431// services for a browser context and sets up extension preferences.
[email protected]a80de9112014-07-31 03:15:0832//
33// NOTE: Use this class only in extensions_unittests, not in Chrome unit_tests.
[email protected]a80de9112014-07-31 03:15:0834// In Chrome those factories assume any BrowserContext is a Profile and will
35// cause crashes if it is not. http://crbug.com/395820
36class ExtensionsTest : public testing::Test {
37 public:
Alex Clarkef0527a9b2019-01-23 10:43:2538 template <typename... Args>
39 constexpr ExtensionsTest(Args... args)
40 : ExtensionsTest(
Gabriel Charette798fde72019-08-20 22:24:0441 std::make_unique<content::BrowserTaskEnvironment>(args...)) {}
Alex Clarkef0527a9b2019-01-23 10:43:2542
dchengf9afb372014-10-27 21:43:1443 ~ExtensionsTest() override;
[email protected]a80de9112014-07-31 03:15:0844
Michael Giuffridae2b0ee572017-08-04 04:06:5245 // Allows setting a custom TestExtensionsBrowserClient. Must only be called
46 // before SetUp().
47 void SetExtensionsBrowserClient(
48 std::unique_ptr<TestExtensionsBrowserClient> extensions_browser_client);
49
jamescook30a1ccd2014-09-09 15:59:2150 // Returned as a BrowserContext since most users don't need methods from
51 // TestBrowserContext.
52 content::BrowserContext* browser_context() { return browser_context_.get(); }
53
karandeepb0daf48a2017-04-06 04:38:3454 // Returns the incognito context associated with the ExtensionsBrowserClient.
55 content::BrowserContext* incognito_context() {
56 return incognito_context_.get();
57 }
58
jamescook30a1ccd2014-09-09 15:59:2159 // Returned as a TestExtensionsBrowserClient since most users need to call
60 // test-specific methods on it.
[email protected]a80de9112014-07-31 03:15:0861 TestExtensionsBrowserClient* extensions_browser_client() {
62 return extensions_browser_client_.get();
63 }
64
karandeepb0daf48a2017-04-06 04:38:3465 PrefService* pref_service() { return pref_service_.get(); }
66
Evan Stade1ba771c2019-12-05 22:55:1967 MockExtensionSystem* extension_system() {
68 return static_cast<MockExtensionSystem*>(
69 extension_system_factory_.GetForBrowserContext(browser_context_.get()));
70 }
71
[email protected]9fc5bdc82014-08-03 23:49:2672 // testing::Test overrides:
dchengf9afb372014-10-27 21:43:1473 void SetUp() override;
74 void TearDown() override;
[email protected]9fc5bdc82014-08-03 23:49:2675
[email protected]a80de9112014-07-31 03:15:0876 private:
Alex Clarkef0527a9b2019-01-23 10:43:2577 // The template constructor has to be in the header but it delegates to this
78 // constructor to initialize all other members out-of-line.
79 explicit ExtensionsTest(
Gabriel Charette798fde72019-08-20 22:24:0480 std::unique_ptr<content::BrowserTaskEnvironment> task_environment);
Alex Clarkef0527a9b2019-01-23 10:43:2581
rockot8cba0362016-08-09 21:43:4382 content::TestContentClientInitializer content_client_initializer_;
dchengf5d241082016-04-21 03:43:1183 std::unique_ptr<content::ContentUtilityClient> content_utility_client_;
dchengf5d241082016-04-21 03:43:1184 std::unique_ptr<content::BrowserContext> browser_context_;
karandeepb0daf48a2017-04-06 04:38:3485 std::unique_ptr<content::BrowserContext> incognito_context_;
dchengf5d241082016-04-21 03:43:1186 std::unique_ptr<TestExtensionsBrowserClient> extensions_browser_client_;
karandeepb0daf48a2017-04-06 04:38:3487 std::unique_ptr<ExtensionPrefValueMap> extension_pref_value_map_;
88 std::unique_ptr<PrefService> pref_service_;
[email protected]a80de9112014-07-31 03:15:0889
danakjaee67172017-06-13 16:37:0290 MockExtensionSystemFactory<MockExtensionSystem> extension_system_factory_;
91
Gabriel Charette798fde72019-08-20 22:24:0492 std::unique_ptr<content::BrowserTaskEnvironment> task_environment_;
danakjaee67172017-06-13 16:37:0293
sudarsana.nagineni745ff1db2015-01-31 00:26:1994 // The existence of this object enables tests via
95 // RenderViewHostTester.
danakjaee67172017-06-13 16:37:0296 std::unique_ptr<content::RenderViewHostTestEnabler> rvh_test_enabler_;
thestig34f7eca2015-02-03 18:42:4097
[email protected]a80de9112014-07-31 03:15:0898 DISALLOW_COPY_AND_ASSIGN(ExtensionsTest);
99};
100
101} // namespace extensions
102
103#endif // EXTENSIONS_BROWSER_EXTENSIONS_TEST_H_