blob: 8a9b0e6700fd17f57c91435953f81a81dfd22092 [file] [log] [blame]
[email protected]11d42c82013-02-01 00:14:321// 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
dchengc963c7142016-04-08 03:55:228#include <memory>
Gabriel Charette0601da42018-03-29 14:46:349#include <string>
dchengc963c7142016-04-08 03:55:2210
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
Gabriel Charette0601da42018-03-29 14:46:3412#include "base/memory/scoped_refptr.h"
avia2f4804a2015-12-24 23:11:1313#include "build/build_config.h"
Yuta Hijikata1290fee22020-11-25 09:46:2814#include "build/chromeos_buildflags.h"
Gabriel Charette0601da42018-03-29 14:46:3415#include "extensions/common/extension.h"
[email protected]11d42c82013-02-01 00:14:3216
Xiaohan Wangbe233b62022-01-14 19:15:5317#if BUILDFLAG(IS_WIN)
[email protected]9a68d3a2013-04-22 16:26:5418#include "ui/base/win/scoped_ole_initializer.h"
[email protected]79bac422013-04-22 15:44:2619#endif
20
[email protected]e0ec3cb12013-04-03 17:35:3021class TestingProfile;
22
23namespace base {
24class Value;
25}
[email protected]11d42c82013-02-01 00:14:3226
27namespace content {
Gabriel Charette798fde72019-08-20 22:24:0428class BrowserTaskEnvironment;
Lukasz Anforowicz2b2699732018-04-12 18:49:1029class WebContents;
[email protected]11d42c82013-02-01 00:14:3230}
31
32namespace extensions {
33
34class Extension;
[email protected]94cf31c2013-12-07 03:58:3235class ExtensionPrefs;
Devlin Cronineea1b7a2018-05-26 02:46:2136class ExtensionService;
[email protected]94cf31c2013-12-07 03:58:3237class TestExtensionSystem;
[email protected]11d42c82013-02-01 00:14:3238
39// This class provides a minimal environment in which to create
40// extensions and tabs for extension-related unittests.
41class TestExtensionEnvironment {
42 public:
taptedc7bd3ce2015-06-16 02:50:4443 // Fetches the TestExtensionSystem in |profile| and creates a default
44 // ExtensionService there,
45 static ExtensionService* CreateExtensionServiceForProfile(
46 TestingProfile* profile);
47
Gabriel Charette0601da42018-03-29 14:46:3448 enum class Type {
Gabriel Charette798fde72019-08-20 22:24:0449 // A TestExtensionEnvironment which will provide a BrowserTaskEnvironment
Gabriel Charette0601da42018-03-29 14:46:3450 // 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 };
taptedc7bd3ce2015-06-16 02:50:4456
Gabriel Charette0601da42018-03-29 14:46:3457 explicit TestExtensionEnvironment(Type type = Type::kWithTaskEnvironment);
taptedc7bd3ce2015-06-16 02:50:4458
Peter Boström53c6c5952021-09-17 09:41:2659 TestExtensionEnvironment(const TestExtensionEnvironment&) = delete;
60 TestExtensionEnvironment& operator=(const TestExtensionEnvironment&) = delete;
61
[email protected]11d42c82013-02-01 00:14:3262 ~TestExtensionEnvironment();
63
[email protected]e0ec3cb12013-04-03 17:35:3064 TestingProfile* profile() const;
[email protected]11d42c82013-02-01 00:14:3265
[email protected]94cf31c2013-12-07 03:58:3266 // Returns the TestExtensionSystem created by the TestingProfile.
67 TestExtensionSystem* GetExtensionSystem();
68
[email protected]11d42c82013-02-01 00:14:3269 // Returns an ExtensionService created (and owned) by the
70 // TestExtensionSystem created by the TestingProfile.
71 ExtensionService* GetExtensionService();
72
[email protected]94cf31c2013-12-07 03:58:3273 // Returns ExtensionPrefs created (and owned) by the
74 // TestExtensionSystem created by the TestingProfile.
75 ExtensionPrefs* GetExtensionPrefs();
76
[email protected]11d42c82013-02-01 00:14:3277 // 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]e0ec3cb12013-04-03 17:35:3081 const Extension* MakeExtension(const base::Value& manifest_extra);
[email protected]11d42c82013-02-01 00:14:3282
jackhouc587f302015-04-13 08:16:3983 // 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
taptedc7bd3ce2015-06-16 02:50:4488 // Generates a valid packaged app manifest with the given ID. If |install|
89 // it gets added to the ExtensionService in |profile|.
Devlin Cronin8e5892f2018-10-04 00:13:4390 scoped_refptr<const Extension> MakePackagedApp(const std::string& id,
91 bool install);
taptedc7bd3ce2015-06-16 02:50:4492
[email protected]11d42c82013-02-01 00:14:3293 // Returns a test web contents that has a tab id.
dchengc963c7142016-04-08 03:55:2294 std::unique_ptr<content::WebContents> MakeTab() const;
[email protected]11d42c82013-02-01 00:14:3295
taptedc7bd3ce2015-06-16 02:50:4496 // Deletes the testing profile to test profile teardown.
97 void DeleteProfile();
98
[email protected]11d42c82013-02-01 00:14:3299 private:
tapteddfd8eda2015-08-04 09:15:23100 class ChromeOSEnv;
[email protected]11d42c82013-02-01 00:14:32101
tapteddfd8eda2015-08-04 09:15:23102 void Init();
103
Gabriel Charette798fde72019-08-20 22:24:04104 // If |task_environment_| is needed, then it needs to constructed before
Lukasz Anforowicz2b2699732018-04-12 18:49:10105 // |profile_| and destroyed after |profile_|.
Gabriel Charette798fde72019-08-20 22:24:04106 const std::unique_ptr<content::BrowserTaskEnvironment> task_environment_;
Lukasz Anforowicz2b2699732018-04-12 18:49:10107
Yuta Hijikata1290fee22020-11-25 09:46:28108#if BUILDFLAG(IS_CHROMEOS_ASH)
Gabriel Charette0601da42018-03-29 14:46:34109 const std::unique_ptr<ChromeOSEnv> chromeos_env_;
110#endif
[email protected]363f5272013-04-23 17:21:42111
Xiaohan Wangbe233b62022-01-14 19:15:53112#if BUILDFLAG(IS_WIN)
[email protected]11d42c82013-02-01 00:14:32113 ui::ScopedOleInitializer ole_initializer_;
114#endif
Lukasz Anforowicz2b2699732018-04-12 18:49:10115
dchengc963c7142016-04-08 03:55:22116 std::unique_ptr<TestingProfile> profile_;
Keishi Hattori0e45c022021-11-27 09:25:52117 raw_ptr<ExtensionService> extension_service_ = nullptr;
[email protected]11d42c82013-02-01 00:14:32118};
119
120} // namespace extensions
121
122#endif // CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_ENVIRONMENT_H_