• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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.
4 
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "base/command_line.h"
12 #include "base/file_path.h"
13 #include "base/memory/scoped_temp_dir.h"
14 #include "chrome/test/in_process_browser_test.h"
15 #include "content/common/notification_details.h"
16 #include "content/common/notification_observer.h"
17 #include "content/common/notification_type.h"
18 
19 class Extension;
20 
21 // Base class for extension browser tests. Provides utilities for loading,
22 // unloading, and installing extensions.
23 class ExtensionBrowserTest
24     : public InProcessBrowserTest, public NotificationObserver {
25  protected:
26   ExtensionBrowserTest();
27 
28   virtual void SetUpCommandLine(CommandLine* command_line);
29   const Extension* LoadExtension(const FilePath& path);
30 
31   // Same as above, but enables the extension in incognito mode first.
32   const Extension* LoadExtensionIncognito(const FilePath& path);
33 
34   // By default, unpacked extensions have file access: this loads them with
35   // that permission removed.
36   const Extension* LoadExtensionNoFileAccess(const FilePath& path);
37 
38   // Same as above, but enables the extension in incognito mode first.
39   const Extension* LoadExtensionIncognitoNoFileAccess(const FilePath& path);
40 
41   // Loads extension and imitates that it is a component extension.
42   bool LoadExtensionAsComponent(const FilePath& path);
43 
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 
48   // |expected_change| indicates how many extensions should be installed (or
49   // disabled, if negative).
50   // 1 means you expect a new install, 0 means you expect an upgrade, -1 means
51   // you expect a failed upgrade.
InstallExtension(const FilePath & path,int expected_change)52   bool InstallExtension(const FilePath& path, int expected_change) {
53     return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NONE,
54                                     expected_change);
55   }
56 
57   // Same as above but passes an id to CrxInstaller and does not allow a
58   // privilege increase.
UpdateExtension(const std::string & id,const FilePath & path,int expected_change)59   bool UpdateExtension(const std::string& id, const FilePath& path,
60                        int expected_change) {
61     return InstallOrUpdateExtension(id, path, INSTALL_UI_TYPE_NONE,
62                                     expected_change);
63   }
64 
65   // Same as |InstallExtension| but with the normal extension UI showing up
66   // (for e.g. info bar on success).
InstallExtensionWithUI(const FilePath & path,int expected_change)67   bool InstallExtensionWithUI(const FilePath& path, int expected_change) {
68     return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NORMAL,
69                                     expected_change);
70   }
InstallExtensionWithUIAutoConfirm(const FilePath & path,int expected_change,Profile * profile)71   bool InstallExtensionWithUIAutoConfirm(const FilePath& path,
72                                          int expected_change,
73                                          Profile* profile) {
74     return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_AUTO_CONFIRM,
75                                     expected_change, profile);
76   }
77 
78   // Begins install process but simulates a user cancel.
StartInstallButCancel(const FilePath & path)79   bool StartInstallButCancel(const FilePath& path) {
80     return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_CANCEL, 0);
81   }
82 
83   void ReloadExtension(const std::string& extension_id);
84 
85   void UnloadExtension(const std::string& extension_id);
86 
87   void UninstallExtension(const std::string& extension_id);
88 
89   void DisableExtension(const std::string& extension_id);
90 
91   void EnableExtension(const std::string& extension_id);
92 
93   // Wait for the total number of page actions to change to |count|.
94   bool WaitForPageActionCountChangeTo(int count);
95 
96   // Wait for the number of visible page actions to change to |count|.
97   bool WaitForPageActionVisibilityChangeTo(int count);
98 
99   // Waits until an extension is installed and loaded. Returns true if an
100   // install happened before timeout.
101   bool WaitForExtensionInstall();
102 
103   // Wait for an extension install error to be raised. Returns true if an
104   // error was raised.
105   bool WaitForExtensionInstallError();
106 
107   // Waits until an extension is loaded.
108   void WaitForExtensionLoad();
109 
110   // Wait for the specified extension to crash. Returns true if it really
111   // crashed.
112   bool WaitForExtensionCrash(const std::string& extension_id);
113 
114   // NotificationObserver
115   virtual void Observe(NotificationType type,
116                        const NotificationSource& source,
117                        const NotificationDetails& details);
118 
119   bool loaded_;
120   bool installed_;
121 
122   // test_data/extensions.
123   FilePath test_data_dir_;
124   std::string last_loaded_extension_id_;
125   int extension_installs_observed_;
126 
127  private:
128   // Temporary directory for testing.
129   ScopedTempDir temp_dir_;
130 
131   // Specifies the type of UI (if any) to show during installation and what
132   // user action to simulate.
133   enum InstallUIType {
134     INSTALL_UI_TYPE_NONE,
135     INSTALL_UI_TYPE_CANCEL,
136     INSTALL_UI_TYPE_NORMAL,
137     INSTALL_UI_TYPE_AUTO_CONFIRM,
138   };
139 
140   bool InstallOrUpdateExtension(const std::string& id, const FilePath& path,
141                                 InstallUIType ui_type,
142                                 int expected_change);
143   bool InstallOrUpdateExtension(const std::string& id, const FilePath& path,
144                                 InstallUIType ui_type,
145                                 int expected_change,
146                                 Profile* profile);
147   const Extension* LoadExtensionImpl(const FilePath& path,
148                                      bool incognito_enabled,
149                                      bool fileaccess_enabled);
150 
151   bool WaitForExtensionHostsToLoad();
152 
153   // When waiting for page action count to change, we wait until it reaches this
154   // value.
155   int target_page_action_count_;
156 
157   // When waiting for visible page action count to change, we wait until it
158   // reaches this value.
159   int target_visible_page_action_count_;
160 };
161 
162 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_
163