| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/notifications/notification_conversion_helper.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "chrome/browser/notifications/notification.h" | |
| 10 #include "chrome/browser/notifications/notification_test_util.h" | |
| 11 #include "chrome/common/extensions/api/notifications.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | |
| 14 #include "ui/message_center/notification.h" | |
| 15 #include "ui/message_center/notification_types.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 class NotificationConversionHelperTest : public testing::Test { | |
| 19 public: | |
| 20 NotificationConversionHelperTest() {} | |
| 21 | |
| 22 void SetUp() override {} | |
| 23 | |
| 24 void TearDown() override {} | |
| 25 | |
| 26 protected: | |
| 27 std::unique_ptr<Notification> CreateNotification( | |
| 28 message_center::NotificationType type) { | |
| 29 message_center::RichNotificationData optional_fields; | |
| 30 optional_fields.priority = 1; | |
| 31 optional_fields.context_message = | |
| 32 base::UTF8ToUTF16("I am a context message."); | |
| 33 optional_fields.timestamp = base::Time::FromDoubleT(12345678.9); | |
| 34 optional_fields.buttons.push_back( | |
| 35 message_center::ButtonInfo(base::UTF8ToUTF16("Button 1"))); | |
| 36 optional_fields.buttons.push_back( | |
| 37 message_center::ButtonInfo(base::UTF8ToUTF16("Button 2"))); | |
| 38 optional_fields.clickable = false; | |
| 39 | |
| 40 if (type == message_center::NOTIFICATION_TYPE_IMAGE) | |
| 41 optional_fields.image = gfx::Image(); | |
| 42 if (type == message_center::NOTIFICATION_TYPE_MULTIPLE) { | |
| 43 optional_fields.items.push_back(message_center::NotificationItem( | |
| 44 base::UTF8ToUTF16("Item 1 Title"), | |
| 45 base::UTF8ToUTF16("Item 1 Message"))); | |
| 46 optional_fields.items.push_back(message_center::NotificationItem( | |
| 47 base::UTF8ToUTF16("Item 2 Title"), | |
| 48 base::UTF8ToUTF16("Item 2 Message"))); | |
| 49 } | |
| 50 if (type == message_center::NOTIFICATION_TYPE_PROGRESS) | |
| 51 optional_fields.progress = 50; | |
| 52 | |
| 53 NotificationDelegate* delegate(new MockNotificationDelegate("id1")); | |
| 54 | |
| 55 SkBitmap bitmap; | |
| 56 bitmap.allocN32Pixels(1, 1); | |
| 57 bitmap.eraseColor(SkColorSetRGB(1, 2, 3)); | |
| 58 gfx::Image icon = gfx::Image::CreateFrom1xBitmap(bitmap); | |
| 59 | |
| 60 std::unique_ptr<Notification> notification(new Notification( | |
| 61 type, base::UTF8ToUTF16("Title"), | |
| 62 base::UTF8ToUTF16("This is a message."), icon, | |
| 63 message_center::NotifierId(message_center::NotifierId::APPLICATION, | |
| 64 "Notifier 1"), | |
| 65 base::UTF8ToUTF16("Notifier's Name"), GURL(), "id1", optional_fields, | |
| 66 delegate)); | |
| 67 | |
| 68 return notification; | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 DISALLOW_COPY_AND_ASSIGN(NotificationConversionHelperTest); | |
| 73 }; | |
| 74 | |
| 75 TEST_F(NotificationConversionHelperTest, NotificationToNotificationOptions) { | |
| 76 // Create a notification of image type | |
| 77 std::unique_ptr<Notification> notification1 = | |
| 78 CreateNotification(message_center::NOTIFICATION_TYPE_IMAGE); | |
| 79 std::unique_ptr<extensions::api::notifications::NotificationOptions> options1( | |
| 80 new extensions::api::notifications::NotificationOptions()); | |
| 81 NotificationConversionHelper::NotificationToNotificationOptions( | |
| 82 *(notification1), options1.get()); | |
| 83 | |
| 84 EXPECT_EQ(options1->type, | |
| 85 extensions::api::notifications::TEMPLATE_TYPE_IMAGE); | |
| 86 EXPECT_EQ(*(options1->title), "Title"); | |
| 87 EXPECT_EQ(*(options1->message), "This is a message."); | |
| 88 EXPECT_EQ(*(options1->priority), 1); | |
| 89 EXPECT_EQ(*(options1->context_message), "I am a context message."); | |
| 90 EXPECT_FALSE(*(options1->is_clickable)); | |
| 91 EXPECT_EQ(*(options1->event_time), 12345678.9); | |
| 92 EXPECT_EQ(options1->buttons->at(0).title, "Button 1"); | |
| 93 EXPECT_EQ(options1->buttons->at(1).title, "Button 2"); | |
| 94 | |
| 95 EXPECT_EQ(options1->icon_bitmap->width, 1); | |
| 96 EXPECT_EQ(options1->icon_bitmap->height, 1); | |
| 97 | |
| 98 // Create a notification of progress type | |
| 99 std::unique_ptr<Notification> notification2 = | |
| 100 CreateNotification(message_center::NOTIFICATION_TYPE_PROGRESS); | |
| 101 std::unique_ptr<extensions::api::notifications::NotificationOptions> options2( | |
| 102 new extensions::api::notifications::NotificationOptions()); | |
| 103 NotificationConversionHelper::NotificationToNotificationOptions( | |
| 104 *(notification2), options2.get()); | |
| 105 EXPECT_EQ(options2->type, | |
| 106 extensions::api::notifications::TEMPLATE_TYPE_PROGRESS); | |
| 107 EXPECT_EQ(*(options2->progress), 50); | |
| 108 | |
| 109 // Create a notification of multiple type | |
| 110 std::unique_ptr<Notification> notification3 = | |
| 111 CreateNotification(message_center::NOTIFICATION_TYPE_MULTIPLE); | |
| 112 std::unique_ptr<extensions::api::notifications::NotificationOptions> options3( | |
| 113 new extensions::api::notifications::NotificationOptions()); | |
| 114 NotificationConversionHelper::NotificationToNotificationOptions( | |
| 115 *(notification3), options3.get()); | |
| 116 EXPECT_EQ(options3->type, extensions::api::notifications::TEMPLATE_TYPE_LIST); | |
| 117 EXPECT_EQ(options3->items->at(0).title, "Item 1 Title"); | |
| 118 EXPECT_EQ(options3->items->at(0).message, "Item 1 Message"); | |
| 119 EXPECT_EQ(options3->items->at(1).title, "Item 2 Title"); | |
| 120 EXPECT_EQ(options3->items->at(1).message, "Item 2 Message"); | |
| 121 } | |
| OLD | NEW |