Extract CreateGameFormWidget from DlgCreateGame

This commit is contained in:
Lukas Brübach 2026-11-07 14:02:54 +01:00
parent bed2a67a2f
commit 6cecb5160c
10 changed files with 873 additions and 326 deletions

View file

@ -462,6 +462,20 @@ cockatrice_add_test(
${CMAKE_SOURCE_DIR}/cockatrice/src/interface/widgets/game_hosting/game_creation_options.cpp
game_creation_options_test.cpp
LIBS
libcockatrice_interfaces
libcockatrice_protocol
INCLUDE_DIRS
${CMAKE_SOURCE_DIR}/cockatrice/src
)
cockatrice_add_test(
NAME
create_game_form_widget_test
SOURCES
${CMAKE_SOURCE_DIR}/cockatrice/src/interface/widgets/game_hosting/create_game_form_widget.cpp
${CMAKE_SOURCE_DIR}/cockatrice/src/interface/widgets/game_hosting/game_creation_options.cpp
create_game_form_widget_test.cpp
LIBS
libcockatrice_interfaces
libcockatrice_protocol
INCLUDE_DIRS
${CMAKE_SOURCE_DIR}/cockatrice/src

View file

@ -0,0 +1,132 @@
#include "interface/widgets/game_hosting/create_game_form_widget.h"
#include <QApplication>
#include <QMap>
#include <gtest/gtest.h>
namespace
{
const QMap<int, QString> gameTypes{{1, "Draft"}, {2, "Commander"}};
void expectSameOptions(const GameCreationOptions &actual, const GameCreationOptions &expected)
{
EXPECT_EQ(actual.description, expected.description);
EXPECT_EQ(actual.password, expected.password);
EXPECT_EQ(actual.maxPlayers, expected.maxPlayers);
EXPECT_EQ(actual.onlyBuddies, expected.onlyBuddies);
EXPECT_EQ(actual.onlyRegistered, expected.onlyRegistered);
EXPECT_EQ(actual.spectatorsAllowed, expected.spectatorsAllowed);
EXPECT_EQ(actual.spectatorsNeedPassword, expected.spectatorsNeedPassword);
EXPECT_EQ(actual.spectatorsCanTalk, expected.spectatorsCanTalk);
EXPECT_EQ(actual.spectatorsSeeEverything, expected.spectatorsSeeEverything);
EXPECT_EQ(actual.joinAsJudge, expected.joinAsJudge);
EXPECT_EQ(actual.joinAsSpectator, expected.joinAsSpectator);
EXPECT_EQ(actual.startingLifeTotal, expected.startingLifeTotal);
EXPECT_EQ(actual.shareDecklistsOnLoad, expected.shareDecklistsOnLoad);
EXPECT_EQ(actual.shufflePlayers, expected.shufflePlayers);
EXPECT_EQ(actual.gameTypeIds, expected.gameTypeIds);
}
} // namespace
TEST(CreateGameFormWidget, RegisteredHostDefaultsToRegisteredOnly)
{
const CreateGameFormWidget form(gameTypes, true, false);
const GameCreationOptions options = form.options();
EXPECT_TRUE(options.onlyRegistered);
EXPECT_FALSE(options.onlyBuddies);
EXPECT_EQ(options.maxPlayers, 2);
EXPECT_TRUE(options.spectatorsAllowed);
EXPECT_EQ(options.startingLifeTotal, 20);
EXPECT_TRUE(options.gameTypeIds.isEmpty());
}
TEST(CreateGameFormWidget, UnregisteredHostDefaultsToOpenJoin)
{
const CreateGameFormWidget form(gameTypes, false, false);
const GameCreationOptions options = form.options();
EXPECT_FALSE(options.onlyRegistered);
EXPECT_FALSE(options.onlyBuddies);
}
TEST(CreateGameFormWidget, SetOptionsRoundTripsEveryField)
{
CreateGameFormWidget form(gameTypes, true, true);
GameCreationOptions expected;
expected.description = "Some game";
expected.password = "hunter2";
expected.maxPlayers = 5;
expected.onlyBuddies = true;
expected.onlyRegistered = false;
expected.spectatorsAllowed = false;
expected.spectatorsNeedPassword = true;
expected.spectatorsCanTalk = true;
expected.spectatorsSeeEverything = true;
expected.joinAsJudge = true;
expected.joinAsSpectator = true;
expected.startingLifeTotal = 40;
expected.shareDecklistsOnLoad = true;
expected.shufflePlayers = true;
expected.gameTypeIds = {2};
form.setOptions(expected);
expectSameOptions(form.options(), expected);
}
TEST(CreateGameFormWidget, ResetRestoresDefaultsAndClearsTheGameType)
{
CreateGameFormWidget form(gameTypes, true, false);
GameCreationOptions configured;
configured.description = "Configured";
configured.maxPlayers = 8;
configured.onlyBuddies = true;
configured.joinAsJudge = true;
configured.gameTypeIds = {2};
form.setOptions(configured);
form.resetToDefaults();
const GameCreationOptions options = form.options();
EXPECT_TRUE(options.description.isEmpty());
EXPECT_EQ(options.maxPlayers, 2);
EXPECT_FALSE(options.onlyBuddies);
EXPECT_TRUE(options.onlyRegistered);
EXPECT_TRUE(options.spectatorsAllowed);
EXPECT_EQ(options.startingLifeTotal, 20);
EXPECT_FALSE(options.joinAsJudge);
EXPECT_TRUE(options.gameTypeIds.isEmpty());
}
TEST(CreateGameFormWidget, RememberSettingsFlagRoundTrips)
{
CreateGameFormWidget form(gameTypes, true, false);
EXPECT_FALSE(form.isRememberSettingsChecked());
form.setRememberSettingsChecked(true);
EXPECT_TRUE(form.isRememberSettingsChecked());
}
TEST(CreateGameFormWidget, SelectedGameTypesSettingStringUsesTheLegacyEncoding)
{
CreateGameFormWidget form(gameTypes, true, false);
GameCreationOptions configured;
configured.gameTypeIds = {1};
form.setOptions(configured);
EXPECT_EQ(form.selectedGameTypesSettingString(), "Draft, ");
}
int main(int argc, char **argv)
{
qputenv("QT_QPA_PLATFORM", "offscreen");
QApplication app(argc, argv);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View file

@ -1,7 +1,99 @@
#include "interface/widgets/game_hosting/game_creation_options.h"
#include <gtest/gtest.h>
#include <libcockatrice/interfaces/interface_game_settings_provider.h>
#include <libcockatrice/protocol/pb/room_commands.pb.h>
#include <libcockatrice/protocol/pb/serverinfo_game.pb.h>
namespace
{
class FakeGameSettings : public IGameSettingsProvider
{
public:
[[nodiscard]] QString getGameDescription() const override
{
return description;
}
[[nodiscard]] int getMaxPlayers() const override
{
return maxPlayers;
}
[[nodiscard]] QString getGameTypes() const override
{
return gameTypes;
}
[[nodiscard]] bool getOnlyBuddies() const override
{
return onlyBuddies;
}
[[nodiscard]] bool getOnlyRegistered() const override
{
return onlyRegistered;
}
[[nodiscard]] bool getSpectatorsAllowed() const override
{
return spectatorsAllowed;
}
[[nodiscard]] bool getSpectatorsNeedPassword() const override
{
return spectatorsNeedPassword;
}
[[nodiscard]] bool getSpectatorsCanTalk() const override
{
return spectatorsCanTalk;
}
[[nodiscard]] bool getSpectatorsCanSeeEverything() const override
{
return spectatorsCanSeeEverything;
}
[[nodiscard]] bool getCreateGameAsSpectator() const override
{
return createGameAsSpectator;
}
[[nodiscard]] int getDefaultStartingLifeTotal() const override
{
return defaultStartingLifeTotal;
}
[[nodiscard]] bool getShareDecklistsOnLoad() const override
{
return shareDecklistsOnLoad;
}
[[nodiscard]] bool getShufflePlayers() const override
{
return shufflePlayers;
}
[[nodiscard]] bool getRememberGameSettings() const override
{
return false;
}
[[nodiscard]] bool getLocalGameRememberSettings() const override
{
return false;
}
[[nodiscard]] int getLocalGameMaxPlayers() const override
{
return 0;
}
[[nodiscard]] int getLocalGameStartingLifeTotal() const override
{
return 0;
}
QString description;
int maxPlayers = 2;
QString gameTypes;
bool onlyBuddies = false;
bool onlyRegistered = false;
bool spectatorsAllowed = true;
bool spectatorsNeedPassword = false;
bool spectatorsCanTalk = false;
bool spectatorsCanSeeEverything = false;
bool createGameAsSpectator = false;
int defaultStartingLifeTotal = 20;
bool shareDecklistsOnLoad = false;
bool shufflePlayers = false;
};
} // namespace
TEST(GameCreationOptions, DefaultsMatchTheDialog)
{
@ -91,3 +183,125 @@ TEST(GameCreationOptions, DescriptionWhitespaceIsSimplified)
EXPECT_EQ(cmd.description(), "a b");
}
TEST(GameCreationOptions, FromSettingsCopiesEveryRememberedField)
{
FakeGameSettings settings;
settings.description = "Remembered game";
settings.maxPlayers = 6;
settings.onlyBuddies = true;
settings.onlyRegistered = true;
settings.spectatorsAllowed = false;
settings.spectatorsNeedPassword = true;
settings.spectatorsCanTalk = true;
settings.spectatorsCanSeeEverything = true;
settings.createGameAsSpectator = true;
settings.defaultStartingLifeTotal = 40;
settings.shareDecklistsOnLoad = true;
settings.shufflePlayers = true;
const GameCreationOptions options = GameCreationOptions::fromSettings(settings, {}, true);
EXPECT_EQ(options.description, "Remembered game");
EXPECT_EQ(options.maxPlayers, 6);
EXPECT_TRUE(options.onlyBuddies);
EXPECT_TRUE(options.onlyRegistered);
EXPECT_FALSE(options.spectatorsAllowed);
EXPECT_TRUE(options.spectatorsNeedPassword);
EXPECT_TRUE(options.spectatorsCanTalk);
EXPECT_TRUE(options.spectatorsSeeEverything);
EXPECT_TRUE(options.joinAsSpectator);
EXPECT_EQ(options.startingLifeTotal, 40);
EXPECT_TRUE(options.shareDecklistsOnLoad);
EXPECT_TRUE(options.shufflePlayers);
}
TEST(GameCreationOptions, FromSettingsDropsJoinRestrictionsForUnregisteredHosts)
{
FakeGameSettings settings;
settings.onlyBuddies = true;
settings.onlyRegistered = true;
const GameCreationOptions options = GameCreationOptions::fromSettings(settings, {}, false);
EXPECT_FALSE(options.onlyBuddies);
EXPECT_FALSE(options.onlyRegistered);
}
TEST(GameCreationOptions, FromSettingsMatchesGameTypesByTheLegacyNameList)
{
FakeGameSettings settings;
settings.gameTypes = "Draft, Commander, ";
const QMap<int, QString> gameTypes{{1, "Draft"}, {2, "Commander"}, {3, "Standard"}};
const GameCreationOptions options = GameCreationOptions::fromSettings(settings, gameTypes, true);
EXPECT_EQ(options.gameTypeIds, QList<int>({1, 2}));
}
TEST(GameCreationOptions, FromSettingsDoesNotMatchGameTypesWithoutTheLegacySeparator)
{
FakeGameSettings settings;
settings.gameTypes = "Draft Commander";
const QMap<int, QString> gameTypes{{1, "Draft"}, {2, "Commander"}};
const GameCreationOptions options = GameCreationOptions::fromSettings(settings, gameTypes, true);
EXPECT_TRUE(options.gameTypeIds.isEmpty());
}
TEST(GameCreationOptions, FromGameInfoCopiesTheVisibleFields)
{
ServerInfo_Game gameInfo;
gameInfo.set_description("Some game");
gameInfo.set_max_players(4);
gameInfo.set_only_buddies(true);
gameInfo.set_only_registered(true);
gameInfo.set_spectators_allowed(false);
gameInfo.set_spectators_need_password(true);
gameInfo.set_spectators_can_chat(true);
gameInfo.set_spectators_omniscient(true);
gameInfo.set_share_decklists_on_load(true);
gameInfo.set_shuffle_players(true);
gameInfo.add_game_types(2);
const QMap<int, QString> gameTypes{{1, "Draft"}, {2, "Commander"}};
const GameCreationOptions options = GameCreationOptions::fromGameInfo(gameInfo, gameTypes);
EXPECT_EQ(options.description, "Some game");
EXPECT_EQ(options.maxPlayers, 4);
EXPECT_TRUE(options.onlyBuddies);
EXPECT_TRUE(options.onlyRegistered);
EXPECT_FALSE(options.spectatorsAllowed);
EXPECT_TRUE(options.spectatorsNeedPassword);
EXPECT_TRUE(options.spectatorsCanTalk);
EXPECT_TRUE(options.spectatorsSeeEverything);
EXPECT_TRUE(options.shareDecklistsOnLoad);
EXPECT_TRUE(options.shufflePlayers);
EXPECT_EQ(options.gameTypeIds, QList<int>({2}));
}
TEST(GameCreationOptions, FromGameInfoLeavesUnshownFieldsAtDefaults)
{
ServerInfo_Game gameInfo;
gameInfo.set_max_players(3);
const GameCreationOptions options = GameCreationOptions::fromGameInfo(gameInfo, {});
EXPECT_TRUE(options.password.isEmpty());
EXPECT_EQ(options.startingLifeTotal, 20);
EXPECT_FALSE(options.joinAsJudge);
EXPECT_FALSE(options.joinAsSpectator);
}
TEST(GameCreationOptions, FromGameInfoIgnoresUnknownGameTypeIds)
{
ServerInfo_Game gameInfo;
gameInfo.add_game_types(7);
gameInfo.add_game_types(9);
const QMap<int, QString> gameTypes{{1, "Draft"}, {7, "Commander"}};
const GameCreationOptions options = GameCreationOptions::fromGameInfo(gameInfo, gameTypes);
EXPECT_EQ(options.gameTypeIds, QList<int>({7}));
}