Chromium Code Reviews| 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/strings/utf_string_conversions.h" | |
| 8 #include "chrome/browser/notifications/notification.h" | |
| 9 #include "chrome/browser/notifications/notification_test_util.h" | |
| 10 #include "chrome/common/extensions/api/notifications.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | |
| 13 #include "ui/message_center/notification.h" | |
| 14 #include "ui/message_center/notification_types.h" | |
| 15 #include "url/gurl.h" | |
| 16 | |
| 17 class NotificationConversionHelperTest : public testing::Test { | |
| 18 public: | |
| 19 NotificationConversionHelperTest() {} | |
| 20 | |
| 21 virtual void SetUp() OVERRIDE {} | |
| 22 | |
| 23 virtual void TearDown() OVERRIDE {} | |
| 24 | |
| 25 protected: | |
| 26 scoped_ptr<Notification> CreateNotification( | |
| 27 message_center::NotificationType type) { | |
| 28 message_center::RichNotificationData optional_fields; | |
| 29 optional_fields.priority = 1; | |
| 30 optional_fields.context_message = | |
| 31 base::UTF8ToUTF16("I am a context message."); | |
| 32 optional_fields.timestamp = base::Time::FromDoubleT(12345678.9); | |
| 33 optional_fields.buttons.push_back( | |
| 34 message_center::ButtonInfo(base::UTF8ToUTF16("Button 1"))); | |
| 35 optional_fields.buttons.push_back( | |
| 36 message_center::ButtonInfo(base::UTF8ToUTF16("Button 2"))); | |
| 37 optional_fields.clickable = false; | |
|
dewittj
2014/08/13 18:30:18
I think this line is unnecessary
liyanhou
2014/08/13 20:28:57
I think the default set in the notification constr
| |
| 38 optional_fields.small_image = gfx::Image(); | |
|
dewittj
2014/08/13 18:30:18
ditto
liyanhou
2014/08/13 20:28:57
Done.
| |
| 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 scoped_ptr<Notification> notification(new Notification( | |
| 61 type, | |
| 62 GURL(), | |
| 63 base::UTF8ToUTF16("Title"), | |
| 64 base::UTF8ToUTF16("This is a message."), | |
| 65 icon, | |
| 66 blink::WebTextDirectionDefault, | |
| 67 message_center::NotifierId(message_center::NotifierId::APPLICATION, | |
| 68 "Notifier 1"), | |
| 69 base::UTF8ToUTF16("Notifier's Name"), | |
| 70 base::UTF8ToUTF16("id1"), | |
| 71 optional_fields, | |
| 72 delegate)); | |
| 73 | |
| 74 return notification.Pass(); | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 DISALLOW_COPY_AND_ASSIGN(NotificationConversionHelperTest); | |
| 79 }; | |
| 80 | |
| 81 TEST_F(NotificationConversionHelperTest, NotificationToNotificationOptions) { | |
| 82 // Create a notification of image type | |
| 83 scoped_ptr<Notification> notification1 = | |
| 84 CreateNotification(message_center::NOTIFICATION_TYPE_IMAGE); | |
| 85 scoped_ptr<extensions::api::notifications::NotificationOptions> options1( | |
| 86 new extensions::api::notifications::NotificationOptions()); | |
| 87 NotificationConversionHelper::NotificationToNotificationOptions( | |
| 88 *(notification1), options1.get()); | |
| 89 | |
| 90 EXPECT_EQ(options1->type, | |
| 91 extensions::api::notifications::TEMPLATE_TYPE_IMAGE); | |
| 92 EXPECT_EQ(*(options1->title), "Title"); | |
| 93 EXPECT_EQ(*(options1->message), "This is a message."); | |
| 94 EXPECT_EQ(*(options1->priority), 1); | |
| 95 EXPECT_EQ(*(options1->context_message), "I am a context message."); | |
| 96 EXPECT_FALSE(*(options1->is_clickable)); | |
| 97 EXPECT_EQ(*(options1->event_time), 12345678.9); | |
| 98 EXPECT_EQ(options1->buttons->at(0)->title, "Button 1"); | |
| 99 EXPECT_EQ(options1->buttons->at(1)->title, "Button 2"); | |
| 100 | |
| 101 EXPECT_EQ(options1->icon_bitmap->width, 1); | |
| 102 EXPECT_EQ(options1->icon_bitmap->height, 1); | |
| 103 | |
| 104 // Create a notification of progress type | |
| 105 scoped_ptr<Notification> notification2 = | |
| 106 CreateNotification(message_center::NOTIFICATION_TYPE_PROGRESS); | |
| 107 scoped_ptr<extensions::api::notifications::NotificationOptions> options2( | |
| 108 new extensions::api::notifications::NotificationOptions()); | |
| 109 NotificationConversionHelper::NotificationToNotificationOptions( | |
| 110 *(notification2), options2.get()); | |
| 111 EXPECT_EQ(options2->type, | |
| 112 extensions::api::notifications::TEMPLATE_TYPE_PROGRESS); | |
| 113 EXPECT_EQ(*(options2->progress), 50); | |
| 114 | |
| 115 // Create a notification of multiple type | |
| 116 scoped_ptr<Notification> notification3 = | |
| 117 CreateNotification(message_center::NOTIFICATION_TYPE_MULTIPLE); | |
| 118 scoped_ptr<extensions::api::notifications::NotificationOptions> options3( | |
| 119 new extensions::api::notifications::NotificationOptions()); | |
| 120 NotificationConversionHelper::NotificationToNotificationOptions( | |
| 121 *(notification3), options3.get()); | |
| 122 EXPECT_EQ(options3->type, extensions::api::notifications::TEMPLATE_TYPE_LIST); | |
| 123 EXPECT_EQ(options3->items->at(0)->title, "Item 1 Title"); | |
| 124 EXPECT_EQ(options3->items->at(0)->message, "Item 1 Message"); | |
| 125 EXPECT_EQ(options3->items->at(1)->title, "Item 2 Title"); | |
| 126 EXPECT_EQ(options3->items->at(1)->message, "Item 2 Message"); | |
| 127 } | |
| OLD | NEW |