Chromium Code Reviews| Index: chrome/browser/extensions/extension_browser_test_notification_observer.cc |
| diff --git a/chrome/browser/extensions/extension_browser_test_notification_observer.cc b/chrome/browser/extensions/extension_browser_test_notification_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..38096ebf71a2c5420c13961a992d2b7a353f26ec |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_browser_test_notification_observer.cc |
| @@ -0,0 +1,134 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/extension_browser_test_notification_observer.h" |
| + |
| +#include <stddef.h> |
| + |
| +#include "base/callback_list.h" |
|
Devlin
2016/10/06 15:52:16
here too, please trim.
Rahul Chaturvedi
2016/10/11 20:12:34
Done.
|
| +#include "base/scoped_observer.h" |
| +#include "chrome/browser/extensions/extension_action_test_util.h" |
| +#include "chrome/browser/extensions/extension_util.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/notification_registrar.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "extensions/browser/extension_registry.h" |
| +#include "extensions/browser/extension_system.h" |
| +#include "extensions/browser/notification_types.h" |
| +#include "extensions/browser/process_manager.h" |
| +#include "extensions/browser/process_manager_observer.h" |
| +#include "extensions/common/extension.h" |
| + |
| +using extensions::Extension; |
| + |
| +namespace { |
| + |
| +// A callback that returns true if the condition has been met and takes no |
| +// arguments. |
| +typedef base::Callback<bool(void)> ConditionCallback; |
|
Devlin
2016/10/06 15:52:16
prefer using syntax
Rahul Chaturvedi
2016/10/11 20:12:34
Done.
|
| + |
| +bool HasPageActionVisibilityReachedTarget( |
| + Browser* browser, |
| + size_t target_visible_page_action_count) { |
| + return extensions::extension_action_test_util::GetVisiblePageActionCount( |
| + browser->tab_strip_model()->GetActiveWebContents()) == |
| + target_visible_page_action_count; |
| +} |
| + |
| +bool HaveAllExtensionRenderFrameHostsFinishedLoading( |
| + extensions::ProcessManager* manager) { |
| + extensions::ProcessManager::FrameSet all_views = manager->GetAllFrames(); |
| + for (content::RenderFrameHost* host : manager->GetAllFrames()) { |
| + if (content::WebContents::FromRenderFrameHost(host)->IsLoading()) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +bool IsExtensionNotIdle(const std::string& extension_id, |
| + content::BrowserContext* context) { |
| + return !extensions::util::IsExtensionIdle(extension_id, context); |
| +} |
| + |
| +} // namespace |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// ExtensionTestNotificationObserver |
| + |
| +ExtensionBrowserTestNotificationObserver:: |
| + ExtensionBrowserTestNotificationObserver(Browser* browser) |
| + : ExtensionTestNotificationObserver(GetBrowserContext()), |
|
Devlin
2016/10/06 15:52:16
This is called before browser_ is set, so it will
Rahul Chaturvedi
2016/10/11 20:12:34
Whoops, initialization order.
Doesn't seem like a
|
| + browser_(browser) {} |
| + |
| +ExtensionBrowserTestNotificationObserver:: |
| + ~ExtensionBrowserTestNotificationObserver() {} |
| + |
| +content::BrowserContext* |
| +ExtensionBrowserTestNotificationObserver::GetBrowserContext() { |
| + if (!context_) { |
| + if (browser_) |
| + context_ = browser_->profile(); |
| + else |
| + context_ = ProfileManager::GetActiveUserProfile(); |
| + } |
| + return context_; |
| +} |
| + |
| +bool ExtensionBrowserTestNotificationObserver:: |
| + WaitForPageActionVisibilityChangeTo(int count) { |
| + extensions::ExtensionActionAPI::Get(GetBrowserContext())->AddObserver(this); |
|
Devlin
2016/10/06 15:52:16
ScopedObserver?
Rahul Chaturvedi
2016/10/11 20:12:34
Done.
|
| + WaitForCondition( |
| + base::Bind(&HasPageActionVisibilityReachedTarget, browser_, count), NULL); |
| + extensions::ExtensionActionAPI::Get(GetBrowserContext()) |
| + ->RemoveObserver(this); |
| + return true; |
| +} |
| + |
| +bool ExtensionBrowserTestNotificationObserver::WaitForExtensionViewsToLoad() { |
| + extensions::ProcessManager* manager = |
| + extensions::ProcessManager::Get(GetBrowserContext()); |
| + NotificationSet notification_set; |
| + notification_set.Add(content::NOTIFICATION_WEB_CONTENTS_DESTROYED); |
| + notification_set.Add(content::NOTIFICATION_LOAD_STOP); |
| + notification_set.AddExtensionFrameUnregistration(manager); |
| + WaitForCondition( |
| + base::Bind(&HaveAllExtensionRenderFrameHostsFinishedLoading, manager), |
| + ¬ification_set); |
| + return true; |
| +} |
| + |
| +bool ExtensionBrowserTestNotificationObserver::WaitForExtensionIdle( |
| + const std::string& extension_id) { |
| + NotificationSet notification_set; |
| + notification_set.Add(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); |
| + WaitForCondition(base::Bind(&extensions::util::IsExtensionIdle, extension_id, |
| + GetBrowserContext()), |
| + ¬ification_set); |
| + return true; |
| +} |
| + |
| +bool ExtensionBrowserTestNotificationObserver::WaitForExtensionNotIdle( |
| + const std::string& extension_id) { |
| + NotificationSet notification_set; |
| + notification_set.Add(content::NOTIFICATION_LOAD_STOP); |
| + WaitForCondition( |
| + base::Bind(&IsExtensionNotIdle, extension_id, GetBrowserContext()), |
|
Devlin
2016/10/06 15:52:16
can we use a lambda here?
Rahul Chaturvedi
2016/10/11 20:12:34
Done.
|
| + ¬ification_set); |
| + return true; |
| +} |
| + |
| +void ExtensionBrowserTestNotificationObserver::WaitForExtensionAndViewLoad() { |
| + this->WaitForExtensionLoad(); |
|
Devlin
2016/10/06 15:52:16
no need for this->
Rahul Chaturvedi
2016/10/11 20:12:34
Done.
|
| + WaitForExtensionViewsToLoad(); |
| +} |
| + |
| +void ExtensionBrowserTestNotificationObserver::OnPageActionsUpdated( |
| + content::WebContents* web_contents) { |
| + MaybeQuit(); |
| +} |