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

Unified Diff: chrome/browser/extensions/extension_test_notification_observer.cc

Issue 30943005: Add WaitForCondition() and use it in ExtensionTestNotificationObserver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698