From 7e2ff2990c76fd0c948e4a042e56000f48846121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ritzl?= Date: Tue, 5 Mar 2019 00:21:23 +0100 Subject: [PATCH] Make sure to also reload pre-loaded screens if required Fixes #39 --- monarch/monarch.lua | 8 +- test/data/screen_preload.collection | 37 ++++++++ test/data/screen_preload.gui | 131 ++++++++++++++++++++++++++++ test/data/screen_preload.gui_script | 5 ++ test/data/screens.collection | 63 +++++++++++++ test/test_monarch.lua | 27 ++++++ 6 files changed, 267 insertions(+), 4 deletions(-) create mode 100644 test/data/screen_preload.collection create mode 100644 test/data/screen_preload.gui create mode 100644 test/data/screen_preload.gui_script diff --git a/monarch/monarch.lua b/monarch/monarch.lua index 7af5f0d..3bebdc5 100644 --- a/monarch/monarch.lua +++ b/monarch/monarch.lua @@ -243,11 +243,11 @@ local function change_context(screen) screen.wait_for = nil end -local function unload(screen) +local function unload(screen, force) log("unload()", screen.id) if screen.proxy then - if screen.auto_preload then + if screen.auto_preload and not force then msg.post(screen.proxy, DISABLE) screen.loaded = false screen.preloaded = true @@ -264,7 +264,7 @@ local function unload(screen) go.delete(instance) end screen.factory_ids = nil - if screen.auto_preload then + if screen.auto_preload and not force then screen.loaded = false screen.preloaded = true else @@ -449,7 +449,7 @@ local function show_in(screen, previous_screen, reload, add_to_stack, cb) change_context(screen) if reload and screen.loaded then log("show_in() reloading", screen.id) - unload(screen) + unload(screen, reload) end load(screen) if add_to_stack then diff --git a/test/data/screen_preload.collection b/test/data/screen_preload.collection new file mode 100644 index 0000000..a83f722 --- /dev/null +++ b/test/data/screen_preload.collection @@ -0,0 +1,37 @@ +name: "screen_preload" +scale_along_z: 0 +embedded_instances { + id: "go" + data: "components {\n" + " id: \"screen_preload\"\n" + " component: \"/test/data/screen_preload.gui\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + "}\n" + "" + 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 + } +} diff --git a/test/data/screen_preload.gui b/test/data/screen_preload.gui new file mode 100644 index 0000000..40f1134 --- /dev/null +++ b/test/data/screen_preload.gui @@ -0,0 +1,131 @@ +script: "/test/data/screen_preload.gui_script" +fonts { + name: "example" + font: "/assets/example.font" +} +background_color { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 +} +nodes { + position { + x: 320.0 + y: 697.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "" + id: "box" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_AUTO +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "PRELOAD" + font: "example" + id: "text" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: false + parent: "box" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + template_node_child: false + text_leading: 1.0 + text_tracking: 0.0 +} +material: "/builtins/materials/gui.material" +adjust_reference: ADJUST_REFERENCE_PARENT +max_nodes: 512 diff --git a/test/data/screen_preload.gui_script b/test/data/screen_preload.gui_script new file mode 100644 index 0000000..267ea9d --- /dev/null +++ b/test/data/screen_preload.gui_script @@ -0,0 +1,5 @@ +function init(self) + local monarch = require "monarch.monarch" + local data = monarch.data(hash("screen_preload")) + data.count = data.count + 1 +end diff --git a/test/data/screens.collection b/test/data/screens.collection index 917926b..4f277e3 100644 --- a/test/data/screens.collection +++ b/test/data/screens.collection @@ -408,3 +408,66 @@ embedded_instances { z: 1.0 } } +embedded_instances { + id: "screen_preload" + data: "components {\n" + " id: \"screen\"\n" + " component: \"/monarch/screen_factory.script\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + " properties {\n" + " id: \"screen_id\"\n" + " value: \"screen_preload\"\n" + " type: PROPERTY_TYPE_HASH\n" + " }\n" + " properties {\n" + " id: \"preload\"\n" + " value: \"true\"\n" + " type: PROPERTY_TYPE_BOOLEAN\n" + " }\n" + "}\n" + "embedded_components {\n" + " id: \"collectionfactory\"\n" + " type: \"collectionfactory\"\n" + " data: \"prototype: \\\"/test/data/screen_preload.collection\\\"\\n" + "load_dynamically: false\\n" + "\"\n" + " position {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " }\n" + " rotation {\n" + " x: 0.0\n" + " y: 0.0\n" + " z: 0.0\n" + " w: 1.0\n" + " }\n" + "}\n" + "" + 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 + } +} diff --git a/test/test_monarch.lua b/test/test_monarch.lua index bd5d37f..3b2b0ff 100644 --- a/test/test_monarch.lua +++ b/test/test_monarch.lua @@ -6,6 +6,7 @@ local monarch = require "monarch.monarch" local SCREEN1_STR = hash("screen1") local SCREEN1 = hash(SCREEN1_STR) local SCREEN2 = hash("screen2") +local SCREEN_PRELOAD = hash("screen_preload") local BACKGROUND = hash("background") local POPUP1 = hash("popup1") local POPUP2 = hash("popup2") @@ -355,5 +356,31 @@ return function() assert(mock_msg.messages(URL1)[10].message_id == monarch.SCREEN_TRANSITION_IN_FINISHED) assert(mock_msg.messages(URL1)[10].message.screen == SCREEN1) end) + + + it("should be able to preload a screen and always keep it loaded", function() + monarch.show(SCREEN_PRELOAD, nil, { count = 1 }) + assert(wait_until_shown(SCREEN_PRELOAD), "Screen_preload was never shown") + -- first time the screen gets loaded it will increment the count + assert(monarch.data(SCREEN_PRELOAD).count == 2) + + monarch.show(SCREEN_PRELOAD, { clear = true }, { count = 1 }) + assert(wait_until_shown(SCREEN_PRELOAD), "Screen_preload was never shown") + -- second time the screen gets shown it will already be loaded and not increment the count + assert(monarch.data(SCREEN_PRELOAD).count == 1) + end) + + + it("should be able to reload a preloaded screen", function() + monarch.show(SCREEN_PRELOAD, nil, { count = 1 }) + assert(wait_until_shown(SCREEN_PRELOAD), "Screen_preload was never shown") + -- first time the screen gets loaded it will increment the count + assert(monarch.data(SCREEN_PRELOAD).count == 2) + + monarch.show(SCREEN_PRELOAD, { clear = true, reload = true }, { count = 1 }) + assert(wait_until_shown(SCREEN_PRELOAD), "Screen_preload was never shown") + -- second time the screen gets shown it will be reloaded and increment the count + assert(monarch.data(SCREEN_PRELOAD).count == 2) + end) end) end