mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-08 11:03:25 -08:00
[Refactor] Split CreateGameFormWidget out of DlgCreateGame (#7801)
* Extract CreateGameFormWidget from DlgCreateGame * Address review findings
This commit is contained in:
parent
554368967d
commit
39220a2ea9
10 changed files with 864 additions and 325 deletions
|
|
@ -1,7 +1,100 @@
|
|||
#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
|
||||
{
|
||||
/**
|
||||
* @brief The remembered settings a test hands to fromSettings.
|
||||
*
|
||||
* The fake cannot itself be an aggregate (it derives from a polymorphic interface), so the
|
||||
* remembered values live in a GameCreationOptions member and the getters read them from
|
||||
* there. That keeps the test data in one designated initializer.
|
||||
*/
|
||||
class FakeGameSettings : public IGameSettingsProvider
|
||||
{
|
||||
public:
|
||||
explicit FakeGameSettings(const GameCreationOptions &_options = {}) : options(_options)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] QString getGameDescription() const override
|
||||
{
|
||||
return options.description;
|
||||
}
|
||||
[[nodiscard]] int getMaxPlayers() const override
|
||||
{
|
||||
return options.maxPlayers;
|
||||
}
|
||||
[[nodiscard]] QString getGameTypes() const override
|
||||
{
|
||||
return gameTypes;
|
||||
}
|
||||
[[nodiscard]] bool getOnlyBuddies() const override
|
||||
{
|
||||
return options.onlyBuddies;
|
||||
}
|
||||
[[nodiscard]] bool getOnlyRegistered() const override
|
||||
{
|
||||
return options.onlyRegistered;
|
||||
}
|
||||
[[nodiscard]] bool getSpectatorsAllowed() const override
|
||||
{
|
||||
return options.spectatorsAllowed;
|
||||
}
|
||||
[[nodiscard]] bool getSpectatorsNeedPassword() const override
|
||||
{
|
||||
return options.spectatorsNeedPassword;
|
||||
}
|
||||
[[nodiscard]] bool getSpectatorsCanTalk() const override
|
||||
{
|
||||
return options.spectatorsCanTalk;
|
||||
}
|
||||
[[nodiscard]] bool getSpectatorsCanSeeEverything() const override
|
||||
{
|
||||
return options.spectatorsSeeEverything;
|
||||
}
|
||||
[[nodiscard]] bool getCreateGameAsSpectator() const override
|
||||
{
|
||||
return options.joinAsSpectator;
|
||||
}
|
||||
[[nodiscard]] int getDefaultStartingLifeTotal() const override
|
||||
{
|
||||
return options.startingLifeTotal;
|
||||
}
|
||||
[[nodiscard]] bool getShareDecklistsOnLoad() const override
|
||||
{
|
||||
return options.shareDecklistsOnLoad;
|
||||
}
|
||||
[[nodiscard]] bool getShufflePlayers() const override
|
||||
{
|
||||
return options.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;
|
||||
}
|
||||
|
||||
GameCreationOptions options;
|
||||
/// The legacy "Name, " list; it has no GameCreationOptions counterpart.
|
||||
QString gameTypes;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
TEST(GameCreationOptions, DefaultsMatchTheDialog)
|
||||
{
|
||||
|
|
@ -91,3 +184,127 @@ TEST(GameCreationOptions, DescriptionWhitespaceIsSimplified)
|
|||
|
||||
EXPECT_EQ(cmd.description(), "a b");
|
||||
}
|
||||
|
||||
TEST(GameCreationOptions, FromSettingsCopiesEveryRememberedField)
|
||||
{
|
||||
const FakeGameSettings settings{GameCreationOptions{.description = "Remembered game",
|
||||
.password = {},
|
||||
.maxPlayers = 6,
|
||||
.onlyBuddies = true,
|
||||
.onlyRegistered = true,
|
||||
.spectatorsAllowed = false,
|
||||
.spectatorsNeedPassword = true,
|
||||
.spectatorsCanTalk = true,
|
||||
.spectatorsSeeEverything = true,
|
||||
.joinAsJudge = false,
|
||||
.joinAsSpectator = true,
|
||||
.startingLifeTotal = 40,
|
||||
.shareDecklistsOnLoad = true,
|
||||
.shufflePlayers = true,
|
||||
.gameTypeIds = {}}};
|
||||
|
||||
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.options.onlyBuddies = true;
|
||||
settings.options.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}));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue