From 2d477383ee9a7258fb536b7e0d3d0bc0ef386602 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sun, 18 Aug 2019 01:53:14 -0400 Subject: [PATCH] tests: Test the notification rules. --- .../wibox/nwidget/rules/add_actions.lua | 61 +++++++++++++ .../wibox/nwidget/rules/has_class.lua | 29 ++++++ .../wibox/nwidget/rules/has_focus.lua | 24 +++++ .../wibox/nwidget/rules/has_instance.lua | 30 ++++++ .../examples/wibox/nwidget/rules/urgency.lua | 60 ++++++++++++ .../wibox/nwidget/rules/widget_template.lua | 91 +++++++++++++++++++ 6 files changed, 295 insertions(+) create mode 100644 tests/examples/wibox/nwidget/rules/add_actions.lua create mode 100644 tests/examples/wibox/nwidget/rules/has_class.lua create mode 100644 tests/examples/wibox/nwidget/rules/has_focus.lua create mode 100644 tests/examples/wibox/nwidget/rules/has_instance.lua create mode 100644 tests/examples/wibox/nwidget/rules/urgency.lua create mode 100644 tests/examples/wibox/nwidget/rules/widget_template.lua diff --git a/tests/examples/wibox/nwidget/rules/add_actions.lua b/tests/examples/wibox/nwidget/rules/add_actions.lua new file mode 100644 index 000000000..9f7835554 --- /dev/null +++ b/tests/examples/wibox/nwidget/rules/add_actions.lua @@ -0,0 +1,61 @@ +--DOC_GEN_IMAGE --DOC_NO_USAGE +local parent = ... --DOC_HIDE +local naughty = require("naughty") --DOC_HIDE +local ruled = {notification = require("ruled.notification")}--DOC_HIDE +local wibox = require("wibox") --DOC_HIDE +local beautiful = require("beautiful") --DOC_HIDE +local def = require("naughty.widget._default") --DOC_HIDE +local acommon = require("awful.widget.common") --DOC_HIDE + +beautiful.notification_bg = beautiful.bg_normal --DOC_HIDE + + ruled.notification.connect_signal("request::rules", function() + -- Add a red background for urgent notifications. + ruled.notification.append_rule { + rule = { }, -- Match everything. + properties = { + append_actions = { + naughty.action { + name = "Snooze (5m)", + }, + naughty.action { + name = "Snooze (15m)", + }, + naughty.action { + name = "Snooze (1h)", + }, + }, + } + } + end) + +awesome.emit_signal("startup") --DOC_HIDE + +--DOC_NEWLINE + + -- Create a normal notification. +local notif = --DOC_HIDE + naughty.notification { + title = "A notification", + message = "This is very informative", + icon = beautiful.awesome_icon, + actions = { + naughty.action { name = "Existing 1" }, + naughty.action { name = "Existing 2" }, + } + } + +local function show_notification(n) --DOC_HIDE + local default = wibox.widget(def) --DOC_HIDE + acommon._set_common_property(default, "notification", n) --DOC_HIDE + + local w, h = default:fit({dpi=96}, 9999, 9999) --DOC_HIDE + default.forced_width = w + 250 --DOC_HIDE + default.forced_height = h --DOC_HIDE + parent.forced_width = w + 250 --DOC_HIDE + + parent:add(default) --DOC_HIDE +end --DOC_HIDE + +parent.spacing = 10 --DOC_HIDE +show_notification(notif) --DOC_HIDE diff --git a/tests/examples/wibox/nwidget/rules/has_class.lua b/tests/examples/wibox/nwidget/rules/has_class.lua new file mode 100644 index 000000000..8f7728ec1 --- /dev/null +++ b/tests/examples/wibox/nwidget/rules/has_class.lua @@ -0,0 +1,29 @@ +local naughty = require("naughty") --DOC_HIDE +local ruled = {notification = require("ruled.notification")}--DOC_HIDE + +local function get_mpris_actions() end --DOC_HIDE +local my_music_widget_template = nil --DOC_HIDE + +ruled.notification.connect_signal("request::rules", function() --DOC_HIDE + ruled.notification.append_rule { + rule = { has_class = "amarok" }, + properties = { + widget_template = my_music_widget_template, + actions = get_mpris_actions(), + works = true --DOC_HIDE + } + } +end) --DOC_HIDE + +awesome.emit_signal("startup") --DOC_HIDE + +local c = client.gen_fake { class = "amarok" } --DOC_HIDE +client.focus = c --DOC_HIDE +assert(client.focus == c) --DOC_HIDE + +local notif = naughty.notification { --DOC_HIDE + message = "I am SPAM", --DOC_HIDE + clients = {c}, --DOC_HIDE note that this is undocumented +} --DOC_HIDE + +assert(notif.works) --DOC_HIDE diff --git a/tests/examples/wibox/nwidget/rules/has_focus.lua b/tests/examples/wibox/nwidget/rules/has_focus.lua new file mode 100644 index 000000000..6a9b05c5e --- /dev/null +++ b/tests/examples/wibox/nwidget/rules/has_focus.lua @@ -0,0 +1,24 @@ +local naughty = require("naughty") --DOC_HIDE +local ruled = {notification = require("ruled.notification")}--DOC_HIDE + +ruled.notification.connect_signal("request::rules", function() --DOC_HIDE + -- Note that the the message is matched as a pattern. + ruled.notification.append_rule { + rule = { message = "I am SPAM", has_focus = true }, + properties = { ignore = true} + } +end) --DOC_HIDE + +awesome.emit_signal("startup") --DOC_HIDE + +local c = client.gen_fake { class = "xchat" } --DOC_HIDE +client.focus = c --DOC_HIDE +assert(client.focus == c) --DOC_HIDE + +local notif = naughty.notification { --DOC_HIDE + message = "I am SPAM", --DOC_HIDE + clients = {c}, --DOC_HIDE note that this is undocumented +} --DOC_HIDE + +assert(#notif.clients == 1 and notif.clients[1] == client.focus) --DOC_HIDE +assert(notif.ignore) --DOC_HIDE diff --git a/tests/examples/wibox/nwidget/rules/has_instance.lua b/tests/examples/wibox/nwidget/rules/has_instance.lua new file mode 100644 index 000000000..e52945479 --- /dev/null +++ b/tests/examples/wibox/nwidget/rules/has_instance.lua @@ -0,0 +1,30 @@ +local naughty = require("naughty") --DOC_HIDE +local ruled = {notification = require("ruled.notification")}--DOC_HIDE + +local function get_mpris_actions() end --DOC_HIDE +local my_music_widget_template = nil --DOC_HIDE + +ruled.notification.connect_signal("request::rules", function() --DOC_HIDE + ruled.notification.append_rule { + rule = { has_instance = "amarok" }, + properties = { + widget_template = my_music_widget_template, + actions = get_mpris_actions(), + works = true --DOC_HIDE + } + } +end) --DOC_HIDE + +awesome.emit_signal("startup") --DOC_HIDE + +local c = client.gen_fake { instance = "amarok" } --DOC_HIDE +assert(c.instance == "amarok") --DOC_HIDE +client.focus = c --DOC_HIDE +assert(client.focus == c) --DOC_HIDE + +local notif = naughty.notification { --DOC_HIDE + message = "I am SPAM", --DOC_HIDE + clients = {c}, --DOC_HIDE note that this is undocumented +} --DOC_HIDE + +assert(notif.works) --DOC_HIDE diff --git a/tests/examples/wibox/nwidget/rules/urgency.lua b/tests/examples/wibox/nwidget/rules/urgency.lua new file mode 100644 index 000000000..45aeb48eb --- /dev/null +++ b/tests/examples/wibox/nwidget/rules/urgency.lua @@ -0,0 +1,60 @@ +--DOC_GEN_IMAGE --DOC_NO_USAGE +local parent = ... --DOC_HIDE +local naughty = require("naughty") --DOC_HIDE +local ruled = {notification = require("ruled.notification")}--DOC_HIDE +local wibox = require("wibox") --DOC_HIDE +local beautiful = require("beautiful") --DOC_HIDE +local def = require("naughty.widget._default") --DOC_HIDE +local acommon = require("awful.widget.common") --DOC_HIDE + +beautiful.notification_bg = beautiful.bg_normal --DOC_HIDE + + ruled.notification.connect_signal("request::rules", function() + -- Add a red background for urgent notifications. + ruled.notification.append_rule { + rule = { urgency = "critical" }, + properties = { bg = "#ff0000", fg = "#ffffff", timeout = 0 } + } + + --DOC_NEWLINE + + -- Or green background for normal ones. + ruled.notification.append_rule { + rule = { urgency = "normal" }, + properties = { bg = "#00ff00", fg = "#000000"} + } + end) + +awesome.emit_signal("startup") --DOC_HIDE + +--DOC_NEWLINE + + -- Create a normal notification. +local notif = --DOC_HIDE + naughty.notification { + title = "A notification 1", + message = "This is very informative", + icon = beautiful.awesome_icon, + urgency = "normal", + } + +--DOC_NEWLINE + + -- Create a normal notification. +local notif2 = --DOC_HIDE + naughty.notification { + title = "A notification 2", + message = "This is very informative", + icon = beautiful.awesome_icon, + urgency = "critical", + } + +local function show_notification(n) --DOC_HIDE + local default = wibox.widget(def) --DOC_HIDE + acommon._set_common_property(default, "notification", n) --DOC_HIDE + parent:add(default) --DOC_HIDE +end --DOC_HIDE + +parent.spacing = 10 --DOC_HIDE +show_notification(notif) --DOC_HIDE +show_notification(notif2) --DOC_HIDE diff --git a/tests/examples/wibox/nwidget/rules/widget_template.lua b/tests/examples/wibox/nwidget/rules/widget_template.lua new file mode 100644 index 000000000..44315ca91 --- /dev/null +++ b/tests/examples/wibox/nwidget/rules/widget_template.lua @@ -0,0 +1,91 @@ +--DOC_GEN_IMAGE --DOC_NO_USAGE +local parent = ... --DOC_HIDE +local naughty = require("naughty") --DOC_HIDE +local ruled = {notification = require("ruled.notification")}--DOC_HIDE +local wibox = require("wibox") --DOC_HIDE +local beautiful = require("beautiful") --DOC_HIDE + +local box = nil --DOC_HIDE +beautiful.notification_bg = beautiful.bg_normal --DOC_HIDE + + ruled.notification.connect_signal("request::rules", function() + -- Add a red background for urgent notifications. + ruled.notification.append_rule { + rule = { app_name = "mdp" }, + properties = { + widget_template = { + { + { + { + { + naughty.widget.icon, + forced_height = 48, + halign = "center", + valign = "center", + widget = wibox.container.place + }, + { + align = "center", + widget = naughty.widget.title, + }, + { + align = "center", + widget = naughty.widget.message, + }, + { + orientation = "horizontal", + widget = wibox.widget.separator, + forced_height = 1, + }, + { + nil, + { + wibox.widget.textbox "⏪", + wibox.widget.textbox "⏸", + wibox.widget.textbox "⏩", + spacing = 20, + layout = wibox.layout.fixed.horizontal, + }, + expand = "outside", + nil, + layout = wibox.layout.align.horizontal, + }, + spacing = 10, + layout = wibox.layout.fixed.vertical, + }, + margins = beautiful.notification_margin, + widget = wibox.container.margin, + }, + id = "background_role", + widget = naughty.container.background, + }, + strategy = "max", + width = 160, + widget = wibox.container.constraint, + } + } + } + end) + + naughty.connect_signal("request::display", function(n) + box = --DOC_HIDE + naughty.layout.box { notification = n } + end) + +awesome.emit_signal("startup") --DOC_HIDE + +local notif2 = --DOC_HIDE + naughty.notification { --DOC_HIDE + title = "Daft Punk", --DOC_HIDE + message = "Harder, Better, Faster, Stronger", --DOC_HIDE + icon = beautiful.awesome_icon, + icon_size = 128, --DOC_HIDE + app_name = "mdp", --DOC_HIDE + } --DOC_HIDE + +assert(notif2.app_name == "mdp") --DOC_HIDE +assert(box) --DOC_HIDE + +box.widget.forced_width = 250 --DOC_HIDE + +parent:add(box.widget) --DOC_HIDE