| 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 "ui/message_center/notification_list.h" | 5 #include "ui/message_center/notification_list.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/i18n/time_formatting.h" | 8 #include "base/i18n/time_formatting.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 counter_ = 0; | 26 counter_ = 0; |
| 27 } | 27 } |
| 28 | 28 |
| 29 protected: | 29 protected: |
| 30 // Currently NotificationListTest doesn't care about some fields like title or | 30 // Currently NotificationListTest doesn't care about some fields like title or |
| 31 // message, so put a simple template on it. Returns the id of the new | 31 // message, so put a simple template on it. Returns the id of the new |
| 32 // notification. | 32 // notification. |
| 33 std::string AddNotification(const base::DictionaryValue* optional_fields) { | 33 std::string AddNotification(const base::DictionaryValue* optional_fields) { |
| 34 std::string new_id = base::StringPrintf(kIdFormat, counter_); | 34 std::string new_id = base::StringPrintf(kIdFormat, counter_); |
| 35 notification_list_->AddNotification( | 35 notification_list_->AddNotification( |
| 36 message_center::NOTIFICATION_TYPE_SIMPLE, new_id, | 36 message_center::NOTIFICATION_TYPE_SIMPLE, |
| 37 new_id, |
| 37 UTF8ToUTF16(base::StringPrintf(kTitleFormat, counter_)), | 38 UTF8ToUTF16(base::StringPrintf(kTitleFormat, counter_)), |
| 38 UTF8ToUTF16(base::StringPrintf(kMessageFormat, counter_)), | 39 UTF8ToUTF16(base::StringPrintf(kMessageFormat, counter_)), |
| 39 UTF8ToUTF16(kDisplaySource), kExtensionId, | 40 UTF8ToUTF16(kDisplaySource), |
| 40 optional_fields); | 41 kExtensionId, |
| 42 optional_fields, |
| 43 NULL); |
| 41 counter_++; | 44 counter_++; |
| 42 return new_id; | 45 return new_id; |
| 43 } | 46 } |
| 44 | 47 |
| 45 // Utility methods of AddNotification. | 48 // Utility methods of AddNotification. |
| 46 std::string AddPriorityNotification(NotificationPriority priority) { | 49 std::string AddPriorityNotification(NotificationPriority priority) { |
| 47 base::DictionaryValue optional; | 50 base::DictionaryValue optional; |
| 48 optional.SetInteger(message_center::kPriorityKey, | 51 optional.SetInteger(message_center::kPriorityKey, |
| 49 static_cast<int>(priority)); | 52 static_cast<int>(priority)); |
| 50 return AddNotification(&optional); | 53 return AddNotification(&optional); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 notification_list()->MarkSinglePopupAsDisplayed(id0); | 157 notification_list()->MarkSinglePopupAsDisplayed(id0); |
| 155 EXPECT_EQ(1u, notification_list()->unread_count()); | 158 EXPECT_EQ(1u, notification_list()->unread_count()); |
| 156 notification_list()->MarkSinglePopupAsDisplayed(id1); | 159 notification_list()->MarkSinglePopupAsDisplayed(id1); |
| 157 EXPECT_EQ(0u, notification_list()->unread_count()); | 160 EXPECT_EQ(0u, notification_list()->unread_count()); |
| 158 } | 161 } |
| 159 | 162 |
| 160 TEST_F(NotificationListTest, UpdateNotification) { | 163 TEST_F(NotificationListTest, UpdateNotification) { |
| 161 std::string id0 = AddNotification(NULL); | 164 std::string id0 = AddNotification(NULL); |
| 162 std::string replaced = id0 + "_replaced"; | 165 std::string replaced = id0 + "_replaced"; |
| 163 EXPECT_EQ(1u, notification_list()->NotificationCount()); | 166 EXPECT_EQ(1u, notification_list()->NotificationCount()); |
| 164 notification_list()->UpdateNotificationMessage( | 167 notification_list()->UpdateNotificationMessage(id0, |
| 165 id0, replaced, UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), NULL); | 168 replaced, |
| 169 UTF8ToUTF16("newtitle"), |
| 170 UTF8ToUTF16("newbody"), |
| 171 NULL, |
| 172 NULL); |
| 166 EXPECT_EQ(1u, notification_list()->NotificationCount()); | 173 EXPECT_EQ(1u, notification_list()->NotificationCount()); |
| 167 const NotificationList::Notifications& notifications = | 174 const NotificationList::Notifications& notifications = |
| 168 notification_list()->GetNotifications(); | 175 notification_list()->GetNotifications(); |
| 169 EXPECT_EQ(replaced, (*notifications.begin())->id()); | 176 EXPECT_EQ(replaced, (*notifications.begin())->id()); |
| 170 EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications.begin())->title()); | 177 EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications.begin())->title()); |
| 171 EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications.begin())->message()); | 178 EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications.begin())->message()); |
| 172 } | 179 } |
| 173 | 180 |
| 174 TEST_F(NotificationListTest, GetNotificationsBySourceOrExtensions) { | 181 TEST_F(NotificationListTest, GetNotificationsBySourceOrExtensions) { |
| 175 notification_list()->AddNotification( | 182 notification_list()->AddNotification(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 176 message_center::NOTIFICATION_TYPE_SIMPLE, "id0", UTF8ToUTF16("title0"), | 183 "id0", |
| 177 UTF8ToUTF16("message0"), UTF8ToUTF16("source0"), "ext0", NULL); | 184 UTF8ToUTF16("title0"), |
| 178 notification_list()->AddNotification( | 185 UTF8ToUTF16("message0"), |
| 179 message_center::NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title1"), | 186 UTF8ToUTF16("source0"), |
| 180 UTF8ToUTF16("message1"), UTF8ToUTF16("source0"), "ext0", NULL); | 187 "ext0", |
| 181 notification_list()->AddNotification( | 188 NULL, |
| 182 message_center::NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title1"), | 189 NULL); |
| 183 UTF8ToUTF16("message1"), UTF8ToUTF16("source1"), "ext0", NULL); | 190 notification_list()->AddNotification(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 184 notification_list()->AddNotification( | 191 "id1", |
| 185 message_center::NOTIFICATION_TYPE_SIMPLE, "id3", UTF8ToUTF16("title1"), | 192 UTF8ToUTF16("title1"), |
| 186 UTF8ToUTF16("message1"), UTF8ToUTF16("source2"), "ext1", NULL); | 193 UTF8ToUTF16("message1"), |
| 194 UTF8ToUTF16("source0"), |
| 195 "ext0", |
| 196 NULL, |
| 197 NULL); |
| 198 notification_list()->AddNotification(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 199 "id2", |
| 200 UTF8ToUTF16("title1"), |
| 201 UTF8ToUTF16("message1"), |
| 202 UTF8ToUTF16("source1"), |
| 203 "ext0", |
| 204 NULL, |
| 205 NULL); |
| 206 notification_list()->AddNotification(message_center::NOTIFICATION_TYPE_SIMPLE, |
| 207 "id3", |
| 208 UTF8ToUTF16("title1"), |
| 209 UTF8ToUTF16("message1"), |
| 210 UTF8ToUTF16("source2"), |
| 211 "ext1", |
| 212 NULL, |
| 213 NULL); |
| 187 | 214 |
| 188 NotificationList::Notifications by_source = | 215 NotificationList::Notifications by_source = |
| 189 notification_list()->GetNotificationsBySource("id0"); | 216 notification_list()->GetNotificationsBySource("id0"); |
| 190 EXPECT_TRUE(IsInNotifications(by_source, "id0")); | 217 EXPECT_TRUE(IsInNotifications(by_source, "id0")); |
| 191 EXPECT_TRUE(IsInNotifications(by_source, "id1")); | 218 EXPECT_TRUE(IsInNotifications(by_source, "id1")); |
| 192 EXPECT_FALSE(IsInNotifications(by_source, "id2")); | 219 EXPECT_FALSE(IsInNotifications(by_source, "id2")); |
| 193 EXPECT_FALSE(IsInNotifications(by_source, "id3")); | 220 EXPECT_FALSE(IsInNotifications(by_source, "id3")); |
| 194 | 221 |
| 195 NotificationList::Notifications by_extension = | 222 NotificationList::Notifications by_extension = |
| 196 notification_list()->GetNotificationsByExtension("id0"); | 223 notification_list()->GetNotificationsByExtension("id0"); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 EXPECT_EQ(1u, GetPopupCounts()); | 301 EXPECT_EQ(1u, GetPopupCounts()); |
| 275 } | 302 } |
| 276 | 303 |
| 277 TEST_F(NotificationListTest, PriorityPromotion) { | 304 TEST_F(NotificationListTest, PriorityPromotion) { |
| 278 std::string id0 = AddPriorityNotification(LOW_PRIORITY); | 305 std::string id0 = AddPriorityNotification(LOW_PRIORITY); |
| 279 std::string replaced = id0 + "_replaced"; | 306 std::string replaced = id0 + "_replaced"; |
| 280 EXPECT_EQ(1u, notification_list()->NotificationCount()); | 307 EXPECT_EQ(1u, notification_list()->NotificationCount()); |
| 281 EXPECT_EQ(0u, GetPopupCounts()); | 308 EXPECT_EQ(0u, GetPopupCounts()); |
| 282 base::DictionaryValue optional; | 309 base::DictionaryValue optional; |
| 283 optional.SetInteger(message_center::kPriorityKey, 1); | 310 optional.SetInteger(message_center::kPriorityKey, 1); |
| 284 notification_list()->UpdateNotificationMessage( | 311 notification_list()->UpdateNotificationMessage(id0, |
| 285 id0, replaced, UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), | 312 replaced, |
| 286 &optional); | 313 UTF8ToUTF16("newtitle"), |
| 314 UTF8ToUTF16("newbody"), |
| 315 &optional, |
| 316 NULL); |
| 287 EXPECT_EQ(1u, notification_list()->NotificationCount()); | 317 EXPECT_EQ(1u, notification_list()->NotificationCount()); |
| 288 EXPECT_EQ(1u, GetPopupCounts()); | 318 EXPECT_EQ(1u, GetPopupCounts()); |
| 289 const NotificationList::Notifications& notifications = | 319 const NotificationList::Notifications& notifications = |
| 290 notification_list()->GetNotifications(); | 320 notification_list()->GetNotifications(); |
| 291 EXPECT_EQ(replaced, (*notifications.begin())->id()); | 321 EXPECT_EQ(replaced, (*notifications.begin())->id()); |
| 292 EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications.begin())->title()); | 322 EXPECT_EQ(UTF8ToUTF16("newtitle"), (*notifications.begin())->title()); |
| 293 EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications.begin())->message()); | 323 EXPECT_EQ(UTF8ToUTF16("newbody"), (*notifications.begin())->message()); |
| 294 EXPECT_EQ(1, (*notifications.begin())->priority()); | 324 EXPECT_EQ(1, (*notifications.begin())->priority()); |
| 295 } | 325 } |
| 296 | 326 |
| 297 TEST_F(NotificationListTest, PriorityPromotionWithPopups) { | 327 TEST_F(NotificationListTest, PriorityPromotionWithPopups) { |
| 298 std::string id0 = AddPriorityNotification(LOW_PRIORITY); | 328 std::string id0 = AddPriorityNotification(LOW_PRIORITY); |
| 299 std::string id1 = AddPriorityNotification(DEFAULT_PRIORITY); | 329 std::string id1 = AddPriorityNotification(DEFAULT_PRIORITY); |
| 300 EXPECT_EQ(1u, GetPopupCounts()); | 330 EXPECT_EQ(1u, GetPopupCounts()); |
| 301 notification_list()->MarkSinglePopupAsShown(id1, true); | 331 notification_list()->MarkSinglePopupAsShown(id1, true); |
| 302 EXPECT_EQ(0u, GetPopupCounts()); | 332 EXPECT_EQ(0u, GetPopupCounts()); |
| 303 | 333 |
| 304 // id0 promoted to LOW->DEFAULT, it'll appear as toast (popup). | 334 // id0 promoted to LOW->DEFAULT, it'll appear as toast (popup). |
| 305 base::DictionaryValue priority_default; | 335 base::DictionaryValue priority_default; |
| 306 priority_default.SetInteger(message_center::kPriorityKey, | 336 priority_default.SetInteger(message_center::kPriorityKey, |
| 307 static_cast<int>(DEFAULT_PRIORITY)); | 337 static_cast<int>(DEFAULT_PRIORITY)); |
| 308 notification_list()->UpdateNotificationMessage( | 338 notification_list()->UpdateNotificationMessage(id0, |
| 309 id0, id0, UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), | 339 id0, |
| 310 &priority_default); | 340 UTF8ToUTF16("newtitle"), |
| 341 UTF8ToUTF16("newbody"), |
| 342 &priority_default, |
| 343 NULL); |
| 311 EXPECT_EQ(1u, GetPopupCounts()); | 344 EXPECT_EQ(1u, GetPopupCounts()); |
| 312 notification_list()->MarkSinglePopupAsShown(id0, true); | 345 notification_list()->MarkSinglePopupAsShown(id0, true); |
| 313 EXPECT_EQ(0u, GetPopupCounts()); | 346 EXPECT_EQ(0u, GetPopupCounts()); |
| 314 | 347 |
| 315 // update with no promotion change for id0, it won't appear as a toast. | 348 // update with no promotion change for id0, it won't appear as a toast. |
| 316 notification_list()->UpdateNotificationMessage( | 349 notification_list()->UpdateNotificationMessage( |
| 317 id0, id0, UTF8ToUTF16("newtitle2"), UTF8ToUTF16("newbody2"), | 350 id0, id0, UTF8ToUTF16("newtitle2"), UTF8ToUTF16("newbody2"), NULL, NULL); |
| 318 NULL); | |
| 319 EXPECT_EQ(0u, GetPopupCounts()); | 351 EXPECT_EQ(0u, GetPopupCounts()); |
| 320 | 352 |
| 321 // id1 promoted to DEFAULT->HIGH, it'll appear as toast (popup). | 353 // id1 promoted to DEFAULT->HIGH, it'll appear as toast (popup). |
| 322 base::DictionaryValue priority_high; | 354 base::DictionaryValue priority_high; |
| 323 priority_high.SetInteger(message_center::kPriorityKey, | 355 priority_high.SetInteger(message_center::kPriorityKey, |
| 324 static_cast<int>(HIGH_PRIORITY)); | 356 static_cast<int>(HIGH_PRIORITY)); |
| 325 notification_list()->UpdateNotificationMessage( | 357 notification_list()->UpdateNotificationMessage(id1, |
| 326 id1, id1, UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), | 358 id1, |
| 327 &priority_high); | 359 UTF8ToUTF16("newtitle"), |
| 360 UTF8ToUTF16("newbody"), |
| 361 &priority_high, |
| 362 NULL); |
| 328 EXPECT_EQ(1u, GetPopupCounts()); | 363 EXPECT_EQ(1u, GetPopupCounts()); |
| 329 notification_list()->MarkSinglePopupAsShown(id1, true); | 364 notification_list()->MarkSinglePopupAsShown(id1, true); |
| 330 EXPECT_EQ(0u, GetPopupCounts()); | 365 EXPECT_EQ(0u, GetPopupCounts()); |
| 331 | 366 |
| 332 // id1 promoted to HIGH->MAX, it'll appear as toast again. | 367 // id1 promoted to HIGH->MAX, it'll appear as toast again. |
| 333 base::DictionaryValue priority_max; | 368 base::DictionaryValue priority_max; |
| 334 priority_max.SetInteger(message_center::kPriorityKey, | 369 priority_max.SetInteger(message_center::kPriorityKey, |
| 335 static_cast<int>(MAX_PRIORITY)); | 370 static_cast<int>(MAX_PRIORITY)); |
| 336 notification_list()->UpdateNotificationMessage( | 371 notification_list()->UpdateNotificationMessage(id1, |
| 337 id1, id1, UTF8ToUTF16("newtitle2"), UTF8ToUTF16("newbody2"), | 372 id1, |
| 338 &priority_max); | 373 UTF8ToUTF16("newtitle2"), |
| 374 UTF8ToUTF16("newbody2"), |
| 375 &priority_max, |
| 376 NULL); |
| 339 EXPECT_EQ(1u, GetPopupCounts()); | 377 EXPECT_EQ(1u, GetPopupCounts()); |
| 340 notification_list()->MarkSinglePopupAsShown(id1, true); | 378 notification_list()->MarkSinglePopupAsShown(id1, true); |
| 341 EXPECT_EQ(0u, GetPopupCounts()); | 379 EXPECT_EQ(0u, GetPopupCounts()); |
| 342 | 380 |
| 343 // id1 demoted to MAX->DEFAULT, no appearing as toast. | 381 // id1 demoted to MAX->DEFAULT, no appearing as toast. |
| 344 notification_list()->UpdateNotificationMessage( | 382 notification_list()->UpdateNotificationMessage(id1, |
| 345 id1, id1, UTF8ToUTF16("newtitle3"), UTF8ToUTF16("newbody3"), | 383 id1, |
| 346 &priority_default); | 384 UTF8ToUTF16("newtitle3"), |
| 385 UTF8ToUTF16("newbody3"), |
| 386 &priority_default, |
| 387 NULL); |
| 347 EXPECT_EQ(0u, GetPopupCounts()); | 388 EXPECT_EQ(0u, GetPopupCounts()); |
| 348 } | 389 } |
| 349 | 390 |
| 350 TEST_F(NotificationListTest, NotificationOrderAndPriority) { | 391 TEST_F(NotificationListTest, NotificationOrderAndPriority) { |
| 351 base::Time now = base::Time::Now(); | 392 base::Time now = base::Time::Now(); |
| 352 base::DictionaryValue optional; | 393 base::DictionaryValue optional; |
| 353 SetupTimestampKey(now, &optional); | 394 SetupTimestampKey(now, &optional); |
| 354 optional.SetInteger(message_center::kPriorityKey, 2); | 395 optional.SetInteger(message_center::kPriorityKey, 2); |
| 355 std::string max_id = AddNotification(&optional); | 396 std::string max_id = AddNotification(&optional); |
| 356 now += base::TimeDelta::FromSeconds(1); | 397 now += base::TimeDelta::FromSeconds(1); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 EXPECT_FALSE(n1->shown_as_popup()); | 506 EXPECT_FALSE(n1->shown_as_popup()); |
| 466 EXPECT_TRUE(n1->is_read()); | 507 EXPECT_TRUE(n1->is_read()); |
| 467 | 508 |
| 468 notification_list()->MarkSinglePopupAsShown(id1, true); | 509 notification_list()->MarkSinglePopupAsShown(id1, true); |
| 469 | 510 |
| 470 n1 = GetNotification(id1); | 511 n1 = GetNotification(id1); |
| 471 EXPECT_TRUE(n1->shown_as_popup()); | 512 EXPECT_TRUE(n1->shown_as_popup()); |
| 472 EXPECT_TRUE(n1->is_read()); | 513 EXPECT_TRUE(n1->is_read()); |
| 473 | 514 |
| 474 const std::string replaced("test-replaced-id"); | 515 const std::string replaced("test-replaced-id"); |
| 475 notification_list()->UpdateNotificationMessage( | 516 notification_list()->UpdateNotificationMessage(id1, |
| 476 id1, replaced, UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), NULL); | 517 replaced, |
| 518 UTF8ToUTF16("newtitle"), |
| 519 UTF8ToUTF16("newbody"), |
| 520 NULL, |
| 521 NULL); |
| 477 n1 = GetNotification(id1); | 522 n1 = GetNotification(id1); |
| 478 EXPECT_TRUE(n1 == NULL); | 523 EXPECT_TRUE(n1 == NULL); |
| 479 const Notification* nr = GetNotification(replaced); | 524 const Notification* nr = GetNotification(replaced); |
| 480 EXPECT_TRUE(nr->shown_as_popup()); | 525 EXPECT_TRUE(nr->shown_as_popup()); |
| 481 EXPECT_TRUE(nr->is_read()); | 526 EXPECT_TRUE(nr->is_read()); |
| 482 } | 527 } |
| 483 | 528 |
| 484 TEST_F(NotificationListTest, QuietMode) { | 529 TEST_F(NotificationListTest, QuietMode) { |
| 485 notification_list()->SetQuietMode(true); | 530 notification_list()->SetQuietMode(true); |
| 486 AddNotification(NULL); | 531 AddNotification(NULL); |
| 487 AddPriorityNotification(HIGH_PRIORITY); | 532 AddPriorityNotification(HIGH_PRIORITY); |
| 488 AddPriorityNotification(MAX_PRIORITY); | 533 AddPriorityNotification(MAX_PRIORITY); |
| 489 EXPECT_EQ(3u, notification_list()->NotificationCount()); | 534 EXPECT_EQ(3u, notification_list()->NotificationCount()); |
| 490 EXPECT_EQ(0u, GetPopupCounts()); | 535 EXPECT_EQ(0u, GetPopupCounts()); |
| 491 // TODO(mukai): fix here when notification_list distinguish dismiss by quiet | 536 // TODO(mukai): fix here when notification_list distinguish dismiss by quiet |
| 492 // mode and by user operation. | 537 // mode and by user operation. |
| 493 EXPECT_EQ(0u, notification_list()->unread_count()); | 538 EXPECT_EQ(0u, notification_list()->unread_count()); |
| 494 | 539 |
| 495 notification_list()->SetQuietMode(false); | 540 notification_list()->SetQuietMode(false); |
| 496 AddNotification(NULL); | 541 AddNotification(NULL); |
| 497 EXPECT_EQ(4u, notification_list()->NotificationCount()); | 542 EXPECT_EQ(4u, notification_list()->NotificationCount()); |
| 498 EXPECT_EQ(1u, GetPopupCounts()); | 543 EXPECT_EQ(1u, GetPopupCounts()); |
| 499 | 544 |
| 500 // TODO(mukai): Add test of quiet mode with expiration. | 545 // TODO(mukai): Add test of quiet mode with expiration. |
| 501 } | 546 } |
| 502 | 547 |
| 503 } // namespace test | 548 } // namespace test |
| 504 } // namespace message_center | 549 } // namespace message_center |
| OLD | NEW |