Make default apps cache multiprofile friendly
[chromium-blink-merge.git] / chrome / browser / extensions / extension_browsertest.h
blob967f10d510543c2ce2e346fd743d704e12db7fc0
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_
8 #include <string>
10 #include "base/command_line.h"
11 #include "base/compiler_specific.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/ui/browser.h"
17 #include "chrome/common/extensions/extension.h"
18 #include "chrome/common/extensions/feature_switch.h"
19 #include "chrome/common/extensions/features/feature_channel.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/web_contents.h"
25 #include "extensions/common/manifest.h"
27 class ExtensionProcessManager;
28 class ExtensionService;
29 class ExtensionSet;
30 class Profile;
32 // Base class for extension browser tests. Provides utilities for loading,
33 // unloading, and installing extensions.
34 class ExtensionBrowserTest : virtual public InProcessBrowserTest,
35 public content::NotificationObserver {
36 protected:
37 // Flags used to configure how the tests are run.
38 enum Flags {
39 kFlagNone = 0,
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
52 // them for testing).
53 kFlagAllowOldManifestVersions = 1 << 3,
56 ExtensionBrowserTest();
57 virtual ~ExtensionBrowserTest();
59 // Useful accessors.
60 ExtensionService* extension_service() {
61 return extensions::ExtensionSystem::Get(profile())->extension_service();
64 // Get the profile to use.
65 Profile* profile();
67 static const extensions::Extension* GetExtensionByPath(
68 const ExtensionSet* extensions, const base::FilePath& path);
70 // InProcessBrowserTest
71 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
72 virtual void SetUpOnMainThread() OVERRIDE;
74 const extensions::Extension* LoadExtension(const base::FilePath& path);
76 // Same as above, but enables the extension in incognito mode first.
77 const extensions::Extension* LoadExtensionIncognito(
78 const base::FilePath& path);
80 const extensions::Extension* LoadExtensionWithFlags(
81 const base::FilePath& path, int flags);
83 // Loads unpacked extension from |path| with manifest |manifest_relative_path|
84 // and imitates that it is a component extension.
85 // |manifest_relative_path| is relative to |path|.
86 const extensions::Extension* LoadExtensionAsComponentWithManifest(
87 const base::FilePath& path,
88 const base::FilePath::CharType* manifest_relative_path);
90 // Loads unpacked extension from |path| and imitates that it is a component
91 // extension. Equivalent to
92 // LoadExtensionAsComponentWithManifest(path, extensions::kManifestFilename).
93 const extensions::Extension* LoadExtensionAsComponent(
94 const base::FilePath& path);
96 // Pack the extension in |dir_path| into a crx file and return its path.
97 // Return an empty FilePath if there were errors.
98 base::FilePath PackExtension(const base::FilePath& dir_path);
100 // Pack the extension in |dir_path| into a crx file at |crx_path|, using the
101 // key |pem_path|. If |pem_path| does not exist, create a new key at
102 // |pem_out_path|.
103 // Return the path to the crx file, or an empty FilePath if there were errors.
104 base::FilePath PackExtensionWithOptions(const base::FilePath& dir_path,
105 const base::FilePath& crx_path,
106 const base::FilePath& pem_path,
107 const base::FilePath& pem_out_path);
109 // |expected_change| indicates how many extensions should be installed (or
110 // disabled, if negative).
111 // 1 means you expect a new install, 0 means you expect an upgrade, -1 means
112 // you expect a failed upgrade.
113 const extensions::Extension* InstallExtension(const base::FilePath& path,
114 int expected_change) {
115 return InstallOrUpdateExtension(
116 std::string(), path, INSTALL_UI_TYPE_NONE, expected_change);
119 // Same as above, but an install source other than Manifest::INTERNAL can be
120 // specified.
121 const extensions::Extension* InstallExtension(
122 const base::FilePath& path,
123 int expected_change,
124 extensions::Manifest::Location install_source) {
125 return InstallOrUpdateExtension(std::string(),
126 path,
127 INSTALL_UI_TYPE_NONE,
128 expected_change,
129 install_source);
132 // Installs extension as if it came from the Chrome Webstore.
133 const extensions::Extension* InstallExtensionFromWebstore(
134 const base::FilePath& path, int expected_change);
136 // Same as above but passes an id to CrxInstaller and does not allow a
137 // privilege increase.
138 const extensions::Extension* UpdateExtension(const std::string& id,
139 const base::FilePath& path,
140 int expected_change) {
141 return InstallOrUpdateExtension(id, path, INSTALL_UI_TYPE_NONE,
142 expected_change);
145 // Same as UpdateExtension but waits for the extension to be idle first.
146 const extensions::Extension* UpdateExtensionWaitForIdle(
147 const std::string& id, const base::FilePath& path, int expected_change);
149 // Same as |InstallExtension| but with the normal extension UI showing up
150 // (for e.g. info bar on success).
151 const extensions::Extension* InstallExtensionWithUI(
152 const base::FilePath& path,
153 int expected_change) {
154 return InstallOrUpdateExtension(
155 std::string(), path, INSTALL_UI_TYPE_NORMAL, expected_change);
158 const extensions::Extension* InstallExtensionWithUIAutoConfirm(
159 const base::FilePath& path,
160 int expected_change,
161 Browser* browser) {
162 return InstallOrUpdateExtension(std::string(),
163 path,
164 INSTALL_UI_TYPE_AUTO_CONFIRM,
165 expected_change,
166 browser,
167 false);
170 // Begins install process but simulates a user cancel.
171 const extensions::Extension* StartInstallButCancel(
172 const base::FilePath& path) {
173 return InstallOrUpdateExtension(
174 std::string(), path, INSTALL_UI_TYPE_CANCEL, 0);
177 void ReloadExtension(const std::string extension_id);
179 void UnloadExtension(const std::string& extension_id);
181 void UninstallExtension(const std::string& extension_id);
183 void DisableExtension(const std::string& extension_id);
185 void EnableExtension(const std::string& extension_id);
187 // Wait for the total number of page actions to change to |count|.
188 bool WaitForPageActionCountChangeTo(int count);
190 // Wait for the number of visible page actions to change to |count|.
191 bool WaitForPageActionVisibilityChangeTo(int count);
193 // Waits until an extension is installed and loaded. Returns true if an
194 // install happened before timeout.
195 bool WaitForExtensionInstall();
197 // Wait for an extension install error to be raised. Returns true if an
198 // error was raised.
199 bool WaitForExtensionInstallError();
201 // Waits until an extension is loaded.
202 void WaitForExtensionLoad();
204 // Waits for an extension load error. Returns true if the error really
205 // happened.
206 bool WaitForExtensionLoadError();
208 // Wait for the specified extension to crash. Returns true if it really
209 // crashed.
210 bool WaitForExtensionCrash(const std::string& extension_id);
212 // Wait for the crx installer to be done. Returns true if it really is done.
213 bool WaitForCrxInstallerDone();
215 // Simulates a page calling window.open on an URL and waits for the
216 // navigation.
217 void OpenWindow(content::WebContents* contents,
218 const GURL& url,
219 bool newtab_process_should_equal_opener,
220 content::WebContents** newtab_result);
222 // Simulates a page navigating itself to an URL and waits for the
223 // navigation.
224 void NavigateInRenderer(content::WebContents* contents, const GURL& url);
226 // Looks for an ExtensionHost whose URL has the given path component
227 // (including leading slash). Also verifies that the expected number of hosts
228 // are loaded.
229 extensions::ExtensionHost* FindHostWithPath(ExtensionProcessManager* manager,
230 const std::string& path,
231 int expected_hosts);
233 // Returns
234 // extensions::browsertest_util::ExecuteScriptInBackgroundPage(profile(),
235 // extension_id, script).
236 std::string ExecuteScriptInBackgroundPage(const std::string& extension_id,
237 const std::string& script);
239 // content::NotificationObserver
240 virtual void Observe(int type,
241 const content::NotificationSource& source,
242 const content::NotificationDetails& details) OVERRIDE;
244 bool loaded_;
245 bool installed_;
247 // test_data/extensions.
248 base::FilePath test_data_dir_;
249 std::string last_loaded_extension_id_;
250 int extension_installs_observed_;
251 int extension_load_errors_observed_;
252 int crx_installers_done_observed_;
254 private:
255 // Temporary directory for testing.
256 base::ScopedTempDir temp_dir_;
258 // Specifies the type of UI (if any) to show during installation and what
259 // user action to simulate.
260 enum InstallUIType {
261 INSTALL_UI_TYPE_NONE,
262 INSTALL_UI_TYPE_CANCEL,
263 INSTALL_UI_TYPE_NORMAL,
264 INSTALL_UI_TYPE_AUTO_CONFIRM,
267 const extensions::Extension* InstallOrUpdateExtension(
268 const std::string& id,
269 const base::FilePath& path,
270 InstallUIType ui_type,
271 int expected_change);
272 const extensions::Extension* InstallOrUpdateExtension(
273 const std::string& id,
274 const base::FilePath& path,
275 InstallUIType ui_type,
276 int expected_change,
277 Browser* browser,
278 bool from_webstore);
279 const extensions::Extension* InstallOrUpdateExtension(
280 const std::string& id,
281 const base::FilePath& path,
282 InstallUIType ui_type,
283 int expected_change,
284 extensions::Manifest::Location install_source);
285 const extensions::Extension* InstallOrUpdateExtension(
286 const std::string& id,
287 const base::FilePath& path,
288 InstallUIType ui_type,
289 int expected_change,
290 extensions::Manifest::Location install_source,
291 Browser* browser,
292 bool from_webstore,
293 bool wait_for_idle);
295 bool WaitForExtensionViewsToLoad();
297 // When waiting for page action count to change, we wait until it reaches this
298 // value.
299 int target_page_action_count_;
301 // When waiting for visible page action count to change, we wait until it
302 // reaches this value.
303 int target_visible_page_action_count_;
305 // Make the current channel "dev" for the duration of the test.
306 extensions::ScopedCurrentChannel current_channel_;
308 // Disable external install UI.
309 extensions::FeatureSwitch::ScopedOverride
310 override_prompt_for_external_extensions_;
312 // The default profile to be used.
313 Profile* profile_;
316 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_