NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / extension_browsertest.h
blob0588a21334839be31f5cb618778d999489c60db7
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"
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_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_system.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/feature_switch.h"
24 #include "extensions/common/manifest.h"
26 class ExtensionService;
27 class Profile;
29 namespace extensions {
30 class ExtensionCacheFake;
31 class ExtensionSet;
32 class ProcessManager;
35 // Base class for extension browser tests. Provides utilities for loading,
36 // unloading, and installing extensions.
37 class ExtensionBrowserTest : virtual public InProcessBrowserTest {
38 protected:
39 // Flags used to configure how the tests are run.
40 enum Flags {
41 kFlagNone = 0,
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
54 // them for testing).
55 kFlagAllowOldManifestVersions = 1 << 3,
58 ExtensionBrowserTest();
59 virtual ~ExtensionBrowserTest();
61 // Useful accessors.
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(CommandLine* command_line) OVERRIDE;
79 virtual void SetUpOnMainThread() OVERRIDE;
81 const extensions::Extension* LoadExtension(const base::FilePath& path);
83 // Same as above, but enables the extension in incognito mode first.
84 const extensions::Extension* LoadExtensionIncognito(
85 const base::FilePath& path);
87 const extensions::Extension* LoadExtensionWithFlags(
88 const base::FilePath& path, int flags);
90 // Loads unpacked extension from |path| with manifest |manifest_relative_path|
91 // and imitates that it is a component extension.
92 // |manifest_relative_path| is relative to |path|.
93 const extensions::Extension* LoadExtensionAsComponentWithManifest(
94 const base::FilePath& path,
95 const base::FilePath::CharType* manifest_relative_path);
97 // Loads unpacked extension from |path| and imitates that it is a component
98 // extension. Equivalent to
99 // LoadExtensionAsComponentWithManifest(path, extensions::kManifestFilename).
100 const extensions::Extension* LoadExtensionAsComponent(
101 const base::FilePath& path);
103 // Pack the extension in |dir_path| into a crx file and return its path.
104 // Return an empty FilePath if there were errors.
105 base::FilePath PackExtension(const base::FilePath& dir_path);
107 // Pack the extension in |dir_path| into a crx file at |crx_path|, using the
108 // key |pem_path|. If |pem_path| does not exist, create a new key at
109 // |pem_out_path|.
110 // Return the path to the crx file, or an empty FilePath if there were errors.
111 base::FilePath PackExtensionWithOptions(const base::FilePath& dir_path,
112 const base::FilePath& crx_path,
113 const base::FilePath& pem_path,
114 const base::FilePath& pem_out_path);
116 // |expected_change| indicates how many extensions should be installed (or
117 // disabled, if negative).
118 // 1 means you expect a new install, 0 means you expect an upgrade, -1 means
119 // you expect a failed upgrade.
120 const extensions::Extension* InstallExtension(const base::FilePath& path,
121 int expected_change) {
122 return InstallOrUpdateExtension(
123 std::string(), path, INSTALL_UI_TYPE_NONE, expected_change);
126 // Same as above, but an install source other than Manifest::INTERNAL can be
127 // specified.
128 const extensions::Extension* InstallExtension(
129 const base::FilePath& path,
130 int expected_change,
131 extensions::Manifest::Location install_source) {
132 return InstallOrUpdateExtension(std::string(),
133 path,
134 INSTALL_UI_TYPE_NONE,
135 expected_change,
136 install_source);
139 // Installs extension as if it came from the Chrome Webstore.
140 const extensions::Extension* InstallExtensionFromWebstore(
141 const base::FilePath& path, int expected_change);
143 // Same as above but passes an id to CrxInstaller and does not allow a
144 // privilege increase.
145 const extensions::Extension* UpdateExtension(const std::string& id,
146 const base::FilePath& path,
147 int expected_change) {
148 return InstallOrUpdateExtension(id, path, INSTALL_UI_TYPE_NONE,
149 expected_change);
152 // Same as UpdateExtension but waits for the extension to be idle first.
153 const extensions::Extension* UpdateExtensionWaitForIdle(
154 const std::string& id, const base::FilePath& path, int expected_change);
156 // Same as |InstallExtension| but with the normal extension UI showing up
157 // (for e.g. info bar on success).
158 const extensions::Extension* InstallExtensionWithUI(
159 const base::FilePath& path,
160 int expected_change) {
161 return InstallOrUpdateExtension(
162 std::string(), path, INSTALL_UI_TYPE_NORMAL, expected_change);
165 const extensions::Extension* InstallExtensionWithUIAutoConfirm(
166 const base::FilePath& path,
167 int expected_change,
168 Browser* browser) {
169 return InstallOrUpdateExtension(std::string(),
170 path,
171 INSTALL_UI_TYPE_AUTO_CONFIRM,
172 expected_change,
173 browser,
174 extensions::Extension::NO_FLAGS);
177 const extensions::Extension* InstallExtensionWithSourceAndFlags(
178 const base::FilePath& path,
179 int expected_change,
180 extensions::Manifest::Location install_source,
181 extensions::Extension::InitFromValueFlags creation_flags) {
182 return InstallOrUpdateExtension(std::string(), path, INSTALL_UI_TYPE_NONE,
183 expected_change, install_source, browser(), creation_flags, false);
186 // Begins install process but simulates a user cancel.
187 const extensions::Extension* StartInstallButCancel(
188 const base::FilePath& path) {
189 return InstallOrUpdateExtension(
190 std::string(), path, INSTALL_UI_TYPE_CANCEL, 0);
193 void ReloadExtension(const std::string extension_id);
195 void UnloadExtension(const std::string& extension_id);
197 void UninstallExtension(const std::string& extension_id);
199 void DisableExtension(const std::string& extension_id);
201 void EnableExtension(const std::string& extension_id);
203 // Wait for the total number of page actions to change to |count|.
204 bool WaitForPageActionCountChangeTo(int count) {
205 return observer_->WaitForPageActionCountChangeTo(count);
208 // Wait for the number of visible page actions to change to |count|.
209 bool WaitForPageActionVisibilityChangeTo(int count) {
210 return observer_->WaitForPageActionVisibilityChangeTo(count);
213 // Waits until an extension is installed and loaded. Returns true if an
214 // install happened before timeout.
215 bool WaitForExtensionInstall() {
216 return observer_->WaitForExtensionInstall();
219 // Wait for an extension install error to be raised. Returns true if an
220 // error was raised.
221 bool WaitForExtensionInstallError() {
222 return observer_->WaitForExtensionInstallError();
225 // Waits until an extension is loaded and all view have loaded.
226 void WaitForExtensionAndViewLoad() {
227 return observer_->WaitForExtensionAndViewLoad();
230 // Waits until an extension is loaded.
231 void WaitForExtensionLoad() {
232 return observer_->WaitForExtensionLoad();
235 // Waits for an extension load error. Returns true if the error really
236 // happened.
237 bool WaitForExtensionLoadError() {
238 return observer_->WaitForExtensionLoadError();
241 // Wait for the specified extension to crash. Returns true if it really
242 // crashed.
243 bool WaitForExtensionCrash(const std::string& extension_id) {
244 return observer_->WaitForExtensionCrash(extension_id);
247 // Wait for the crx installer to be done. Returns true if it really is done.
248 bool WaitForCrxInstallerDone() {
249 return observer_->WaitForCrxInstallerDone();
252 // Wait for all extension views to load.
253 bool WaitForExtensionViewsToLoad() {
254 return observer_->WaitForExtensionViewsToLoad();
257 // Simulates a page calling window.open on an URL and waits for the
258 // navigation.
259 void OpenWindow(content::WebContents* contents,
260 const GURL& url,
261 bool newtab_process_should_equal_opener,
262 content::WebContents** newtab_result);
264 // Simulates a page navigating itself to an URL and waits for the
265 // navigation.
266 void NavigateInRenderer(content::WebContents* contents, const GURL& url);
268 // Looks for an ExtensionHost whose URL has the given path component
269 // (including leading slash). Also verifies that the expected number of hosts
270 // are loaded.
271 extensions::ExtensionHost* FindHostWithPath(
272 extensions::ProcessManager* manager,
273 const std::string& path,
274 int expected_hosts);
276 // Returns
277 // extensions::browsertest_util::ExecuteScriptInBackgroundPage(profile(),
278 // extension_id, script).
279 std::string ExecuteScriptInBackgroundPage(const std::string& extension_id,
280 const std::string& script);
282 bool loaded_;
283 bool installed_;
285 #if defined(OS_CHROMEOS)
286 // True if the command line should be tweaked as if ChromeOS user is
287 // already logged in.
288 bool set_chromeos_user_;
289 #endif
291 // test_data/extensions.
292 base::FilePath test_data_dir_;
294 scoped_ptr<ExtensionTestNotificationObserver> observer_;
296 private:
297 // Temporary directory for testing.
298 base::ScopedTempDir temp_dir_;
300 // Specifies the type of UI (if any) to show during installation and what
301 // user action to simulate.
302 enum InstallUIType {
303 INSTALL_UI_TYPE_NONE,
304 INSTALL_UI_TYPE_CANCEL,
305 INSTALL_UI_TYPE_NORMAL,
306 INSTALL_UI_TYPE_AUTO_CONFIRM,
309 const extensions::Extension* InstallOrUpdateExtension(
310 const std::string& id,
311 const base::FilePath& path,
312 InstallUIType ui_type,
313 int expected_change);
314 const extensions::Extension* InstallOrUpdateExtension(
315 const std::string& id,
316 const base::FilePath& path,
317 InstallUIType ui_type,
318 int expected_change,
319 Browser* browser,
320 extensions::Extension::InitFromValueFlags creation_flags);
321 const extensions::Extension* InstallOrUpdateExtension(
322 const std::string& id,
323 const base::FilePath& path,
324 InstallUIType ui_type,
325 int expected_change,
326 extensions::Manifest::Location install_source);
327 const extensions::Extension* InstallOrUpdateExtension(
328 const std::string& id,
329 const base::FilePath& path,
330 InstallUIType ui_type,
331 int expected_change,
332 extensions::Manifest::Location install_source,
333 Browser* browser,
334 extensions::Extension::InitFromValueFlags creation_flags,
335 bool wait_for_idle);
337 // Make the current channel "dev" for the duration of the test.
338 extensions::ScopedCurrentChannel current_channel_;
340 // Disable external install UI.
341 extensions::FeatureSwitch::ScopedOverride
342 override_prompt_for_external_extensions_;
344 // The default profile to be used.
345 Profile* profile_;
347 // Cache cache implementation.
348 scoped_ptr<extensions::ExtensionCacheFake> test_extension_cache_;
351 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_