Skip to content

Commit

Permalink
#5127: Add unit test covering favourites persistence to user.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Dec 27, 2020
1 parent cf98ee4 commit 83546cd
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions test/Favourites.cpp
Expand Up @@ -10,6 +10,9 @@ class FavouritesTest :
public RadiantTest
{
protected:
// A function that is invoked after modules have been shut down
std::function<void()> checkAfterShutdown;

void copyUserXmlFileToSettingsPath(const std::string& userXmlFile)
{
fs::path sourcePath = _context.getTestResourcePath();
Expand All @@ -22,6 +25,16 @@ class FavouritesTest :
fs::remove(targetPath);
fs::copy(sourcePath, targetPath);
}

void TearDown() override
{
RadiantTest::TearDown();

if (checkAfterShutdown)
{
checkAfterShutdown();
}
}
};

class FavouritesTestWithLegacyFavourites :
Expand Down Expand Up @@ -104,6 +117,40 @@ TEST_F(FavouritesTest, AddingEmptyPaths)
EXPECT_FALSE(GlobalFavouritesManager().isFavourite(decl::Type::Material, ""));
}

TEST_F(FavouritesTest, FavouritesArePersisted)
{
EXPECT_TRUE(GlobalFavouritesManager().getFavourites(decl::Type::Material).empty());

// Add caulk and clip
GlobalFavouritesManager().addFavourite(decl::Type::Material, "textures/common/caulk");
GlobalFavouritesManager().addFavourite(decl::Type::Material, "textures/common/clip");

EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).size(), 2);
EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).count("textures/common/caulk"), 1);
EXPECT_EQ(GlobalFavouritesManager().getFavourites(decl::Type::Material).count("textures/common/clip"), 1);

checkAfterShutdown = [&]()
{
fs::path userXml = _context.getSettingsPath();
userXml /= "user.xml";

xml::Document doc(userXml.string());

auto savedNodes = doc.findXPath("/user/ui/favourites/materials//favourite");

EXPECT_EQ(savedNodes.size(), 2);

std::set<std::string> savedFavourites;
for (const auto& node : savedNodes)
{
savedFavourites.emplace(node.getAttributeValue("value"));
}

EXPECT_EQ(savedFavourites.count("textures/common/caulk"), 1);
EXPECT_EQ(savedFavourites.count("textures/common/clip"), 1);
};
}

TEST_F(FavouritesTestWithLegacyFavourites, LegacyFavouritesAreImported)
{
// The settings in the old location in the user.xml file should have been imported
Expand Down

0 comments on commit 83546cd

Please sign in to comment.