Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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_template_builder.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "third_party/libxml/chromium/libxml_utils.h" | |
| 15 #include "ui/message_center/notification.h" | |
| 16 | |
| 17 using message_center::Notification; | |
| 18 using message_center::NotifierId; | |
| 19 using message_center::RichNotificationData; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const char kTextElement[] = "text"; | |
| 24 const char kTextElementIdAttribute[] = "id"; | |
| 25 | |
| 26 const char kNotificationId[] = "notification_id"; | |
| 27 const char kNotificationTitle[] = "My Title"; | |
| 28 const char kNotificationMessage[] = "My Message"; | |
| 29 const char kNotificationOrigin[] = "https://example.com"; | |
| 30 | |
| 31 // Intermediary format for the options available when creating a notification, | |
| 32 // with default values specific to this test suite to avoid endless repetition. | |
| 33 struct NotificationData { | |
| 34 NotificationData() | |
| 35 : id(kNotificationId), | |
| 36 title(kNotificationTitle), | |
| 37 message(kNotificationMessage), | |
| 38 origin(kNotificationOrigin) {} | |
| 39 | |
| 40 std::string id; | |
| 41 std::string title; | |
| 42 std::string message; | |
| 43 GURL origin; | |
| 44 }; | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 class NotificationTemplateBuilderTest : public ::testing::Test { | |
| 49 public: | |
| 50 NotificationTemplateBuilderTest() = default; | |
| 51 ~NotificationTemplateBuilderTest() override = default; | |
| 52 | |
| 53 protected: | |
| 54 // Builds the message_center::Notification object and then the template for | |
| 55 // the given |notification_data| and loads its XML content in |xml_reader_|. | |
| 56 // Calls must be wrapped in ASSERT_NO_FATAL_FAILURE(). | |
| 57 void BuildTemplate(const NotificationData& notification_data) { | |
| 58 GURL origin_url(notification_data.origin); | |
| 59 | |
| 60 Notification notification( | |
| 61 message_center::NOTIFICATION_TYPE_SIMPLE, notification_data.id, | |
| 62 base::UTF8ToUTF16(notification_data.title), | |
| 63 base::UTF8ToUTF16(notification_data.message), gfx::Image() /* icon */, | |
| 64 base::string16() /* display_source */, origin_url, | |
| 65 NotifierId(origin_url), RichNotificationData(), nullptr /* delegate */); | |
| 66 | |
| 67 template_ = | |
| 68 NotificationTemplateBuilder::Build(notification_data.id, notification); | |
| 69 ASSERT_TRUE(template_); | |
| 70 | |
| 71 xml_template_ = template_->GetNotificationTemplate(); | |
| 72 xml_reader_ = base::MakeUnique<XmlReader>(); | |
| 73 | |
| 74 ASSERT_TRUE(xml_reader_->Load(xml_template_)); | |
| 75 } | |
| 76 | |
| 77 // Skips to the next element whose node name is |node_name|. | |
| 78 // Calls must be wrapped in ASSERT_NO_FATAL_FAILURE(). | |
| 79 void ForwardToNextNamedElement(const std::string& node_name) { | |
| 80 DCHECK(xml_reader_); | |
| 81 | |
| 82 while (xml_reader_->Read()) { | |
| 83 if (xml_reader_->IsClosingElement()) | |
| 84 continue; | |
| 85 | |
| 86 if (xml_reader_->NodeName() == node_name) | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 FAIL() << "No further nodes named <" << node_name << "> were found."; | |
| 91 } | |
| 92 | |
| 93 // Returns the |origin| formatted by the NotificationTemplateBuilder. | |
| 94 std::string FormatOrigin(const GURL& origin) { | |
| 95 DCHECK(template_); | |
| 96 return template_->FormatOrigin(origin); | |
| 97 } | |
| 98 | |
| 99 protected: | |
| 100 std::unique_ptr<NotificationTemplateBuilder> template_; | |
| 101 | |
| 102 // The |xml_template_| must outlive the |xml_reader_|. | |
| 103 std::string xml_template_; | |
| 104 std::unique_ptr<XmlReader> xml_reader_; | |
| 105 | |
| 106 private: | |
| 107 DISALLOW_COPY_AND_ASSIGN(NotificationTemplateBuilderTest); | |
| 108 }; | |
| 109 | |
| 110 TEST_F(NotificationTemplateBuilderTest, SimpleToast) { | |
| 111 NotificationData notification_data; | |
| 112 ASSERT_NO_FATAL_FAILURE(BuildTemplate(notification_data)); | |
| 113 | |
| 114 std::string attribute_value, content; | |
| 115 | |
| 116 // First <text> element is the title. | |
| 117 ASSERT_NO_FATAL_FAILURE(ForwardToNextNamedElement(kTextElement)); | |
| 118 ASSERT_TRUE( | |
| 119 xml_reader_->NodeAttribute(kTextElementIdAttribute, &attribute_value)); | |
| 120 EXPECT_EQ(attribute_value, "1"); | |
| 121 | |
| 122 ASSERT_TRUE(xml_reader_->ReadElementContent(&content)); | |
| 123 EXPECT_EQ(content, notification_data.title); | |
| 124 | |
| 125 content.clear(); | |
| 126 | |
| 127 // Second <text> element is the message. | |
| 128 ASSERT_NO_FATAL_FAILURE(ForwardToNextNamedElement(kTextElement)); | |
| 129 ASSERT_TRUE( | |
| 130 xml_reader_->NodeAttribute(kTextElementIdAttribute, &attribute_value)); | |
| 131 EXPECT_EQ(attribute_value, "2"); | |
| 132 | |
| 133 ASSERT_TRUE(xml_reader_->ReadElementContent(&content)); | |
| 134 EXPECT_EQ(content, notification_data.message); | |
| 135 | |
| 136 content.clear(); | |
| 137 | |
| 138 // Third <text> element is the origin attribution. | |
| 139 ASSERT_NO_FATAL_FAILURE(ForwardToNextNamedElement(kTextElement)); | |
| 140 ASSERT_TRUE( | |
| 141 xml_reader_->NodeAttribute(kTextElementIdAttribute, &attribute_value)); | |
| 142 EXPECT_EQ(attribute_value, "3"); | |
| 143 | |
| 144 ASSERT_TRUE(xml_reader_->ReadElementContent(&content)); | |
| 145 EXPECT_EQ(content, FormatOrigin(notification_data.origin)); | |
| 146 } | |
| 147 | |
| 148 TEST_F(NotificationTemplateBuilderTest, SimpleToastXml) { | |
|
Peter Beverloo
2017/06/05 17:06:25
I'm inclined to prefer this method of testing. It
Finnur
2017/06/06 11:00:09
Yeah, I think I agree with you...
Peter Beverloo
2017/06/16 15:40:21
Ok, done.
| |
| 149 NotificationData notification_data; | |
| 150 ASSERT_NO_FATAL_FAILURE(BuildTemplate(notification_data)); | |
| 151 | |
| 152 const char kExpectedXml[] = R"(<toast launch="notification_id"> | |
| 153 <visual> | |
| 154 <binding template="ToastText04"> | |
| 155 <text id="1">My Title</text> | |
| 156 <text id="2">My Message</text> | |
| 157 <text id="3">example.com</text> | |
| 158 </binding> | |
| 159 </visual> | |
| 160 </toast> | |
| 161 )"; | |
| 162 | |
| 163 EXPECT_EQ(xml_template_, kExpectedXml); | |
| 164 } | |
| OLD | NEW |