OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/extensions/extension_browser_test_notification_observer .h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #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.
| |
10 #include "base/scoped_observer.h" | |
11 #include "chrome/browser/extensions/extension_action_test_util.h" | |
12 #include "chrome/browser/extensions/extension_util.h" | |
13 #include "chrome/browser/profiles/profile_manager.h" | |
14 #include "chrome/browser/ui/browser.h" | |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
16 #include "content/public/browser/browser_context.h" | |
17 #include "content/public/browser/notification_registrar.h" | |
18 #include "content/public/browser/notification_service.h" | |
19 #include "content/public/browser/render_view_host.h" | |
20 #include "content/public/test/test_utils.h" | |
21 #include "extensions/browser/extension_registry.h" | |
22 #include "extensions/browser/extension_system.h" | |
23 #include "extensions/browser/notification_types.h" | |
24 #include "extensions/browser/process_manager.h" | |
25 #include "extensions/browser/process_manager_observer.h" | |
26 #include "extensions/common/extension.h" | |
27 | |
28 using extensions::Extension; | |
29 | |
30 namespace { | |
31 | |
32 // A callback that returns true if the condition has been met and takes no | |
33 // arguments. | |
34 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.
| |
35 | |
36 bool HasPageActionVisibilityReachedTarget( | |
37 Browser* browser, | |
38 size_t target_visible_page_action_count) { | |
39 return extensions::extension_action_test_util::GetVisiblePageActionCount( | |
40 browser->tab_strip_model()->GetActiveWebContents()) == | |
41 target_visible_page_action_count; | |
42 } | |
43 | |
44 bool HaveAllExtensionRenderFrameHostsFinishedLoading( | |
45 extensions::ProcessManager* manager) { | |
46 extensions::ProcessManager::FrameSet all_views = manager->GetAllFrames(); | |
47 for (content::RenderFrameHost* host : manager->GetAllFrames()) { | |
48 if (content::WebContents::FromRenderFrameHost(host)->IsLoading()) | |
49 return false; | |
50 } | |
51 return true; | |
52 } | |
53 | |
54 bool IsExtensionNotIdle(const std::string& extension_id, | |
55 content::BrowserContext* context) { | |
56 return !extensions::util::IsExtensionIdle(extension_id, context); | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 //////////////////////////////////////////////////////////////////////////////// | |
62 // ExtensionTestNotificationObserver | |
63 | |
64 ExtensionBrowserTestNotificationObserver:: | |
65 ExtensionBrowserTestNotificationObserver(Browser* browser) | |
66 : 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
| |
67 browser_(browser) {} | |
68 | |
69 ExtensionBrowserTestNotificationObserver:: | |
70 ~ExtensionBrowserTestNotificationObserver() {} | |
71 | |
72 content::BrowserContext* | |
73 ExtensionBrowserTestNotificationObserver::GetBrowserContext() { | |
74 if (!context_) { | |
75 if (browser_) | |
76 context_ = browser_->profile(); | |
77 else | |
78 context_ = ProfileManager::GetActiveUserProfile(); | |
79 } | |
80 return context_; | |
81 } | |
82 | |
83 bool ExtensionBrowserTestNotificationObserver:: | |
84 WaitForPageActionVisibilityChangeTo(int count) { | |
85 extensions::ExtensionActionAPI::Get(GetBrowserContext())->AddObserver(this); | |
Devlin
2016/10/06 15:52:16
ScopedObserver?
Rahul Chaturvedi
2016/10/11 20:12:34
Done.
| |
86 WaitForCondition( | |
87 base::Bind(&HasPageActionVisibilityReachedTarget, browser_, count), NULL); | |
88 extensions::ExtensionActionAPI::Get(GetBrowserContext()) | |
89 ->RemoveObserver(this); | |
90 return true; | |
91 } | |
92 | |
93 bool ExtensionBrowserTestNotificationObserver::WaitForExtensionViewsToLoad() { | |
94 extensions::ProcessManager* manager = | |
95 extensions::ProcessManager::Get(GetBrowserContext()); | |
96 NotificationSet notification_set; | |
97 notification_set.Add(content::NOTIFICATION_WEB_CONTENTS_DESTROYED); | |
98 notification_set.Add(content::NOTIFICATION_LOAD_STOP); | |
99 notification_set.AddExtensionFrameUnregistration(manager); | |
100 WaitForCondition( | |
101 base::Bind(&HaveAllExtensionRenderFrameHostsFinishedLoading, manager), | |
102 ¬ification_set); | |
103 return true; | |
104 } | |
105 | |
106 bool ExtensionBrowserTestNotificationObserver::WaitForExtensionIdle( | |
107 const std::string& extension_id) { | |
108 NotificationSet notification_set; | |
109 notification_set.Add(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED); | |
110 WaitForCondition(base::Bind(&extensions::util::IsExtensionIdle, extension_id, | |
111 GetBrowserContext()), | |
112 ¬ification_set); | |
113 return true; | |
114 } | |
115 | |
116 bool ExtensionBrowserTestNotificationObserver::WaitForExtensionNotIdle( | |
117 const std::string& extension_id) { | |
118 NotificationSet notification_set; | |
119 notification_set.Add(content::NOTIFICATION_LOAD_STOP); | |
120 WaitForCondition( | |
121 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.
| |
122 ¬ification_set); | |
123 return true; | |
124 } | |
125 | |
126 void ExtensionBrowserTestNotificationObserver::WaitForExtensionAndViewLoad() { | |
127 this->WaitForExtensionLoad(); | |
Devlin
2016/10/06 15:52:16
no need for this->
Rahul Chaturvedi
2016/10/11 20:12:34
Done.
| |
128 WaitForExtensionViewsToLoad(); | |
129 } | |
130 | |
131 void ExtensionBrowserTestNotificationObserver::OnPageActionsUpdated( | |
132 content::WebContents* web_contents) { | |
133 MaybeQuit(); | |
134 } | |
OLD | NEW |