2014-08-23 21:57:52 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2014 Uli Schlachter
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local object = require("gears.object")
|
|
|
|
|
|
|
|
describe("gears.object", function()
|
|
|
|
local obj
|
|
|
|
before_each(function()
|
|
|
|
obj = object()
|
|
|
|
end)
|
|
|
|
|
2015-06-14 14:21:23 +02:00
|
|
|
it("strong connecting and emitting signal", function()
|
2014-08-23 21:57:52 +02:00
|
|
|
local called = false
|
2015-06-14 14:21:23 +02:00
|
|
|
local function cb()
|
2014-08-23 21:57:52 +02:00
|
|
|
called = true
|
2015-06-14 14:21:23 +02:00
|
|
|
end
|
|
|
|
obj:connect_signal("signal", cb)
|
2014-08-23 21:57:52 +02:00
|
|
|
obj:emit_signal("signal")
|
|
|
|
assert.is_true(called)
|
|
|
|
end)
|
|
|
|
|
2015-06-14 14:21:23 +02:00
|
|
|
it("weak connecting and emitting signal", function()
|
|
|
|
local called = false
|
|
|
|
local function cb()
|
|
|
|
called = true
|
|
|
|
end
|
|
|
|
obj:weak_connect_signal("signal", cb)
|
2015-11-19 17:58:49 +01:00
|
|
|
|
|
|
|
-- Check that the GC doesn't disconnect the signal
|
2016-02-07 13:10:21 +01:00
|
|
|
for _ = 1, 10 do
|
2015-11-19 17:58:49 +01:00
|
|
|
collectgarbage("collect")
|
|
|
|
end
|
|
|
|
|
2015-06-14 14:21:23 +02:00
|
|
|
obj:emit_signal("signal")
|
|
|
|
assert.is_true(called)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("strong connecting, disconnecting and emitting signal", function()
|
2014-08-23 21:57:52 +02:00
|
|
|
local called = false
|
|
|
|
local function cb()
|
|
|
|
called = true
|
|
|
|
end
|
|
|
|
obj:connect_signal("signal", cb)
|
|
|
|
obj:disconnect_signal("signal", cb)
|
|
|
|
obj:emit_signal("signal")
|
|
|
|
assert.is_false(called)
|
|
|
|
end)
|
|
|
|
|
2015-06-14 14:21:23 +02:00
|
|
|
it("weak connecting, disconnecting and emitting signal", function()
|
|
|
|
local called = false
|
|
|
|
local function cb()
|
|
|
|
called = true
|
|
|
|
end
|
|
|
|
obj:weak_connect_signal("signal", cb)
|
|
|
|
obj:disconnect_signal("signal", cb)
|
|
|
|
obj:emit_signal("signal")
|
|
|
|
assert.is_false(called)
|
|
|
|
end)
|
|
|
|
|
2014-08-23 21:57:52 +02:00
|
|
|
it("arguments to signal", function()
|
|
|
|
obj:connect_signal("signal", function(arg1, arg2)
|
|
|
|
assert.is.equal(obj, arg1)
|
|
|
|
assert.is.same(42, arg2)
|
|
|
|
end)
|
2015-06-14 14:21:23 +02:00
|
|
|
obj:weak_connect_signal("signal", function(arg1, arg2)
|
|
|
|
assert.is.equal(obj, arg1)
|
|
|
|
assert.is.same(42, arg2)
|
|
|
|
end)
|
2014-08-23 21:57:52 +02:00
|
|
|
obj:emit_signal("signal", 42)
|
|
|
|
end)
|
2015-06-14 14:21:23 +02:00
|
|
|
|
|
|
|
it("strong non-auto disconnect", function()
|
|
|
|
local called = false
|
|
|
|
obj:connect_signal("signal", function()
|
|
|
|
called = true
|
|
|
|
end)
|
|
|
|
collectgarbage("collect")
|
|
|
|
obj:emit_signal("signal")
|
|
|
|
assert.is_true(called)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("weak auto disconnect", function()
|
|
|
|
local called = false
|
|
|
|
obj:weak_connect_signal("signal", function()
|
|
|
|
called = true
|
|
|
|
end)
|
|
|
|
collectgarbage("collect")
|
|
|
|
obj:emit_signal("signal")
|
|
|
|
assert.is_false(called)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("strong connect after weak connect", function()
|
|
|
|
local function cb() end
|
|
|
|
obj:weak_connect_signal("signal", cb)
|
|
|
|
assert.has.errors(function()
|
|
|
|
obj:connect_signal("signal", cb)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("weak connect after strong connect", function()
|
|
|
|
local function cb() end
|
|
|
|
obj:connect_signal("signal", cb)
|
|
|
|
assert.has.errors(function()
|
|
|
|
obj:weak_connect_signal("signal", cb)
|
|
|
|
end)
|
|
|
|
end)
|
2015-11-19 18:06:58 +01:00
|
|
|
|
|
|
|
it("weak signal is disconnected before __gc runs", function()
|
|
|
|
local finalized = false
|
|
|
|
do
|
|
|
|
local userdata
|
|
|
|
local function callback()
|
|
|
|
error("Signal should already be disconnected")
|
|
|
|
|
|
|
|
-- This should reference the object...
|
|
|
|
print("userdata:", userdata)
|
|
|
|
end
|
|
|
|
obj:weak_connect_signal("signal", callback)
|
|
|
|
|
|
|
|
-- Now create an object and tie its lifetime to the callback
|
|
|
|
local function gc()
|
|
|
|
finalized = true
|
|
|
|
end
|
|
|
|
if _VERSION <= "Lua 5.1" then
|
2016-02-07 13:10:21 +01:00
|
|
|
-- luacheck: globals newproxy
|
|
|
|
userdata = newproxy(true)
|
2015-11-19 18:06:58 +01:00
|
|
|
getmetatable(userdata).__gc = gc
|
|
|
|
getmetatable(userdata).callback = callback
|
|
|
|
else
|
|
|
|
userdata = setmetatable({callback}, { __gc = gc })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
collectgarbage("collect")
|
|
|
|
assert.is_true(finalized)
|
|
|
|
obj:emit_signal("signal")
|
|
|
|
end)
|
2016-05-18 06:47:46 +02:00
|
|
|
|
|
|
|
it("dynamic property disabled", function()
|
|
|
|
local class = {}
|
|
|
|
function class:get_foo() return "bar" end
|
|
|
|
|
|
|
|
local obj2 = object{class=class}
|
|
|
|
|
|
|
|
obj2.foo = 42
|
|
|
|
|
2016-06-04 15:41:41 +02:00
|
|
|
assert.is.equal(obj2.foo, 42)
|
2016-05-18 06:47:46 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("dynamic property disabled", function()
|
|
|
|
local class = {}
|
|
|
|
function class:get_foo() return "bar" end
|
|
|
|
|
|
|
|
local obj2 = object{class=class, enable_properties = true}
|
|
|
|
|
2016-06-04 15:47:09 +02:00
|
|
|
assert.has_error(function()
|
|
|
|
obj2.foo = 42
|
|
|
|
end)
|
2016-05-18 06:47:46 +02:00
|
|
|
|
2016-06-04 15:41:41 +02:00
|
|
|
assert.is.equal(obj2.foo, "bar")
|
2016-05-18 06:47:46 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("auto emit disabled", function()
|
|
|
|
local got_it = false
|
|
|
|
obj:connect_signal("property::foo", function() got_it=true end)
|
|
|
|
|
|
|
|
obj.foo = 42
|
|
|
|
|
|
|
|
assert.is_false(got_it)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("auto emit enabled", function()
|
|
|
|
local got_it = false
|
|
|
|
local obj2 = object{enable_auto_signals=true, enable_properties=true}
|
|
|
|
obj2:connect_signal("property::foo", function() got_it=true end)
|
|
|
|
|
|
|
|
obj2.foo = 42
|
|
|
|
|
|
|
|
assert.is_true(got_it)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it("auto emit without dynamic properties", function()
|
|
|
|
assert.has.errors(function()
|
|
|
|
object{enable_auto_signals=true, enable_properties=false}
|
|
|
|
end)
|
|
|
|
end)
|
2014-08-23 21:57:52 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|