[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 3f58d855 | 2009-08-14 23:59:37 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/command_line.h" |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 13 | #include "base/file_path.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 14 | #include "base/scoped_temp_dir.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 15 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 16 | #include "content/public/browser/notification_details.h" |
| 17 | #include "content/public/browser/notification_observer.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 18 | #include "content/public/browser/notification_types.h" |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 19 | |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 20 | class Extension; |
| 21 | |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 22 | // Base class for extension browser tests. Provides utilities for loading, |
| 23 | // unloading, and installing extensions. |
| 24 | class ExtensionBrowserTest |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 25 | : public InProcessBrowserTest, public content::NotificationObserver { |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 26 | protected: |
[email protected] | d818e07f | 2010-02-10 13:10:03 | [diff] [blame] | 27 | ExtensionBrowserTest(); |
[email protected] | 178f851 | 2012-02-09 01:49:36 | [diff] [blame] | 28 | virtual ~ExtensionBrowserTest(); |
[email protected] | d818e07f | 2010-02-10 13:10:03 | [diff] [blame] | 29 | |
[email protected] | 1cc91fe | 2011-11-21 14:48:43 | [diff] [blame] | 30 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE; |
[email protected] | 84f4dc0 | 2011-11-29 21:58:26 | [diff] [blame] | 31 | |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 32 | const Extension* LoadExtension(const FilePath& path); |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 33 | |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 34 | // Same as above, but enables the extension in incognito mode first. |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 35 | const Extension* LoadExtensionIncognito(const FilePath& path); |
[email protected] | 61b55b6 | 2011-03-24 09:03:10 | [diff] [blame] | 36 | |
[email protected] | c1bf62c7 | 2011-07-15 22:18:42 | [diff] [blame] | 37 | const Extension* LoadExtensionWithOptions(const FilePath& path, |
| 38 | bool incognito_enabled, |
| 39 | bool fileaccess_enabled); |
[email protected] | a964e11 | 2011-04-14 21:52:51 | [diff] [blame] | 40 | |
| 41 | // Loads extension and imitates that it is a component extension. |
[email protected] | 863e647 | 2012-01-24 19:33:58 | [diff] [blame] | 42 | const Extension* LoadExtensionAsComponent(const FilePath& path); |
[email protected] | c7c401d | 2011-03-16 10:20:01 | [diff] [blame] | 43 | |
[email protected] | 59e0336 | 2011-01-21 21:24:08 | [diff] [blame] | 44 | // 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] | f66a50a | 2011-11-02 23:53:46 | [diff] [blame] | 48 | // 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] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 57 | // |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] | 84f4dc0 | 2011-11-29 21:58:26 | [diff] [blame] | 61 | const Extension* InstallExtension(const FilePath& path, int expected_change) { |
[email protected] | 8fd16f50 | 2010-04-22 18:23:18 | [diff] [blame] | 62 | return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NONE, |
| 63 | expected_change); |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | cefa749d | 2011-08-11 22:21:48 | [diff] [blame] | 66 | // Installs extension as if it came from the Chrome Webstore. |
[email protected] | 84f4dc0 | 2011-11-29 21:58:26 | [diff] [blame] | 67 | const Extension* InstallExtensionFromWebstore( |
| 68 | const FilePath& path, int expected_change); |
[email protected] | cefa749d | 2011-08-11 22:21:48 | [diff] [blame] | 69 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 70 | // Same as above but passes an id to CrxInstaller and does not allow a |
| 71 | // privilege increase. |
[email protected] | 84f4dc0 | 2011-11-29 21:58:26 | [diff] [blame] | 72 | const Extension* UpdateExtension(const std::string& id, const FilePath& path, |
| 73 | int expected_change) { |
[email protected] | 8fd16f50 | 2010-04-22 18:23:18 | [diff] [blame] | 74 | 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] | 84f4dc0 | 2011-11-29 21:58:26 | [diff] [blame] | 80 | const Extension* InstallExtensionWithUI(const FilePath& path, |
| 81 | int expected_change) { |
[email protected] | 8fd16f50 | 2010-04-22 18:23:18 | [diff] [blame] | 82 | return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NORMAL, |
| 83 | expected_change); |
[email protected] | c70013bd | 2010-01-20 21:50:03 | [diff] [blame] | 84 | } |
[email protected] | 84f4dc0 | 2011-11-29 21:58:26 | [diff] [blame] | 85 | const Extension* InstallExtensionWithUIAutoConfirm(const FilePath& path, |
| 86 | int expected_change, |
| 87 | Profile* profile) { |
[email protected] | 59e0336 | 2011-01-21 21:24:08 | [diff] [blame] | 88 | return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_AUTO_CONFIRM, |
[email protected] | cefa749d | 2011-08-11 22:21:48 | [diff] [blame] | 89 | expected_change, profile, false); |
[email protected] | 59e0336 | 2011-01-21 21:24:08 | [diff] [blame] | 90 | } |
[email protected] | c70013bd | 2010-01-20 21:50:03 | [diff] [blame] | 91 | |
| 92 | // Begins install process but simulates a user cancel. |
[email protected] | 84f4dc0 | 2011-11-29 21:58:26 | [diff] [blame] | 93 | const Extension* StartInstallButCancel(const FilePath& path) { |
[email protected] | 8fd16f50 | 2010-04-22 18:23:18 | [diff] [blame] | 94 | return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_CANCEL, 0); |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | e172584 | 2009-10-20 06:40:15 | [diff] [blame] | 97 | void ReloadExtension(const std::string& extension_id); |
| 98 | |
[email protected] | 57f71b9 | 2009-09-11 19:31:38 | [diff] [blame] | 99 | void UnloadExtension(const std::string& extension_id); |
| 100 | |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 101 | void UninstallExtension(const std::string& extension_id); |
| 102 | |
[email protected] | 7d9ad0b3 | 2010-02-12 21:44:45 | [diff] [blame] | 103 | void DisableExtension(const std::string& extension_id); |
| 104 | |
| 105 | void EnableExtension(const std::string& extension_id); |
| 106 | |
[email protected] | 57f71b9 | 2009-09-11 19:31:38 | [diff] [blame] | 107 | // Wait for the total number of page actions to change to |count|. |
| 108 | bool WaitForPageActionCountChangeTo(int count); |
| 109 | |
[email protected] | 361b28a | 2009-07-09 21:30:53 | [diff] [blame] | 110 | // Wait for the number of visible page actions to change to |count|. |
| 111 | bool WaitForPageActionVisibilityChangeTo(int count); |
| 112 | |
[email protected] | f4ea1128 | 2009-10-14 00:19:31 | [diff] [blame] | 113 | // 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] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 121 | // Waits until an extension is loaded. |
| 122 | void WaitForExtensionLoad(); |
| 123 | |
[email protected] | bbcde910 | 2012-03-25 22:40:49 | [diff] [blame^] | 124 | // Waits for an extension load error. Returns true if the error really |
| 125 | // happened. |
| 126 | bool WaitForExtensionLoadError(); |
| 127 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 128 | // 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] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 132 | // content::NotificationObserver |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 133 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 134 | const content::NotificationSource& source, |
| 135 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 25fd1b2e | 2009-08-17 20:57:14 | [diff] [blame] | 136 | |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 137 | bool loaded_; |
| 138 | bool installed_; |
[email protected] | 3b35564 | 2010-02-05 16:01:49 | [diff] [blame] | 139 | |
| 140 | // test_data/extensions. |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 141 | FilePath test_data_dir_; |
[email protected] | 84ac7f3 | 2009-10-06 06:17:54 | [diff] [blame] | 142 | std::string last_loaded_extension_id_; |
[email protected] | f4ea1128 | 2009-10-14 00:19:31 | [diff] [blame] | 143 | int extension_installs_observed_; |
[email protected] | bbcde910 | 2012-03-25 22:40:49 | [diff] [blame^] | 144 | int extension_load_errors_observed_; |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 145 | |
| 146 | private: |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 147 | // Temporary directory for testing. |
| 148 | ScopedTempDir temp_dir_; |
| 149 | |
[email protected] | 8fd16f50 | 2010-04-22 18:23:18 | [diff] [blame] | 150 | // 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] | 59e0336 | 2011-01-21 21:24:08 | [diff] [blame] | 156 | INSTALL_UI_TYPE_AUTO_CONFIRM, |
[email protected] | 8fd16f50 | 2010-04-22 18:23:18 | [diff] [blame] | 157 | }; |
| 158 | |
[email protected] | 84f4dc0 | 2011-11-29 21:58:26 | [diff] [blame] | 159 | 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] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 169 | |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 170 | bool WaitForExtensionHostsToLoad(); |
[email protected] | d818e07f | 2010-02-10 13:10:03 | [diff] [blame] | 171 | |
| 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] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 179 | }; |
| 180 | |
[email protected] | 3f58d855 | 2009-08-14 23:59:37 | [diff] [blame] | 181 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_ |