blob: 2ae201f5a5e61fdc2c9d5c9272866f57402f5a9f [file] [log] [blame]
[email protected]863e6472012-01-24 19:33:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]17c4f3c2009-07-04 16:36:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]3f58d8552009-08-14 23:59:375#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]17c4f3c2009-07-04 16:36:258
9#include <string>
10
11#include "base/command_line.h"
[email protected]17902752011-08-31 22:52:5412#include "base/compiler_specific.h"
[email protected]17c4f3c2009-07-04 16:36:2513#include "base/file_path.h"
[email protected]e0785902011-05-19 23:34:1714#include "base/scoped_temp_dir.h"
[email protected]af44e7fb2011-07-29 18:32:3215#include "chrome/test/base/in_process_browser_test.h"
[email protected]6c2381d2011-10-19 02:52:5316#include "content/public/browser/notification_details.h"
17#include "content/public/browser/notification_observer.h"
[email protected]0d6e9bd2011-10-18 04:29:1618#include "content/public/browser/notification_types.h"
[email protected]17c4f3c2009-07-04 16:36:2519
[email protected]a964e112011-04-14 21:52:5120class Extension;
21
[email protected]17c4f3c2009-07-04 16:36:2522// Base class for extension browser tests. Provides utilities for loading,
23// unloading, and installing extensions.
24class ExtensionBrowserTest
[email protected]6c2381d2011-10-19 02:52:5325 : public InProcessBrowserTest, public content::NotificationObserver {
[email protected]17c4f3c2009-07-04 16:36:2526 protected:
[email protected]d818e07f2010-02-10 13:10:0327 ExtensionBrowserTest();
[email protected]178f8512012-02-09 01:49:3628 virtual ~ExtensionBrowserTest();
[email protected]d818e07f2010-02-10 13:10:0329
[email protected]1cc91fe2011-11-21 14:48:4330 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
[email protected]84f4dc02011-11-29 21:58:2631
[email protected]a964e112011-04-14 21:52:5132 const Extension* LoadExtension(const FilePath& path);
[email protected]2a409532009-08-28 19:39:4433
[email protected]db7331a2010-02-25 22:10:5034 // Same as above, but enables the extension in incognito mode first.
[email protected]a964e112011-04-14 21:52:5135 const Extension* LoadExtensionIncognito(const FilePath& path);
[email protected]61b55b62011-03-24 09:03:1036
[email protected]c1bf62c72011-07-15 22:18:4237 const Extension* LoadExtensionWithOptions(const FilePath& path,
38 bool incognito_enabled,
39 bool fileaccess_enabled);
[email protected]a964e112011-04-14 21:52:5140
41 // Loads extension and imitates that it is a component extension.
[email protected]863e6472012-01-24 19:33:5842 const Extension* LoadExtensionAsComponent(const FilePath& path);
[email protected]c7c401d2011-03-16 10:20:0143
[email protected]59e03362011-01-21 21:24:0844 // Pack the extension in |dir_path| into a crx file and return its path.
45 // Return an empty FilePath if there were errors.
46 FilePath PackExtension(const FilePath& dir_path);
47
[email protected]f66a50a2011-11-02 23:53:4648 // Pack the extension in |dir_path| into a crx file at |crx_path|, using the
49 // key |pem_path|. If |pem_path| does not exist, create a new key at
50 // |pem_out_path|.
51 // Return the path to the crx file, or an empty FilePath if there were errors.
52 FilePath PackExtensionWithOptions(const FilePath& dir_path,
53 const FilePath& crx_path,
54 const FilePath& pem_path,
55 const FilePath& pem_out_path);
56
[email protected]0c6da502009-08-14 22:32:3957 // |expected_change| indicates how many extensions should be installed (or
58 // disabled, if negative).
59 // 1 means you expect a new install, 0 means you expect an upgrade, -1 means
60 // you expect a failed upgrade.
[email protected]84f4dc02011-11-29 21:58:2661 const Extension* InstallExtension(const FilePath& path, int expected_change) {
[email protected]8fd16f502010-04-22 18:23:1862 return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NONE,
63 expected_change);
[email protected]2a409532009-08-28 19:39:4464 }
65
[email protected]cefa749d2011-08-11 22:21:4866 // Installs extension as if it came from the Chrome Webstore.
[email protected]84f4dc02011-11-29 21:58:2667 const Extension* InstallExtensionFromWebstore(
68 const FilePath& path, int expected_change);
[email protected]cefa749d2011-08-11 22:21:4869
[email protected]6dfbbf82010-03-12 23:09:1670 // Same as above but passes an id to CrxInstaller and does not allow a
71 // privilege increase.
[email protected]84f4dc02011-11-29 21:58:2672 const Extension* UpdateExtension(const std::string& id, const FilePath& path,
73 int expected_change) {
[email protected]8fd16f502010-04-22 18:23:1874 return InstallOrUpdateExtension(id, path, INSTALL_UI_TYPE_NONE,
75 expected_change);
76 }
77
78 // Same as |InstallExtension| but with the normal extension UI showing up
79 // (for e.g. info bar on success).
[email protected]84f4dc02011-11-29 21:58:2680 const Extension* InstallExtensionWithUI(const FilePath& path,
81 int expected_change) {
[email protected]8fd16f502010-04-22 18:23:1882 return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NORMAL,
83 expected_change);
[email protected]c70013bd2010-01-20 21:50:0384 }
[email protected]84f4dc02011-11-29 21:58:2685 const Extension* InstallExtensionWithUIAutoConfirm(const FilePath& path,
86 int expected_change,
87 Profile* profile) {
[email protected]59e03362011-01-21 21:24:0888 return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_AUTO_CONFIRM,
[email protected]cefa749d2011-08-11 22:21:4889 expected_change, profile, false);
[email protected]59e03362011-01-21 21:24:0890 }
[email protected]c70013bd2010-01-20 21:50:0391
92 // Begins install process but simulates a user cancel.
[email protected]84f4dc02011-11-29 21:58:2693 const Extension* StartInstallButCancel(const FilePath& path) {
[email protected]8fd16f502010-04-22 18:23:1894 return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_CANCEL, 0);
[email protected]2a409532009-08-28 19:39:4495 }
96
[email protected]e1725842009-10-20 06:40:1597 void ReloadExtension(const std::string& extension_id);
98
[email protected]57f71b92009-09-11 19:31:3899 void UnloadExtension(const std::string& extension_id);
100
[email protected]17c4f3c2009-07-04 16:36:25101 void UninstallExtension(const std::string& extension_id);
102
[email protected]7d9ad0b32010-02-12 21:44:45103 void DisableExtension(const std::string& extension_id);
104
105 void EnableExtension(const std::string& extension_id);
106
[email protected]57f71b92009-09-11 19:31:38107 // Wait for the total number of page actions to change to |count|.
108 bool WaitForPageActionCountChangeTo(int count);
109
[email protected]361b28a2009-07-09 21:30:53110 // Wait for the number of visible page actions to change to |count|.
111 bool WaitForPageActionVisibilityChangeTo(int count);
112
[email protected]f4ea11282009-10-14 00:19:31113 // Waits until an extension is installed and loaded. Returns true if an
114 // install happened before timeout.
115 bool WaitForExtensionInstall();
116
117 // Wait for an extension install error to be raised. Returns true if an
118 // error was raised.
119 bool WaitForExtensionInstallError();
120
[email protected]1eb175082010-02-10 09:26:16121 // Waits until an extension is loaded.
122 void WaitForExtensionLoad();
123
[email protected]bbcde9102012-03-25 22:40:49124 // Waits for an extension load error. Returns true if the error really
125 // happened.
126 bool WaitForExtensionLoadError();
127
[email protected]1eb175082010-02-10 09:26:16128 // Wait for the specified extension to crash. Returns true if it really
129 // crashed.
130 bool WaitForExtensionCrash(const std::string& extension_id);
131
[email protected]6c2381d2011-10-19 02:52:53132 // content::NotificationObserver
[email protected]432115822011-07-10 15:52:27133 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53134 const content::NotificationSource& source,
135 const content::NotificationDetails& details) OVERRIDE;
[email protected]25fd1b2e2009-08-17 20:57:14136
[email protected]17c4f3c2009-07-04 16:36:25137 bool loaded_;
138 bool installed_;
[email protected]3b355642010-02-05 16:01:49139
140 // test_data/extensions.
[email protected]17c4f3c2009-07-04 16:36:25141 FilePath test_data_dir_;
[email protected]84ac7f32009-10-06 06:17:54142 std::string last_loaded_extension_id_;
[email protected]f4ea11282009-10-14 00:19:31143 int extension_installs_observed_;
[email protected]bbcde9102012-03-25 22:40:49144 int extension_load_errors_observed_;
[email protected]17c4f3c2009-07-04 16:36:25145
146 private:
[email protected]3a305db2011-04-12 13:40:53147 // Temporary directory for testing.
148 ScopedTempDir temp_dir_;
149
[email protected]8fd16f502010-04-22 18:23:18150 // Specifies the type of UI (if any) to show during installation and what
151 // user action to simulate.
152 enum InstallUIType {
153 INSTALL_UI_TYPE_NONE,
154 INSTALL_UI_TYPE_CANCEL,
155 INSTALL_UI_TYPE_NORMAL,
[email protected]59e03362011-01-21 21:24:08156 INSTALL_UI_TYPE_AUTO_CONFIRM,
[email protected]8fd16f502010-04-22 18:23:18157 };
158
[email protected]84f4dc02011-11-29 21:58:26159 const Extension* InstallOrUpdateExtension(const std::string& id,
160 const FilePath& path,
161 InstallUIType ui_type,
162 int expected_change);
163 const Extension* InstallOrUpdateExtension(const std::string& id,
164 const FilePath& path,
165 InstallUIType ui_type,
166 int expected_change,
167 Profile* profile,
168 bool from_webstore);
[email protected]2a409532009-08-28 19:39:44169
[email protected]17c4f3c2009-07-04 16:36:25170 bool WaitForExtensionHostsToLoad();
[email protected]d818e07f2010-02-10 13:10:03171
172 // When waiting for page action count to change, we wait until it reaches this
173 // value.
174 int target_page_action_count_;
175
176 // When waiting for visible page action count to change, we wait until it
177 // reaches this value.
178 int target_visible_page_action_count_;
[email protected]17c4f3c2009-07-04 16:36:25179};
180
[email protected]3f58d8552009-08-14 23:59:37181#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_