From bb62960faa9f2408ba914bbf72baab1c7897d77e Mon Sep 17 00:00:00 2001 From: codereader Date: Thu, 14 Oct 2021 12:13:02 +0200 Subject: [PATCH] #5780: Simulate the radiant app shutdown scenario in the unit tests. The Map module should ask for saving and deny the shutdown request on cancel. --- test/MapSavingLoading.cpp | 56 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/test/MapSavingLoading.cpp b/test/MapSavingLoading.cpp index d4209aeb2e..70d702cae2 100644 --- a/test/MapSavingLoading.cpp +++ b/test/MapSavingLoading.cpp @@ -11,6 +11,7 @@ #include "iselectiongroup.h" #include "ilightnode.h" #include "icommandsystem.h" +#include "messages/ApplicationShutdownRequest.h" #include "messages/FileSelectionRequest.h" #include "messages/FileOverwriteConfirmation.h" #include "messages/MapFileOperation.h" @@ -1296,7 +1297,7 @@ void checkBehaviourWithoutUnsavedChanges(std::function action) EXPECT_FALSE(GlobalMapModule().isModified()) << "New Map should be unmodified"; } -void checkBehaviourDiscardingUnsavedChanges(const std::string& testProjectPath, std::function action) +void checkBehaviourDiscardingUnsavedChanges(const std::string& testProjectPath, std::function action, bool expectUnmodifiedMapAfterAction = true) { std::string modRelativePath = "maps/altar.map"; GlobalCommandSystem().executeCommand("OpenMap", modRelativePath); @@ -1316,7 +1317,12 @@ void checkBehaviourDiscardingUnsavedChanges(const std::string& testProjectPath, EXPECT_TRUE(helper.messageReceived()) << "Map must ask for save"; EXPECT_EQ(fs::file_size(mapPath), sizeBefore) << "File size has changed after discarding"; - EXPECT_FALSE(GlobalMapModule().isModified()) << "Map should be unmodified again"; + + // In some scenario like app shutdown the map modified flag is not cleared + if (expectUnmodifiedMapAfterAction) + { + EXPECT_FALSE(GlobalMapModule().isModified()) << "Map should be unmodified again"; + } } void checkBehaviourSavingUnsavedChanges(const std::string& fullMapPath, std::function action) @@ -1371,6 +1377,15 @@ void openMapFromArchive(const radiant::TestContext& context) GlobalCommandSystem().executeCommand("OpenMapFromArchive", pakPath.string(), archiveRelativePath); } +void sendShutdownRequest(bool expectDenial) +{ + // Send the shutdown request, the Map Module should be receiving this + radiant::ApplicationShutdownRequest request; + GlobalRadiantCore().getMessageBus().sendMessage(request); + + EXPECT_EQ(expectDenial, request.isDenied()) << "Shutdown Request denial flag has unexpected value"; +} + } TEST_F(MapSavingTest, NewMapWithoutUnsavedChanges) @@ -1399,6 +1414,14 @@ TEST_F(MapSavingTest, OpenMapFromArchiveWithoutUnsavedChanges) }); } +TEST_F(MapSavingTest, RadiantShutdownWithoutUnsavedChanges) +{ + checkBehaviourWithoutUnsavedChanges([]() + { + sendShutdownRequest(false); // no denial + }); +} + TEST_F(MapSavingTest, NewMapDiscardingUnsavedChanges) { checkBehaviourDiscardingUnsavedChanges(_context.getTestProjectPath(), []() @@ -1425,6 +1448,15 @@ TEST_F(MapSavingTest, OpenMapFromArchiveDiscardingUnsavedChanges) }); } +TEST_F(MapSavingTest, RadiantShutdownDiscardingUnsavedChanges) +{ + // Pass false to expectUnmodifiedMapAfterAction => discarding the map on shutdown will not clear the modified flag + checkBehaviourDiscardingUnsavedChanges(_context.getTestProjectPath(), []() + { + sendShutdownRequest(false); // no denial + }, false); +} + TEST_F(MapSavingTest, NewMapSavingChanges) { auto mapPath = createMapCopyInTempDataPath("altar.map", "altar_NewMapSavingChanges.map"); @@ -1457,6 +1489,16 @@ TEST_F(MapSavingTest, OpenMapFromArchiveSavingChanges) }); } +TEST_F(MapSavingTest, RadiantShutdownSavingChanges) +{ + auto mapPath = createMapCopyInTempDataPath("altar.map", "altar_NewMapSavingChanges.map"); + + checkBehaviourSavingUnsavedChanges(mapPath.string(), []() + { + sendShutdownRequest(false); // no denial + }); +} + TEST_F(MapSavingTest, NewMapCancelWithUnsavedChanges) { auto mapPath = createMapCopyInTempDataPath("altar.map", "altar_NewMapCancelWithUnsavedChanges.map"); @@ -1489,4 +1531,14 @@ TEST_F(MapSavingTest, OpenMapFromArchiveCancelWithUnsavedChanges) }); } +TEST_F(MapSavingTest, RadiantShutdownCancelWithUnsavedChanges) +{ + auto mapPath = createMapCopyInTempDataPath("altar.map", "altar_NewMapCancelWithUnsavedChanges.map"); + + checkBehaviourCancellingMapChange(mapPath.string(), []() + { + sendShutdownRequest(true); // request should be denied + }); +} + }