From c9f8690a60afa345840f5cb4fe5800469731aba2 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sat, 19 Mar 2016 04:57:37 -0400 Subject: [PATCH] tests: Add some shims to emulate CAPI without an X server --- tests/examples/shims/awesome.lua | 60 ++++++ tests/examples/shims/beautiful.lua | 24 +++ tests/examples/shims/beautiful/xresources.lua | 8 + tests/examples/shims/client.lua | 171 ++++++++++++++++++ tests/examples/shims/mouse.lua | 27 +++ tests/examples/shims/root.lua | 9 + tests/examples/shims/screen.lua | 101 +++++++++++ tests/examples/shims/tag.lua | 53 ++++++ 8 files changed, 453 insertions(+) create mode 100644 tests/examples/shims/awesome.lua create mode 100644 tests/examples/shims/beautiful.lua create mode 100644 tests/examples/shims/beautiful/xresources.lua create mode 100644 tests/examples/shims/client.lua create mode 100644 tests/examples/shims/mouse.lua create mode 100644 tests/examples/shims/root.lua create mode 100644 tests/examples/shims/screen.lua create mode 100644 tests/examples/shims/tag.lua diff --git a/tests/examples/shims/awesome.lua b/tests/examples/shims/awesome.lua new file mode 100644 index 000000000..30c2c39b4 --- /dev/null +++ b/tests/examples/shims/awesome.lua @@ -0,0 +1,60 @@ +local gears_obj = require("gears.object") + +-- Emulate the C API classes. They differ from C API objects as connect_signal +-- doesn't take an object as first argument and they support fallback properties +-- handlers. +local function _shim_fake_class() + local obj = gears_obj() + + local meta = { + __index = function()end, + __new_index = function()end, + } + + obj._connect_signal = obj.connect_signal + + function obj.connect_signal(name, func) + return obj._connect_signal(obj, name, func) + end + + obj._add_signal = obj.add_signal + + function obj.add_signal(name) + return obj._add_signal(obj, name) + end + + function obj.set_index_miss_handler(handler) + meta.__index = handler + end + + function obj.set_newindex_miss_handler(handler) + meta.__new_index = handler + end + + function obj.emit_signal(name, c, ...) + local conns = obj._signals[name] or {strong={}} + for func in pairs(conns.strong) do + func(c, ...) + end + end + + return obj, meta +end + +local awesome = _shim_fake_class() +awesome._shim_fake_class = _shim_fake_class + +-- Avoid c.screen = acreen.focused() to be called, all tests will fail +awesome.startup = true + +function awesome.register_xproperty() +end + +awesome.add_signal("refresh") +awesome.add_signal("wallpaper_changed") +awesome.add_signal("spawn::canceled") +awesome.add_signal("spawn::timeout") + +return awesome + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/examples/shims/beautiful.lua b/tests/examples/shims/beautiful.lua new file mode 100644 index 000000000..26349e97b --- /dev/null +++ b/tests/examples/shims/beautiful.lua @@ -0,0 +1,24 @@ +local lgi = require("lgi") +local Pango = lgi.Pango + +-- Default theme for the documentation examples +local module = { + fg_normal = "#000000" , + bg_normal = "#6181FF7D", + bg_highlight = "#AA00FF7D", + border_color = "#6181FF" , + border_width = 1.5 , + + -- Fake resources handling + xresources = require("beautiful.xresources") +} + +local f = Pango.FontDescription.from_string("sans 8") + +function module.get_font() + return f +end + +return module + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/examples/shims/beautiful/xresources.lua b/tests/examples/shims/beautiful/xresources.lua new file mode 100644 index 000000000..70e130b7c --- /dev/null +++ b/tests/examples/shims/beautiful/xresources.lua @@ -0,0 +1,8 @@ +return { + apply_dpi = function(size, _) + return size + end, + get_dpi = function() return 96 end, +} + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/examples/shims/client.lua b/tests/examples/shims/client.lua new file mode 100644 index 000000000..f4f78d1db --- /dev/null +++ b/tests/examples/shims/client.lua @@ -0,0 +1,171 @@ +local gears_obj = require("gears.object") + +local clients = {} + +local client = awesome._shim_fake_class() + +local function add_signals(c) + c:add_signal("property::width") + c:add_signal("property::height") + c:add_signal("property::x") + c:add_signal("property::y") + c:add_signal("property::screen") + c:add_signal("property::geometry") + c:add_signal("request::geometry") + c:add_signal("swapped") + c:add_signal("raised") + c:add_signal("property::_label") --Used internally +end + +-- Keep an history of the geometry for validation and images +local function push_geometry(c) + table.insert(c._old_geo, c:geometry()) +end + +-- Undo the last move, but keep it in history +-- local function pop_geometry(c) +-- CURRENTLY UNUSED +-- end + +-- Create fake clients to move around +function client.gen_fake(args) + local ret = gears_obj() + ret.type = "normal" + ret.valid = true + ret.size_hints = {} + ret.border_width = 1 + + -- Apply all properties + for k,v in pairs(args or {}) do + ret[k] = v + end + + -- Tests should always set a geometry, but just in case + for _, v in ipairs{"x","y","width","height"} do + ret[v] = ret[v] or 1 + end + + + add_signals(ret) + + -- Emulate capi.client.geometry + function ret:geometry(new) + if new then + for k,v in pairs(new) do + ret[k] = v + ret:emit_signal("property::"..k, v) + end + ret:emit_signal("property::geometry", ret:geometry()) + push_geometry(ret) + end + + return { + x = ret.x, + y = ret.y, + width = ret.width, + height = ret.height, + label = ret._label, + } + end + + function ret:isvisible() + return true + end + + -- Used for screenshots + function ret:set_label(text) + ret._old_geo[#ret._old_geo]._label = text + end + + -- Used for screenshots, hide the current client position + function ret:_hide() + ret._old_geo[#ret._old_geo]._hide = true + end + + function ret:get_xproperty() + return nil + end + + function ret:tags(new) --FIXME + if new then + ret._tags = new + end + + if ret._tags then + return ret._tags + end + + for _, t in ipairs(root._tags) do + if t.screen == ret.screen then + return {t} + end + end + + return {} + end + + -- Record the geometry + ret._old_geo = {} + push_geometry(ret) + + -- Set the attributes + ret.screen = args.screen or screen[1] + + -- Add to the client list + table.insert(clients, ret) + + client.focus = ret + + client.emit_signal("manage", ret) + assert(not args.screen or (args.screen == ret.screen)) + + return ret +end + +function client.get(s) + if not s then return clients end + + local s2 = screen[s] + + local ret = {} + + for _,c in ipairs(clients) do + if c.screen == s2 then + table.insert(ret, c) + end + end + + return ret +end + +client:_add_signal("manage") +client:_add_signal("unmanage") +client:_add_signal("property::urgent") +client:_add_signal("untagged") +client:_add_signal("tagged") +client:_add_signal("property::shape_client_bounding") +client:_add_signal("property::shape_client_clip") +client:_add_signal("property::width") +client:_add_signal("property::height") +client:_add_signal("property::x") +client:_add_signal("property::y") +client:_add_signal("property::geometry") +client:_add_signal("focus") +client:_add_signal("new") +client:_add_signal("property::size_hints_honor") +client:_add_signal("property::struts") +client:_add_signal("property::minimized") +client:_add_signal("property::maximized_horizontal") +client:_add_signal("property::maximized_vertical") +client:_add_signal("property::sticky") +client:_add_signal("property::fullscreen") +client:_add_signal("property::border_width") +client:_add_signal("property::hidden") +client:_add_signal("property::screen") +client:_add_signal("raised") +client:_add_signal("lowered") +client:_add_signal("list") + +return client + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/examples/shims/mouse.lua b/tests/examples/shims/mouse.lua new file mode 100644 index 000000000..01d7bb40d --- /dev/null +++ b/tests/examples/shims/mouse.lua @@ -0,0 +1,27 @@ +local screen = require("screen") + +local coords = {x=100,y=100} + +local mouse = { + screen = screen[1], + old_histories = {}, + history = {}, +} + +function mouse.coords(args) + if args then + coords.x, coords.y = args.x, args.y + table.insert(mouse.history, {x=coords.x, y=coords.y}) + end + + return coords +end + +function mouse.push_history() + table.insert(mouse.old_histories, mouse.history) + mouse.history = {} +end + +return mouse + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/examples/shims/root.lua b/tests/examples/shims/root.lua new file mode 100644 index 000000000..dbe8754f2 --- /dev/null +++ b/tests/examples/shims/root.lua @@ -0,0 +1,9 @@ +local root = {_tags={}} + +function root:tags() + return root._tags +end + +return root + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/examples/shims/screen.lua b/tests/examples/shims/screen.lua new file mode 100644 index 000000000..d7f25860d --- /dev/null +++ b/tests/examples/shims/screen.lua @@ -0,0 +1,101 @@ +local gears_obj = require("gears.object") + +local screen = awesome._shim_fake_class() + +screen.count = 1 + +local function create_screen(args) + local s = gears_obj() + + s:add_signal("property::workarea") + s:add_signal("padding") + + -- Copy the geo in case the args are mutated + local geo = { + x = args.x , + y = args.y , + width = args.width , + height = args.height, + } + + function s._resize(args2) + geo.x = args2.x or geo.x + geo.y = args2.y or geo.y + geo.width = args2.width or geo.width + geo.height = args2.height or geo.height + end + + local wa = args.workarea_sides or 10 + + return setmetatable(s,{ __index = function(_, key) + if key == "geometry" then + return { + x = geo.x or 0, + y = geo.y or 0, + width = geo.width , + height = geo.height, + } + elseif key == "workarea" then + return { + x = (geo.x or 0) + wa , + y = (geo.y or 0) + wa , + width = geo.width - 2*wa, + height = geo.height - 2*wa, + } + end + end, + }) +end + +local screens = {} + +function screen._add_screen(args) + local s = create_screen(args) + table.insert(screens, s) + s.index = #screens + screen[#screen+1] = s + screen[s] = s +end + +function screen._get_extents() + local xmax, ymax + for _, v in ipairs(screen) do + if not xmax or v.geometry.x+v.geometry.width > xmax.geometry.x+xmax.geometry.width then + xmax = v + end + if not ymax or v.geometry.y+v.geometry.height > ymax.geometry.y+ymax.geometry.height then + ymax = v + end + end + + return xmax.geometry.x+xmax.geometry.width, ymax.geometry.y+ymax.geometry.height +end + +function screen._clear() + for i=1, #screen do + screen[screen[i]] = nil + screen[i] = nil + end + screens = {} +end + +function screen._setup_grid(w, h, rows, args) + args = args or {} + screen._clear() + for i, row in ipairs(rows) do + for j=1, row do + args.x = (j-1)*w + (j-1)*10 + args.y = (i-1)*h + (i-1)*10 + args.width = w + args.height = h + screen._add_screen(args) + end + end +end + +screen._add_screen {width=320, height=240} + +return screen + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 + diff --git a/tests/examples/shims/tag.lua b/tests/examples/shims/tag.lua new file mode 100644 index 000000000..fe10da121 --- /dev/null +++ b/tests/examples/shims/tag.lua @@ -0,0 +1,53 @@ +local gears_obj = require("gears.object") + +local tag, meta = awesome._shim_fake_class() + +local function new_tag(_, args) + local ret = gears_obj() + + ret:add_signal("property::layout") + ret:add_signal("property::name") + ret:add_signal("property::geometry") + ret:add_signal("property::screen") + ret:add_signal("property::mwfact") + ret:add_signal("property::ncol") + ret:add_signal("property::nmaster") + ret:add_signal("property::index") + ret:add_signal("property::useless_gap") + ret:add_signal("property::_wa_tracker") + + ret.name = args.name or "test" + ret.activated = true + ret.selected = true + + function ret:clients(_) --TODO handle new + local list = {} + for _, c in ipairs(client.get()) do + if c.screen == (ret.screen or screen[1]) then + table.insert(list, c) + end + end + + return list + end + + table.insert(root._tags, ret) + + return setmetatable(ret, { + __index = function(...) return meta.__index(...) end, + __new_index = function(...) return meta.__new_index(...) end + }) +end + +tag:_add_signal("request::select") +tag:_add_signal("property::selected") +tag:_add_signal("property::activated") +tag:_add_signal("tagged") +tag:_add_signal("untagged") + +return setmetatable(tag, { + __call = new_tag, +}) + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 +