| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/strings/string_number_conversions.h" | 5 #include "base/strings/string_number_conversions.h" |
| 6 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/extensions/api/notifications/notifications_api.h" | 10 #include "chrome/browser/extensions/api/notifications/notifications_api.h" |
| 11 #include "chrome/browser/extensions/extension_apitest.h" | 11 #include "chrome/browser/extensions/extension_apitest.h" |
| 12 #include "chrome/browser/extensions/extension_function_test_utils.h" | 12 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 13 #include "chrome/browser/notifications/notification.h" |
| 14 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 13 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
| 14 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
| 15 #include "extensions/browser/api/test/test_api.h" | 17 #include "extensions/browser/api/test/test_api.h" |
| 16 #include "extensions/common/features/feature.h" | 18 #include "extensions/common/features/feature.h" |
| 17 #include "ui/message_center/message_center.h" | 19 #include "ui/message_center/message_center.h" |
| 18 #include "ui/message_center/notification_list.h" | 20 #include "ui/message_center/notification_list.h" |
| 19 #include "ui/message_center/notifier_settings.h" | 21 #include "ui/message_center/notifier_settings.h" |
| 20 | 22 |
| 21 using extensions::Extension; | 23 using extensions::Extension; |
| 22 | 24 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 const extensions::Extension* extension = LoadExtension(extdir); | 88 const extensions::Extension* extension = LoadExtension(extdir); |
| 87 if (extension) { | 89 if (extension) { |
| 88 page_created.Wait(); | 90 page_created.Wait(); |
| 89 } | 91 } |
| 90 return extension; | 92 return extension; |
| 91 } | 93 } |
| 92 }; | 94 }; |
| 93 | 95 |
| 94 } // namespace | 96 } // namespace |
| 95 | 97 |
| 96 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestIdUsage) { | 98 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestBasicUsage) { |
| 97 // Create a new notification. A lingering output of this block is the | 99 ASSERT_TRUE(RunExtensionTest("notifications/api/basic_usage")) << message_; |
| 98 // notifications ID, which we'll use in later parts of this test. | |
| 99 std::string notification_id; | |
| 100 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | |
| 101 { | |
| 102 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 103 notification_function( | |
| 104 new extensions::NotificationsCreateFunction()); | |
| 105 | |
| 106 notification_function->set_extension(empty_extension.get()); | |
| 107 notification_function->set_has_callback(true); | |
| 108 | |
| 109 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 110 notification_function.get(), | |
| 111 "[\"\", " // Empty string: ask API to generate ID | |
| 112 "{" | |
| 113 "\"type\": \"basic\"," | |
| 114 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 115 "\"title\": \"Attention!\"," | |
| 116 "\"message\": \"Check out Cirque du Soleil\"" | |
| 117 "}]", | |
| 118 browser(), | |
| 119 utils::NONE)); | |
| 120 | |
| 121 ASSERT_EQ(base::Value::TYPE_STRING, result->GetType()); | |
| 122 ASSERT_TRUE(result->GetAsString(¬ification_id)); | |
| 123 ASSERT_TRUE(notification_id.length() > 0); | |
| 124 } | |
| 125 | |
| 126 // Update the existing notification. | |
| 127 { | |
| 128 scoped_refptr<extensions::NotificationsUpdateFunction> | |
| 129 notification_function( | |
| 130 new extensions::NotificationsUpdateFunction()); | |
| 131 | |
| 132 notification_function->set_extension(empty_extension.get()); | |
| 133 notification_function->set_has_callback(true); | |
| 134 | |
| 135 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 136 notification_function.get(), | |
| 137 "[\"" + notification_id + | |
| 138 "\", " | |
| 139 "{" | |
| 140 "\"type\": \"basic\"," | |
| 141 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 142 "\"title\": \"Attention!\"," | |
| 143 "\"message\": \"Too late! The show ended yesterday\"" | |
| 144 "}]", | |
| 145 browser(), | |
| 146 utils::NONE)); | |
| 147 | |
| 148 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
| 149 bool copy_bool_value = false; | |
| 150 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
| 151 ASSERT_TRUE(copy_bool_value); | |
| 152 | |
| 153 // TODO(miket): add a testing method to query the message from the | |
| 154 // displayed notification, and assert it matches the updated message. | |
| 155 // | |
| 156 // TODO(miket): add a method to count the number of outstanding | |
| 157 // notifications, and confirm it remains at one at this point. | |
| 158 } | |
| 159 | |
| 160 // Update a nonexistent notification. | |
| 161 { | |
| 162 scoped_refptr<extensions::NotificationsUpdateFunction> | |
| 163 notification_function( | |
| 164 new extensions::NotificationsUpdateFunction()); | |
| 165 | |
| 166 notification_function->set_extension(empty_extension.get()); | |
| 167 notification_function->set_has_callback(true); | |
| 168 | |
| 169 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 170 notification_function.get(), | |
| 171 "[\"xxxxxxxxxxxx\", " | |
| 172 "{" | |
| 173 "\"type\": \"basic\"," | |
| 174 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 175 "\"title\": \"!\"," | |
| 176 "\"message\": \"!\"" | |
| 177 "}]", | |
| 178 browser(), | |
| 179 utils::NONE)); | |
| 180 | |
| 181 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
| 182 bool copy_bool_value = false; | |
| 183 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
| 184 ASSERT_FALSE(copy_bool_value); | |
| 185 } | |
| 186 | |
| 187 // Clear a nonexistent notification. | |
| 188 { | |
| 189 scoped_refptr<extensions::NotificationsClearFunction> | |
| 190 notification_function( | |
| 191 new extensions::NotificationsClearFunction()); | |
| 192 | |
| 193 notification_function->set_extension(empty_extension.get()); | |
| 194 notification_function->set_has_callback(true); | |
| 195 | |
| 196 scoped_ptr<base::Value> result( | |
| 197 utils::RunFunctionAndReturnSingleResult(notification_function.get(), | |
| 198 "[\"xxxxxxxxxxx\"]", | |
| 199 browser(), | |
| 200 utils::NONE)); | |
| 201 | |
| 202 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
| 203 bool copy_bool_value = false; | |
| 204 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
| 205 ASSERT_FALSE(copy_bool_value); | |
| 206 } | |
| 207 | |
| 208 // Clear the notification we created. | |
| 209 { | |
| 210 scoped_refptr<extensions::NotificationsClearFunction> | |
| 211 notification_function( | |
| 212 new extensions::NotificationsClearFunction()); | |
| 213 | |
| 214 notification_function->set_extension(empty_extension.get()); | |
| 215 notification_function->set_has_callback(true); | |
| 216 | |
| 217 scoped_ptr<base::Value> result( | |
| 218 utils::RunFunctionAndReturnSingleResult(notification_function.get(), | |
| 219 "[\"" + notification_id + "\"]", | |
| 220 browser(), | |
| 221 utils::NONE)); | |
| 222 | |
| 223 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
| 224 bool copy_bool_value = false; | |
| 225 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
| 226 ASSERT_TRUE(copy_bool_value); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestBaseFormatNotification) { | |
| 231 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | |
| 232 | |
| 233 // Create a new notification with the minimum required properties. | |
| 234 { | |
| 235 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 236 notification_create_function( | |
| 237 new extensions::NotificationsCreateFunction()); | |
| 238 notification_create_function->set_extension(empty_extension.get()); | |
| 239 notification_create_function->set_has_callback(true); | |
| 240 | |
| 241 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 242 notification_create_function.get(), | |
| 243 "[\"\", " | |
| 244 "{" | |
| 245 "\"type\": \"basic\"," | |
| 246 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 247 "\"title\": \"Attention!\"," | |
| 248 "\"message\": \"Check out Cirque du Soleil\"" | |
| 249 "}]", | |
| 250 browser(), | |
| 251 utils::NONE)); | |
| 252 | |
| 253 std::string notification_id; | |
| 254 ASSERT_EQ(base::Value::TYPE_STRING, result->GetType()); | |
| 255 ASSERT_TRUE(result->GetAsString(¬ification_id)); | |
| 256 ASSERT_TRUE(notification_id.length() > 0); | |
| 257 } | |
| 258 | |
| 259 // Create another new notification with more than the required properties. | |
| 260 { | |
| 261 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 262 notification_create_function( | |
| 263 new extensions::NotificationsCreateFunction()); | |
| 264 notification_create_function->set_extension(empty_extension.get()); | |
| 265 notification_create_function->set_has_callback(true); | |
| 266 | |
| 267 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 268 notification_create_function.get(), | |
| 269 "[\"\", " | |
| 270 "{" | |
| 271 "\"type\": \"basic\"," | |
| 272 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 273 "\"title\": \"Attention!\"," | |
| 274 "\"message\": \"Check out Cirque du Soleil\"," | |
| 275 "\"priority\": 1," | |
| 276 "\"eventTime\": 1234567890.12345678," | |
| 277 "\"buttons\": [" | |
| 278 " {" | |
| 279 " \"title\": \"Up\"," | |
| 280 " \"iconUrl\":\"http://www.google.com/logos/2012/\"" | |
| 281 " }," | |
| 282 " {" | |
| 283 " \"title\": \"Down\"" // note: no iconUrl | |
| 284 " }" | |
| 285 "]," | |
| 286 "\"expandedMessage\": \"This is a longer expanded message.\"," | |
| 287 "\"imageUrl\": \"http://www.google.com/logos/2012/election12-hp.jpg\"" | |
| 288 "}]", | |
| 289 browser(), | |
| 290 utils::NONE)); | |
| 291 | |
| 292 std::string notification_id; | |
| 293 ASSERT_EQ(base::Value::TYPE_STRING, result->GetType()); | |
| 294 ASSERT_TRUE(result->GetAsString(¬ification_id)); | |
| 295 ASSERT_TRUE(notification_id.length() > 0); | |
| 296 } | |
| 297 | |
| 298 // Error case: missing type property. | |
| 299 { | |
| 300 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 301 notification_create_function( | |
| 302 new extensions::NotificationsCreateFunction()); | |
| 303 notification_create_function->set_extension(empty_extension.get()); | |
| 304 notification_create_function->set_has_callback(true); | |
| 305 | |
| 306 utils::RunFunction( | |
| 307 notification_create_function.get(), | |
| 308 "[\"\", " | |
| 309 "{" | |
| 310 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 311 "\"title\": \"Attention!\"," | |
| 312 "\"message\": \"Check out Cirque du Soleil\"" | |
| 313 "}]", | |
| 314 browser(), | |
| 315 utils::NONE); | |
| 316 | |
| 317 EXPECT_FALSE(notification_create_function->GetError().empty()); | |
| 318 } | |
| 319 | |
| 320 // Error case: missing iconUrl property. | |
| 321 { | |
| 322 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 323 notification_create_function( | |
| 324 new extensions::NotificationsCreateFunction()); | |
| 325 notification_create_function->set_extension(empty_extension.get()); | |
| 326 notification_create_function->set_has_callback(true); | |
| 327 | |
| 328 utils::RunFunction( | |
| 329 notification_create_function.get(), | |
| 330 "[\"\", " | |
| 331 "{" | |
| 332 "\"type\": \"basic\"," | |
| 333 "\"title\": \"Attention!\"," | |
| 334 "\"message\": \"Check out Cirque du Soleil\"" | |
| 335 "}]", | |
| 336 browser(), | |
| 337 utils::NONE); | |
| 338 | |
| 339 EXPECT_FALSE(notification_create_function->GetError().empty()); | |
| 340 } | |
| 341 | |
| 342 // Error case: missing title property. | |
| 343 { | |
| 344 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 345 notification_create_function( | |
| 346 new extensions::NotificationsCreateFunction()); | |
| 347 notification_create_function->set_extension(empty_extension.get()); | |
| 348 notification_create_function->set_has_callback(true); | |
| 349 | |
| 350 utils::RunFunction( | |
| 351 notification_create_function.get(), | |
| 352 "[\"\", " | |
| 353 "{" | |
| 354 "\"type\": \"basic\"," | |
| 355 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 356 "\"message\": \"Check out Cirque du Soleil\"" | |
| 357 "}]", | |
| 358 browser(), | |
| 359 utils::NONE); | |
| 360 | |
| 361 EXPECT_FALSE(notification_create_function->GetError().empty()); | |
| 362 } | |
| 363 | |
| 364 // Error case: missing message property. | |
| 365 { | |
| 366 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 367 notification_create_function( | |
| 368 new extensions::NotificationsCreateFunction()); | |
| 369 notification_create_function->set_extension(empty_extension.get()); | |
| 370 notification_create_function->set_has_callback(true); | |
| 371 | |
| 372 utils::RunFunction( | |
| 373 notification_create_function.get(), | |
| 374 "[\"\", " | |
| 375 "{" | |
| 376 "\"type\": \"basic\"," | |
| 377 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 378 "\"title\": \"Attention!\"" | |
| 379 "}]", | |
| 380 browser(), | |
| 381 utils::NONE); | |
| 382 | |
| 383 EXPECT_FALSE(notification_create_function->GetError().empty()); | |
| 384 } | |
| 385 } | |
| 386 | |
| 387 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestMultipleItemNotification) { | |
| 388 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 389 notification_create_function( | |
| 390 new extensions::NotificationsCreateFunction()); | |
| 391 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | |
| 392 | |
| 393 notification_create_function->set_extension(empty_extension.get()); | |
| 394 notification_create_function->set_has_callback(true); | |
| 395 | |
| 396 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 397 notification_create_function.get(), | |
| 398 "[\"\", " | |
| 399 "{" | |
| 400 "\"type\": \"list\"," | |
| 401 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 402 "\"title\": \"Multiple Item Notification Title\"," | |
| 403 "\"message\": \"Multiple item notification message.\"," | |
| 404 "\"items\": [" | |
| 405 " {\"title\": \"Brett Boe\"," | |
| 406 " \"message\": \"This is an important message!\"}," | |
| 407 " {\"title\": \"Carla Coe\"," | |
| 408 " \"message\": \"Just took a look at the proposal\"}," | |
| 409 " {\"title\": \"Donna Doe\"," | |
| 410 " \"message\": \"I see that you went to the conference\"}," | |
| 411 " {\"title\": \"Frank Foe\"," | |
| 412 " \"message\": \"I ate Harry's sandwich!\"}," | |
| 413 " {\"title\": \"Grace Goe\"," | |
| 414 " \"message\": \"I saw Frank steal a sandwich :-)\"}" | |
| 415 "]," | |
| 416 "\"priority\": 1," | |
| 417 "\"eventTime\": 1361488019.9999999" | |
| 418 "}]", | |
| 419 browser(), | |
| 420 utils::NONE)); | |
| 421 // TODO(dharcourt): [...], items = [{title: foo, message: bar}, ...], [...] | |
| 422 | |
| 423 std::string notification_id; | |
| 424 ASSERT_EQ(base::Value::TYPE_STRING, result->GetType()); | |
| 425 ASSERT_TRUE(result->GetAsString(¬ification_id)); | |
| 426 ASSERT_TRUE(notification_id.length() > 0); | |
| 427 } | |
| 428 | |
| 429 #if defined(OS_LINUX) | |
| 430 #define MAYBE_TestGetAll DISABLED_TestGetAll | |
| 431 #else | |
| 432 #define MAYBE_TestGetAll TestGetAll | |
| 433 #endif | |
| 434 | |
| 435 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, MAYBE_TestGetAll) { | |
| 436 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | |
| 437 | |
| 438 { | |
| 439 scoped_refptr<extensions::NotificationsGetAllFunction> | |
| 440 notification_get_all_function( | |
| 441 new extensions::NotificationsGetAllFunction()); | |
| 442 notification_get_all_function->set_extension(empty_extension.get()); | |
| 443 notification_get_all_function->set_has_callback(true); | |
| 444 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 445 notification_get_all_function.get(), "[]", browser(), utils::NONE)); | |
| 446 | |
| 447 base::DictionaryValue* return_value; | |
| 448 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); | |
| 449 ASSERT_TRUE(result->GetAsDictionary(&return_value)); | |
| 450 ASSERT_TRUE(return_value->size() == 0); | |
| 451 } | |
| 452 | |
| 453 const unsigned int kNotificationsToCreate = 4; | |
| 454 | |
| 455 for (unsigned int i = 0; i < kNotificationsToCreate; i++) { | |
| 456 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 457 notification_create_function( | |
| 458 new extensions::NotificationsCreateFunction()); | |
| 459 | |
| 460 notification_create_function->set_extension(empty_extension.get()); | |
| 461 notification_create_function->set_has_callback(true); | |
| 462 | |
| 463 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 464 notification_create_function.get(), | |
| 465 base::StringPrintf("[\"identifier-%u\", " | |
| 466 "{" | |
| 467 "\"type\": \"basic\"," | |
| 468 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 469 "\"title\": \"Title\"," | |
| 470 "\"message\": \"Message.\"," | |
| 471 "\"priority\": 1," | |
| 472 "\"eventTime\": 1361488019.9999999" | |
| 473 "}]", | |
| 474 i), | |
| 475 browser(), | |
| 476 utils::NONE)); | |
| 477 } | |
| 478 | |
| 479 { | |
| 480 scoped_refptr<extensions::NotificationsGetAllFunction> | |
| 481 notification_get_all_function( | |
| 482 new extensions::NotificationsGetAllFunction()); | |
| 483 notification_get_all_function->set_extension(empty_extension.get()); | |
| 484 notification_get_all_function->set_has_callback(true); | |
| 485 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 486 notification_get_all_function.get(), "[]", browser(), utils::NONE)); | |
| 487 | |
| 488 base::DictionaryValue* return_value; | |
| 489 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); | |
| 490 ASSERT_TRUE(result->GetAsDictionary(&return_value)); | |
| 491 ASSERT_EQ(return_value->size(), kNotificationsToCreate); | |
| 492 bool dictionary_bool = false; | |
| 493 for (unsigned int i = 0; i < kNotificationsToCreate; i++) { | |
| 494 std::string id = base::StringPrintf("identifier-%u", i); | |
| 495 ASSERT_TRUE(return_value->GetBoolean(id, &dictionary_bool)); | |
| 496 ASSERT_TRUE(dictionary_bool); | |
| 497 } | |
| 498 } | |
| 499 } | 100 } |
| 500 | 101 |
| 501 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestEvents) { | 102 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestEvents) { |
| 502 ASSERT_TRUE(RunExtensionTest("notifications/api/events")) << message_; | 103 ASSERT_TRUE(RunExtensionTest("notifications/api/events")) << message_; |
| 503 } | 104 } |
| 504 | 105 |
| 505 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestCSP) { | 106 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestCSP) { |
| 506 ASSERT_TRUE(RunExtensionTest("notifications/api/csp")) << message_; | 107 ASSERT_TRUE(RunExtensionTest("notifications/api/csp")) << message_; |
| 507 } | 108 } |
| 508 | 109 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 519 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 120 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 520 } | 121 } |
| 521 | 122 |
| 522 { | 123 { |
| 523 ResultCatcher catcher; | 124 ResultCatcher catcher; |
| 524 g_browser_process->message_center()->RemoveNotification( | 125 g_browser_process->message_center()->RemoveNotification( |
| 525 extension->id() + "-BAR", | 126 extension->id() + "-BAR", |
| 526 true); | 127 true); |
| 527 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 128 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 528 } | 129 } |
| 130 |
| 131 { |
| 132 ResultCatcher catcher; |
| 133 g_browser_process->message_center()->RemoveAllNotifications(false); |
| 134 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 135 } |
| 136 { |
| 137 ResultCatcher catcher; |
| 138 g_browser_process->message_center()->RemoveAllNotifications(true); |
| 139 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 140 } |
| 529 } | 141 } |
| 530 | 142 |
| 143 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestPartialUpdate) { |
| 144 ASSERT_TRUE(RunExtensionTest("notifications/api/partial_update")) << message_; |
| 145 const extensions::Extension* extension = GetSingleLoadedExtension(); |
| 146 ASSERT_TRUE(extension) << message_; |
| 531 | 147 |
| 532 #if defined(OS_LINUX) | |
| 533 #define MAYBE_TestProgressNotification DISABLED_TestProgressNotification | |
| 534 #else | |
| 535 #define MAYBE_TestProgressNotification TestProgressNotification | |
| 536 #endif | |
| 537 | |
| 538 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, MAYBE_TestProgressNotification) { | |
| 539 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | |
| 540 | |
| 541 // Create a new progress notification. | |
| 542 std::string notification_id; | |
| 543 { | |
| 544 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 545 notification_create_function( | |
| 546 new extensions::NotificationsCreateFunction()); | |
| 547 notification_create_function->set_extension(empty_extension.get()); | |
| 548 notification_create_function->set_has_callback(true); | |
| 549 | |
| 550 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 551 notification_create_function.get(), | |
| 552 "[\"\", " | |
| 553 "{" | |
| 554 "\"type\": \"progress\"," | |
| 555 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 556 "\"title\": \"Test!\"," | |
| 557 "\"message\": \"This is a progress notification.\"," | |
| 558 "\"priority\": 1," | |
| 559 "\"eventTime\": 1234567890.12345678," | |
| 560 "\"progress\": 30" | |
| 561 "}]", | |
| 562 browser(), | |
| 563 utils::NONE)); | |
| 564 | |
| 565 EXPECT_EQ(base::Value::TYPE_STRING, result->GetType()); | |
| 566 EXPECT_TRUE(result->GetAsString(¬ification_id)); | |
| 567 EXPECT_TRUE(notification_id.length() > 0); | |
| 568 } | |
| 569 | |
| 570 // Update the progress property only. | |
| 571 { | |
| 572 scoped_refptr<extensions::NotificationsUpdateFunction> | |
| 573 notification_function( | |
| 574 new extensions::NotificationsUpdateFunction()); | |
| 575 notification_function->set_extension(empty_extension.get()); | |
| 576 notification_function->set_has_callback(true); | |
| 577 | |
| 578 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 579 notification_function.get(), | |
| 580 "[\"" + notification_id + | |
| 581 "\", " | |
| 582 "{" | |
| 583 "\"progress\": 60" | |
| 584 "}]", | |
| 585 browser(), | |
| 586 utils::NONE)); | |
| 587 | |
| 588 EXPECT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
| 589 bool copy_bool_value = false; | |
| 590 EXPECT_TRUE(result->GetAsBoolean(©_bool_value)); | |
| 591 EXPECT_TRUE(copy_bool_value); | |
| 592 } | |
| 593 | |
| 594 // Error case: progress value provided for non-progress type. | |
| 595 { | |
| 596 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 597 notification_create_function( | |
| 598 new extensions::NotificationsCreateFunction()); | |
| 599 notification_create_function->set_extension(empty_extension.get()); | |
| 600 notification_create_function->set_has_callback(true); | |
| 601 | |
| 602 utils::RunFunction( | |
| 603 notification_create_function.get(), | |
| 604 "[\"\", " | |
| 605 "{" | |
| 606 "\"type\": \"basic\"," | |
| 607 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 608 "\"title\": \"Test!\"," | |
| 609 "\"message\": \"This is a progress notification.\"," | |
| 610 "\"priority\": 1," | |
| 611 "\"eventTime\": 1234567890.12345678," | |
| 612 "\"progress\": 10" | |
| 613 "}]", | |
| 614 browser(), | |
| 615 utils::NONE); | |
| 616 EXPECT_FALSE(notification_create_function->GetError().empty()); | |
| 617 } | |
| 618 | |
| 619 // Error case: progress value less than lower bound. | |
| 620 { | |
| 621 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 622 notification_create_function( | |
| 623 new extensions::NotificationsCreateFunction()); | |
| 624 notification_create_function->set_extension(empty_extension.get()); | |
| 625 notification_create_function->set_has_callback(true); | |
| 626 | |
| 627 utils::RunFunction( | |
| 628 notification_create_function.get(), | |
| 629 "[\"\", " | |
| 630 "{" | |
| 631 "\"type\": \"progress\"," | |
| 632 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 633 "\"title\": \"Test!\"," | |
| 634 "\"message\": \"This is a progress notification.\"," | |
| 635 "\"priority\": 1," | |
| 636 "\"eventTime\": 1234567890.12345678," | |
| 637 "\"progress\": -10" | |
| 638 "}]", | |
| 639 browser(), | |
| 640 utils::NONE); | |
| 641 EXPECT_FALSE(notification_create_function->GetError().empty()); | |
| 642 } | |
| 643 | |
| 644 // Error case: progress value greater than upper bound. | |
| 645 { | |
| 646 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 647 notification_create_function( | |
| 648 new extensions::NotificationsCreateFunction()); | |
| 649 notification_create_function->set_extension(empty_extension.get()); | |
| 650 notification_create_function->set_has_callback(true); | |
| 651 | |
| 652 utils::RunFunction( | |
| 653 notification_create_function.get(), | |
| 654 "[\"\", " | |
| 655 "{" | |
| 656 "\"type\": \"progress\"," | |
| 657 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 658 "\"title\": \"Test!\"," | |
| 659 "\"message\": \"This is a progress notification.\"," | |
| 660 "\"priority\": 1," | |
| 661 "\"eventTime\": 1234567890.12345678," | |
| 662 "\"progress\": 101" | |
| 663 "}]", | |
| 664 browser(), | |
| 665 utils::NONE); | |
| 666 EXPECT_FALSE(notification_create_function->GetError().empty()); | |
| 667 } | |
| 668 } | |
| 669 | |
| 670 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestPartialUpdate) { | |
| 671 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | |
| 672 | |
| 673 // Create a new notification. | |
| 674 std::string notification_id; | |
| 675 { | |
| 676 scoped_refptr<extensions::NotificationsCreateFunction> | |
| 677 notification_function( | |
| 678 new extensions::NotificationsCreateFunction()); | |
| 679 notification_function->set_extension(empty_extension.get()); | |
| 680 notification_function->set_has_callback(true); | |
| 681 | |
| 682 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 683 notification_function.get(), | |
| 684 "[\"\", " // Empty string: ask API to generate ID | |
| 685 "{" | |
| 686 "\"type\": \"basic\"," | |
| 687 "\"iconUrl\": \"an/image/that/does/not/exist.png\"," | |
| 688 "\"title\": \"Attention!\"," | |
| 689 "\"message\": \"Check out Cirque du Soleil\"," | |
| 690 "\"buttons\": [{\"title\": \"Button\"}]" | |
| 691 "}]", | |
| 692 browser(), | |
| 693 utils::NONE)); | |
| 694 | |
| 695 ASSERT_EQ(base::Value::TYPE_STRING, result->GetType()); | |
| 696 ASSERT_TRUE(result->GetAsString(¬ification_id)); | |
| 697 ASSERT_TRUE(notification_id.length() > 0); | |
| 698 } | |
| 699 | |
| 700 // Update a few properties in the existing notification. | |
| 701 const char kNewTitle[] = "Changed!"; | 148 const char kNewTitle[] = "Changed!"; |
| 702 const char kNewMessage[] = "Too late! The show ended yesterday"; | 149 const char kNewMessage[] = "Too late! The show ended yesterday"; |
| 703 { | 150 int kNewPriority = 2; |
| 704 scoped_refptr<extensions::NotificationsUpdateFunction> | |
| 705 notification_function( | |
| 706 new extensions::NotificationsUpdateFunction()); | |
| 707 notification_function->set_extension(empty_extension.get()); | |
| 708 notification_function->set_has_callback(true); | |
| 709 | 151 |
| 710 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 711 notification_function.get(), | |
| 712 "[\"" + notification_id + | |
| 713 "\", " | |
| 714 "{" | |
| 715 "\"title\": \"" + kNewTitle + "\"," | |
| 716 "\"message\": \"" + kNewMessage + "\"" | |
| 717 "}]", | |
| 718 browser(), | |
| 719 utils::NONE)); | |
| 720 | |
| 721 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
| 722 bool copy_bool_value = false; | |
| 723 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
| 724 ASSERT_TRUE(copy_bool_value); | |
| 725 } | |
| 726 | |
| 727 // Update some other properties in the existing notification. | |
| 728 int kNewPriority = 2; | |
| 729 { | |
| 730 scoped_refptr<extensions::NotificationsUpdateFunction> | |
| 731 notification_function( | |
| 732 new extensions::NotificationsUpdateFunction()); | |
| 733 notification_function->set_extension(empty_extension.get()); | |
| 734 notification_function->set_has_callback(true); | |
| 735 | |
| 736 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult( | |
| 737 notification_function.get(), | |
| 738 "[\"" + notification_id + | |
| 739 "\", " | |
| 740 "{" | |
| 741 "\"priority\": " + base::IntToString(kNewPriority) + "," | |
| 742 "\"buttons\": []" | |
| 743 "}]", | |
| 744 browser(), | |
| 745 utils::NONE)); | |
| 746 | |
| 747 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | |
| 748 bool copy_bool_value = false; | |
| 749 ASSERT_TRUE(result->GetAsBoolean(©_bool_value)); | |
| 750 ASSERT_TRUE(copy_bool_value); | |
| 751 } | |
| 752 | |
| 753 // Get the updated notification and verify its data. | |
| 754 const message_center::NotificationList::Notifications& notifications = | 152 const message_center::NotificationList::Notifications& notifications = |
| 755 g_browser_process->message_center()->GetVisibleNotifications(); | 153 g_browser_process->message_center()->GetVisibleNotifications(); |
| 756 ASSERT_EQ(1u, notifications.size()); | 154 ASSERT_EQ(1u, notifications.size()); |
| 757 message_center::Notification* notification = *(notifications.begin()); | 155 message_center::Notification* notification = *(notifications.begin()); |
| 156 LOG(INFO) << "Notification ID: " << notification->id(); |
| 157 |
| 758 EXPECT_EQ(base::ASCIIToUTF16(kNewTitle), notification->title()); | 158 EXPECT_EQ(base::ASCIIToUTF16(kNewTitle), notification->title()); |
| 759 EXPECT_EQ(base::ASCIIToUTF16(kNewMessage), notification->message()); | 159 EXPECT_EQ(base::ASCIIToUTF16(kNewMessage), notification->message()); |
| 760 EXPECT_EQ(kNewPriority, notification->priority()); | 160 EXPECT_EQ(kNewPriority, notification->priority()); |
| 761 EXPECT_EQ(0u, notification->buttons().size()); | 161 EXPECT_EQ(0u, notification->buttons().size()); |
| 762 } | 162 } |
| 763 | 163 |
| 764 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestGetPermissionLevel) { | 164 IN_PROC_BROWSER_TEST_F(NotificationsApiTest, TestGetPermissionLevel) { |
| 765 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); | 165 scoped_refptr<Extension> empty_extension(utils::CreateEmptyExtension()); |
| 766 | 166 |
| 767 // Get permission level for the extension whose notifications are enabled. | 167 // Get permission level for the extension whose notifications are enabled. |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 notification->ButtonClick(0); | 263 notification->ButtonClick(0); |
| 864 EXPECT_TRUE(catcher.GetNextResult()); | 264 EXPECT_TRUE(catcher.GetNextResult()); |
| 865 notification->Click(); | 265 notification->Click(); |
| 866 EXPECT_TRUE(catcher.GetNextResult()); | 266 EXPECT_TRUE(catcher.GetNextResult()); |
| 867 notification->Close(true); | 267 notification->Close(true); |
| 868 EXPECT_TRUE(catcher.GetNextResult()); | 268 EXPECT_TRUE(catcher.GetNextResult()); |
| 869 notification->Close(false); | 269 notification->Close(false); |
| 870 EXPECT_FALSE(catcher.GetNextResult()); | 270 EXPECT_FALSE(catcher.GetNextResult()); |
| 871 } | 271 } |
| 872 } | 272 } |
| OLD | NEW |