extension_browsertest.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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 "chrome/common/notification_details.h"
14#include "chrome/common/notification_observer.h"
15#include "chrome/common/notification_type.h"
16#include "chrome/test/in_process_browser_test.h"
17
18// Base class for extension browser tests. Provides utilities for loading,
19// unloading, and installing extensions.
20class ExtensionBrowserTest
21    : public InProcessBrowserTest, public NotificationObserver {
22 protected:
23  ExtensionBrowserTest();
24
25  virtual void SetUpCommandLine(CommandLine* command_line);
26  bool LoadExtension(const FilePath& path);
27
28  // Same as above, but enables the extension in incognito mode first.
29  bool LoadExtensionIncognito(const FilePath& path);
30
31  // Pack the extension in |dir_path| into a crx file and return its path.
32  // Return an empty FilePath if there were errors.
33  FilePath PackExtension(const FilePath& dir_path);
34
35  // |expected_change| indicates how many extensions should be installed (or
36  // disabled, if negative).
37  // 1 means you expect a new install, 0 means you expect an upgrade, -1 means
38  // you expect a failed upgrade.
39  bool InstallExtension(const FilePath& path, int expected_change) {
40    return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NONE,
41                                    expected_change);
42  }
43
44  // Same as above but passes an id to CrxInstaller and does not allow a
45  // privilege increase.
46  bool UpdateExtension(const std::string& id, const FilePath& path,
47                       int expected_change) {
48    return InstallOrUpdateExtension(id, path, INSTALL_UI_TYPE_NONE,
49                                    expected_change);
50  }
51
52  // Same as |InstallExtension| but with the normal extension UI showing up
53  // (for e.g. info bar on success).
54  bool InstallExtensionWithUI(const FilePath& path, int expected_change) {
55    return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_NORMAL,
56                                    expected_change);
57  }
58  bool InstallExtensionWithUIAutoConfirm(const FilePath& path,
59                                         int expected_change,
60                                         Profile* profile) {
61    return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_AUTO_CONFIRM,
62                                    expected_change, profile);
63  }
64
65  // Begins install process but simulates a user cancel.
66  bool StartInstallButCancel(const FilePath& path) {
67    return InstallOrUpdateExtension("", path, INSTALL_UI_TYPE_CANCEL, 0);
68  }
69
70  void ReloadExtension(const std::string& extension_id);
71
72  void UnloadExtension(const std::string& extension_id);
73
74  void UninstallExtension(const std::string& extension_id);
75
76  void DisableExtension(const std::string& extension_id);
77
78  void EnableExtension(const std::string& extension_id);
79
80  // Wait for the total number of page actions to change to |count|.
81  bool WaitForPageActionCountChangeTo(int count);
82
83  // Wait for the number of visible page actions to change to |count|.
84  bool WaitForPageActionVisibilityChangeTo(int count);
85
86  // Waits until an extension is installed and loaded. Returns true if an
87  // install happened before timeout.
88  bool WaitForExtensionInstall();
89
90  // Wait for an extension install error to be raised. Returns true if an
91  // error was raised.
92  bool WaitForExtensionInstallError();
93
94  // Waits until an extension is loaded.
95  void WaitForExtensionLoad();
96
97  // Wait for the specified extension to crash. Returns true if it really
98  // crashed.
99  bool WaitForExtensionCrash(const std::string& extension_id);
100
101  // NotificationObserver
102  virtual void Observe(NotificationType type,
103                       const NotificationSource& source,
104                       const NotificationDetails& details);
105
106  bool loaded_;
107  bool installed_;
108
109  // test_data/extensions.
110  FilePath test_data_dir_;
111  std::string last_loaded_extension_id_;
112  int extension_installs_observed_;
113
114 private:
115  // Specifies the type of UI (if any) to show during installation and what
116  // user action to simulate.
117  enum InstallUIType {
118    INSTALL_UI_TYPE_NONE,
119    INSTALL_UI_TYPE_CANCEL,
120    INSTALL_UI_TYPE_NORMAL,
121    INSTALL_UI_TYPE_AUTO_CONFIRM,
122  };
123
124  bool InstallOrUpdateExtension(const std::string& id, const FilePath& path,
125                                InstallUIType ui_type,
126                                int expected_change);
127  bool InstallOrUpdateExtension(const std::string& id, const FilePath& path,
128                                InstallUIType ui_type,
129                                int expected_change,
130                                Profile* profile);
131  bool LoadExtensionImpl(const FilePath& path, bool incognito_enabled);
132
133  bool WaitForExtensionHostsToLoad();
134
135  // When waiting for page action count to change, we wait until it reaches this
136  // value.
137  int target_page_action_count_;
138
139  // When waiting for visible page action count to change, we wait until it
140  // reaches this value.
141  int target_visible_page_action_count_;
142};
143
144#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_BROWSERTEST_H_
145