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 "base/test/scoped_path_override.h"
15 #include "chrome/browser/extensions/extension_test_notification_observer.h"
16 #include "chrome/browser/profiles/profile.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/browser/extension_host.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/common/feature_switch.h"
25 #include "extensions/common/manifest.h"
27 class ExtensionService
;
30 namespace extensions
{
31 class ExtensionCacheFake
;
36 // Base class for extension browser tests. Provides utilities for loading,
37 // unloading, and installing extensions.
38 class ExtensionBrowserTest
: virtual public InProcessBrowserTest
{
40 // Flags used to configure how the tests are run.
44 // Allow the extension to run in incognito mode.
45 kFlagEnableIncognito
= 1 << 0,
47 // Allow file access for the extension.
48 kFlagEnableFileAccess
= 1 << 1,
50 // Don't fail when the loaded manifest has warnings (should only be used
51 // when testing deprecated features).
52 kFlagIgnoreManifestWarnings
= 1 << 2,
54 // Allow older manifest versions (typically these can't be loaded - we allow
56 kFlagAllowOldManifestVersions
= 1 << 3,
59 ExtensionBrowserTest();
60 ~ExtensionBrowserTest() override
;
63 ExtensionService
* extension_service() {
64 return extensions::ExtensionSystem::Get(profile())->extension_service();
67 const std::string
& last_loaded_extension_id() {
68 return observer_
->last_loaded_extension_id();
71 // Get the profile to use.
72 virtual Profile
* profile();
74 static const extensions::Extension
* GetExtensionByPath(
75 const extensions::ExtensionSet
& extensions
,
76 const base::FilePath
& path
);
78 // InProcessBrowserTest
79 void SetUp() override
;
80 void SetUpCommandLine(base::CommandLine
* command_line
) override
;
81 void SetUpOnMainThread() override
;
83 const extensions::Extension
* LoadExtension(const base::FilePath
& path
);
85 // Load extension and enable it in incognito mode.
86 const extensions::Extension
* LoadExtensionIncognito(
87 const base::FilePath
& path
);
89 // Load extension from the |path| folder. |flags| is bit mask of values from
91 const extensions::Extension
* LoadExtensionWithFlags(
92 const base::FilePath
& path
, int flags
);
94 // Same as above, but sets the installation parameter to the extension
96 const extensions::Extension
* LoadExtensionWithInstallParam(
97 const base::FilePath
& path
,
99 const std::string
& install_param
);
101 // Loads unpacked extension from |path| with manifest |manifest_relative_path|
102 // and imitates that it is a component extension.
103 // |manifest_relative_path| is relative to |path|.
104 const extensions::Extension
* LoadExtensionAsComponentWithManifest(
105 const base::FilePath
& path
,
106 const base::FilePath::CharType
* manifest_relative_path
);
108 // Loads unpacked extension from |path| and imitates that it is a component
109 // extension. Equivalent to
110 // LoadExtensionAsComponentWithManifest(path, extensions::kManifestFilename).
111 const extensions::Extension
* LoadExtensionAsComponent(
112 const base::FilePath
& path
);
114 // Pack the extension in |dir_path| into a crx file and return its path.
115 // Return an empty FilePath if there were errors.
116 base::FilePath
PackExtension(const base::FilePath
& dir_path
);
118 // Pack the extension in |dir_path| into a crx file at |crx_path|, using the
119 // key |pem_path|. If |pem_path| does not exist, create a new key at
121 // Return the path to the crx file, or an empty FilePath if there were errors.
122 base::FilePath
PackExtensionWithOptions(const base::FilePath
& dir_path
,
123 const base::FilePath
& crx_path
,
124 const base::FilePath
& pem_path
,
125 const base::FilePath
& pem_out_path
);
127 // |expected_change| indicates how many extensions should be installed (or
128 // disabled, if negative).
129 // 1 means you expect a new install, 0 means you expect an upgrade, -1 means
130 // you expect a failed upgrade.
131 const extensions::Extension
* InstallExtension(const base::FilePath
& path
,
132 int expected_change
) {
133 return InstallOrUpdateExtension(
134 std::string(), path
, INSTALL_UI_TYPE_NONE
, expected_change
);
137 // Same as above, but an install source other than Manifest::INTERNAL can be
139 const extensions::Extension
* InstallExtension(
140 const base::FilePath
& path
,
142 extensions::Manifest::Location install_source
) {
143 return InstallOrUpdateExtension(std::string(),
145 INSTALL_UI_TYPE_NONE
,
150 // Installs extension as if it came from the Chrome Webstore.
151 const extensions::Extension
* InstallExtensionFromWebstore(
152 const base::FilePath
& path
, int expected_change
);
154 // Same as above but passes an id to CrxInstaller and does not allow a
155 // privilege increase.
156 const extensions::Extension
* UpdateExtension(const std::string
& id
,
157 const base::FilePath
& path
,
158 int expected_change
) {
159 return InstallOrUpdateExtension(id
, path
, INSTALL_UI_TYPE_NONE
,
163 // Same as UpdateExtension but waits for the extension to be idle first.
164 const extensions::Extension
* UpdateExtensionWaitForIdle(
165 const std::string
& id
, const base::FilePath
& path
, int expected_change
);
167 // Same as |InstallExtension| but with the normal extension UI showing up
168 // (for e.g. info bar on success).
169 const extensions::Extension
* InstallExtensionWithUI(
170 const base::FilePath
& path
,
171 int expected_change
) {
172 return InstallOrUpdateExtension(
173 std::string(), path
, INSTALL_UI_TYPE_NORMAL
, expected_change
);
176 const extensions::Extension
* InstallExtensionWithUIAutoConfirm(
177 const base::FilePath
& path
,
180 return InstallOrUpdateExtension(std::string(),
182 INSTALL_UI_TYPE_AUTO_CONFIRM
,
185 extensions::Extension::NO_FLAGS
);
188 const extensions::Extension
* InstallExtensionWithSourceAndFlags(
189 const base::FilePath
& path
,
191 extensions::Manifest::Location install_source
,
192 extensions::Extension::InitFromValueFlags creation_flags
) {
193 return InstallOrUpdateExtension(std::string(),
195 INSTALL_UI_TYPE_NONE
,
204 const extensions::Extension
* InstallEphemeralAppWithSourceAndFlags(
205 const base::FilePath
& path
,
207 extensions::Manifest::Location install_source
,
208 extensions::Extension::InitFromValueFlags creation_flags
) {
209 return InstallOrUpdateExtension(std::string(),
211 INSTALL_UI_TYPE_NONE
,
220 // Begins install process but simulates a user cancel.
221 const extensions::Extension
* StartInstallButCancel(
222 const base::FilePath
& path
) {
223 return InstallOrUpdateExtension(
224 std::string(), path
, INSTALL_UI_TYPE_CANCEL
, 0);
227 void ReloadExtension(const std::string extension_id
);
229 void UnloadExtension(const std::string
& extension_id
);
231 void UninstallExtension(const std::string
& extension_id
);
233 void DisableExtension(const std::string
& extension_id
);
235 void EnableExtension(const std::string
& extension_id
);
237 // Wait for the number of visible page actions to change to |count|.
238 bool WaitForPageActionVisibilityChangeTo(int count
) {
239 return observer_
->WaitForPageActionVisibilityChangeTo(count
);
242 // Waits until an extension is installed and loaded. Returns true if an
243 // install happened before timeout.
244 bool WaitForExtensionInstall() {
245 return observer_
->WaitForExtensionInstall();
248 // Wait for an extension install error to be raised. Returns true if an
250 bool WaitForExtensionInstallError() {
251 return observer_
->WaitForExtensionInstallError();
254 // Waits until an extension is loaded and all view have loaded.
255 void WaitForExtensionAndViewLoad() {
256 return observer_
->WaitForExtensionAndViewLoad();
259 // Waits until an extension is loaded.
260 void WaitForExtensionLoad() {
261 return observer_
->WaitForExtensionLoad();
264 // Waits for an extension load error. Returns true if the error really
266 bool WaitForExtensionLoadError() {
267 return observer_
->WaitForExtensionLoadError();
270 // Wait for the specified extension to crash. Returns true if it really
272 bool WaitForExtensionCrash(const std::string
& extension_id
) {
273 return observer_
->WaitForExtensionCrash(extension_id
);
276 // Wait for the crx installer to be done. Returns true if it really is done.
277 bool WaitForCrxInstallerDone() {
278 return observer_
->WaitForCrxInstallerDone();
281 // Wait for all extension views to load.
282 bool WaitForExtensionViewsToLoad() {
283 return observer_
->WaitForExtensionViewsToLoad();
286 // Wait for the extension to be idle.
287 bool WaitForExtensionIdle(const std::string
& extension_id
) {
288 return observer_
->WaitForExtensionIdle(extension_id
);
291 // Wait for the extension to not be idle.
292 bool WaitForExtensionNotIdle(const std::string
& extension_id
) {
293 return observer_
->WaitForExtensionNotIdle(extension_id
);
296 // Simulates a page calling window.open on an URL and waits for the
298 void OpenWindow(content::WebContents
* contents
,
300 bool newtab_process_should_equal_opener
,
301 content::WebContents
** newtab_result
);
303 // Simulates a page navigating itself to an URL and waits for the
305 void NavigateInRenderer(content::WebContents
* contents
, const GURL
& url
);
307 // Looks for an ExtensionHost whose URL has the given path component
308 // (including leading slash). Also verifies that the expected number of hosts
310 extensions::ExtensionHost
* FindHostWithPath(
311 extensions::ProcessManager
* manager
,
312 const std::string
& path
,
316 // extensions::browsertest_util::ExecuteScriptInBackgroundPage(profile(),
317 // extension_id, script).
318 std::string
ExecuteScriptInBackgroundPage(const std::string
& extension_id
,
319 const std::string
& script
);
322 // extensions::browsertest_util::ExecuteScriptInBackgroundPageNoWait(
323 // profile(), extension_id, script).
324 bool ExecuteScriptInBackgroundPageNoWait(const std::string
& extension_id
,
325 const std::string
& script
);
330 #if defined(OS_CHROMEOS)
331 // True if the command line should be tweaked as if ChromeOS user is
332 // already logged in.
333 bool set_chromeos_user_
;
336 // test_data/extensions.
337 base::FilePath test_data_dir_
;
339 scoped_ptr
<ExtensionTestNotificationObserver
> observer_
;
342 // Temporary directory for testing.
343 base::ScopedTempDir temp_dir_
;
345 // Specifies the type of UI (if any) to show during installation and what
346 // user action to simulate.
348 INSTALL_UI_TYPE_NONE
,
349 INSTALL_UI_TYPE_CANCEL
,
350 INSTALL_UI_TYPE_NORMAL
,
351 INSTALL_UI_TYPE_AUTO_CONFIRM
,
354 const extensions::Extension
* InstallOrUpdateExtension(
355 const std::string
& id
,
356 const base::FilePath
& path
,
357 InstallUIType ui_type
,
358 int expected_change
);
359 const extensions::Extension
* InstallOrUpdateExtension(
360 const std::string
& id
,
361 const base::FilePath
& path
,
362 InstallUIType ui_type
,
365 extensions::Extension::InitFromValueFlags creation_flags
);
366 const extensions::Extension
* InstallOrUpdateExtension(
367 const std::string
& id
,
368 const base::FilePath
& path
,
369 InstallUIType ui_type
,
371 extensions::Manifest::Location install_source
);
372 const extensions::Extension
* InstallOrUpdateExtension(
373 const std::string
& id
,
374 const base::FilePath
& path
,
375 InstallUIType ui_type
,
377 extensions::Manifest::Location install_source
,
379 extensions::Extension::InitFromValueFlags creation_flags
,
383 // Make the current channel "dev" for the duration of the test.
384 extensions::ScopedCurrentChannel current_channel_
;
386 // Disable external install UI.
387 extensions::FeatureSwitch::ScopedOverride
388 override_prompt_for_external_extensions_
;
391 // Use mock shortcut directories to ensure app shortcuts are cleaned up.
392 base::ScopedPathOverride user_desktop_override_
;
393 base::ScopedPathOverride common_desktop_override_
;
394 base::ScopedPathOverride user_quick_launch_override_
;
395 base::ScopedPathOverride start_menu_override_
;
396 base::ScopedPathOverride common_start_menu_override_
;
399 // The default profile to be used.
402 // Cache cache implementation.
403 scoped_ptr
<extensions::ExtensionCacheFake
> test_extension_cache_
;
406 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_