| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/extension_test_notification_observer.h" |
| 6 | 6 |
| 7 #include "base/callback_list.h" | 7 #include "base/callback_list.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
| 10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_window.h" | 11 #include "chrome/browser/ui/browser_window.h" |
| 12 #include "content/public/browser/notification_registrar.h" | 12 #include "content/public/browser/notification_registrar.h" |
| 13 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
| 14 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/test/test_utils.h" | 15 #include "content/public/test/test_utils.h" |
| 16 #include "extensions/browser/extension_system.h" | 16 #include "extensions/browser/extension_system.h" |
| 17 #include "extensions/browser/process_manager.h" | 17 #include "extensions/browser/process_manager.h" |
| 18 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
| 19 | 19 |
| 20 using extensions::Extension; | 20 using extensions::Extension; |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 bool HasExtensionPageActionVisibilityReachedTarget( | 24 // A callback that returns true if the condition has been met and takes no |
| 25 LocationBarTesting* location_bar, | 25 // arguments. |
| 26 int target_visible_page_action_count) { | 26 typedef base::Callback<bool(void)> ConditionCallback; |
| 27 VLOG(1) << "Number of visible page actions: " | 27 |
| 28 << location_bar->PageActionVisibleCount(); | 28 bool HasPageActionVisibilityReachedTarget( |
| 29 LocationBarTesting* location_bar, int target_visible_page_action_count) { |
| 29 return location_bar->PageActionVisibleCount() == | 30 return location_bar->PageActionVisibleCount() == |
| 30 target_visible_page_action_count; | 31 target_visible_page_action_count; |
| 31 } | 32 } |
| 32 | 33 |
| 33 bool HaveAllExtensionRenderViewHostsFinishedLoading( | 34 bool HaveAllExtensionRenderViewHostsFinishedLoading( |
| 34 extensions::ProcessManager* manager) { | 35 extensions::ProcessManager* manager) { |
| 35 extensions::ProcessManager::ViewSet all_views = manager->GetAllViews(); | 36 extensions::ProcessManager::ViewSet all_views = manager->GetAllViews(); |
| 36 for (extensions::ProcessManager::ViewSet::const_iterator iter = | 37 for (extensions::ProcessManager::ViewSet::const_iterator iter = |
| 37 all_views.begin(); | 38 all_views.begin(); |
| 38 iter != all_views.end(); ++iter) { | 39 iter != all_views.end(); ++iter) { |
| 39 if ((*iter)->IsLoading()) | 40 if ((*iter)->IsLoading()) |
| 40 return false; | 41 return false; |
| 41 } | 42 } |
| 42 return true; | 43 return true; |
| 43 } | 44 } |
| 44 | 45 |
| 45 class NotificationSet : public content::NotificationObserver { | 46 } // namespace |
| 47 |
| 48 //////////////////////////////////////////////////////////////////////////////// |
| 49 // ExtensionTestNotificationObserver::NotificationSet |
| 50 |
| 51 class ExtensionTestNotificationObserver::NotificationSet |
| 52 : public content::NotificationObserver { |
| 46 public: | 53 public: |
| 47 void Add(int type, const content::NotificationSource& source); | 54 void Add(int type, const content::NotificationSource& source); |
| 48 void Add(int type); | 55 void Add(int type); |
| 49 | 56 |
| 50 // Notified any time an Add()ed notification is received. | 57 // Notified any time an Add()ed notification is received. |
| 51 // The details of the notification are dropped. | 58 // The details of the notification are dropped. |
| 52 base::CallbackList<void()>& callback_list() { | 59 base::CallbackList<void()>& callback_list() { |
| 53 return callback_list_; | 60 return callback_list_; |
| 54 } | 61 } |
| 55 | 62 |
| 56 private: | 63 private: |
| 57 // content::NotificationObserver: | 64 // content::NotificationObserver: |
| 58 virtual void Observe(int type, | 65 virtual void Observe(int type, |
| 59 const content::NotificationSource& source, | 66 const content::NotificationSource& source, |
| 60 const content::NotificationDetails& details) OVERRIDE; | 67 const content::NotificationDetails& details) OVERRIDE; |
| 61 | 68 |
| 62 content::NotificationRegistrar notification_registrar_; | 69 content::NotificationRegistrar notification_registrar_; |
| 63 base::CallbackList<void()> callback_list_; | 70 base::CallbackList<void()> callback_list_; |
| 64 }; | 71 }; |
| 65 | 72 |
| 66 void NotificationSet::Add( | 73 void ExtensionTestNotificationObserver::NotificationSet::Add( |
| 67 int type, | 74 int type, |
| 68 const content::NotificationSource& source) { | 75 const content::NotificationSource& source) { |
| 69 notification_registrar_.Add(this, type, source); | 76 notification_registrar_.Add(this, type, source); |
| 70 } | 77 } |
| 71 | 78 |
| 72 void NotificationSet::Add(int type) { | 79 void ExtensionTestNotificationObserver::NotificationSet::Add(int type) { |
| 73 Add(type, content::NotificationService::AllSources()); | 80 Add(type, content::NotificationService::AllSources()); |
| 74 } | 81 } |
| 75 | 82 |
| 76 void NotificationSet::Observe( | 83 void ExtensionTestNotificationObserver::NotificationSet::Observe( |
| 77 int type, | 84 int type, |
| 78 const content::NotificationSource& source, | 85 const content::NotificationSource& source, |
| 79 const content::NotificationDetails& details) { | 86 const content::NotificationDetails& details) { |
| 80 callback_list_.Notify(); | 87 callback_list_.Notify(); |
| 81 } | 88 } |
| 82 | 89 |
| 83 void MaybeQuit(content::MessageLoopRunner* runner, | 90 //////////////////////////////////////////////////////////////////////////////// |
| 84 const base::Callback<bool(void)>& condition) { | 91 // ExtensionTestNotificationObserver |
| 85 if (condition.Run()) | |
| 86 runner->Quit(); | |
| 87 } | |
| 88 | |
| 89 void WaitForCondition( | |
| 90 const base::Callback<bool(void)>& condition, | |
| 91 NotificationSet* notification_set) { | |
| 92 if (condition.Run()) | |
| 93 return; | |
| 94 | |
| 95 scoped_refptr<content::MessageLoopRunner> runner( | |
| 96 new content::MessageLoopRunner); | |
| 97 scoped_ptr<base::CallbackList<void()>::Subscription> subscription = | |
| 98 notification_set->callback_list().Add( | |
| 99 base::Bind(&MaybeQuit, base::Unretained(runner.get()), condition)); | |
| 100 runner->Run(); | |
| 101 } | |
| 102 | |
| 103 void WaitForCondition( | |
| 104 const base::Callback<bool(void)>& condition, | |
| 105 int type) { | |
| 106 NotificationSet notification_set; | |
| 107 notification_set.Add(type); | |
| 108 WaitForCondition(condition, ¬ification_set); | |
| 109 } | |
| 110 | |
| 111 } // namespace | |
| 112 | 92 |
| 113 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( | 93 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( |
| 114 Browser* browser) | 94 Browser* browser) |
| 115 : browser_(browser), | 95 : browser_(browser), |
| 116 profile_(NULL), | 96 profile_(NULL), |
| 117 extension_installs_observed_(0), | 97 extension_installs_observed_(0), |
| 118 extension_load_errors_observed_(0), | 98 extension_load_errors_observed_(0), |
| 119 crx_installers_done_observed_(0) { | 99 crx_installers_done_observed_(0) { |
| 120 } | 100 } |
| 121 | 101 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 140 registrar.Add( | 120 registrar.Add( |
| 141 this, notification_type, content::NotificationService::AllSources()); | 121 this, notification_type, content::NotificationService::AllSources()); |
| 142 content::WindowedNotificationObserver( | 122 content::WindowedNotificationObserver( |
| 143 notification_type, content::NotificationService::AllSources()).Wait(); | 123 notification_type, content::NotificationService::AllSources()).Wait(); |
| 144 } | 124 } |
| 145 | 125 |
| 146 bool ExtensionTestNotificationObserver::WaitForPageActionVisibilityChangeTo( | 126 bool ExtensionTestNotificationObserver::WaitForPageActionVisibilityChangeTo( |
| 147 int count) { | 127 int count) { |
| 148 LocationBarTesting* location_bar = | 128 LocationBarTesting* location_bar = |
| 149 browser_->window()->GetLocationBar()->GetLocationBarForTesting(); | 129 browser_->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 130 extensions::ExtensionActionAPI::Get(GetProfile())->AddObserver(this); |
| 150 WaitForCondition( | 131 WaitForCondition( |
| 151 base::Bind( | 132 base::Bind(&HasPageActionVisibilityReachedTarget, location_bar, count), |
| 152 &HasExtensionPageActionVisibilityReachedTarget, location_bar, count), | 133 NULL); |
| 153 extensions::NOTIFICATION_EXTENSION_PAGE_ACTIONS_UPDATED); | 134 extensions::ExtensionActionAPI::Get(GetProfile())-> |
| 135 RemoveObserver(this); |
| 154 return true; | 136 return true; |
| 155 } | 137 } |
| 156 | 138 |
| 157 bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() { | 139 bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() { |
| 158 extensions::ProcessManager* manager = | 140 extensions::ProcessManager* manager = |
| 159 extensions::ExtensionSystem::Get(GetProfile())->process_manager(); | 141 extensions::ExtensionSystem::Get(GetProfile())->process_manager(); |
| 160 NotificationSet notification_set; | 142 NotificationSet notification_set; |
| 161 notification_set.Add(content::NOTIFICATION_WEB_CONTENTS_DESTROYED); | 143 notification_set.Add(content::NOTIFICATION_WEB_CONTENTS_DESTROYED); |
| 162 notification_set.Add(content::NOTIFICATION_LOAD_STOP); | 144 notification_set.Add(content::NOTIFICATION_LOAD_STOP); |
| 163 WaitForCondition( | 145 WaitForCondition( |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 case extensions::NOTIFICATION_EXTENSION_LOAD_ERROR: | 246 case extensions::NOTIFICATION_EXTENSION_LOAD_ERROR: |
| 265 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; | 247 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; |
| 266 ++extension_load_errors_observed_; | 248 ++extension_load_errors_observed_; |
| 267 break; | 249 break; |
| 268 | 250 |
| 269 default: | 251 default: |
| 270 NOTREACHED(); | 252 NOTREACHED(); |
| 271 break; | 253 break; |
| 272 } | 254 } |
| 273 } | 255 } |
| 256 |
| 257 void ExtensionTestNotificationObserver::OnPageActionsUpdated( |
| 258 content::WebContents* web_contents) { |
| 259 MaybeQuit(); |
| 260 } |
| 261 |
| 262 void ExtensionTestNotificationObserver::WaitForCondition( |
| 263 const ConditionCallback& condition, |
| 264 NotificationSet* notification_set) { |
| 265 if (condition.Run()) |
| 266 return; |
| 267 condition_ = condition; |
| 268 |
| 269 scoped_refptr<content::MessageLoopRunner> runner( |
| 270 new content::MessageLoopRunner); |
| 271 quit_closure_ = runner->QuitClosure(); |
| 272 |
| 273 scoped_ptr<base::CallbackList<void()>::Subscription> subscription; |
| 274 if (notification_set) { |
| 275 subscription = notification_set->callback_list().Add( |
| 276 base::Bind(&ExtensionTestNotificationObserver::MaybeQuit, |
| 277 base::Unretained(this))); |
| 278 } |
| 279 runner->Run(); |
| 280 |
| 281 condition_.Reset(); |
| 282 quit_closure_.Reset(); |
| 283 } |
| 284 |
| 285 void ExtensionTestNotificationObserver::MaybeQuit() { |
| 286 if (condition_.Run()) |
| 287 quit_closure_.Run(); |
| 288 } |
| OLD | NEW |