Chromium Code Reviews| Index: chrome/browser/extensions/extension_test_notification_observer.cc |
| diff --git a/chrome/browser/extensions/extension_test_notification_observer.cc b/chrome/browser/extensions/extension_test_notification_observer.cc |
| index f8b7a6d4ecb1865c55ee1b6a87f2938dac03ba69..01554b296f91c483c4668ed4d628dcd0f0ae4d09 100644 |
| --- a/chrome/browser/extensions/extension_test_notification_observer.cc |
| +++ b/chrome/browser/extensions/extension_test_notification_observer.cc |
| @@ -4,6 +4,7 @@ |
| #include "chrome/browser/extensions/extension_test_notification_observer.h" |
| +#include "base/callback_list.h" |
| #include "chrome/browser/extensions/extension_process_manager.h" |
| #include "chrome/browser/extensions/extension_service.h" |
| #include "chrome/browser/extensions/extension_system.h" |
| @@ -20,6 +21,37 @@ using extensions::Extension; |
| namespace { |
| +class ConditionRunLoop { |
| + public: |
| + typedef base::Callback<bool(void)> ConditionCallback; |
| + |
| + explicit ConditionRunLoop(const ConditionCallback& callback); |
| + |
| + void Wait(); |
| + |
| + void Check(); |
| + |
| + private: |
| + scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| + ConditionCallback condition_callback_; |
| +}; |
| + |
| +ConditionRunLoop::ConditionRunLoop(const ConditionCallback& callback) |
| + : message_loop_runner_(new content::MessageLoopRunner), |
| + condition_callback_(callback) {} |
| + |
| +void ConditionRunLoop::Wait() { |
| + if (condition_callback_.Run()) |
| + return; |
| + |
| + message_loop_runner_->Run(); |
| +} |
| + |
| +void ConditionRunLoop::Check() { |
| + if (condition_callback_.Run()) |
| + message_loop_runner_->Quit(); |
| +} |
| + |
| bool HasExtensionPageActionCountReachedTarget(LocationBarTesting* location_bar, |
| int target_page_action_count) { |
| VLOG(1) << "Number of page actions: " << location_bar->PageActionCount(); |
| @@ -35,6 +67,74 @@ bool HasExtensionPageActionVisibilityReachedTarget( |
| target_visible_page_action_count; |
| } |
| +bool HaveAllExtensionRenderViewHostsFinishedLoading( |
| + ExtensionProcessManager* manager) { |
| + ExtensionProcessManager::ViewSet all_views = manager->GetAllViews(); |
| + for (ExtensionProcessManager::ViewSet::const_iterator iter = |
| + all_views.begin(); |
| + iter != all_views.end(); ++iter) { |
| + if ((*iter)->IsLoading()) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +class NotificationSet : public content::NotificationObserver { |
| + public: |
| + void Add(int type, const content::NotificationSource& source); |
| + void Add(int type); |
| + |
| + // Notified any time an Add()ed notification is received. |
| + // The details of the notification are dropped. |
| + base::CallbackList<void()>& callback_list() { |
| + return callback_list_; |
| + } |
| + |
| + private: |
| + // content::NotificationObserver: |
| + virtual void Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) OVERRIDE; |
| + |
| + content::NotificationRegistrar notification_registrar_; |
| + base::CallbackList<void()> callback_list_; |
| +}; |
| + |
| +void NotificationSet::Add( |
| + int type, |
| + const content::NotificationSource& source) { |
| + notification_registrar_.Add(this, type, source); |
| +} |
| + |
| +void NotificationSet::Add(int type) { |
| + Add(type, content::NotificationService::AllSources()); |
| +} |
| + |
| +void NotificationSet::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + callback_list_.Notify(); |
| +} |
| + |
| +void WaitForCondition( |
| + const base::Callback<bool(void)>& condition, |
| + NotificationSet* notification_set) { |
| + ConditionRunLoop run_loop(condition); |
|
Jeffrey Yasskin
2013/11/06 03:33:59
Since this is now the only use of ConditionRunLoop
Bernhard Bauer
2013/11/11 14:37:44
Done!
|
| + scoped_ptr<base::CallbackList<void()>::Subscription> subscription = |
| + notification_set->callback_list().Add( |
| + base::Bind(&ConditionRunLoop::Check, base::Unretained(&run_loop))); |
| + run_loop.Wait(); |
| +} |
| + |
| +void WaitForCondition( |
| + const base::Callback<bool(void)>& condition, |
| + int type) { |
| + NotificationSet notification_set; |
|
Jeffrey Yasskin
2013/11/06 03:33:59
I think I'd weakly prefer duplicating these two li
Bernhard Bauer
2013/11/11 14:37:44
Hm... I kind of like having the short cut.
|
| + notification_set.Add(type); |
| + WaitForCondition(condition, ¬ification_set); |
| +} |
| + |
| } // namespace |
| ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( |
| @@ -74,57 +174,33 @@ bool ExtensionTestNotificationObserver::WaitForPageActionCountChangeTo( |
| int count) { |
| LocationBarTesting* location_bar = |
| browser_->window()->GetLocationBar()->GetLocationBarForTesting(); |
| - if (!HasExtensionPageActionCountReachedTarget(location_bar, count)) { |
| - content::WindowedNotificationObserver( |
| - chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED, |
| - base::Bind( |
| - &HasExtensionPageActionCountReachedTarget, location_bar, count)) |
| - .Wait(); |
| - } |
| - return HasExtensionPageActionCountReachedTarget(location_bar, count); |
| + WaitForCondition( |
| + base::Bind( |
| + &HasExtensionPageActionCountReachedTarget, location_bar, count), |
| + chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED); |
| + return true; |
| } |
| bool ExtensionTestNotificationObserver::WaitForPageActionVisibilityChangeTo( |
| int count) { |
| LocationBarTesting* location_bar = |
| browser_->window()->GetLocationBar()->GetLocationBarForTesting(); |
| - if (!HasExtensionPageActionVisibilityReachedTarget(location_bar, count)) { |
| - content::WindowedNotificationObserver( |
| - chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, |
| - base::Bind(&HasExtensionPageActionVisibilityReachedTarget, |
| - location_bar, |
| - count)).Wait(); |
| - } |
| - return HasExtensionPageActionVisibilityReachedTarget(location_bar, count); |
| + WaitForCondition( |
| + base::Bind( |
| + &HasExtensionPageActionVisibilityReachedTarget, location_bar, count), |
| + chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED); |
| + return true; |
| } |
| bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() { |
| ExtensionProcessManager* manager = |
| - extensions::ExtensionSystem::Get(GetProfile())->process_manager(); |
| - ExtensionProcessManager::ViewSet all_views = manager->GetAllViews(); |
| - for (ExtensionProcessManager::ViewSet::const_iterator iter = |
| - all_views.begin(); |
| - iter != all_views.end();) { |
| - if (!(*iter)->IsLoading()) { |
| - ++iter; |
| - } else { |
| - // Wait for all the extension render view hosts that exist to finish |
| - // loading. |
| - content::WindowedNotificationObserver observer( |
| - content::NOTIFICATION_LOAD_STOP, |
| - content::NotificationService::AllSources()); |
| - observer.AddNotificationType( |
| - content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| - content::NotificationService::AllSources()); |
| - observer.Wait(); |
| - |
| - // Test activity may have modified the set of extension processes during |
| - // message processing, so re-start the iteration to catch added/removed |
| - // processes. |
| - all_views = manager->GetAllViews(); |
| - iter = all_views.begin(); |
| - } |
| - } |
| + extensions::ExtensionSystem::Get(GetProfile())->process_manager(); |
| + NotificationSet notification_set; |
| + notification_set.Add(content::NOTIFICATION_WEB_CONTENTS_DESTROYED); |
| + notification_set.Add(content::NOTIFICATION_LOAD_STOP); |
| + WaitForCondition( |
| + base::Bind(&HaveAllExtensionRenderViewHostsFinishedLoading, manager), |
| + ¬ification_set); |
| return true; |
| } |