1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_
10 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "chrome/browser/extensions/extension_host.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_test_notification_observer.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/common/extensions/features/feature_channel.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "content/public/browser/web_contents.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/feature_switch.h"
23 #include "extensions/common/manifest.h"
25 class ExtensionService
;
28 namespace extensions
{
33 // Base class for extension browser tests. Provides utilities for loading,
34 // unloading, and installing extensions.
35 class ExtensionBrowserTest
: virtual public InProcessBrowserTest
{
37 // Flags used to configure how the tests are run.
41 // Allow the extension to run in incognito mode.
42 kFlagEnableIncognito
= 1 << 0,
44 // Allow file access for the extension.
45 kFlagEnableFileAccess
= 1 << 1,
47 // Don't fail when the loaded manifest has warnings (should only be used
48 // when testing deprecated features).
49 kFlagIgnoreManifestWarnings
= 1 << 2,
51 // Allow older manifest versions (typically these can't be loaded - we allow
53 kFlagAllowOldManifestVersions
= 1 << 3,
56 ExtensionBrowserTest();
57 virtual ~ExtensionBrowserTest();
60 ExtensionService
* extension_service() {
61 return extensions::ExtensionSystem::Get(profile())->extension_service();
64 const std::string
& last_loaded_extension_id() {
65 return observer_
->last_loaded_extension_id();
68 // Get the profile to use.
69 virtual Profile
* profile();
71 static const extensions::Extension
* GetExtensionByPath(
72 const extensions::ExtensionSet
* extensions
, const base::FilePath
& path
);
74 // InProcessBrowserTest
75 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
;
76 virtual void SetUpOnMainThread() OVERRIDE
;
78 const extensions::Extension
* LoadExtension(const base::FilePath
& path
);
80 // Same as above, but enables the extension in incognito mode first.
81 const extensions::Extension
* LoadExtensionIncognito(
82 const base::FilePath
& path
);
84 const extensions::Extension
* LoadExtensionWithFlags(
85 const base::FilePath
& path
, int flags
);
87 // Loads unpacked extension from |path| with manifest |manifest_relative_path|
88 // and imitates that it is a component extension.
89 // |manifest_relative_path| is relative to |path|.
90 const extensions::Extension
* LoadExtensionAsComponentWithManifest(
91 const base::FilePath
& path
,
92 const base::FilePath::CharType
* manifest_relative_path
);
94 // Loads unpacked extension from |path| and imitates that it is a component
95 // extension. Equivalent to
96 // LoadExtensionAsComponentWithManifest(path, extensions::kManifestFilename).
97 const extensions::Extension
* LoadExtensionAsComponent(
98 const base::FilePath
& path
);
100 // Pack the extension in |dir_path| into a crx file and return its path.
101 // Return an empty FilePath if there were errors.
102 base::FilePath
PackExtension(const base::FilePath
& dir_path
);
104 // Pack the extension in |dir_path| into a crx file at |crx_path|, using the
105 // key |pem_path|. If |pem_path| does not exist, create a new key at
107 // Return the path to the crx file, or an empty FilePath if there were errors.
108 base::FilePath
PackExtensionWithOptions(const base::FilePath
& dir_path
,
109 const base::FilePath
& crx_path
,
110 const base::FilePath
& pem_path
,
111 const base::FilePath
& pem_out_path
);
113 // |expected_change| indicates how many extensions should be installed (or
114 // disabled, if negative).
115 // 1 means you expect a new install, 0 means you expect an upgrade, -1 means
116 // you expect a failed upgrade.
117 const extensions::Extension
* InstallExtension(const base::FilePath
& path
,
118 int expected_change
) {
119 return InstallOrUpdateExtension(
120 std::string(), path
, INSTALL_UI_TYPE_NONE
, expected_change
);
123 // Same as above, but an install source other than Manifest::INTERNAL can be
125 const extensions::Extension
* InstallExtension(
126 const base::FilePath
& path
,
128 extensions::Manifest::Location install_source
) {
129 return InstallOrUpdateExtension(std::string(),
131 INSTALL_UI_TYPE_NONE
,
136 // Installs extension as if it came from the Chrome Webstore.
137 const extensions::Extension
* InstallExtensionFromWebstore(
138 const base::FilePath
& path
, int expected_change
);
140 // Same as above but passes an id to CrxInstaller and does not allow a
141 // privilege increase.
142 const extensions::Extension
* UpdateExtension(const std::string
& id
,
143 const base::FilePath
& path
,
144 int expected_change
) {
145 return InstallOrUpdateExtension(id
, path
, INSTALL_UI_TYPE_NONE
,
149 // Same as UpdateExtension but waits for the extension to be idle first.
150 const extensions::Extension
* UpdateExtensionWaitForIdle(
151 const std::string
& id
, const base::FilePath
& path
, int expected_change
);
153 // Same as |InstallExtension| but with the normal extension UI showing up
154 // (for e.g. info bar on success).
155 const extensions::Extension
* InstallExtensionWithUI(
156 const base::FilePath
& path
,
157 int expected_change
) {
158 return InstallOrUpdateExtension(
159 std::string(), path
, INSTALL_UI_TYPE_NORMAL
, expected_change
);
162 const extensions::Extension
* InstallExtensionWithUIAutoConfirm(
163 const base::FilePath
& path
,
166 return InstallOrUpdateExtension(std::string(),
168 INSTALL_UI_TYPE_AUTO_CONFIRM
,
171 extensions::Extension::NO_FLAGS
);
174 const extensions::Extension
* InstallExtensionWithSourceAndFlags(
175 const base::FilePath
& path
,
177 extensions::Manifest::Location install_source
,
178 extensions::Extension::InitFromValueFlags creation_flags
) {
179 return InstallOrUpdateExtension(std::string(), path
, INSTALL_UI_TYPE_NONE
,
180 expected_change
, install_source
, browser(), creation_flags
, false);
183 // Begins install process but simulates a user cancel.
184 const extensions::Extension
* StartInstallButCancel(
185 const base::FilePath
& path
) {
186 return InstallOrUpdateExtension(
187 std::string(), path
, INSTALL_UI_TYPE_CANCEL
, 0);
190 void ReloadExtension(const std::string extension_id
);
192 void UnloadExtension(const std::string
& extension_id
);
194 void UninstallExtension(const std::string
& extension_id
);
196 void DisableExtension(const std::string
& extension_id
);
198 void EnableExtension(const std::string
& extension_id
);
200 // Wait for the total number of page actions to change to |count|.
201 bool WaitForPageActionCountChangeTo(int count
) {
202 return observer_
->WaitForPageActionCountChangeTo(count
);
205 // Wait for the number of visible page actions to change to |count|.
206 bool WaitForPageActionVisibilityChangeTo(int count
) {
207 return observer_
->WaitForPageActionVisibilityChangeTo(count
);
210 // Waits until an extension is installed and loaded. Returns true if an
211 // install happened before timeout.
212 bool WaitForExtensionInstall() {
213 return observer_
->WaitForExtensionInstall();
216 // Wait for an extension install error to be raised. Returns true if an
218 bool WaitForExtensionInstallError() {
219 return observer_
->WaitForExtensionInstallError();
222 // Waits until an extension is loaded and all view have loaded.
223 void WaitForExtensionAndViewLoad() {
224 return observer_
->WaitForExtensionAndViewLoad();
227 // Waits until an extension is loaded.
228 void WaitForExtensionLoad() {
229 return observer_
->WaitForExtensionLoad();
232 // Waits for an extension load error. Returns true if the error really
234 bool WaitForExtensionLoadError() {
235 return observer_
->WaitForExtensionLoadError();
238 // Wait for the specified extension to crash. Returns true if it really
240 bool WaitForExtensionCrash(const std::string
& extension_id
) {
241 return observer_
->WaitForExtensionCrash(extension_id
);
244 // Wait for the crx installer to be done. Returns true if it really is done.
245 bool WaitForCrxInstallerDone() {
246 return observer_
->WaitForCrxInstallerDone();
249 // Wait for all extension views to load.
250 bool WaitForExtensionViewsToLoad() {
251 return observer_
->WaitForExtensionViewsToLoad();
254 // Simulates a page calling window.open on an URL and waits for the
256 void OpenWindow(content::WebContents
* contents
,
258 bool newtab_process_should_equal_opener
,
259 content::WebContents
** newtab_result
);
261 // Simulates a page navigating itself to an URL and waits for the
263 void NavigateInRenderer(content::WebContents
* contents
, const GURL
& url
);
265 // Looks for an ExtensionHost whose URL has the given path component
266 // (including leading slash). Also verifies that the expected number of hosts
268 extensions::ExtensionHost
* FindHostWithPath(
269 extensions::ProcessManager
* manager
,
270 const std::string
& path
,
274 // extensions::browsertest_util::ExecuteScriptInBackgroundPage(profile(),
275 // extension_id, script).
276 std::string
ExecuteScriptInBackgroundPage(const std::string
& extension_id
,
277 const std::string
& script
);
282 #if defined(OS_CHROMEOS)
283 // True if the command line should be tweaked as if ChromeOS user is
284 // already logged in.
285 bool set_chromeos_user_
;
288 // test_data/extensions.
289 base::FilePath test_data_dir_
;
291 scoped_ptr
<ExtensionTestNotificationObserver
> observer_
;
294 // Temporary directory for testing.
295 base::ScopedTempDir temp_dir_
;
297 // Specifies the type of UI (if any) to show during installation and what
298 // user action to simulate.
300 INSTALL_UI_TYPE_NONE
,
301 INSTALL_UI_TYPE_CANCEL
,
302 INSTALL_UI_TYPE_NORMAL
,
303 INSTALL_UI_TYPE_AUTO_CONFIRM
,
306 const extensions::Extension
* InstallOrUpdateExtension(
307 const std::string
& id
,
308 const base::FilePath
& path
,
309 InstallUIType ui_type
,
310 int expected_change
);
311 const extensions::Extension
* InstallOrUpdateExtension(
312 const std::string
& id
,
313 const base::FilePath
& path
,
314 InstallUIType ui_type
,
317 extensions::Extension::InitFromValueFlags creation_flags
);
318 const extensions::Extension
* InstallOrUpdateExtension(
319 const std::string
& id
,
320 const base::FilePath
& path
,
321 InstallUIType ui_type
,
323 extensions::Manifest::Location install_source
);
324 const extensions::Extension
* InstallOrUpdateExtension(
325 const std::string
& id
,
326 const base::FilePath
& path
,
327 InstallUIType ui_type
,
329 extensions::Manifest::Location install_source
,
331 extensions::Extension::InitFromValueFlags creation_flags
,
334 // Make the current channel "dev" for the duration of the test.
335 extensions::ScopedCurrentChannel current_channel_
;
337 // Disable external install UI.
338 extensions::FeatureSwitch::ScopedOverride
339 override_prompt_for_external_extensions_
;
341 // The default profile to be used.
345 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_