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..ca62ab497b5c6d7090e8c46ec46aaef497b0d6f7 100644 |
| --- a/chrome/browser/extensions/extension_test_notification_observer.cc |
| +++ b/chrome/browser/extensions/extension_test_notification_observer.cc |
| @@ -20,6 +20,36 @@ using extensions::Extension; |
| namespace { |
| +class ConditionRunLoop { |
| + public: |
| + typedef base::Callback<bool(void)> ConditionCallback; |
| + |
| + explicit ConditionRunLoop(const ConditionCallback& callback); |
| + |
| + void Wait(); |
| + |
| + void Check(); |
| + |
| + private: |
| + base::RunLoop run_loop_; |
|
Avi (use Gerrit)
2013/10/23 19:21:53
MessageLoopRunner is probably a better choice.
|
| + ConditionCallback condition_callback_; |
| +}; |
| + |
| +ConditionRunLoop::ConditionRunLoop(const ConditionCallback& callback) |
| + : condition_callback_(callback) {} |
| + |
| +void ConditionRunLoop::Wait() { |
| + if (condition_callback_.Run()) |
| + return; |
| + |
| + run_loop_.Run(); |
| +} |
| + |
| +void ConditionRunLoop::Check() { |
| + if (condition_callback_.Run()) |
| + run_loop_.Quit(); |
| +} |
| + |
| bool HasExtensionPageActionCountReachedTarget(LocationBarTesting* location_bar, |
| int target_page_action_count) { |
| VLOG(1) << "Number of page actions: " << location_bar->PageActionCount(); |
| @@ -35,6 +65,56 @@ bool HasExtensionPageActionVisibilityReachedTarget( |
| target_visible_page_action_count; |
| } |
| +class NotificationCallbackRunner : public content::NotificationObserver { |
| + public: |
| + NotificationCallbackRunner(int type, |
| + const content::NotificationSource& source, |
| + const base::Closure& callback); |
| + |
| + private: |
| + // content::NotificationObserver |
| + virtual void Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) OVERRIDE; |
| + |
| + content::NotificationRegistrar notification_registrar_; |
| + base::Closure callback_; |
| +}; |
| + |
| +NotificationCallbackRunner::NotificationCallbackRunner( |
| + int type, |
| + const content::NotificationSource& source, |
| + const base::Closure& callback) |
| + : callback_(callback) { |
| + notification_registrar_.Add(this, type, source); |
| +} |
| + |
| +void NotificationCallbackRunner::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + callback_.Run(); |
| +} |
| + |
| +void WaitForConditionWithNotification( |
| + const base::Callback<bool(void)>& condition, |
| + int type, |
| + const content::NotificationSource& source) { |
| + ConditionRunLoop run_loop(condition); |
| + NotificationCallbackRunner callback_runner( |
| + type, |
| + source, |
| + base::Bind(&ConditionRunLoop::Check, base::Unretained(&run_loop))); |
| + run_loop.Wait(); |
| +} |
| + |
| +void WaitForConditionWithNotification( |
| + const base::Callback<bool(void)>& condition, |
| + int type) { |
| + WaitForConditionWithNotification( |
| + condition, type, content::NotificationService::AllSources()); |
| +} |
| + |
| } // namespace |
| ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( |
| @@ -74,33 +154,27 @@ 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); |
| + WaitForConditionWithNotification( |
| + 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); |
| + WaitForConditionWithNotification( |
| + 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(); |
| + extensions::ExtensionSystem::Get(GetProfile())->process_manager(); |
| ExtensionProcessManager::ViewSet all_views = manager->GetAllViews(); |
| for (ExtensionProcessManager::ViewSet::const_iterator iter = |
| all_views.begin(); |
| @@ -113,8 +187,7 @@ bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() { |
| content::WindowedNotificationObserver observer( |
| content::NOTIFICATION_LOAD_STOP, |
| content::NotificationService::AllSources()); |
| - observer.AddNotificationType( |
| - content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| + observer.AddNotificationType(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| content::NotificationService::AllSources()); |
| observer.Wait(); |