mirror of
https://github.com/britzl/monarch.git
synced 2025-09-27 10:02:21 +02:00
Added navigation listeners
This commit is contained in:
@@ -315,3 +315,43 @@ embedded_instances {
|
||||
z: 1.0
|
||||
}
|
||||
}
|
||||
embedded_instances {
|
||||
id: "listener1"
|
||||
data: ""
|
||||
position {
|
||||
x: 0.0
|
||||
y: 0.0
|
||||
z: 0.0
|
||||
}
|
||||
rotation {
|
||||
x: 0.0
|
||||
y: 0.0
|
||||
z: 0.0
|
||||
w: 1.0
|
||||
}
|
||||
scale3 {
|
||||
x: 1.0
|
||||
y: 1.0
|
||||
z: 1.0
|
||||
}
|
||||
}
|
||||
embedded_instances {
|
||||
id: "listener2"
|
||||
data: ""
|
||||
position {
|
||||
x: 0.0
|
||||
y: 0.0
|
||||
z: 0.0
|
||||
}
|
||||
rotation {
|
||||
x: 0.0
|
||||
y: 0.0
|
||||
z: 0.0
|
||||
w: 1.0
|
||||
}
|
||||
scale3 {
|
||||
x: 1.0
|
||||
y: 1.0
|
||||
z: 1.0
|
||||
}
|
||||
}
|
||||
|
52
test/msg.lua
Normal file
52
test/msg.lua
Normal file
@@ -0,0 +1,52 @@
|
||||
local mock = require "deftest.mock.mock"
|
||||
|
||||
|
||||
local M = {}
|
||||
|
||||
|
||||
local recipients = {}
|
||||
|
||||
local history = {}
|
||||
|
||||
local function get_recipient(url)
|
||||
recipients[url] = recipients[url] or {}
|
||||
return recipients[url]
|
||||
end
|
||||
|
||||
local function post(url, message_id, message)
|
||||
local data = { url = url, message_id = message_id, message = message }
|
||||
history[#history + 1] = data
|
||||
local recipient = get_recipient(url)
|
||||
recipient[#recipient + 1] = data
|
||||
msg.post.original(url, message_id, message or {})
|
||||
end
|
||||
|
||||
function M.mock()
|
||||
recipients = {}
|
||||
history = {}
|
||||
mock.mock(msg)
|
||||
msg.post.replace(post)
|
||||
end
|
||||
|
||||
function M.unmock()
|
||||
mock.unmock(msg)
|
||||
end
|
||||
|
||||
|
||||
|
||||
function M.messages(url)
|
||||
return url and get_recipient(url) or history
|
||||
end
|
||||
|
||||
function M.first(url)
|
||||
local messages = url and get_recipient(url) or history
|
||||
return messages[1]
|
||||
end
|
||||
|
||||
function M.last(url)
|
||||
local messages = url and get_recipient(url) or history
|
||||
return messages[#messages]
|
||||
end
|
||||
|
||||
|
||||
return M
|
@@ -1,4 +1,6 @@
|
||||
local cowait = require "test.cowait"
|
||||
local mock_msg = require "test.msg"
|
||||
local unload = require "deftest.util.unload"
|
||||
local monarch = require "monarch.monarch"
|
||||
|
||||
local SCREEN1_STR = hash("screen1")
|
||||
@@ -55,12 +57,14 @@ return function()
|
||||
|
||||
describe("monarch", function()
|
||||
before(function()
|
||||
mock_msg.mock()
|
||||
monarch = require "monarch.monarch"
|
||||
screens_instances = collectionfactory.create("#screensfactory")
|
||||
end)
|
||||
|
||||
after(function()
|
||||
package.loaded["monarch.monarch"] = nil
|
||||
mock_msg.unmock()
|
||||
unload.unload("monarch%..*")
|
||||
for id,instance_id in pairs(screens_instances) do
|
||||
go.delete(instance_id)
|
||||
end
|
||||
@@ -229,5 +233,34 @@ return function()
|
||||
assert(monarch.is_busy())
|
||||
assert(wait_until_not_busy())
|
||||
end)
|
||||
|
||||
it("should be able to notify listeners of navigation events", function()
|
||||
local URL1 = msg.url(screens_instances[hash("/listener1")])
|
||||
local URL2 = msg.url(screens_instances[hash("/listener2")])
|
||||
monarch.add_listener(URL1)
|
||||
monarch.add_listener(URL2)
|
||||
|
||||
monarch.show(SCREEN1)
|
||||
assert(wait_until_not_busy())
|
||||
|
||||
monarch.remove_listener(URL2)
|
||||
monarch.show(SCREEN2)
|
||||
assert(wait_until_not_busy())
|
||||
|
||||
monarch.back()
|
||||
assert(wait_until_not_busy())
|
||||
|
||||
local messages_1 = mock_msg.messages(URL1)
|
||||
local messages_2 = mock_msg.messages(URL2)
|
||||
assert(#messages_1 == 5)
|
||||
assert(#messages_2 == 1)
|
||||
assert(messages_2[1].message_id == monarch.SCREEN_VISIBLE and messages_1[1].message.screen == SCREEN1)
|
||||
|
||||
assert(messages_1[1].message_id == monarch.SCREEN_VISIBLE and messages_1[1].message.screen == SCREEN1)
|
||||
assert(messages_1[2].message_id == monarch.SCREEN_HIDDEN and messages_1[2].message.screen == SCREEN1)
|
||||
assert(messages_1[3].message_id == monarch.SCREEN_VISIBLE and messages_1[3].message.screen == SCREEN2)
|
||||
assert(messages_1[4].message_id == monarch.SCREEN_HIDDEN and messages_1[4].message.screen == SCREEN2)
|
||||
assert(messages_1[5].message_id == monarch.SCREEN_VISIBLE and messages_1[5].message.screen == SCREEN1)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
Reference in New Issue
Block a user