OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/notifications/notification_platform_bridge_linux.h" | 5 #include "chrome/browser/notifications/notification_platform_bridge_linux.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 gfx::Image(), | 40 gfx::Image(), |
41 message_center::NotifierId(GURL()), | 41 message_center::NotifierId(GURL()), |
42 base::string16(), | 42 base::string16(), |
43 GURL(), | 43 GURL(), |
44 id, | 44 id, |
45 message_center::RichNotificationData(), | 45 message_center::RichNotificationData(), |
46 new MockNotificationDelegate(id)) {} | 46 new MockNotificationDelegate(id)) {} |
47 | 47 |
48 Notification GetResult() { return notification_; } | 48 Notification GetResult() { return notification_; } |
49 | 49 |
| 50 NotificationBuilder& SetItems( |
| 51 const std::vector<message_center::NotificationItem>& items) { |
| 52 notification_.set_items(items); |
| 53 return *this; |
| 54 } |
| 55 |
| 56 NotificationBuilder& SetMessage(const base::string16& message) { |
| 57 notification_.set_message(message); |
| 58 return *this; |
| 59 } |
| 60 |
| 61 NotificationBuilder& SetNeverTimeout(bool never_timeout) { |
| 62 notification_.set_never_timeout(never_timeout); |
| 63 return *this; |
| 64 } |
| 65 |
| 66 NotificationBuilder& SetOriginUrl(const GURL& origin_url) { |
| 67 notification_.set_origin_url(origin_url); |
| 68 return *this; |
| 69 } |
| 70 |
50 NotificationBuilder& SetProgress(int progress) { | 71 NotificationBuilder& SetProgress(int progress) { |
51 notification_.set_progress(progress); | 72 notification_.set_progress(progress); |
52 return *this; | 73 return *this; |
53 } | 74 } |
54 | 75 |
55 NotificationBuilder& SetTitle(const base::string16& title) { | 76 NotificationBuilder& SetTitle(const base::string16& title) { |
56 notification_.set_title(title); | 77 notification_.set_title(title); |
57 return *this; | 78 return *this; |
58 } | 79 } |
59 | 80 |
60 NotificationBuilder& SetType(message_center::NotificationType type) { | 81 NotificationBuilder& SetType(message_center::NotificationType type) { |
61 notification_.set_type(type); | 82 notification_.set_type(type); |
62 return *this; | 83 return *this; |
63 } | 84 } |
64 | 85 |
65 private: | 86 private: |
66 Notification notification_; | 87 Notification notification_; |
67 }; | 88 }; |
68 | 89 |
69 struct NotificationRequest { | 90 struct NotificationRequest { |
70 std::string summary; | 91 std::string summary; |
| 92 std::string body; |
| 93 int32_t expire_timeout = 0; |
71 }; | 94 }; |
72 | 95 |
73 NotificationRequest ParseRequest(dbus::MethodCall* method_call) { | 96 NotificationRequest ParseRequest(dbus::MethodCall* method_call) { |
74 // The "Notify" message must have type (susssasa{sv}i). | 97 // The "Notify" message must have type (susssasa{sv}i). |
75 // https://developer.gnome.org/notification-spec/#command-notify | 98 // https://developer.gnome.org/notification-spec/#command-notify |
76 NotificationRequest request; | 99 NotificationRequest request; |
77 | 100 |
78 dbus::MessageReader reader(method_call); | 101 dbus::MessageReader reader(method_call); |
79 std::string str; | 102 std::string str; |
80 uint32_t uint32; | 103 uint32_t uint32; |
81 int32_t int32; | |
82 EXPECT_TRUE(reader.PopString(&str)); // app_name | 104 EXPECT_TRUE(reader.PopString(&str)); // app_name |
83 EXPECT_TRUE(reader.PopUint32(&uint32)); // replaces_id | 105 EXPECT_TRUE(reader.PopUint32(&uint32)); // replaces_id |
84 EXPECT_TRUE(reader.PopString(&str)); // app_icon | 106 EXPECT_TRUE(reader.PopString(&str)); // app_icon |
85 EXPECT_TRUE(reader.PopString(&request.summary)); // summary | 107 EXPECT_TRUE(reader.PopString(&request.summary)); // summary |
86 EXPECT_TRUE(reader.PopString(&str)); // body | 108 EXPECT_TRUE(reader.PopString(&request.body)); // body |
87 | 109 |
88 { | 110 { |
89 dbus::MessageReader actions_reader(nullptr); | 111 dbus::MessageReader actions_reader(nullptr); |
90 EXPECT_TRUE(reader.PopArray(&actions_reader)); | 112 EXPECT_TRUE(reader.PopArray(&actions_reader)); |
91 while (actions_reader.HasMoreData()) { | 113 while (actions_reader.HasMoreData()) { |
92 // Actions come in pairs. | 114 // Actions come in pairs. |
93 EXPECT_TRUE(actions_reader.PopString(&str)); | 115 EXPECT_TRUE(actions_reader.PopString(&str)); |
94 EXPECT_TRUE(actions_reader.PopString(&str)); | 116 EXPECT_TRUE(actions_reader.PopString(&str)); |
95 } | 117 } |
96 } | 118 } |
97 | 119 |
98 { | 120 { |
99 dbus::MessageReader hints_reader(nullptr); | 121 dbus::MessageReader hints_reader(nullptr); |
100 EXPECT_TRUE(reader.PopArray(&hints_reader)); | 122 EXPECT_TRUE(reader.PopArray(&hints_reader)); |
101 while (hints_reader.HasMoreData()) { | 123 while (hints_reader.HasMoreData()) { |
102 dbus::MessageReader dict_entry_reader(nullptr); | 124 dbus::MessageReader dict_entry_reader(nullptr); |
103 EXPECT_TRUE(hints_reader.PopDictEntry(&dict_entry_reader)); | 125 EXPECT_TRUE(hints_reader.PopDictEntry(&dict_entry_reader)); |
104 EXPECT_TRUE(dict_entry_reader.PopString(&str)); | 126 EXPECT_TRUE(dict_entry_reader.PopString(&str)); |
105 dbus::MessageReader variant_reader(nullptr); | 127 dbus::MessageReader variant_reader(nullptr); |
106 EXPECT_TRUE(dict_entry_reader.PopVariant(&variant_reader)); | 128 EXPECT_TRUE(dict_entry_reader.PopVariant(&variant_reader)); |
107 EXPECT_FALSE(dict_entry_reader.HasMoreData()); | 129 EXPECT_FALSE(dict_entry_reader.HasMoreData()); |
108 } | 130 } |
109 } | 131 } |
110 | 132 |
111 EXPECT_TRUE(reader.PopInt32(&int32)); // expire_timeout | 133 EXPECT_TRUE(reader.PopInt32(&request.expire_timeout)); |
112 EXPECT_FALSE(reader.HasMoreData()); | 134 EXPECT_FALSE(reader.HasMoreData()); |
113 | 135 |
114 return request; | 136 return request; |
115 } | 137 } |
116 | 138 |
117 dbus::Response* GetIdResponse(uint32_t id) { | 139 dbus::Response* GetIdResponse(uint32_t id) { |
118 dbus::Response* response = dbus::Response::CreateEmpty().release(); | 140 dbus::Response* response = dbus::Response::CreateEmpty().release(); |
119 dbus::MessageWriter writer(response); | 141 dbus::MessageWriter writer(response); |
120 writer.AppendUint32(id); | 142 writer.AppendUint32(id); |
121 return response; | 143 return response; |
(...skipping 16 matching lines...) Expand all Loading... |
138 ACTION_P(OnGetServerInformation, spec_version) { | 160 ACTION_P(OnGetServerInformation, spec_version) { |
139 dbus::Response* response = dbus::Response::CreateEmpty().release(); | 161 dbus::Response* response = dbus::Response::CreateEmpty().release(); |
140 dbus::MessageWriter writer(response); | 162 dbus::MessageWriter writer(response); |
141 writer.AppendString(""); // name | 163 writer.AppendString(""); // name |
142 writer.AppendString(""); // vendor | 164 writer.AppendString(""); // vendor |
143 writer.AppendString(""); // version | 165 writer.AppendString(""); // version |
144 writer.AppendString(spec_version); | 166 writer.AppendString(spec_version); |
145 return response; | 167 return response; |
146 } | 168 } |
147 | 169 |
148 ACTION_P(OnNotify, id) { | 170 ACTION_P2(OnNotify, verifier, id) { |
149 ParseRequest(arg0); | 171 verifier(ParseRequest(arg0)); |
150 return GetIdResponse(id); | 172 return GetIdResponse(id); |
151 } | 173 } |
152 | 174 |
153 ACTION(OnCloseNotification) { | 175 ACTION(OnCloseNotification) { |
154 // The "CloseNotification" message must have type (u). | 176 // The "CloseNotification" message must have type (u). |
155 // https://developer.gnome.org/notification-spec/#command-close-notification | 177 // https://developer.gnome.org/notification-spec/#command-close-notification |
156 dbus::MethodCall* method_call = arg0; | 178 dbus::MethodCall* method_call = arg0; |
157 dbus::MessageReader reader(method_call); | 179 dbus::MessageReader reader(method_call); |
158 uint32_t uint32; | 180 uint32_t uint32; |
159 EXPECT_TRUE(reader.PopUint32(&uint32)); | 181 EXPECT_TRUE(reader.PopUint32(&uint32)); |
(...skipping 20 matching lines...) Expand all Loading... |
180 mock_bus_.get(), kFreedesktopNotificationsName, | 202 mock_bus_.get(), kFreedesktopNotificationsName, |
181 dbus::ObjectPath(kFreedesktopNotificationsPath)); | 203 dbus::ObjectPath(kFreedesktopNotificationsPath)); |
182 | 204 |
183 EXPECT_CALL(*mock_bus_.get(), | 205 EXPECT_CALL(*mock_bus_.get(), |
184 GetObjectProxy(kFreedesktopNotificationsName, | 206 GetObjectProxy(kFreedesktopNotificationsName, |
185 dbus::ObjectPath(kFreedesktopNotificationsPath))) | 207 dbus::ObjectPath(kFreedesktopNotificationsPath))) |
186 .WillOnce(Return(mock_notification_proxy_.get())); | 208 .WillOnce(Return(mock_notification_proxy_.get())); |
187 | 209 |
188 EXPECT_CALL(*mock_notification_proxy_.get(), | 210 EXPECT_CALL(*mock_notification_proxy_.get(), |
189 MockCallMethodAndBlock(Calls("GetCapabilities"), _)) | 211 MockCallMethodAndBlock(Calls("GetCapabilities"), _)) |
190 .WillOnce(OnGetCapabilities(std::vector<std::string>())); | 212 .WillOnce(OnGetCapabilities(std::vector<std::string>{"body"})); |
191 | 213 |
192 EXPECT_CALL(*mock_notification_proxy_.get(), | 214 EXPECT_CALL(*mock_notification_proxy_.get(), |
193 MockCallMethodAndBlock(Calls("GetServerInformation"), _)) | 215 MockCallMethodAndBlock(Calls("GetServerInformation"), _)) |
194 .WillOnce(OnGetServerInformation("1.2")); | 216 .WillOnce(OnGetServerInformation("1.2")); |
195 | 217 |
196 EXPECT_CALL( | 218 EXPECT_CALL( |
197 *mock_notification_proxy_.get(), | 219 *mock_notification_proxy_.get(), |
198 ConnectToSignal(kFreedesktopNotificationsName, "ActionInvoked", _, _)) | 220 ConnectToSignal(kFreedesktopNotificationsName, "ActionInvoked", _, _)) |
199 .WillOnce(RegisterSignalCallback(&action_invoked_callback_)); | 221 .WillOnce(RegisterSignalCallback(&action_invoked_callback_)); |
200 | 222 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 DISALLOW_COPY_AND_ASSIGN(NotificationPlatformBridgeLinuxTest); | 255 DISALLOW_COPY_AND_ASSIGN(NotificationPlatformBridgeLinuxTest); |
234 }; | 256 }; |
235 | 257 |
236 TEST_F(NotificationPlatformBridgeLinuxTest, SetUpAndTearDown) { | 258 TEST_F(NotificationPlatformBridgeLinuxTest, SetUpAndTearDown) { |
237 CreateNotificationBridgeLinux(); | 259 CreateNotificationBridgeLinux(); |
238 } | 260 } |
239 | 261 |
240 TEST_F(NotificationPlatformBridgeLinuxTest, NotifyAndCloseFormat) { | 262 TEST_F(NotificationPlatformBridgeLinuxTest, NotifyAndCloseFormat) { |
241 EXPECT_CALL(*mock_notification_proxy_.get(), | 263 EXPECT_CALL(*mock_notification_proxy_.get(), |
242 MockCallMethodAndBlock(Calls("Notify"), _)) | 264 MockCallMethodAndBlock(Calls("Notify"), _)) |
243 .WillOnce(OnNotify(1)); | 265 .WillOnce(OnNotify([](const NotificationRequest&) {}, 1)); |
244 EXPECT_CALL(*mock_notification_proxy_.get(), | 266 EXPECT_CALL(*mock_notification_proxy_.get(), |
245 MockCallMethodAndBlock(Calls("CloseNotification"), _)) | 267 MockCallMethodAndBlock(Calls("CloseNotification"), _)) |
246 .WillOnce(OnCloseNotification()); | 268 .WillOnce(OnCloseNotification()); |
247 | 269 |
248 CreateNotificationBridgeLinux(); | 270 CreateNotificationBridgeLinux(); |
249 notification_bridge_linux_->Display(NotificationCommon::PERSISTENT, "", "", | 271 notification_bridge_linux_->Display(NotificationCommon::PERSISTENT, "", "", |
250 false, | 272 false, |
251 NotificationBuilder("").GetResult()); | 273 NotificationBuilder("").GetResult()); |
252 notification_bridge_linux_->Close("", ""); | 274 notification_bridge_linux_->Close("", ""); |
253 } | 275 } |
254 | 276 |
255 ACTION_P2(VerifySummary, summary, id) { | |
256 NotificationRequest request = ParseRequest(arg0); | |
257 EXPECT_EQ(summary, request.summary); | |
258 return GetIdResponse(id); | |
259 } | |
260 | |
261 TEST_F(NotificationPlatformBridgeLinuxTest, ProgressPercentageAddedToSummary) { | 277 TEST_F(NotificationPlatformBridgeLinuxTest, ProgressPercentageAddedToSummary) { |
262 EXPECT_CALL(*mock_notification_proxy_.get(), | 278 EXPECT_CALL(*mock_notification_proxy_.get(), |
263 MockCallMethodAndBlock(Calls("Notify"), _)) | 279 MockCallMethodAndBlock(Calls("Notify"), _)) |
264 .WillOnce(VerifySummary( | 280 .WillOnce(OnNotify( |
265 base::UTF16ToUTF8(base::FormatPercent(42)) + " - The Title", 1)); | 281 [](const NotificationRequest& request) { |
| 282 EXPECT_EQ( |
| 283 base::UTF16ToUTF8(base::FormatPercent(42)) + " - The Title", |
| 284 request.summary); |
| 285 }, |
| 286 1)); |
266 | 287 |
267 CreateNotificationBridgeLinux(); | 288 CreateNotificationBridgeLinux(); |
268 notification_bridge_linux_->Display( | 289 notification_bridge_linux_->Display( |
269 NotificationCommon::PERSISTENT, "", "", false, | 290 NotificationCommon::PERSISTENT, "", "", false, |
270 NotificationBuilder("") | 291 NotificationBuilder("") |
271 .SetType(message_center::NOTIFICATION_TYPE_PROGRESS) | 292 .SetType(message_center::NOTIFICATION_TYPE_PROGRESS) |
272 .SetProgress(42) | 293 .SetProgress(42) |
273 .SetTitle(base::UTF8ToUTF16("The Title")) | 294 .SetTitle(base::UTF8ToUTF16("The Title")) |
274 .GetResult()); | 295 .GetResult()); |
275 } | 296 } |
| 297 |
| 298 TEST_F(NotificationPlatformBridgeLinuxTest, NotificationListItemsInBody) { |
| 299 EXPECT_CALL(*mock_notification_proxy_.get(), |
| 300 MockCallMethodAndBlock(Calls("Notify"), _)) |
| 301 .WillOnce(OnNotify( |
| 302 [](const NotificationRequest& request) { |
| 303 EXPECT_EQ("abc - 123\ndef - 456", request.body); |
| 304 }, |
| 305 1)); |
| 306 |
| 307 CreateNotificationBridgeLinux(); |
| 308 notification_bridge_linux_->Display( |
| 309 NotificationCommon::PERSISTENT, "", "", false, |
| 310 NotificationBuilder("") |
| 311 .SetType(message_center::NOTIFICATION_TYPE_MULTIPLE) |
| 312 .SetItems(std::vector<message_center::NotificationItem>{ |
| 313 {base::UTF8ToUTF16("abc"), base::UTF8ToUTF16("123")}, |
| 314 {base::UTF8ToUTF16("def"), base::UTF8ToUTF16("456")}}) |
| 315 .GetResult()); |
| 316 } |
| 317 |
| 318 TEST_F(NotificationPlatformBridgeLinuxTest, NotificationTimeouts) { |
| 319 const int32_t kExpireTimeoutDefault = -1; |
| 320 const int32_t kExpireTimeoutNever = 0; |
| 321 EXPECT_CALL(*mock_notification_proxy_.get(), |
| 322 MockCallMethodAndBlock(Calls("Notify"), _)) |
| 323 .WillOnce(OnNotify( |
| 324 [=](const NotificationRequest& request) { |
| 325 EXPECT_EQ(kExpireTimeoutDefault, request.expire_timeout); |
| 326 }, |
| 327 1)) |
| 328 .WillOnce(OnNotify( |
| 329 [=](const NotificationRequest& request) { |
| 330 EXPECT_EQ(kExpireTimeoutNever, request.expire_timeout); |
| 331 }, |
| 332 2)); |
| 333 |
| 334 CreateNotificationBridgeLinux(); |
| 335 notification_bridge_linux_->Display( |
| 336 NotificationCommon::PERSISTENT, "", "", false, |
| 337 NotificationBuilder("1").SetNeverTimeout(false).GetResult()); |
| 338 notification_bridge_linux_->Display( |
| 339 NotificationCommon::PERSISTENT, "", "", false, |
| 340 NotificationBuilder("2").SetNeverTimeout(true).GetResult()); |
| 341 } |
| 342 |
| 343 TEST_F(NotificationPlatformBridgeLinuxTest, NotificationAttribution) { |
| 344 EXPECT_CALL(*mock_notification_proxy_.get(), |
| 345 MockCallMethodAndBlock(Calls("Notify"), _)) |
| 346 .WillOnce(OnNotify( |
| 347 [](const NotificationRequest& request) { |
| 348 EXPECT_EQ("Body text\ngoogle.com", request.body); |
| 349 }, |
| 350 1)); |
| 351 |
| 352 CreateNotificationBridgeLinux(); |
| 353 notification_bridge_linux_->Display( |
| 354 NotificationCommon::PERSISTENT, "", "", false, |
| 355 NotificationBuilder("") |
| 356 .SetMessage(base::UTF8ToUTF16("Body text")) |
| 357 .SetOriginUrl(GURL("https://google.com/")) |
| 358 .GetResult()); |
| 359 } |
OLD | NEW |