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

Side by Side Diff: content/test/mock_platform_notification_service.cc

Issue 2672313004: Test the platform notification context synchronize operation (Closed)
Patch Set: - Created 3 years, 10 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/shell/browser/layout_test/layout_test_notification_manager.h" 5 #include "content/test/mock_platform_notification_service.h"
6 6
7 #include "base/guid.h" 7 #include "base/guid.h"
8 #include "base/strings/nullable_string16.h" 8 #include "base/strings/nullable_string16.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/desktop_notification_delegate.h" 11 #include "content/public/browser/desktop_notification_delegate.h"
12 #include "content/public/browser/notification_event_dispatcher.h" 12 #include "content/public/browser/notification_event_dispatcher.h"
13 #include "content/public/browser/permission_type.h" 13 #include "content/public/browser/permission_type.h"
14 #include "content/public/common/persistent_notification_status.h" 14 #include "content/public/common/persistent_notification_status.h"
15 #include "content/public/common/platform_notification_data.h" 15 #include "content/public/common/platform_notification_data.h"
16 #include "content/shell/browser/layout_test/layout_test_browser_context.h"
17 #include "content/shell/browser/layout_test/layout_test_content_browser_client.h "
18 #include "content/shell/browser/layout_test/layout_test_permission_manager.h"
19 16
20 namespace content { 17 namespace content {
21 namespace { 18 namespace {
22 19
23 // The Web Notification layout tests don't care about the lifetime of the 20 // The Web Notification layout tests don't care about the lifetime of the
24 // Service Worker when a notificationclick event has been dispatched. 21 // Service Worker when a notificationclick event has been dispatched.
25 void OnEventDispatchComplete(PersistentNotificationStatus status) {} 22 void OnEventDispatchComplete(PersistentNotificationStatus status) {}
26 23
27 } // namespace 24 } // namespace
28 25
29 LayoutTestNotificationManager::LayoutTestNotificationManager() 26 MockPlatformNotificationService::MockPlatformNotificationService()
30 : weak_factory_(this) {} 27 : weak_factory_(this) {}
31 28
32 LayoutTestNotificationManager::~LayoutTestNotificationManager() {} 29 MockPlatformNotificationService::~MockPlatformNotificationService() {}
33 30
34 void LayoutTestNotificationManager::DisplayNotification( 31 void MockPlatformNotificationService::DisplayNotification(
35 BrowserContext* browser_context, 32 BrowserContext* browser_context,
36 const std::string& notification_id, 33 const std::string& notification_id,
37 const GURL& origin, 34 const GURL& origin,
38 const PlatformNotificationData& notification_data, 35 const PlatformNotificationData& notification_data,
39 const NotificationResources& notification_resources, 36 const NotificationResources& notification_resources,
40 std::unique_ptr<DesktopNotificationDelegate> delegate, 37 std::unique_ptr<DesktopNotificationDelegate> delegate,
41 base::Closure* cancel_callback) { 38 base::Closure* cancel_callback) {
42 DCHECK_CURRENTLY_ON(BrowserThread::UI); 39 DCHECK_CURRENTLY_ON(BrowserThread::UI);
43 DCHECK(cancel_callback); 40 DCHECK(cancel_callback);
44 41
45 *cancel_callback = base::Bind(&LayoutTestNotificationManager::Close, 42 *cancel_callback = base::Bind(&MockPlatformNotificationService::Close,
46 weak_factory_.GetWeakPtr(), notification_id); 43 weak_factory_.GetWeakPtr(), notification_id);
47 44
48 ReplaceNotificationIfNeeded(notification_id); 45 ReplaceNotificationIfNeeded(notification_id);
49 46
50 non_persistent_notifications_[notification_id] = std::move(delegate); 47 non_persistent_notifications_[notification_id] = std::move(delegate);
51 non_persistent_notifications_[notification_id]->NotificationDisplayed(); 48 non_persistent_notifications_[notification_id]->NotificationDisplayed();
52 49
53 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] = 50 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] =
54 notification_id; 51 notification_id;
55 } 52 }
56 53
57 void LayoutTestNotificationManager::DisplayPersistentNotification( 54 void MockPlatformNotificationService::DisplayPersistentNotification(
58 BrowserContext* browser_context, 55 BrowserContext* browser_context,
59 const std::string& notification_id, 56 const std::string& notification_id,
60 const GURL& service_worker_scope, 57 const GURL& service_worker_scope,
61 const GURL& origin, 58 const GURL& origin,
62 const PlatformNotificationData& notification_data, 59 const PlatformNotificationData& notification_data,
63 const NotificationResources& notification_resources) { 60 const NotificationResources& notification_resources) {
64 DCHECK_CURRENTLY_ON(BrowserThread::UI); 61 DCHECK_CURRENTLY_ON(BrowserThread::UI);
65 62
66 ReplaceNotificationIfNeeded(notification_id); 63 ReplaceNotificationIfNeeded(notification_id);
67 64
68 PersistentNotification notification; 65 PersistentNotification notification;
69 notification.browser_context = browser_context; 66 notification.browser_context = browser_context;
70 notification.origin = origin; 67 notification.origin = origin;
71 68
72 persistent_notifications_[notification_id] = notification; 69 persistent_notifications_[notification_id] = notification;
73 70
74 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] = 71 notification_id_map_[base::UTF16ToUTF8(notification_data.title)] =
75 notification_id; 72 notification_id;
76 } 73 }
77 74
78 void LayoutTestNotificationManager::ClosePersistentNotification( 75 void MockPlatformNotificationService::ClosePersistentNotification(
79 BrowserContext* browser_context, 76 BrowserContext* browser_context,
80 const std::string& notification_id) { 77 const std::string& notification_id) {
81 DCHECK_CURRENTLY_ON(BrowserThread::UI); 78 DCHECK_CURRENTLY_ON(BrowserThread::UI);
82 79
83 persistent_notifications_.erase(notification_id); 80 persistent_notifications_.erase(notification_id);
84 } 81 }
85 82
86 bool LayoutTestNotificationManager::GetDisplayedNotifications( 83 bool MockPlatformNotificationService::GetDisplayedNotifications(
87 BrowserContext* browser_context, 84 BrowserContext* browser_context,
88 std::set<std::string>* displayed_notifications) { 85 std::set<std::string>* displayed_notifications) {
89 DCHECK_CURRENTLY_ON(BrowserThread::UI); 86 DCHECK_CURRENTLY_ON(BrowserThread::UI);
90 DCHECK(displayed_notifications); 87 DCHECK(displayed_notifications);
91 88
92 // Notifications will never outlive the lifetime of running layout tests. 89 for (auto kv : persistent_notifications_) {
Peter Beverloo 2017/02/08 14:05:11 nit: drop the {} for one-line statements
Peter Beverloo 2017/02/08 14:05:11 auto kv -> const auto& pair (using just "auto" wi
Miguel Garcia 2017/02/08 17:03:41 Done.
Miguel Garcia 2017/02/08 17:03:41 Done.
93 return false; 90 displayed_notifications->insert(kv.first);
91 }
92
93 return true;
94 } 94 }
95 95
96 void LayoutTestNotificationManager::SimulateClick( 96 void MockPlatformNotificationService::SimulateClick(
97 const std::string& title, 97 const std::string& title,
98 int action_index, 98 int action_index,
99 const base::NullableString16& reply) { 99 const base::NullableString16& reply) {
100 DCHECK_CURRENTLY_ON(BrowserThread::UI); 100 DCHECK_CURRENTLY_ON(BrowserThread::UI);
101 101
102 const auto notification_id_iter = notification_id_map_.find(title); 102 const auto notification_id_iter = notification_id_map_.find(title);
103 if (notification_id_iter == notification_id_map_.end()) 103 if (notification_id_iter == notification_id_map_.end())
104 return; 104 return;
105 105
106 const std::string& notification_id = notification_id_iter->second; 106 const std::string& notification_id = notification_id_iter->second;
(...skipping 11 matching lines...) Expand all
118 notification.browser_context, notification_id, notification.origin, 118 notification.browser_context, notification_id, notification.origin,
119 action_index, reply, base::Bind(&OnEventDispatchComplete)); 119 action_index, reply, base::Bind(&OnEventDispatchComplete));
120 } else if (non_persistent_iter != non_persistent_notifications_.end()) { 120 } else if (non_persistent_iter != non_persistent_notifications_.end()) {
121 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for " 121 DCHECK_EQ(action_index, -1) << "Action buttons are only supported for "
122 "persistent notifications"; 122 "persistent notifications";
123 123
124 non_persistent_iter->second->NotificationClick(); 124 non_persistent_iter->second->NotificationClick();
125 } 125 }
126 } 126 }
127 127
128 void LayoutTestNotificationManager::SimulateClose(const std::string& title, 128 void MockPlatformNotificationService::SimulateClose(const std::string& title,
129 bool by_user) { 129 bool by_user) {
130 DCHECK_CURRENTLY_ON(BrowserThread::UI); 130 DCHECK_CURRENTLY_ON(BrowserThread::UI);
131 131
132 const auto notification_id_iter = notification_id_map_.find(title); 132 const auto notification_id_iter = notification_id_map_.find(title);
133 if (notification_id_iter == notification_id_map_.end()) 133 if (notification_id_iter == notification_id_map_.end())
134 return; 134 return;
135 135
136 const std::string& notification_id = notification_id_iter->second; 136 const std::string& notification_id = notification_id_iter->second;
137 137
138 const auto& persistent_iter = persistent_notifications_.find(notification_id); 138 const auto& persistent_iter = persistent_notifications_.find(notification_id);
139 if (persistent_iter == persistent_notifications_.end()) 139 if (persistent_iter == persistent_notifications_.end())
140 return; 140 return;
141 141
142 const PersistentNotification& notification = persistent_iter->second; 142 const PersistentNotification& notification = persistent_iter->second;
143 content::NotificationEventDispatcher::GetInstance() 143 content::NotificationEventDispatcher::GetInstance()
144 ->DispatchNotificationCloseEvent( 144 ->DispatchNotificationCloseEvent(
145 notification.browser_context, notification_id, notification.origin, 145 notification.browser_context, notification_id, notification.origin,
146 by_user, base::Bind(&OnEventDispatchComplete)); 146 by_user, base::Bind(&OnEventDispatchComplete));
147 } 147 }
148 148
149 blink::mojom::PermissionStatus 149 blink::mojom::PermissionStatus
150 LayoutTestNotificationManager::CheckPermissionOnUIThread( 150 MockPlatformNotificationService::CheckPermissionOnUIThread(
151 BrowserContext* browser_context, 151 BrowserContext* browser_context,
152 const GURL& origin, 152 const GURL& origin,
153 int render_process_id) { 153 int render_process_id) {
154 DCHECK_CURRENTLY_ON(BrowserThread::UI); 154 DCHECK_CURRENTLY_ON(BrowserThread::UI);
155 return CheckPermission(origin); 155 return CheckPermission(origin);
156 } 156 }
157 157
158 blink::mojom::PermissionStatus 158 blink::mojom::PermissionStatus
159 LayoutTestNotificationManager::CheckPermissionOnIOThread( 159 MockPlatformNotificationService::CheckPermissionOnIOThread(
160 ResourceContext* resource_context, 160 ResourceContext* resource_context,
161 const GURL& origin, 161 const GURL& origin,
162 int render_process_id) { 162 int render_process_id) {
163 DCHECK_CURRENTLY_ON(BrowserThread::IO); 163 DCHECK_CURRENTLY_ON(BrowserThread::IO);
164 return CheckPermission(origin); 164 return CheckPermission(origin);
165 } 165 }
166 166
167 void LayoutTestNotificationManager::Close(const std::string& notification_id) { 167 void MockPlatformNotificationService::Close(
168 const std::string& notification_id) {
168 DCHECK_CURRENTLY_ON(BrowserThread::UI); 169 DCHECK_CURRENTLY_ON(BrowserThread::UI);
169 auto iterator = non_persistent_notifications_.find(notification_id); 170 auto iterator = non_persistent_notifications_.find(notification_id);
170 if (iterator == non_persistent_notifications_.end()) 171 if (iterator == non_persistent_notifications_.end())
171 return; 172 return;
172 173
173 iterator->second->NotificationClosed(); 174 iterator->second->NotificationClosed();
174 } 175 }
175 176
176 void LayoutTestNotificationManager::ReplaceNotificationIfNeeded( 177 void MockPlatformNotificationService::ReplaceNotificationIfNeeded(
177 const std::string& notification_id) { 178 const std::string& notification_id) {
178 const auto persistent_iter = persistent_notifications_.find(notification_id); 179 const auto persistent_iter = persistent_notifications_.find(notification_id);
179 const auto non_persistent_iter = 180 const auto non_persistent_iter =
180 non_persistent_notifications_.find(notification_id); 181 non_persistent_notifications_.find(notification_id);
181 182
182 if (persistent_iter != persistent_notifications_.end()) { 183 if (persistent_iter != persistent_notifications_.end()) {
183 DCHECK(non_persistent_iter == non_persistent_notifications_.end()); 184 DCHECK(non_persistent_iter == non_persistent_notifications_.end());
184 persistent_notifications_.erase(persistent_iter); 185 persistent_notifications_.erase(persistent_iter);
185 } else if (non_persistent_iter != non_persistent_notifications_.end()) { 186 } else if (non_persistent_iter != non_persistent_notifications_.end()) {
186 non_persistent_iter->second->NotificationClosed(); 187 non_persistent_iter->second->NotificationClosed();
187 non_persistent_notifications_.erase(non_persistent_iter); 188 non_persistent_notifications_.erase(non_persistent_iter);
188 } 189 }
189 } 190 }
190 191
191 blink::mojom::PermissionStatus 192 blink::mojom::PermissionStatus MockPlatformNotificationService::CheckPermission(
192 LayoutTestNotificationManager::CheckPermission(const GURL& origin) { 193 const GURL& origin) {
193 return LayoutTestContentBrowserClient::Get() 194 return blink::mojom::PermissionStatus::GRANTED;
194 ->GetLayoutTestBrowserContext()
195 ->GetLayoutTestPermissionManager()
196 ->GetPermissionStatus(PermissionType::NOTIFICATIONS,
197 origin,
198 origin);
199 } 195 }
200 196
201 } // namespace content 197 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698