Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: extensions/test/extension_test_notification_observer.cc

Issue 2393343002: Split ExtensionTestObserver and move to //extensions. (Closed)
Patch Set: . Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "chrome/browser/extensions/extension_test_notification_observer.h" 5 #include "extensions/test/extension_test_notification_observer.h"
6 6
7 #include <stddef.h> 7 #include "content/public/browser/browser_context.h"
8
9 #include "base/callback_list.h"
10 #include "base/scoped_observer.h"
11 #include "chrome/browser/extensions/extension_action_test_util.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_util.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "content/public/browser/notification_registrar.h" 8 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/notification_service.h" 9 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/render_view_host.h" 10 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/test/test_utils.h" 11 #include "content/public/test/test_utils.h"
22 #include "extensions/browser/extension_system.h" 12 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/process_manager.h" 13 #include "extensions/browser/notification_types.h"
24 #include "extensions/browser/process_manager_observer.h"
25 #include "extensions/common/extension.h" 14 #include "extensions/common/extension.h"
26 15
27 using extensions::Extension; 16 using extensions::Extension;
28 17
29 namespace { 18 namespace {
30 19
31 // A callback that returns true if the condition has been met and takes no 20 // A callback that returns true if the condition has been met and takes no
32 // arguments. 21 // arguments.
33 typedef base::Callback<bool(void)> ConditionCallback; 22 using ConditionCallback = base::Callback<bool(void)>;
34 23
35 bool HasPageActionVisibilityReachedTarget( 24 const Extension* GetNonTerminatedExtensions(const std::string& id,
36 Browser* browser, size_t target_visible_page_action_count) { 25 content::BrowserContext* context) {
37 return extensions::extension_action_test_util::GetVisiblePageActionCount( 26 return extensions::ExtensionRegistry::Get(context)->GetExtensionById(
38 browser->tab_strip_model()->GetActiveWebContents()) == 27 id, extensions::ExtensionRegistry::EVERYTHING &
39 target_visible_page_action_count; 28 ~extensions::ExtensionRegistry::TERMINATED);
40 }
41
42 bool HaveAllExtensionRenderFrameHostsFinishedLoading(
43 extensions::ProcessManager* manager) {
44 extensions::ProcessManager::FrameSet all_views = manager->GetAllFrames();
45 for (content::RenderFrameHost* host : manager->GetAllFrames()) {
46 if (content::WebContents::FromRenderFrameHost(host)->IsLoading())
47 return false;
48 }
49 return true;
50 }
51
52 bool IsExtensionNotIdle(const std::string& extension_id,
53 content::BrowserContext* context) {
54 return !extensions::util::IsExtensionIdle(extension_id, context);
55 } 29 }
56 30
57 } // namespace 31 } // namespace
58 32
59 //////////////////////////////////////////////////////////////////////////////// 33 ////////////////////////////////////////////////////////////////////////////////
60 // ExtensionTestNotificationObserver::NotificationSet 34 // ExtensionTestNotificationObserver::NotificationSet
61 35
62 class ExtensionTestNotificationObserver::NotificationSet 36 ExtensionTestNotificationObserver::NotificationSet::NotificationSet()
63 : public content::NotificationObserver, 37 : process_manager_observer_(this) {}
64 public extensions::ProcessManagerObserver { 38 ExtensionTestNotificationObserver::NotificationSet::~NotificationSet() {}
65 public:
66 NotificationSet() : process_manager_observer_(this) {}
67 ~NotificationSet() override {}
68
69 void Add(int type, const content::NotificationSource& source);
70 void Add(int type);
71 void AddExtensionFrameUnregistration(extensions::ProcessManager* manager);
72
73 // Notified any time an Add()ed notification is received.
74 // The details of the notification are dropped.
75 base::CallbackList<void()>& callback_list() {
76 return callback_list_;
77 }
78
79 private:
80 // content::NotificationObserver:
81 void Observe(int type,
82 const content::NotificationSource& source,
83 const content::NotificationDetails& details) override;
84
85 // extensions::ProcessManagerObserver:
86 void OnExtensionFrameUnregistered(
87 const std::string& extension_id,
88 content::RenderFrameHost* render_frame_host) override;
89
90 content::NotificationRegistrar notification_registrar_;
91 base::CallbackList<void()> callback_list_;
92 ScopedObserver<extensions::ProcessManager, extensions::ProcessManagerObserver>
93 process_manager_observer_;
94 };
95 39
96 void ExtensionTestNotificationObserver::NotificationSet::Add( 40 void ExtensionTestNotificationObserver::NotificationSet::Add(
97 int type, 41 int type,
98 const content::NotificationSource& source) { 42 const content::NotificationSource& source) {
99 notification_registrar_.Add(this, type, source); 43 notification_registrar_.Add(this, type, source);
100 } 44 }
101 45
102 void ExtensionTestNotificationObserver::NotificationSet::Add(int type) { 46 void ExtensionTestNotificationObserver::NotificationSet::Add(int type) {
103 Add(type, content::NotificationService::AllSources()); 47 Add(type, content::NotificationService::AllSources());
104 } 48 }
(...skipping 13 matching lines...) Expand all
118 void ExtensionTestNotificationObserver::NotificationSet:: 62 void ExtensionTestNotificationObserver::NotificationSet::
119 OnExtensionFrameUnregistered(const std::string& extension_id, 63 OnExtensionFrameUnregistered(const std::string& extension_id,
120 content::RenderFrameHost* render_frame_host) { 64 content::RenderFrameHost* render_frame_host) {
121 callback_list_.Notify(); 65 callback_list_.Notify();
122 } 66 }
123 67
124 //////////////////////////////////////////////////////////////////////////////// 68 ////////////////////////////////////////////////////////////////////////////////
125 // ExtensionTestNotificationObserver 69 // ExtensionTestNotificationObserver
126 70
127 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( 71 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver(
128 Browser* browser) 72 content::BrowserContext* context)
129 : browser_(browser), 73 : context_(context),
130 profile_(NULL),
131 extension_installs_observed_(0), 74 extension_installs_observed_(0),
132 extension_load_errors_observed_(0), 75 extension_load_errors_observed_(0),
133 crx_installers_done_observed_(0) { 76 crx_installers_done_observed_(0) {}
134 }
135 77
136 ExtensionTestNotificationObserver::~ExtensionTestNotificationObserver() {} 78 ExtensionTestNotificationObserver::~ExtensionTestNotificationObserver() {}
137 79
138 Profile* ExtensionTestNotificationObserver::GetProfile() {
139 if (!profile_) {
140 if (browser_)
141 profile_ = browser_->profile();
142 else
143 profile_ = ProfileManager::GetActiveUserProfile();
144 }
145 return profile_;
146 }
147
148 void ExtensionTestNotificationObserver::WaitForNotification( 80 void ExtensionTestNotificationObserver::WaitForNotification(
149 int notification_type) { 81 int notification_type) {
150 // TODO(bauerb): Using a WindowedNotificationObserver like this can break 82 // TODO(bauerb): Using a WindowedNotificationObserver like this can break
151 // easily, if the notification we're waiting for is sent before this method. 83 // easily, if the notification we're waiting for is sent before this method.
152 // Change it so that the WindowedNotificationObserver is constructed earlier. 84 // Change it so that the WindowedNotificationObserver is constructed earlier.
153 content::NotificationRegistrar registrar; 85 content::NotificationRegistrar registrar;
154 registrar.Add( 86 registrar.Add(this, notification_type,
155 this, notification_type, content::NotificationService::AllSources()); 87 content::NotificationService::AllSources());
156 content::WindowedNotificationObserver( 88 content::WindowedNotificationObserver(
157 notification_type, content::NotificationService::AllSources()).Wait(); 89 notification_type, content::NotificationService::AllSources())
158 } 90 .Wait();
159
160 bool ExtensionTestNotificationObserver::WaitForPageActionVisibilityChangeTo(
161 int count) {
162 extensions::ExtensionActionAPI::Get(GetProfile())->AddObserver(this);
163 WaitForCondition(
164 base::Bind(&HasPageActionVisibilityReachedTarget, browser_, count),
165 NULL);
166 extensions::ExtensionActionAPI::Get(GetProfile())->
167 RemoveObserver(this);
168 return true;
169 }
170
171 bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() {
172 extensions::ProcessManager* manager =
173 extensions::ProcessManager::Get(GetProfile());
174 NotificationSet notification_set;
175 notification_set.Add(content::NOTIFICATION_WEB_CONTENTS_DESTROYED);
176 notification_set.Add(content::NOTIFICATION_LOAD_STOP);
177 notification_set.AddExtensionFrameUnregistration(manager);
178 WaitForCondition(
179 base::Bind(&HaveAllExtensionRenderFrameHostsFinishedLoading, manager),
180 &notification_set);
181 return true;
182 }
183
184 bool ExtensionTestNotificationObserver::WaitForExtensionIdle(
185 const std::string& extension_id) {
186 NotificationSet notification_set;
187 notification_set.Add(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
188 WaitForCondition(base::Bind(&extensions::util::IsExtensionIdle, extension_id,
189 GetProfile()),
190 &notification_set);
191 return true;
192 }
193
194 bool ExtensionTestNotificationObserver::WaitForExtensionNotIdle(
195 const std::string& extension_id) {
196 NotificationSet notification_set;
197 notification_set.Add(content::NOTIFICATION_LOAD_STOP);
198 WaitForCondition(base::Bind(&IsExtensionNotIdle, extension_id, GetProfile()),
199 &notification_set);
200 return true;
201 } 91 }
202 92
203 bool ExtensionTestNotificationObserver::WaitForExtensionInstallError() { 93 bool ExtensionTestNotificationObserver::WaitForExtensionInstallError() {
204 int before = extension_installs_observed_; 94 int before = extension_installs_observed_;
205 content::WindowedNotificationObserver( 95 content::WindowedNotificationObserver(
206 extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR, 96 extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR,
207 content::NotificationService::AllSources()).Wait(); 97 content::NotificationService::AllSources())
98 .Wait();
208 return extension_installs_observed_ == before; 99 return extension_installs_observed_ == before;
209 } 100 }
210 101
211 void ExtensionTestNotificationObserver::WaitForExtensionLoad() { 102 void ExtensionTestNotificationObserver::WaitForExtensionLoad() {
212 WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED); 103 WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED);
213 } 104 }
214 105
215 void ExtensionTestNotificationObserver::WaitForExtensionAndViewLoad() {
216 this->WaitForExtensionLoad();
217 WaitForExtensionViewsToLoad();
218 }
219
220 bool ExtensionTestNotificationObserver::WaitForExtensionLoadError() { 106 bool ExtensionTestNotificationObserver::WaitForExtensionLoadError() {
221 int before = extension_load_errors_observed_; 107 int before = extension_load_errors_observed_;
222 WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOAD_ERROR); 108 WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOAD_ERROR);
223 return extension_load_errors_observed_ != before; 109 return extension_load_errors_observed_ != before;
224 } 110 }
225 111
226 bool ExtensionTestNotificationObserver::WaitForExtensionCrash( 112 bool ExtensionTestNotificationObserver::WaitForExtensionCrash(
227 const std::string& extension_id) { 113 const std::string& extension_id) {
228 ExtensionService* service = extensions::ExtensionSystem::Get( 114 if (!GetNonTerminatedExtensions(extension_id, context_)) {
229 GetProfile())->extension_service();
230
231 if (!service->GetExtensionById(extension_id, true)) {
232 // The extension is already unloaded, presumably due to a crash. 115 // The extension is already unloaded, presumably due to a crash.
233 return true; 116 return true;
234 } 117 }
118
235 content::WindowedNotificationObserver( 119 content::WindowedNotificationObserver(
236 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, 120 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
237 content::NotificationService::AllSources()).Wait(); 121 content::NotificationService::AllSources())
238 return (service->GetExtensionById(extension_id, true) == NULL); 122 .Wait();
123 return (GetNonTerminatedExtensions(extension_id, context_) == NULL);
239 } 124 }
240 125
241 bool ExtensionTestNotificationObserver::WaitForCrxInstallerDone() { 126 bool ExtensionTestNotificationObserver::WaitForCrxInstallerDone() {
242 int before = crx_installers_done_observed_; 127 int before = crx_installers_done_observed_;
243 WaitForNotification(extensions::NOTIFICATION_CRX_INSTALLER_DONE); 128 WaitForNotification(extensions::NOTIFICATION_CRX_INSTALLER_DONE);
244 return crx_installers_done_observed_ == (before + 1); 129 return crx_installers_done_observed_ == before + 1;
245 } 130 }
246 131
247 void ExtensionTestNotificationObserver::Watch( 132 void ExtensionTestNotificationObserver::Watch(
248 int type, 133 int type,
249 const content::NotificationSource& source) { 134 const content::NotificationSource& source) {
250 CHECK(!observer_); 135 CHECK(!observer_);
251 observer_.reset(new content::WindowedNotificationObserver(type, source)); 136 observer_.reset(new content::WindowedNotificationObserver(type, source));
252 registrar_.Add(this, type, source); 137 registrar_.Add(this, type, source);
253 } 138 }
254 139
255 void ExtensionTestNotificationObserver::Wait() { 140 void ExtensionTestNotificationObserver::Wait() {
256 observer_->Wait(); 141 observer_->Wait();
257 142
258 registrar_.RemoveAll(); 143 registrar_.RemoveAll();
259 observer_.reset(); 144 observer_.reset();
260 } 145 }
261 146
262 void ExtensionTestNotificationObserver::Observe( 147 void ExtensionTestNotificationObserver::Observe(
263 int type, 148 int type,
264 const content::NotificationSource& source, 149 const content::NotificationSource& source,
265 const content::NotificationDetails& details) { 150 const content::NotificationDetails& details) {
266 switch (type) { 151 switch (type) {
267 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: 152 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED:
268 last_loaded_extension_id_ = 153 last_loaded_extension_id_ =
269 content::Details<const Extension>(details).ptr()->id(); 154 content::Details<const Extension>(details).ptr()->id();
270 VLOG(1) << "Got EXTENSION_LOADED notification."; 155 VLOG(1) << "Got EXTENSION_LOADED notification.";
271 break; 156 break;
272 157
273 case extensions::NOTIFICATION_CRX_INSTALLER_DONE: 158 case extensions::NOTIFICATION_CRX_INSTALLER_DONE:
274 VLOG(1) << "Got CRX_INSTALLER_DONE notification."; 159 VLOG(1) << "Got CRX_INSTALLER_DONE notification.";
275 { 160 {
276 const Extension* extension = 161 const Extension* extension =
277 content::Details<const Extension>(details).ptr(); 162 content::Details<const Extension>(details).ptr();
278 if (extension) 163 if (extension)
279 last_loaded_extension_id_ = extension->id(); 164 last_loaded_extension_id_ = extension->id();
280 else 165 else
281 last_loaded_extension_id_.clear(); 166 last_loaded_extension_id_.clear();
282 } 167 }
283 ++crx_installers_done_observed_; 168 ++crx_installers_done_observed_;
284 break; 169 break;
285 170
286 case extensions::NOTIFICATION_EXTENSION_LOAD_ERROR: 171 case extensions::NOTIFICATION_EXTENSION_LOAD_ERROR:
287 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; 172 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification.";
288 ++extension_load_errors_observed_; 173 ++extension_load_errors_observed_;
289 break; 174 break;
290 175
291 default: 176 default:
292 NOTREACHED(); 177 NOTREACHED();
293 break; 178 break;
294 } 179 }
295 } 180 }
296 181
297 void ExtensionTestNotificationObserver::OnPageActionsUpdated(
298 content::WebContents* web_contents) {
299 MaybeQuit();
300 }
301
302 void ExtensionTestNotificationObserver::WaitForCondition( 182 void ExtensionTestNotificationObserver::WaitForCondition(
303 const ConditionCallback& condition, 183 const ConditionCallback& condition,
304 NotificationSet* notification_set) { 184 NotificationSet* notification_set) {
305 if (condition.Run()) 185 if (condition.Run())
306 return; 186 return;
307 condition_ = condition; 187 condition_ = condition;
308 188
309 scoped_refptr<content::MessageLoopRunner> runner( 189 scoped_refptr<content::MessageLoopRunner> runner(
310 new content::MessageLoopRunner); 190 new content::MessageLoopRunner);
311 quit_closure_ = runner->QuitClosure(); 191 quit_closure_ = runner->QuitClosure();
312 192
313 std::unique_ptr<base::CallbackList<void()>::Subscription> subscription; 193 std::unique_ptr<base::CallbackList<void()>::Subscription> subscription;
314 if (notification_set) { 194 if (notification_set) {
315 subscription = notification_set->callback_list().Add( 195 subscription = notification_set->callback_list().Add(base::Bind(
316 base::Bind(&ExtensionTestNotificationObserver::MaybeQuit, 196 &ExtensionTestNotificationObserver::MaybeQuit, base::Unretained(this)));
317 base::Unretained(this)));
318 } 197 }
319 runner->Run(); 198 runner->Run();
320 199
321 condition_.Reset(); 200 condition_.Reset();
322 quit_closure_.Reset(); 201 quit_closure_.Reset();
323 } 202 }
324 203
325 void ExtensionTestNotificationObserver::MaybeQuit() { 204 void ExtensionTestNotificationObserver::MaybeQuit() {
326 if (condition_.Run()) 205 if (condition_.Run())
327 quit_closure_.Run(); 206 quit_closure_.Run();
328 } 207 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698