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_test_notification_observer.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/extensions/features/feature_channel.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "content/public/browser/web_contents.h"
20 #include "extensions/browser/extension_host.h"
21 #include "extensions/browser/extension_system.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/feature_switch.h"
24 #include "extensions/common/manifest.h"
26 class ExtensionService
;
29 namespace extensions
{
30 class ExtensionCacheFake
;
35 // Base class for extension browser tests. Provides utilities for loading,
36 // unloading, and installing extensions.
37 class ExtensionBrowserTest
: virtual public InProcessBrowserTest
{
39 // Flags used to configure how the tests are run.
43 // Allow the extension to run in incognito mode.
44 kFlagEnableIncognito
= 1 << 0,
46 // Allow file access for the extension.
47 kFlagEnableFileAccess
= 1 << 1,
49 // Don't fail when the loaded manifest has warnings (should only be used
50 // when testing deprecated features).
51 kFlagIgnoreManifestWarnings
= 1 << 2,
53 // Allow older manifest versions (typically these can't be loaded - we allow
55 kFlagAllowOldManifestVersions
= 1 << 3,
58 ExtensionBrowserTest();
59 virtual ~ExtensionBrowserTest();
62 ExtensionService
* extension_service() {
63 return extensions::ExtensionSystem::Get(profile())->extension_service();
66 const std::string
& last_loaded_extension_id() {
67 return observer_
->last_loaded_extension_id();
70 // Get the profile to use.
71 virtual Profile
* profile();
73 static const extensions::Extension
* GetExtensionByPath(
74 const extensions::ExtensionSet
* extensions
, const base::FilePath
& path
);
76 // InProcessBrowserTest
77 virtual void SetUp() OVERRIDE
;
78 virtual void SetUpCommandLine(base::CommandLine
* command_line
) OVERRIDE
;
79 virtual void SetUpOnMainThread() OVERRIDE
;
81 const extensions::Extension
* LoadExtension(const base::FilePath
& path
);
83 // Load extension and enable it in incognito mode.
84 const extensions::Extension
* LoadExtensionIncognito(
85 const base::FilePath
& path
);
87 // Load extension from the |path| folder. |flags| is bit mask of values from
89 const extensions::Extension
* LoadExtensionWithFlags(
90 const base::FilePath
& path
, int flags
);
92 // Same as above, but sets the installation parameter to the extension
94 const extensions::Extension
* LoadExtensionWithInstallParam(
95 const base::FilePath
& path
,
97 const std::string
& install_param
);
99 // Loads unpacked extension from |path| with manifest |manifest_relative_path|
100 // and imitates that it is a component extension.
101 // |manifest_relative_path| is relative to |path|.
102 const extensions::Extension
* LoadExtensionAsComponentWithManifest(
103 const base::FilePath
& path
,
104 const base::FilePath::CharType
* manifest_relative_path
);
106 // Loads unpacked extension from |path| and imitates that it is a component
107 // extension. Equivalent to
108 // LoadExtensionAsComponentWithManifest(path, extensions::kManifestFilename).
109 const extensions::Extension
* LoadExtensionAsComponent(
110 const base::FilePath
& path
);
112 // Pack the extension in |dir_path| into a crx file and return its path.
113 // Return an empty FilePath if there were errors.
114 base::FilePath
PackExtension(const base::FilePath
& dir_path
);
116 // Pack the extension in |dir_path| into a crx file at |crx_path|, using the
117 // key |pem_path|. If |pem_path| does not exist, create a new key at
119 // Return the path to the crx file, or an empty FilePath if there were errors.
120 base::FilePath
PackExtensionWithOptions(const base::FilePath
& dir_path
,
121 const base::FilePath
& crx_path
,
122 const base::FilePath
& pem_path
,
123 const base::FilePath
& pem_out_path
);
125 // |expected_change| indicates how many extensions should be installed (or
126 // disabled, if negative).
127 // 1 means you expect a new install, 0 means you expect an upgrade, -1 means
128 // you expect a failed upgrade.
129 const extensions::Extension
* InstallExtension(const base::FilePath
& path
,
130 int expected_change
) {
131 return InstallOrUpdateExtension(
132 std::string(), path
, INSTALL_UI_TYPE_NONE
, expected_change
);
135 // Same as above, but an install source other than Manifest::INTERNAL can be
137 const extensions::Extension
* InstallExtension(
138 const base::FilePath
& path
,
140 extensions::Manifest::Location install_source
) {
141 return InstallOrUpdateExtension(std::string(),
143 INSTALL_UI_TYPE_NONE
,
148 // Installs extension as if it came from the Chrome Webstore.
149 const extensions::Extension
* InstallExtensionFromWebstore(
150 const base::FilePath
& path
, int expected_change
);
152 // Same as above but passes an id to CrxInstaller and does not allow a
153 // privilege increase.
154 const extensions::Extension
* UpdateExtension(const std::string
& id
,
155 const base::FilePath
& path
,
156 int expected_change
) {
157 return InstallOrUpdateExtension(id
, path
, INSTALL_UI_TYPE_NONE
,
161 // Same as UpdateExtension but waits for the extension to be idle first.
162 const extensions::Extension
* UpdateExtensionWaitForIdle(
163 const std::string
& id
, const base::FilePath
& path
, int expected_change
);
165 // Same as |InstallExtension| but with the normal extension UI showing up
166 // (for e.g. info bar on success).
167 const extensions::Extension
* InstallExtensionWithUI(
168 const base::FilePath
& path
,
169 int expected_change
) {
170 return InstallOrUpdateExtension(
171 std::string(), path
, INSTALL_UI_TYPE_NORMAL
, expected_change
);
174 const extensions::Extension
* InstallExtensionWithUIAutoConfirm(
175 const base::FilePath
& path
,
178 return InstallOrUpdateExtension(std::string(),
180 INSTALL_UI_TYPE_AUTO_CONFIRM
,
183 extensions::Extension::NO_FLAGS
);
186 const extensions::Extension
* InstallExtensionWithSourceAndFlags(
187 const base::FilePath
& path
,
189 extensions::Manifest::Location install_source
,
190 extensions::Extension::InitFromValueFlags creation_flags
) {
191 return InstallOrUpdateExtension(std::string(), path
, INSTALL_UI_TYPE_NONE
,
192 expected_change
, install_source
, browser(), creation_flags
, false);
195 // Begins install process but simulates a user cancel.
196 const extensions::Extension
* StartInstallButCancel(
197 const base::FilePath
& path
) {
198 return InstallOrUpdateExtension(
199 std::string(), path
, INSTALL_UI_TYPE_CANCEL
, 0);
202 void ReloadExtension(const std::string extension_id
);
204 void UnloadExtension(const std::string
& extension_id
);
206 void UninstallExtension(const std::string
& extension_id
);
208 void DisableExtension(const std::string
& extension_id
);
210 void EnableExtension(const std::string
& extension_id
);
212 // Wait for the total number of page actions to change to |count|.
213 bool WaitForPageActionCountChangeTo(int count
) {
214 return observer_
->WaitForPageActionCountChangeTo(count
);
217 // Wait for the number of visible page actions to change to |count|.
218 bool WaitForPageActionVisibilityChangeTo(int count
) {
219 return observer_
->WaitForPageActionVisibilityChangeTo(count
);
222 // Waits until an extension is installed and loaded. Returns true if an
223 // install happened before timeout.
224 bool WaitForExtensionInstall() {
225 return observer_
->WaitForExtensionInstall();
228 // Wait for an extension install error to be raised. Returns true if an
230 bool WaitForExtensionInstallError() {
231 return observer_
->WaitForExtensionInstallError();
234 // Waits until an extension is loaded and all view have loaded.
235 void WaitForExtensionAndViewLoad() {
236 return observer_
->WaitForExtensionAndViewLoad();
239 // Waits until an extension is loaded.
240 void WaitForExtensionLoad() {
241 return observer_
->WaitForExtensionLoad();
244 // Waits for an extension load error. Returns true if the error really
246 bool WaitForExtensionLoadError() {
247 return observer_
->WaitForExtensionLoadError();
250 // Wait for the specified extension to crash. Returns true if it really
252 bool WaitForExtensionCrash(const std::string
& extension_id
) {
253 return observer_
->WaitForExtensionCrash(extension_id
);
256 // Wait for the crx installer to be done. Returns true if it really is done.
257 bool WaitForCrxInstallerDone() {
258 return observer_
->WaitForCrxInstallerDone();
261 // Wait for all extension views to load.
262 bool WaitForExtensionViewsToLoad() {
263 return observer_
->WaitForExtensionViewsToLoad();
266 // Simulates a page calling window.open on an URL and waits for the
268 void OpenWindow(content::WebContents
* contents
,
270 bool newtab_process_should_equal_opener
,
271 content::WebContents
** newtab_result
);
273 // Simulates a page navigating itself to an URL and waits for the
275 void NavigateInRenderer(content::WebContents
* contents
, const GURL
& url
);
277 // Looks for an ExtensionHost whose URL has the given path component
278 // (including leading slash). Also verifies that the expected number of hosts
280 extensions::ExtensionHost
* FindHostWithPath(
281 extensions::ProcessManager
* manager
,
282 const std::string
& path
,
286 // extensions::browsertest_util::ExecuteScriptInBackgroundPage(profile(),
287 // extension_id, script).
288 std::string
ExecuteScriptInBackgroundPage(const std::string
& extension_id
,
289 const std::string
& script
);
292 // extensions::browsertest_util::ExecuteScriptInBackgroundPageNoWait(
293 // profile(), extension_id, script).
294 bool ExecuteScriptInBackgroundPageNoWait(const std::string
& extension_id
,
295 const std::string
& script
);
300 #if defined(OS_CHROMEOS)
301 // True if the command line should be tweaked as if ChromeOS user is
302 // already logged in.
303 bool set_chromeos_user_
;
306 // test_data/extensions.
307 base::FilePath test_data_dir_
;
309 scoped_ptr
<ExtensionTestNotificationObserver
> observer_
;
312 // Temporary directory for testing.
313 base::ScopedTempDir temp_dir_
;
315 // Specifies the type of UI (if any) to show during installation and what
316 // user action to simulate.
318 INSTALL_UI_TYPE_NONE
,
319 INSTALL_UI_TYPE_CANCEL
,
320 INSTALL_UI_TYPE_NORMAL
,
321 INSTALL_UI_TYPE_AUTO_CONFIRM
,
324 const extensions::Extension
* InstallOrUpdateExtension(
325 const std::string
& id
,
326 const base::FilePath
& path
,
327 InstallUIType ui_type
,
328 int expected_change
);
329 const extensions::Extension
* InstallOrUpdateExtension(
330 const std::string
& id
,
331 const base::FilePath
& path
,
332 InstallUIType ui_type
,
335 extensions::Extension::InitFromValueFlags creation_flags
);
336 const extensions::Extension
* InstallOrUpdateExtension(
337 const std::string
& id
,
338 const base::FilePath
& path
,
339 InstallUIType ui_type
,
341 extensions::Manifest::Location install_source
);
342 const extensions::Extension
* InstallOrUpdateExtension(
343 const std::string
& id
,
344 const base::FilePath
& path
,
345 InstallUIType ui_type
,
347 extensions::Manifest::Location install_source
,
349 extensions::Extension::InitFromValueFlags creation_flags
,
352 // Make the current channel "dev" for the duration of the test.
353 extensions::ScopedCurrentChannel current_channel_
;
355 // Disable external install UI.
356 extensions::FeatureSwitch::ScopedOverride
357 override_prompt_for_external_extensions_
;
359 // The default profile to be used.
362 // Cache cache implementation.
363 scoped_ptr
<extensions::ExtensionCacheFake
> test_extension_cache_
;
366 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_