Commit ef2e9827 authored by Finnur Thorarinsson's avatar Finnur Thorarinsson Committed by Commit Bot

Win10 Notifications: Implement simple buttons.

Bug: 734095
Change-Id: I49b37f0b36c657e28dd85c6f29bb291c083c7467
Reviewed-on: https://chromium-review.googlesource.com/685834
Commit-Queue: Finnur Thorarinsson <finnur@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505238}
parent e5063b64
......@@ -5,6 +5,7 @@
#include "chrome/browser/notifications/notification_template_builder.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/url_formatter/elide_url.h"
......@@ -15,17 +16,24 @@
namespace {
// Constants used for the XML element names and their attributes.
const char kToastElement[] = "toast";
const char kToastElementLaunchAttribute[] = "launch";
const char kVisualElement[] = "visual";
const char kActionElement[] = "action";
const char kActionsElement[] = "actions";
const char kActivationType[] = "activationType";
const char kArguments[] = "arguments";
const char kBindingElement[] = "binding";
const char kBindingElementTemplateAttribute[] = "template";
const char kButtonIndex[] = "buttonIndex=";
const char kContent[] = "content";
const char kForeground[] = "foreground";
const char kTextElement[] = "text";
const char kTextElementIdAttribute[] = "id";
const char kToastElement[] = "toast";
const char kToastElementLaunchAttribute[] = "launch";
const char kVisualElement[] = "visual";
// Name of the template used for default Chrome notifications.
// https://msdn.microsoft.com/library/1a437614-4259-426b-8e3f-ca57368b2e7a
const char kDefaultTemplate[] = "ToastText04";
const char kDefaultTemplate[] = "ToastGeneric";
// The XML version header that has to be stripped from the output.
const char kXmlVersionHeader[] = "<?xml version=\"1.0\"?>\n";
......@@ -55,8 +63,10 @@ std::unique_ptr<NotificationTemplateBuilder> NotificationTemplateBuilder::Build(
builder->FormatOrigin(notification.origin_url()));
builder->EndBindingElement();
builder->EndVisualElement();
builder->AddActions(notification.buttons());
builder->EndToastElement();
return builder;
......@@ -124,3 +134,39 @@ void NotificationTemplateBuilder::WriteTextElement(const std::string& id,
xml_writer_->AppendElementContent(content);
xml_writer_->EndElement();
}
void NotificationTemplateBuilder::AddActions(
const std::vector<message_center::ButtonInfo>& buttons) {
if (!buttons.size())
return;
StartActionsElement();
// TODO(finnur): Add inline replies.
for (size_t i = 0; i < buttons.size(); ++i) {
const auto& button = buttons[i];
WriteActionElement(button, i);
}
EndActionsElement();
}
void NotificationTemplateBuilder::StartActionsElement() {
xml_writer_->StartElement(kActionsElement);
}
void NotificationTemplateBuilder::EndActionsElement() {
xml_writer_->EndElement();
}
void NotificationTemplateBuilder::WriteActionElement(
const message_center::ButtonInfo& button,
int index) {
// TODO(finnur): Implement button images (imageUri).
xml_writer_->StartElement(kActionElement);
xml_writer_->AddAttribute(kActivationType, kForeground);
xml_writer_->AddAttribute(kContent, base::UTF16ToUTF8(button.title).c_str());
std::string param = std::string(kButtonIndex) + base::IntToString(index);
xml_writer_->AddAttribute(kArguments, param.c_str());
xml_writer_->EndElement();
}
......@@ -7,6 +7,7 @@
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/strings/string16.h"
......@@ -15,6 +16,7 @@ class GURL;
class XmlWriter;
namespace message_center {
struct ButtonInfo;
class Notification;
}
......@@ -68,6 +70,15 @@ class NotificationTemplateBuilder {
// https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-text
void WriteTextElement(const std::string& id, const std::string& content);
// Writes the <actions> element.
// TODO(finnur): Add link to doc, once it becomes available.
void StartActionsElement();
void EndActionsElement();
// Fills in the details for the actions.
void AddActions(const std::vector<message_center::ButtonInfo>& buttons);
void WriteActionElement(const message_center::ButtonInfo& button, int index);
// The XML writer to which the template will be written.
std::unique_ptr<XmlWriter> xml_writer_;
......
......@@ -52,6 +52,7 @@ class NotificationTemplateBuilderTest : public ::testing::Test {
// the given |notification_data|, and writes that to |*xml_template|. Calls
// must be wrapped in ASSERT_NO_FATAL_FAILURE().
void BuildTemplate(const NotificationData& notification_data,
const std::vector<message_center::ButtonInfo>& buttons,
base::string16* xml_template) {
GURL origin_url(notification_data.origin);
......@@ -61,6 +62,8 @@ class NotificationTemplateBuilderTest : public ::testing::Test {
base::UTF8ToUTF16(notification_data.message), gfx::Image() /* icon */,
base::string16() /* display_source */, origin_url,
NotifierId(origin_url), RichNotificationData(), nullptr /* delegate */);
if (buttons.size())
notification.set_buttons(buttons);
template_ =
NotificationTemplateBuilder::Build(notification_data.id, notification);
......@@ -79,13 +82,15 @@ class NotificationTemplateBuilderTest : public ::testing::Test {
TEST_F(NotificationTemplateBuilderTest, SimpleToast) {
NotificationData notification_data;
base::string16 xml_template;
std::vector<message_center::ButtonInfo> buttons;
ASSERT_NO_FATAL_FAILURE(BuildTemplate(notification_data, &xml_template));
ASSERT_NO_FATAL_FAILURE(
BuildTemplate(notification_data, buttons, &xml_template));
const wchar_t kExpectedXml[] =
LR"(<toast launch="notification_id">
<visual>
<binding template="ToastText04">
<binding template="ToastGeneric">
<text id="1">My Title</text>
<text id="2">My Message</text>
<text id="3">example.com</text>
......@@ -96,3 +101,33 @@ TEST_F(NotificationTemplateBuilderTest, SimpleToast) {
EXPECT_EQ(xml_template, kExpectedXml);
}
TEST_F(NotificationTemplateBuilderTest, Buttons) {
NotificationData notification_data;
base::string16 xml_template;
std::vector<message_center::ButtonInfo> buttons;
buttons.emplace_back(base::ASCIIToUTF16("Button1"));
buttons.emplace_back(base::ASCIIToUTF16("Button2"));
ASSERT_NO_FATAL_FAILURE(
BuildTemplate(notification_data, buttons, &xml_template));
const wchar_t kExpectedXml[] =
LR"(<toast launch="notification_id">
<visual>
<binding template="ToastGeneric">
<text id="1">My Title</text>
<text id="2">My Message</text>
<text id="3">example.com</text>
</binding>
</visual>
<actions>
<action activationType="foreground" content="Button1" arguments="buttonIndex=0"/>
<action activationType="foreground" content="Button2" arguments="buttonIndex=1"/>
</actions>
</toast>
)";
EXPECT_EQ(xml_template, kExpectedXml);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment