Chromium Code Reviews| 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 "chrome/browser/extensions/extension_process_manager.h" | 7 #include "chrome/browser/extensions/extension_process_manager.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" | 9 #include "chrome/browser/extensions/extension_system.h" |
| 10 #include "chrome/browser/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
| 11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
| 13 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
| 15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 17 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
| 18 | 18 |
| 19 using extensions::Extension; | 19 using extensions::Extension; |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class ConditionRunLoop { | |
| 24 public: | |
| 25 typedef base::Callback<bool(void)> ConditionCallback; | |
| 26 | |
| 27 explicit ConditionRunLoop(const ConditionCallback& callback); | |
| 28 | |
| 29 void Wait(); | |
| 30 | |
| 31 void Check(); | |
| 32 | |
| 33 private: | |
| 34 base::RunLoop run_loop_; | |
|
Avi (use Gerrit)
2013/10/23 19:21:53
MessageLoopRunner is probably a better choice.
| |
| 35 ConditionCallback condition_callback_; | |
| 36 }; | |
| 37 | |
| 38 ConditionRunLoop::ConditionRunLoop(const ConditionCallback& callback) | |
| 39 : condition_callback_(callback) {} | |
| 40 | |
| 41 void ConditionRunLoop::Wait() { | |
| 42 if (condition_callback_.Run()) | |
| 43 return; | |
| 44 | |
| 45 run_loop_.Run(); | |
| 46 } | |
| 47 | |
| 48 void ConditionRunLoop::Check() { | |
| 49 if (condition_callback_.Run()) | |
| 50 run_loop_.Quit(); | |
| 51 } | |
| 52 | |
| 23 bool HasExtensionPageActionCountReachedTarget(LocationBarTesting* location_bar, | 53 bool HasExtensionPageActionCountReachedTarget(LocationBarTesting* location_bar, |
| 24 int target_page_action_count) { | 54 int target_page_action_count) { |
| 25 VLOG(1) << "Number of page actions: " << location_bar->PageActionCount(); | 55 VLOG(1) << "Number of page actions: " << location_bar->PageActionCount(); |
| 26 return location_bar->PageActionCount() == target_page_action_count; | 56 return location_bar->PageActionCount() == target_page_action_count; |
| 27 } | 57 } |
| 28 | 58 |
| 29 bool HasExtensionPageActionVisibilityReachedTarget( | 59 bool HasExtensionPageActionVisibilityReachedTarget( |
| 30 LocationBarTesting* location_bar, | 60 LocationBarTesting* location_bar, |
| 31 int target_visible_page_action_count) { | 61 int target_visible_page_action_count) { |
| 32 VLOG(1) << "Number of visible page actions: " | 62 VLOG(1) << "Number of visible page actions: " |
| 33 << location_bar->PageActionVisibleCount(); | 63 << location_bar->PageActionVisibleCount(); |
| 34 return location_bar->PageActionVisibleCount() == | 64 return location_bar->PageActionVisibleCount() == |
| 35 target_visible_page_action_count; | 65 target_visible_page_action_count; |
| 36 } | 66 } |
| 37 | 67 |
| 68 class NotificationCallbackRunner : public content::NotificationObserver { | |
| 69 public: | |
| 70 NotificationCallbackRunner(int type, | |
| 71 const content::NotificationSource& source, | |
| 72 const base::Closure& callback); | |
| 73 | |
| 74 private: | |
| 75 // content::NotificationObserver | |
| 76 virtual void Observe(int type, | |
| 77 const content::NotificationSource& source, | |
| 78 const content::NotificationDetails& details) OVERRIDE; | |
| 79 | |
| 80 content::NotificationRegistrar notification_registrar_; | |
| 81 base::Closure callback_; | |
| 82 }; | |
| 83 | |
| 84 NotificationCallbackRunner::NotificationCallbackRunner( | |
| 85 int type, | |
| 86 const content::NotificationSource& source, | |
| 87 const base::Closure& callback) | |
| 88 : callback_(callback) { | |
| 89 notification_registrar_.Add(this, type, source); | |
| 90 } | |
| 91 | |
| 92 void NotificationCallbackRunner::Observe( | |
| 93 int type, | |
| 94 const content::NotificationSource& source, | |
| 95 const content::NotificationDetails& details) { | |
| 96 callback_.Run(); | |
| 97 } | |
| 98 | |
| 99 void WaitForConditionWithNotification( | |
| 100 const base::Callback<bool(void)>& condition, | |
| 101 int type, | |
| 102 const content::NotificationSource& source) { | |
| 103 ConditionRunLoop run_loop(condition); | |
| 104 NotificationCallbackRunner callback_runner( | |
| 105 type, | |
| 106 source, | |
| 107 base::Bind(&ConditionRunLoop::Check, base::Unretained(&run_loop))); | |
| 108 run_loop.Wait(); | |
| 109 } | |
| 110 | |
| 111 void WaitForConditionWithNotification( | |
| 112 const base::Callback<bool(void)>& condition, | |
| 113 int type) { | |
| 114 WaitForConditionWithNotification( | |
| 115 condition, type, content::NotificationService::AllSources()); | |
| 116 } | |
| 117 | |
| 38 } // namespace | 118 } // namespace |
| 39 | 119 |
| 40 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( | 120 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( |
| 41 Browser* browser) | 121 Browser* browser) |
| 42 : browser_(browser), | 122 : browser_(browser), |
| 43 profile_(NULL), | 123 profile_(NULL), |
| 44 extension_installs_observed_(0), | 124 extension_installs_observed_(0), |
| 45 extension_load_errors_observed_(0), | 125 extension_load_errors_observed_(0), |
| 46 crx_installers_done_observed_(0) { | 126 crx_installers_done_observed_(0) { |
| 47 } | 127 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 67 registrar.Add( | 147 registrar.Add( |
| 68 this, notification_type, content::NotificationService::AllSources()); | 148 this, notification_type, content::NotificationService::AllSources()); |
| 69 content::WindowedNotificationObserver( | 149 content::WindowedNotificationObserver( |
| 70 notification_type, content::NotificationService::AllSources()).Wait(); | 150 notification_type, content::NotificationService::AllSources()).Wait(); |
| 71 } | 151 } |
| 72 | 152 |
| 73 bool ExtensionTestNotificationObserver::WaitForPageActionCountChangeTo( | 153 bool ExtensionTestNotificationObserver::WaitForPageActionCountChangeTo( |
| 74 int count) { | 154 int count) { |
| 75 LocationBarTesting* location_bar = | 155 LocationBarTesting* location_bar = |
| 76 browser_->window()->GetLocationBar()->GetLocationBarForTesting(); | 156 browser_->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 77 if (!HasExtensionPageActionCountReachedTarget(location_bar, count)) { | 157 WaitForConditionWithNotification( |
| 78 content::WindowedNotificationObserver( | 158 base::Bind( |
| 79 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED, | 159 &HasExtensionPageActionCountReachedTarget, location_bar, count), |
| 80 base::Bind( | 160 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED); |
| 81 &HasExtensionPageActionCountReachedTarget, location_bar, count)) | 161 return true; |
| 82 .Wait(); | |
| 83 } | |
| 84 return HasExtensionPageActionCountReachedTarget(location_bar, count); | |
| 85 } | 162 } |
| 86 | 163 |
| 87 bool ExtensionTestNotificationObserver::WaitForPageActionVisibilityChangeTo( | 164 bool ExtensionTestNotificationObserver::WaitForPageActionVisibilityChangeTo( |
| 88 int count) { | 165 int count) { |
| 89 LocationBarTesting* location_bar = | 166 LocationBarTesting* location_bar = |
| 90 browser_->window()->GetLocationBar()->GetLocationBarForTesting(); | 167 browser_->window()->GetLocationBar()->GetLocationBarForTesting(); |
| 91 if (!HasExtensionPageActionVisibilityReachedTarget(location_bar, count)) { | 168 WaitForConditionWithNotification( |
| 92 content::WindowedNotificationObserver( | 169 base::Bind( |
| 93 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, | 170 &HasExtensionPageActionVisibilityReachedTarget, location_bar, count), |
| 94 base::Bind(&HasExtensionPageActionVisibilityReachedTarget, | 171 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED); |
| 95 location_bar, | 172 return true; |
| 96 count)).Wait(); | |
| 97 } | |
| 98 return HasExtensionPageActionVisibilityReachedTarget(location_bar, count); | |
| 99 } | 173 } |
| 100 | 174 |
| 101 bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() { | 175 bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() { |
| 102 ExtensionProcessManager* manager = | 176 ExtensionProcessManager* manager = |
| 103 extensions::ExtensionSystem::Get(GetProfile())->process_manager(); | 177 extensions::ExtensionSystem::Get(GetProfile())->process_manager(); |
| 104 ExtensionProcessManager::ViewSet all_views = manager->GetAllViews(); | 178 ExtensionProcessManager::ViewSet all_views = manager->GetAllViews(); |
| 105 for (ExtensionProcessManager::ViewSet::const_iterator iter = | 179 for (ExtensionProcessManager::ViewSet::const_iterator iter = |
| 106 all_views.begin(); | 180 all_views.begin(); |
| 107 iter != all_views.end();) { | 181 iter != all_views.end();) { |
| 108 if (!(*iter)->IsLoading()) { | 182 if (!(*iter)->IsLoading()) { |
| 109 ++iter; | 183 ++iter; |
| 110 } else { | 184 } else { |
| 111 // Wait for all the extension render view hosts that exist to finish | 185 // Wait for all the extension render view hosts that exist to finish |
| 112 // loading. | 186 // loading. |
| 113 content::WindowedNotificationObserver observer( | 187 content::WindowedNotificationObserver observer( |
| 114 content::NOTIFICATION_LOAD_STOP, | 188 content::NOTIFICATION_LOAD_STOP, |
| 115 content::NotificationService::AllSources()); | 189 content::NotificationService::AllSources()); |
| 116 observer.AddNotificationType( | 190 observer.AddNotificationType(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 117 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
| 118 content::NotificationService::AllSources()); | 191 content::NotificationService::AllSources()); |
| 119 observer.Wait(); | 192 observer.Wait(); |
| 120 | 193 |
| 121 // Test activity may have modified the set of extension processes during | 194 // Test activity may have modified the set of extension processes during |
| 122 // message processing, so re-start the iteration to catch added/removed | 195 // message processing, so re-start the iteration to catch added/removed |
| 123 // processes. | 196 // processes. |
| 124 all_views = manager->GetAllViews(); | 197 all_views = manager->GetAllViews(); |
| 125 iter = all_views.begin(); | 198 iter = all_views.begin(); |
| 126 } | 199 } |
| 127 } | 200 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 case chrome::NOTIFICATION_EXTENSION_LOAD_ERROR: | 298 case chrome::NOTIFICATION_EXTENSION_LOAD_ERROR: |
| 226 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; | 299 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; |
| 227 ++extension_load_errors_observed_; | 300 ++extension_load_errors_observed_; |
| 228 break; | 301 break; |
| 229 | 302 |
| 230 default: | 303 default: |
| 231 NOTREACHED(); | 304 NOTREACHED(); |
| 232 break; | 305 break; |
| 233 } | 306 } |
| 234 } | 307 } |
| OLD | NEW |