tests: Test `gears.watcher`.
This commit is contained in:
parent
9d55d385c7
commit
b28a84fbc3
|
@ -0,0 +1,18 @@
|
|||
--DOC_NO_USAGE --DOC_GEN_OUTPUT
|
||||
local gears = { watcher = require("gears.watcher") } --DOC_HIDE
|
||||
|
||||
function gears.watcher._easy_async(_, cb) --DOC_HIDE
|
||||
cb("one two three", "", nil, 0) --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
|
||||
local w = gears.watcher {
|
||||
interval = 0.05,
|
||||
command = "echo one two three",
|
||||
}
|
||||
|
||||
--DOC_NEWLINE
|
||||
-- [wait some time]
|
||||
|
||||
--DOC_NEWLINE
|
||||
-- It will be "one two three"
|
||||
print(w.value)
|
|
@ -0,0 +1,19 @@
|
|||
--DOC_NO_USAGE --DOC_GEN_OUTPUT
|
||||
local gears = { watcher = require("gears.watcher") } --DOC_HIDE
|
||||
|
||||
function gears.watcher._easy_async(_, cb) --DOC_HIDE
|
||||
cb("three", "", nil, 0) --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
|
||||
local w = gears.watcher {
|
||||
interval = 0.05,
|
||||
command = "echo one two three | cut -f3 -d ' '",
|
||||
shell = true,
|
||||
}
|
||||
|
||||
--DOC_NEWLINE
|
||||
-- [wait some time]
|
||||
|
||||
--DOC_NEWLINE
|
||||
-- It will be "one two three"
|
||||
print(w.value)
|
|
@ -0,0 +1,26 @@
|
|||
--DOC_GEN_IMAGE --DOC_NO_USAGE
|
||||
local gears = { watcher = require("gears.watcher") } --DOC_HIDE
|
||||
|
||||
function gears.watcher._read_async(path, callback, _) --DOC_HIDE
|
||||
if path == '/sys/class/power_supply/AC/online' then --DOC_HIDE
|
||||
callback('/sys/class/power_supply/AC/online', "1") --DOC_HIDE
|
||||
elseif path == "/sys/class/power_supply/BAT0/status" then --DOC_HIDE
|
||||
callback('/sys/class/power_supply/BAT0/status', "Full") --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
|
||||
local mybatterycharging = gears.watcher {
|
||||
files = {
|
||||
"/sys/class/power_supply/AC/online",
|
||||
"/sys/class/power_supply/BAT0/status"
|
||||
},
|
||||
filter = function(content)
|
||||
assert(content['/sys/class/power_supply/AC/online'] == "1") --DOC_HIDE
|
||||
assert(content['/sys/class/power_supply/BAT0/status'] == "Full") --DOC_HIDE
|
||||
return content['/sys/class/power_supply/AC/online' ] == "1"
|
||||
or content['/sys/class/power_supply/BAT0/status'] == "Full"
|
||||
end,
|
||||
interval = 5,
|
||||
}
|
||||
|
||||
assert(mybatterycharging) --DOC_HIDE
|
|
@ -0,0 +1,34 @@
|
|||
--DOC_GEN_IMAGE --DOC_NO_USAGE
|
||||
local gears = { watcher = require("gears.watcher") } --DOC_HIDE
|
||||
|
||||
local called = 0 --DOC_HIDE
|
||||
|
||||
function gears.watcher._read_async(path, callback) --DOC_HIDE
|
||||
if path == '/sys/class/power_supply/AC/online' then --DOC_HIDE
|
||||
called = called + 1 --DOC_HIDE
|
||||
callback('/sys/class/power_supply/AC/online', "1") --DOC_HIDE
|
||||
elseif path == "/sys/class/power_supply/BAT0/status" then --DOC_HIDE
|
||||
called = called + 1 --DOC_HIDE
|
||||
callback('/sys/class/power_supply/BAT0/status', "Full") --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
|
||||
local mybatterycharging = gears.watcher {
|
||||
files = {
|
||||
plugged = "/sys/class/power_supply/AC/online",
|
||||
charged = "/sys/class/power_supply/BAT0/status"
|
||||
},
|
||||
filter = function(content)
|
||||
assert(content.plugged == "1") --DOC_HIDE
|
||||
assert(content.charged == "Full") --DOC_HIDE
|
||||
return content.plugged == "1" or content.charged == "Full"
|
||||
end,
|
||||
interval = 5,
|
||||
}
|
||||
|
||||
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
|
||||
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
|
||||
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
|
||||
|
||||
assert(called >= 2) --DOC_HIDE
|
||||
assert(mybatterycharging) --DOC_HIDE
|
|
@ -0,0 +1,14 @@
|
|||
--DOC_GEN_IMAGE
|
||||
local gears = { watcher = require("gears.watcher") } --DOC_HIDE
|
||||
|
||||
function gears.watcher._easy_async(_, callback) --DOC_HIDE
|
||||
callback("three") --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
|
||||
local watcher = gears.watcher {
|
||||
interval = 5,
|
||||
command = "echo one:two:three | cut -f3 -d :",
|
||||
shell = true,
|
||||
}
|
||||
|
||||
assert(watcher) --DOC_HIDE
|
|
@ -0,0 +1,9 @@
|
|||
--DOC_GEN_IMAGE
|
||||
local gears = { watcher = require("gears.watcher") } --DOC_HIDE
|
||||
|
||||
local mybatterydischarging = gears.watcher {
|
||||
file = "/sys/class/power_supply/BAT0/status",
|
||||
interval = 5,
|
||||
}
|
||||
|
||||
assert(mybatterydischarging) --DOC_HIDE
|
|
@ -0,0 +1,51 @@
|
|||
--DOC_GEN_IMAGE --DOC_NO_USAGE
|
||||
local parent = ... --DOC_HIDE
|
||||
local gears = { --DOC_HIDE
|
||||
watcher = require("gears.watcher"), --DOC_HIDE
|
||||
reactive = require("gears.reactive"), --DOC_HIDE
|
||||
} --DOC_HIDE
|
||||
local wibox = require("wibox") --DOC_HIDE
|
||||
|
||||
local called = {} --DOC_HIDE
|
||||
|
||||
function gears.watcher._read_async(path, callback, _) --DOC_HIDE
|
||||
if path == '/sys/class/power_supply/BAT0/energy_full' then --DOC_HIDE
|
||||
callback('/sys/class/power_supply/BAT0/energy_full', "89470000") --DOC_HIDE
|
||||
elseif path == '/sys/class/power_supply/BAT0/energy_now' then --DOC_HIDE
|
||||
callback('/sys/class/power_supply/BAT0/energy_now', "85090000") --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
|
||||
|
||||
local battery = gears.watcher {
|
||||
labels_as_properties = true,
|
||||
interval = 5,
|
||||
files = {
|
||||
capacity = "/sys/class/power_supply/BAT0/energy_full",
|
||||
current = "/sys/class/power_supply/BAT0/energy_now"
|
||||
},
|
||||
filter = function(label, content)
|
||||
called[label] = true --DOC_HIDE
|
||||
return tonumber(content)
|
||||
end,
|
||||
}
|
||||
|
||||
--DOC_NEWLINE
|
||||
|
||||
-- Use the above in a widget.
|
||||
local w = wibox.widget {
|
||||
text = gears.reactive(function()
|
||||
return (battery.current / battery.capacity) .. "%"
|
||||
end),
|
||||
widget = wibox.widget.textbox
|
||||
}
|
||||
|
||||
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
|
||||
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
|
||||
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
|
||||
|
||||
assert(called["capacity"]) --DOC_HIDE
|
||||
assert(called["current"]) --DOC_HIDE
|
||||
assert(battery) --DOC_HIDE
|
||||
|
||||
parent:add(w) --DOC_HIDE
|
|
@ -0,0 +1,41 @@
|
|||
--DOC_GEN_IMAGE --DOC_NO_USAGE --DOC_NO_DASH
|
||||
local parent = ... --DOC_NO_USAGE --DOC_HIDE
|
||||
local gears = { watcher = require("gears.watcher") } --DOC_HIDE
|
||||
local wibox = { widget = require("wibox.widget") }--DOC_HIDE
|
||||
|
||||
function gears.watcher._read_async(path, callback, _) --DOC_HIDE
|
||||
if path == '/sys/class/power_supply/BAT0/energy_full' then --DOC_HIDE
|
||||
callback('/sys/class/power_supply/BAT0/energy_full', "89470000") --DOC_HIDE
|
||||
elseif path == '/sys/class/power_supply/BAT0/energy_now' then --DOC_HIDE
|
||||
callback('/sys/class/power_supply/BAT0/energy_now', "85090000") --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
end --DOC_HIDE
|
||||
|
||||
-- In this example, the value of energy_full is 89470000 and the value
|
||||
-- of energy_now is 85090000. The result will be 95%.
|
||||
local w = --DOC_HIDE
|
||||
wibox.widget {
|
||||
value = gears.watcher {
|
||||
files = {
|
||||
capacity = "/sys/class/power_supply/BAT0/energy_full",
|
||||
current = "/sys/class/power_supply/BAT0/energy_now"
|
||||
},
|
||||
filter = function(content)
|
||||
return tonumber(content.current) / tonumber(content.capacity)
|
||||
end,
|
||||
interval = 5,
|
||||
},
|
||||
forced_width = 100, --DOC_HIDE
|
||||
forced_height = 30, --DOC_HIDE
|
||||
max_value = 1,
|
||||
min_value = 0,
|
||||
widget = wibox.widget.progressbar
|
||||
}
|
||||
|
||||
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
|
||||
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
|
||||
require("gears.timer").run_delayed_calls_now() --DOC_HIDE
|
||||
assert(w and w.value >= 0.94 and w.value <= 0.96) --DOC_HIDE
|
||||
|
||||
parent:add(w) --DOC_HIDE
|
||||
|
|
@ -0,0 +1,181 @@
|
|||
--- Test for gears.watcher.
|
||||
|
||||
local runner = require("_runner")
|
||||
local watcher = require("gears.watcher")
|
||||
local gtimer = require("gears.timer")
|
||||
local gdebug = require("gears.debug")
|
||||
local filesystem = require("gears.filesystem")
|
||||
local util = require("awful.util")
|
||||
|
||||
local obj1 = nil
|
||||
|
||||
local sig_count = {}
|
||||
|
||||
local steps = {
|
||||
function()
|
||||
obj1 = watcher {
|
||||
interval = 0.05,
|
||||
command = "echo one two three | cut -f3 -d ' '",
|
||||
shell = true,
|
||||
initial_value = 1337
|
||||
}
|
||||
|
||||
assert(obj1.command == "echo one two three | cut -f3 -d ' '")
|
||||
assert(obj1.shell)
|
||||
assert(obj1.interval == 0.05)
|
||||
assert(obj1.value == 1337)
|
||||
|
||||
return true
|
||||
end,
|
||||
-- Test the filters.
|
||||
function()
|
||||
if obj1.value ~= "three" then return end
|
||||
|
||||
obj1.filter = function(lines)
|
||||
if lines == "three" then return "four" end
|
||||
end
|
||||
|
||||
return true
|
||||
end,
|
||||
-- Switch to file mode.
|
||||
function()
|
||||
if obj1.value ~= "four" then return end
|
||||
|
||||
obj1.filter = function(lines)
|
||||
return lines:match("theme") ~= nil
|
||||
end
|
||||
|
||||
obj1:connect_signal("file::acquired", function()
|
||||
sig_count["file::acquired"] = sig_count["file::acquired"] or 0
|
||||
sig_count["file::acquired"] = sig_count["file::acquired"] + 1
|
||||
end)
|
||||
|
||||
obj1:connect_signal("files::acquired", function()
|
||||
sig_count["files::acquired"] = sig_count["files::acquired"] or 0
|
||||
sig_count["files::acquired"] = sig_count["files::acquired"] + 1
|
||||
end)
|
||||
|
||||
obj1:connect_signal("file::failed", function()
|
||||
sig_count["file::failed"] = sig_count["file::failed"] or 0
|
||||
sig_count["file::failed"] = sig_count["file::failed"] + 1
|
||||
end)
|
||||
|
||||
obj1.file = filesystem.get_themes_dir().."/default/theme.lua"
|
||||
assert(obj1.file == filesystem.get_themes_dir().."/default/theme.lua")
|
||||
|
||||
os.execute("echo 12345 > /tmp/test_gears_watcher1")
|
||||
os.execute("echo 67890 > /tmp/test_gears_watcher2")
|
||||
os.execute("rm -rf /tmp/test_gears_watcher3")
|
||||
|
||||
return true
|
||||
end,
|
||||
-- Remove the filter.
|
||||
function()
|
||||
if obj1.value ~= true then return end
|
||||
|
||||
assert(sig_count["file::acquired"] > 0)
|
||||
assert(sig_count["files::acquired"] > 0)
|
||||
assert(not sig_count["file::failed"])
|
||||
|
||||
obj1.file = '/tmp/test_gears_watcher1'
|
||||
obj1.filter = nil
|
||||
|
||||
return true
|
||||
end,
|
||||
-- Test strip_newline.
|
||||
function()
|
||||
if obj1.value ~= "12345" then return end
|
||||
|
||||
obj1.strip_newline = false
|
||||
|
||||
return true
|
||||
end,
|
||||
-- Test append_file.
|
||||
function()
|
||||
if obj1.value ~= "12345\n" then return end
|
||||
|
||||
obj1.strip_newline = true
|
||||
obj1:append_file('/tmp/test_gears_watcher2', 'foo')
|
||||
|
||||
return true
|
||||
end,
|
||||
-- Test remove_file.
|
||||
function()
|
||||
if type(obj1.value) ~= "table" or obj1.value["foo"] ~= "67890" then return end
|
||||
|
||||
return true
|
||||
end,
|
||||
-- Test non-existant files.
|
||||
function()
|
||||
obj1.file = '/tmp/test_gears_watcher3'
|
||||
|
||||
return true
|
||||
end,
|
||||
-- Test `started` and `:stop()`
|
||||
function()
|
||||
if not sig_count["file::failed"] then return end
|
||||
|
||||
assert(obj1.started)
|
||||
obj1.started = false
|
||||
assert(not obj1.started)
|
||||
|
||||
local real_error = gdebug.print_error
|
||||
|
||||
local called = false
|
||||
|
||||
gdebug.print_error = function()
|
||||
called = true
|
||||
end
|
||||
|
||||
obj1:stop()
|
||||
assert(not obj1.started)
|
||||
assert(called)
|
||||
called = false
|
||||
|
||||
obj1:start()
|
||||
assert(obj1.started)
|
||||
assert(not called)
|
||||
obj1:stop()
|
||||
assert(not obj1.started)
|
||||
assert(not called)
|
||||
|
||||
gdebug.print_error = real_error
|
||||
|
||||
return true
|
||||
end,
|
||||
-- Test `awful.util.shell` and `gears.timer` compatibility mode.
|
||||
function()
|
||||
assert(util.shell == watcher._shell)
|
||||
watcher._shell = "foo"
|
||||
assert(util.shell == "foo")
|
||||
util.shell = "bar"
|
||||
assert(watcher._shell == "bar")
|
||||
|
||||
local called = false
|
||||
|
||||
local real_deprecate = gdebug.deprecate
|
||||
gdebug.deprecate = function()
|
||||
called = true
|
||||
end
|
||||
|
||||
local t = gtimer {
|
||||
timeout = 5
|
||||
}
|
||||
|
||||
assert(t.data.timeout)
|
||||
assert(called)
|
||||
called = false
|
||||
t.data.foo = "bar"
|
||||
assert(t._private.foo == "bar")
|
||||
assert(called)
|
||||
t._private.foo = "baz"
|
||||
assert(t.data.foo == "baz")
|
||||
|
||||
gdebug.deprecate = real_deprecate
|
||||
|
||||
return true
|
||||
end,
|
||||
}
|
||||
runner.run_steps(steps)
|
||||
|
||||
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
Loading…
Reference in New Issue