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
17class 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;
38
39    if (type == message_center::NOTIFICATION_TYPE_IMAGE)
40      optional_fields.image = gfx::Image();
41    if (type == message_center::NOTIFICATION_TYPE_MULTIPLE) {
42      optional_fields.items.push_back(message_center::NotificationItem(
43          base::UTF8ToUTF16("Item 1 Title"),
44          base::UTF8ToUTF16("Item 1 Message")));
45      optional_fields.items.push_back(message_center::NotificationItem(
46          base::UTF8ToUTF16("Item 2 Title"),
47          base::UTF8ToUTF16("Item 2 Message")));
48    }
49    if (type == message_center::NOTIFICATION_TYPE_PROGRESS)
50      optional_fields.progress = 50;
51
52    NotificationDelegate* delegate(new MockNotificationDelegate("id1"));
53
54    SkBitmap bitmap;
55    bitmap.allocN32Pixels(1, 1);
56    bitmap.eraseColor(SkColorSetRGB(1, 2, 3));
57    gfx::Image icon = gfx::Image::CreateFrom1xBitmap(bitmap);
58
59    scoped_ptr<Notification> notification(new Notification(
60        type,
61        GURL(),
62        base::UTF8ToUTF16("Title"),
63        base::UTF8ToUTF16("This is a message."),
64        icon,
65        blink::WebTextDirectionDefault,
66        message_center::NotifierId(message_center::NotifierId::APPLICATION,
67                                   "Notifier 1"),
68        base::UTF8ToUTF16("Notifier's Name"),
69        base::UTF8ToUTF16("id1"),
70        optional_fields,
71        delegate));
72
73    return notification.Pass();
74  }
75
76 private:
77  DISALLOW_COPY_AND_ASSIGN(NotificationConversionHelperTest);
78};
79
80TEST_F(NotificationConversionHelperTest, NotificationToNotificationOptions) {
81  // Create a notification of image type
82  scoped_ptr<Notification> notification1 =
83      CreateNotification(message_center::NOTIFICATION_TYPE_IMAGE);
84  scoped_ptr<extensions::api::notifications::NotificationOptions> options1(
85      new extensions::api::notifications::NotificationOptions());
86  NotificationConversionHelper::NotificationToNotificationOptions(
87      *(notification1), options1.get());
88
89  EXPECT_EQ(options1->type,
90            extensions::api::notifications::TEMPLATE_TYPE_IMAGE);
91  EXPECT_EQ(*(options1->title), "Title");
92  EXPECT_EQ(*(options1->message), "This is a message.");
93  EXPECT_EQ(*(options1->priority), 1);
94  EXPECT_EQ(*(options1->context_message), "I am a context message.");
95  EXPECT_FALSE(*(options1->is_clickable));
96  EXPECT_EQ(*(options1->event_time), 12345678.9);
97  EXPECT_EQ(options1->buttons->at(0)->title, "Button 1");
98  EXPECT_EQ(options1->buttons->at(1)->title, "Button 2");
99
100  EXPECT_EQ(options1->icon_bitmap->width, 1);
101  EXPECT_EQ(options1->icon_bitmap->height, 1);
102
103  // Create a notification of progress type
104  scoped_ptr<Notification> notification2 =
105      CreateNotification(message_center::NOTIFICATION_TYPE_PROGRESS);
106  scoped_ptr<extensions::api::notifications::NotificationOptions> options2(
107      new extensions::api::notifications::NotificationOptions());
108  NotificationConversionHelper::NotificationToNotificationOptions(
109      *(notification2), options2.get());
110  EXPECT_EQ(options2->type,
111            extensions::api::notifications::TEMPLATE_TYPE_PROGRESS);
112  EXPECT_EQ(*(options2->progress), 50);
113
114  // Create a notification of multiple type
115  scoped_ptr<Notification> notification3 =
116      CreateNotification(message_center::NOTIFICATION_TYPE_MULTIPLE);
117  scoped_ptr<extensions::api::notifications::NotificationOptions> options3(
118      new extensions::api::notifications::NotificationOptions());
119  NotificationConversionHelper::NotificationToNotificationOptions(
120      *(notification3), options3.get());
121  EXPECT_EQ(options3->type, extensions::api::notifications::TEMPLATE_TYPE_LIST);
122  EXPECT_EQ(options3->items->at(0)->title, "Item 1 Title");
123  EXPECT_EQ(options3->items->at(0)->message, "Item 1 Message");
124  EXPECT_EQ(options3->items->at(1)->title, "Item 2 Title");
125  EXPECT_EQ(options3->items->at(1)->message, "Item 2 Message");
126}
127