[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 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 | #ifndef EXTENSIONS_BROWSER_EXTENSIONS_TEST_H_ | ||||
6 | #define EXTENSIONS_BROWSER_EXTENSIONS_TEST_H_ | ||||
7 | |||||
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 8 | #include <memory> |
9 | |||||
[email protected] | 9fc5bdc8 | 2014-08-03 23:49:26 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 11 | #include "base/macros.h" |
rockot | 8cba036 | 2016-08-09 21:43:43 | [diff] [blame] | 12 | #include "content/public/test/test_content_client_initializer.h" |
sudarsana.nagineni | 745ff1db | 2015-01-31 00:26:19 | [diff] [blame] | 13 | #include "content/public/test/test_renderer_host.h" |
thestig | 34f7eca | 2015-02-03 18:42:40 | [diff] [blame] | 14 | #include "extensions/browser/mock_extension_system.h" |
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
16 | |||||
karandeepb | 0daf48a | 2017-04-06 04:38:34 | [diff] [blame] | 17 | class ExtensionPrefValueMap; |
18 | class PrefService; | ||||
19 | |||||
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 20 | namespace content { |
jamescook | 30a1ccd | 2014-09-09 15:59:21 | [diff] [blame] | 21 | class BrowserContext; |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 22 | class ContentUtilityClient; |
sudarsana.nagineni | 745ff1db | 2015-01-31 00:26:19 | [diff] [blame] | 23 | class RenderViewHostTestEnabler; |
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 24 | } |
25 | |||||
26 | namespace extensions { | ||||
27 | class 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 | ||||
karandeepb | 0daf48a | 2017-04-06 04:38:34 | [diff] [blame] | 31 | // services for a browser context and sets up extension preferences. |
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 32 | // |
33 | // NOTE: Use this class only in extensions_unittests, not in Chrome unit_tests. | ||||
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 34 | // In Chrome those factories assume any BrowserContext is a Profile and will |
35 | // cause crashes if it is not. http://crbug.com/395820 | ||||
36 | class ExtensionsTest : public testing::Test { | ||||
37 | public: | ||||
Alex Clarke | f0527a9b | 2019-01-23 10:43:25 | [diff] [blame] | 38 | template <typename... Args> |
39 | constexpr ExtensionsTest(Args... args) | ||||
40 | : ExtensionsTest( | ||||
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 41 | std::make_unique<content::BrowserTaskEnvironment>(args...)) {} |
Alex Clarke | f0527a9b | 2019-01-23 10:43:25 | [diff] [blame] | 42 | |
dcheng | f9afb37 | 2014-10-27 21:43:14 | [diff] [blame] | 43 | ~ExtensionsTest() override; |
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 44 | |
Michael Giuffrida | e2b0ee57 | 2017-08-04 04:06:52 | [diff] [blame] | 45 | // Allows setting a custom TestExtensionsBrowserClient. Must only be called |
46 | // before SetUp(). | ||||
47 | void SetExtensionsBrowserClient( | ||||
48 | std::unique_ptr<TestExtensionsBrowserClient> extensions_browser_client); | ||||
49 | |||||
jamescook | 30a1ccd | 2014-09-09 15:59:21 | [diff] [blame] | 50 | // Returned as a BrowserContext since most users don't need methods from |
51 | // TestBrowserContext. | ||||
52 | content::BrowserContext* browser_context() { return browser_context_.get(); } | ||||
53 | |||||
karandeepb | 0daf48a | 2017-04-06 04:38:34 | [diff] [blame] | 54 | // Returns the incognito context associated with the ExtensionsBrowserClient. |
55 | content::BrowserContext* incognito_context() { | ||||
56 | return incognito_context_.get(); | ||||
57 | } | ||||
58 | |||||
jamescook | 30a1ccd | 2014-09-09 15:59:21 | [diff] [blame] | 59 | // Returned as a TestExtensionsBrowserClient since most users need to call |
60 | // test-specific methods on it. | ||||
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 61 | TestExtensionsBrowserClient* extensions_browser_client() { |
62 | return extensions_browser_client_.get(); | ||||
63 | } | ||||
64 | |||||
karandeepb | 0daf48a | 2017-04-06 04:38:34 | [diff] [blame] | 65 | PrefService* pref_service() { return pref_service_.get(); } |
66 | |||||
Evan Stade | 1ba771c | 2019-12-05 22:55:19 | [diff] [blame] | 67 | MockExtensionSystem* extension_system() { |
68 | return static_cast<MockExtensionSystem*>( | ||||
69 | extension_system_factory_.GetForBrowserContext(browser_context_.get())); | ||||
70 | } | ||||
71 | |||||
[email protected] | 9fc5bdc8 | 2014-08-03 23:49:26 | [diff] [blame] | 72 | // testing::Test overrides: |
dcheng | f9afb37 | 2014-10-27 21:43:14 | [diff] [blame] | 73 | void SetUp() override; |
74 | void TearDown() override; | ||||
[email protected] | 9fc5bdc8 | 2014-08-03 23:49:26 | [diff] [blame] | 75 | |
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 76 | private: |
Alex Clarke | f0527a9b | 2019-01-23 10:43:25 | [diff] [blame] | 77 | // 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 Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 80 | std::unique_ptr<content::BrowserTaskEnvironment> task_environment); |
Alex Clarke | f0527a9b | 2019-01-23 10:43:25 | [diff] [blame] | 81 | |
rockot | 8cba036 | 2016-08-09 21:43:43 | [diff] [blame] | 82 | content::TestContentClientInitializer content_client_initializer_; |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 83 | std::unique_ptr<content::ContentUtilityClient> content_utility_client_; |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 84 | std::unique_ptr<content::BrowserContext> browser_context_; |
karandeepb | 0daf48a | 2017-04-06 04:38:34 | [diff] [blame] | 85 | std::unique_ptr<content::BrowserContext> incognito_context_; |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 86 | std::unique_ptr<TestExtensionsBrowserClient> extensions_browser_client_; |
karandeepb | 0daf48a | 2017-04-06 04:38:34 | [diff] [blame] | 87 | std::unique_ptr<ExtensionPrefValueMap> extension_pref_value_map_; |
88 | std::unique_ptr<PrefService> pref_service_; | ||||
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 89 | |
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 90 | MockExtensionSystemFactory<MockExtensionSystem> extension_system_factory_; |
91 | |||||
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 92 | std::unique_ptr<content::BrowserTaskEnvironment> task_environment_; |
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 93 | |
sudarsana.nagineni | 745ff1db | 2015-01-31 00:26:19 | [diff] [blame] | 94 | // The existence of this object enables tests via |
95 | // RenderViewHostTester. | ||||
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 96 | std::unique_ptr<content::RenderViewHostTestEnabler> rvh_test_enabler_; |
thestig | 34f7eca | 2015-02-03 18:42:40 | [diff] [blame] | 97 | |
[email protected] | a80de911 | 2014-07-31 03:15:08 | [diff] [blame] | 98 | DISALLOW_COPY_AND_ASSIGN(ExtensionsTest); |
99 | }; | ||||
100 | |||||
101 | } // namespace extensions | ||||
102 | |||||
103 | #endif // EXTENSIONS_BROWSER_EXTENSIONS_TEST_H_ |