Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1797)

Unified Diff: chrome/browser/notifications/notification_template_builder_unittest.cc

Issue 2920303002: Add a NotificationTemplateBuilder for Action Center toasts (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/notifications/notification_template_builder_unittest.cc
diff --git a/chrome/browser/notifications/notification_template_builder_unittest.cc b/chrome/browser/notifications/notification_template_builder_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6f41807f7368875f005daae9956250bda6b5081c
--- /dev/null
+++ b/chrome/browser/notifications/notification_template_builder_unittest.cc
@@ -0,0 +1,164 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/notifications/notification_template_builder.h"
+
+#include <memory>
+#include <string>
+
+#include "base/macros.h"
+#include "base/memory/ptr_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/libxml/chromium/libxml_utils.h"
+#include "ui/message_center/notification.h"
+
+using message_center::Notification;
+using message_center::NotifierId;
+using message_center::RichNotificationData;
+
+namespace {
+
+const char kTextElement[] = "text";
+const char kTextElementIdAttribute[] = "id";
+
+const char kNotificationId[] = "notification_id";
+const char kNotificationTitle[] = "My Title";
+const char kNotificationMessage[] = "My Message";
+const char kNotificationOrigin[] = "https://example.com";
+
+// Intermediary format for the options available when creating a notification,
+// with default values specific to this test suite to avoid endless repetition.
+struct NotificationData {
+ NotificationData()
+ : id(kNotificationId),
+ title(kNotificationTitle),
+ message(kNotificationMessage),
+ origin(kNotificationOrigin) {}
+
+ std::string id;
+ std::string title;
+ std::string message;
+ GURL origin;
+};
+
+} // namespace
+
+class NotificationTemplateBuilderTest : public ::testing::Test {
+ public:
+ NotificationTemplateBuilderTest() = default;
+ ~NotificationTemplateBuilderTest() override = default;
+
+ protected:
+ // Builds the message_center::Notification object and then the template for
+ // the given |notification_data| and loads its XML content in |xml_reader_|.
+ // Calls must be wrapped in ASSERT_NO_FATAL_FAILURE().
+ void BuildTemplate(const NotificationData& notification_data) {
+ GURL origin_url(notification_data.origin);
+
+ Notification notification(
+ message_center::NOTIFICATION_TYPE_SIMPLE, notification_data.id,
+ base::UTF8ToUTF16(notification_data.title),
+ base::UTF8ToUTF16(notification_data.message), gfx::Image() /* icon */,
+ base::string16() /* display_source */, origin_url,
+ NotifierId(origin_url), RichNotificationData(), nullptr /* delegate */);
+
+ template_ =
+ NotificationTemplateBuilder::Build(notification_data.id, notification);
+ ASSERT_TRUE(template_);
+
+ xml_template_ = template_->GetNotificationTemplate();
+ xml_reader_ = base::MakeUnique<XmlReader>();
+
+ ASSERT_TRUE(xml_reader_->Load(xml_template_));
+ }
+
+ // Skips to the next element whose node name is |node_name|.
+ // Calls must be wrapped in ASSERT_NO_FATAL_FAILURE().
+ void ForwardToNextNamedElement(const std::string& node_name) {
+ DCHECK(xml_reader_);
+
+ while (xml_reader_->Read()) {
+ if (xml_reader_->IsClosingElement())
+ continue;
+
+ if (xml_reader_->NodeName() == node_name)
+ return;
+ }
+
+ FAIL() << "No further nodes named <" << node_name << "> were found.";
+ }
+
+ // Returns the |origin| formatted by the NotificationTemplateBuilder.
+ std::string FormatOrigin(const GURL& origin) {
+ DCHECK(template_);
+ return template_->FormatOrigin(origin);
+ }
+
+ protected:
+ std::unique_ptr<NotificationTemplateBuilder> template_;
+
+ // The |xml_template_| must outlive the |xml_reader_|.
+ std::string xml_template_;
+ std::unique_ptr<XmlReader> xml_reader_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(NotificationTemplateBuilderTest);
+};
+
+TEST_F(NotificationTemplateBuilderTest, SimpleToast) {
+ NotificationData notification_data;
+ ASSERT_NO_FATAL_FAILURE(BuildTemplate(notification_data));
+
+ std::string attribute_value, content;
+
+ // First <text> element is the title.
+ ASSERT_NO_FATAL_FAILURE(ForwardToNextNamedElement(kTextElement));
+ ASSERT_TRUE(
+ xml_reader_->NodeAttribute(kTextElementIdAttribute, &attribute_value));
+ EXPECT_EQ(attribute_value, "1");
+
+ ASSERT_TRUE(xml_reader_->ReadElementContent(&content));
+ EXPECT_EQ(content, notification_data.title);
+
+ content.clear();
+
+ // Second <text> element is the message.
+ ASSERT_NO_FATAL_FAILURE(ForwardToNextNamedElement(kTextElement));
+ ASSERT_TRUE(
+ xml_reader_->NodeAttribute(kTextElementIdAttribute, &attribute_value));
+ EXPECT_EQ(attribute_value, "2");
+
+ ASSERT_TRUE(xml_reader_->ReadElementContent(&content));
+ EXPECT_EQ(content, notification_data.message);
+
+ content.clear();
+
+ // Third <text> element is the origin attribution.
+ ASSERT_NO_FATAL_FAILURE(ForwardToNextNamedElement(kTextElement));
+ ASSERT_TRUE(
+ xml_reader_->NodeAttribute(kTextElementIdAttribute, &attribute_value));
+ EXPECT_EQ(attribute_value, "3");
+
+ ASSERT_TRUE(xml_reader_->ReadElementContent(&content));
+ EXPECT_EQ(content, FormatOrigin(notification_data.origin));
+}
+
+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.
+ NotificationData notification_data;
+ ASSERT_NO_FATAL_FAILURE(BuildTemplate(notification_data));
+
+ const char kExpectedXml[] = R"(<toast launch="notification_id">
+ <visual>
+ <binding template="ToastText04">
+ <text id="1">My Title</text>
+ <text id="2">My Message</text>
+ <text id="3">example.com</text>
+ </binding>
+ </visual>
+</toast>
+)";
+
+ EXPECT_EQ(xml_template_, kExpectedXml);
+}

Powered by Google App Engine
This is Rietveld 408576698