OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_CHROME_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_CHROME_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/macros.h" |
11 #include "base/compiler_specific.h" | |
12 #include "chrome/browser/chrome_notification_types.h" | |
13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 11 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
14 #include "chrome/browser/ui/browser.h" | 12 #include "extensions/test/extension_test_notification_observer.h" |
15 #include "chrome/browser/ui/location_bar/location_bar.h" | 13 |
16 #include "content/public/browser/notification_details.h" | 14 class Browser; |
17 #include "content/public/browser/notification_observer.h" | |
18 #include "content/public/browser/notification_types.h" | |
19 | 15 |
20 namespace content { | 16 namespace content { |
| 17 class BrowserContext; |
21 class WindowedNotificationObserver; | 18 class WindowedNotificationObserver; |
22 } | 19 } |
23 | 20 |
24 // Test helper class for observing extension-related events. | 21 // Test helper class for observing extension-related events. |
25 class ExtensionTestNotificationObserver | 22 // TODO(devlin): This should be in the extensions namespace. |
26 : public content::NotificationObserver, | 23 class ChromeExtensionTestNotificationObserver |
| 24 : public ExtensionTestNotificationObserver, |
27 public extensions::ExtensionActionAPI::Observer { | 25 public extensions::ExtensionActionAPI::Observer { |
28 public: | 26 public: |
29 explicit ExtensionTestNotificationObserver(Browser* browser); | 27 explicit ChromeExtensionTestNotificationObserver(Browser* browser); |
30 ~ExtensionTestNotificationObserver() override; | 28 ~ChromeExtensionTestNotificationObserver() override; |
31 | 29 |
32 // Wait for the number of visible page actions to change to |count|. | 30 // Waits for the number of visible page actions to change to |count|. |
33 bool WaitForPageActionVisibilityChangeTo(int count); | 31 bool WaitForPageActionVisibilityChangeTo(int count); |
34 | 32 |
35 // Wait for an extension install error to be raised. Returns true if an | |
36 // error was raised. | |
37 bool WaitForExtensionInstallError(); | |
38 | |
39 // Waits until an extension is loaded and all view have loaded. | 33 // Waits until an extension is loaded and all view have loaded. |
40 void WaitForExtensionAndViewLoad(); | 34 void WaitForExtensionAndViewLoad(); |
41 | 35 |
42 // Waits until an extension is loaded. | 36 // Waits for all extension views to load. |
43 void WaitForExtensionLoad(); | |
44 | |
45 // Waits for an extension load error. Returns true if the error really | |
46 // happened. | |
47 bool WaitForExtensionLoadError(); | |
48 | |
49 // Wait for the specified extension to crash. Returns true if it really | |
50 // crashed. | |
51 bool WaitForExtensionCrash(const std::string& extension_id); | |
52 | |
53 // Wait for the crx installer to be done. Returns true if it really is done. | |
54 bool WaitForCrxInstallerDone(); | |
55 | |
56 // Wait for all extension views to load. | |
57 bool WaitForExtensionViewsToLoad(); | 37 bool WaitForExtensionViewsToLoad(); |
58 | 38 |
59 // Wait for extension to be idle. | 39 // Waits for extension to be idle. |
60 bool WaitForExtensionIdle(const std::string& extension_id); | 40 bool WaitForExtensionIdle(const std::string& extension_id); |
61 | 41 |
62 // Wait for extension to be not idle. | 42 // Waits for extension to be not idle. |
63 bool WaitForExtensionNotIdle(const std::string& extension_id); | 43 bool WaitForExtensionNotIdle(const std::string& extension_id); |
64 | 44 |
65 // Watch for the given event type from the given source. | |
66 // After calling this method, call Wait() to ensure that RunMessageLoop() is | |
67 // called appropriately and cleanup is performed. | |
68 void Watch(int type, const content::NotificationSource& source); | |
69 | |
70 // After registering one or more event types with Watch(), call | |
71 // this method to run the message loop and perform cleanup. | |
72 void Wait(); | |
73 | |
74 const std::string& last_loaded_extension_id() { | |
75 return last_loaded_extension_id_; | |
76 } | |
77 void set_last_loaded_extension_id( | |
78 const std::string& last_loaded_extension_id) { | |
79 last_loaded_extension_id_ = last_loaded_extension_id; | |
80 } | |
81 | |
82 // content::NotificationObserver | |
83 void Observe(int type, | |
84 const content::NotificationSource& source, | |
85 const content::NotificationDetails& details) override; | |
86 | |
87 private: | 45 private: |
88 class NotificationSet; | 46 content::BrowserContext* GetBrowserContext(); |
89 | |
90 Profile* GetProfile(); | |
91 | |
92 void WaitForNotification(int notification_type); | |
93 | |
94 // Wait for |condition_| to be met. |notification_set| is the set of | |
95 // notifications to wait for and to check |condition| when observing. This | |
96 // can be NULL if we are instead waiting for a different observer method, like | |
97 // OnPageActionsUpdated(). | |
98 void WaitForCondition(const base::Callback<bool(void)>& condition, | |
99 NotificationSet* notification_set); | |
100 | |
101 // Quits the message loop if |condition_| is met. | |
102 void MaybeQuit(); | |
103 | 47 |
104 // extensions::ExtensionActionAPI::Observer: | 48 // extensions::ExtensionActionAPI::Observer: |
105 void OnPageActionsUpdated(content::WebContents* contents) override; | 49 void OnPageActionsUpdated(content::WebContents* contents) override; |
106 | 50 |
107 Browser* browser_; | 51 Browser* browser_; |
108 Profile* profile_; | |
109 | 52 |
110 content::NotificationRegistrar registrar_; | 53 DISALLOW_COPY_AND_ASSIGN(ChromeExtensionTestNotificationObserver); |
111 std::unique_ptr<content::WindowedNotificationObserver> observer_; | |
112 | |
113 std::string last_loaded_extension_id_; | |
114 int extension_installs_observed_; | |
115 int extension_load_errors_observed_; | |
116 int crx_installers_done_observed_; | |
117 | |
118 // The condition for which we are waiting. This should be checked in any | |
119 // observing methods that could trigger it. | |
120 base::Callback<bool(void)> condition_; | |
121 | |
122 // The closure to quit the currently-running message loop. | |
123 base::Closure quit_closure_; | |
124 }; | 54 }; |
125 | 55 |
126 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_ | 56 #endif // CHROME_BROWSER_EXTENSIONS_CHROME_EXTENSION_TEST_NOTIFICATION_OBSERVER
_H_ |
OLD | NEW |