[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 1 | // Copyright (c) 2013 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 CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_ENVIRONMENT_H_ | ||||
6 | #define CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_ENVIRONMENT_H_ | ||||
7 | |||||
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 8 | #include <memory> |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 9 | #include <string> |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 10 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 12 | #include "base/memory/scoped_refptr.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 13 | #include "build/build_config.h" |
Yuta Hijikata | 1290fee2 | 2020-11-25 09:46:28 | [diff] [blame] | 14 | #include "build/chromeos_buildflags.h" |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 15 | #include "extensions/common/extension.h" |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 16 | |
Xiaohan Wang | be233b6 | 2022-01-14 19:15:53 | [diff] [blame] | 17 | #if BUILDFLAG(IS_WIN) |
[email protected] | 9a68d3a | 2013-04-22 16:26:54 | [diff] [blame] | 18 | #include "ui/base/win/scoped_ole_initializer.h" |
[email protected] | 79bac42 | 2013-04-22 15:44:26 | [diff] [blame] | 19 | #endif |
20 | |||||
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 21 | class TestingProfile; |
22 | |||||
23 | namespace base { | ||||
24 | class Value; | ||||
25 | } | ||||
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 26 | |
27 | namespace content { | ||||
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 28 | class BrowserTaskEnvironment; |
Lukasz Anforowicz | 2b269973 | 2018-04-12 18:49:10 | [diff] [blame] | 29 | class WebContents; |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 30 | } |
31 | |||||
32 | namespace extensions { | ||||
33 | |||||
34 | class Extension; | ||||
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 35 | class ExtensionPrefs; |
Devlin Cronin | eea1b7a | 2018-05-26 02:46:21 | [diff] [blame] | 36 | class ExtensionService; |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 37 | class TestExtensionSystem; |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 38 | |
39 | // This class provides a minimal environment in which to create | ||||
40 | // extensions and tabs for extension-related unittests. | ||||
41 | class TestExtensionEnvironment { | ||||
42 | public: | ||||
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 43 | // Fetches the TestExtensionSystem in |profile| and creates a default |
44 | // ExtensionService there, | ||||
45 | static ExtensionService* CreateExtensionServiceForProfile( | ||||
46 | TestingProfile* profile); | ||||
47 | |||||
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 48 | enum class Type { |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 49 | // A TestExtensionEnvironment which will provide a BrowserTaskEnvironment |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 50 | // in its scope. |
51 | kWithTaskEnvironment, | ||||
52 | // A TestExtensionEnvironment which will run on top of the existing task | ||||
53 | // environment without trying to provide one. | ||||
54 | kInheritExistingTaskEnvironment, | ||||
55 | }; | ||||
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 56 | |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 57 | explicit TestExtensionEnvironment(Type type = Type::kWithTaskEnvironment); |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 58 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 59 | TestExtensionEnvironment(const TestExtensionEnvironment&) = delete; |
60 | TestExtensionEnvironment& operator=(const TestExtensionEnvironment&) = delete; | ||||
61 | |||||
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 62 | ~TestExtensionEnvironment(); |
63 | |||||
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 64 | TestingProfile* profile() const; |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 65 | |
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 66 | // Returns the TestExtensionSystem created by the TestingProfile. |
67 | TestExtensionSystem* GetExtensionSystem(); | ||||
68 | |||||
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 69 | // Returns an ExtensionService created (and owned) by the |
70 | // TestExtensionSystem created by the TestingProfile. | ||||
71 | ExtensionService* GetExtensionService(); | ||||
72 | |||||
[email protected] | 94cf31c | 2013-12-07 03:58:32 | [diff] [blame] | 73 | // Returns ExtensionPrefs created (and owned) by the |
74 | // TestExtensionSystem created by the TestingProfile. | ||||
75 | ExtensionPrefs* GetExtensionPrefs(); | ||||
76 | |||||
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 77 | // Creates an Extension and registers it with the ExtensionService. |
78 | // The Extension has a default manifest of {name: "Extension", | ||||
79 | // version: "1.0", manifest_version: 2}, and values in | ||||
80 | // manifest_extra override these defaults. | ||||
[email protected] | e0ec3cb1 | 2013-04-03 17:35:30 | [diff] [blame] | 81 | const Extension* MakeExtension(const base::Value& manifest_extra); |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 82 | |
jackhou | c587f30 | 2015-04-13 08:16:39 | [diff] [blame] | 83 | // Use a specific extension ID instead of the default generated in |
84 | // Extension::Create. | ||||
85 | const Extension* MakeExtension(const base::Value& manifest_extra, | ||||
86 | const std::string& id); | ||||
87 | |||||
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 88 | // Generates a valid packaged app manifest with the given ID. If |install| |
89 | // it gets added to the ExtensionService in |profile|. | ||||
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 90 | scoped_refptr<const Extension> MakePackagedApp(const std::string& id, |
91 | bool install); | ||||
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 92 | |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 93 | // Returns a test web contents that has a tab id. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 94 | std::unique_ptr<content::WebContents> MakeTab() const; |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 95 | |
tapted | c7bd3ce | 2015-06-16 02:50:44 | [diff] [blame] | 96 | // Deletes the testing profile to test profile teardown. |
97 | void DeleteProfile(); | ||||
98 | |||||
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 99 | private: |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 100 | class ChromeOSEnv; |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 101 | |
tapted | dfd8eda | 2015-08-04 09:15:23 | [diff] [blame] | 102 | void Init(); |
103 | |||||
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 104 | // If |task_environment_| is needed, then it needs to constructed before |
Lukasz Anforowicz | 2b269973 | 2018-04-12 18:49:10 | [diff] [blame] | 105 | // |profile_| and destroyed after |profile_|. |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 106 | const std::unique_ptr<content::BrowserTaskEnvironment> task_environment_; |
Lukasz Anforowicz | 2b269973 | 2018-04-12 18:49:10 | [diff] [blame] | 107 | |
Yuta Hijikata | 1290fee2 | 2020-11-25 09:46:28 | [diff] [blame] | 108 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Gabriel Charette | 0601da4 | 2018-03-29 14:46:34 | [diff] [blame] | 109 | const std::unique_ptr<ChromeOSEnv> chromeos_env_; |
110 | #endif | ||||
[email protected] | 363f527 | 2013-04-23 17:21:42 | [diff] [blame] | 111 | |
Xiaohan Wang | be233b6 | 2022-01-14 19:15:53 | [diff] [blame] | 112 | #if BUILDFLAG(IS_WIN) |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 113 | ui::ScopedOleInitializer ole_initializer_; |
114 | #endif | ||||
Lukasz Anforowicz | 2b269973 | 2018-04-12 18:49:10 | [diff] [blame] | 115 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 116 | std::unique_ptr<TestingProfile> profile_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 117 | raw_ptr<ExtensionService> extension_service_ = nullptr; |
[email protected] | 11d42c8 | 2013-02-01 00:14:32 | [diff] [blame] | 118 | }; |
119 | |||||
120 | } // namespace extensions | ||||
121 | |||||
122 | #endif // CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_ENVIRONMENT_H_ |