GameStateTest.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * GameStateTest.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "StdInc.h"
  12. #include "../mock/mock_Services.h"
  13. #include "../mock/mock_MapService.h"
  14. #include "../mock/mock_IGameEventCallback.h"
  15. #include "../../lib/gameState/CGameState.h"
  16. #include "../../lib/mapping/CMap.h"
  17. #include "../../lib/StartInfo.h"
  18. #include "../../lib/CRandomGenerator.h"
  19. #include "../../lib/callback/GameRandomizer.h"
  20. #include "../../lib/filesystem/ResourcePath.h"
  21. /**
  22. * Base test fixture for game state tests that need a fully initialized game.
  23. *
  24. * This class provides:
  25. * - Game state initialization with a test map
  26. * - Server callback implementation for applying netpacks
  27. * - Mock services and event callbacks
  28. * - Helper methods for common game state operations
  29. *
  30. * Inherit from this class to test netpacks and other game state operations.
  31. */
  32. class GameStateTest : public ::testing::Test, public ServerCallback, public MapListener
  33. {
  34. public:
  35. GameStateTest()
  36. : gameEventCallback(std::make_shared<GameEventCallbackMock>(this)),
  37. mapService("test/MiniTest/", this),
  38. map(nullptr)
  39. {
  40. }
  41. void SetUp() override
  42. {
  43. gameState = std::make_shared<CGameState>();
  44. gameState->preInit(&services);
  45. }
  46. void TearDown() override
  47. {
  48. gameState.reset();
  49. }
  50. bool describeChanges() const override
  51. {
  52. return true;
  53. }
  54. void apply(CPackForClient & pack) override
  55. {
  56. gameState->apply(pack);
  57. }
  58. void complain(const std::string & problem) override
  59. {
  60. FAIL() << "Server-side assertion: " << problem;
  61. }
  62. vstd::RNG * getRNG() override
  63. {
  64. return &randomGenerator;
  65. }
  66. // Unused battle-specific overrides - implement as needed in derived classes
  67. void apply(BattleLogMessage &) override {}
  68. void apply(BattleStackMoved &) override {}
  69. void apply(BattleUnitsChanged &) override {}
  70. void apply(SetStackEffect &) override {}
  71. void apply(StacksInjured &) override {}
  72. void apply(BattleObstaclesChanged &) override {}
  73. void apply(CatapultAttack &) override {}
  74. void mapLoaded(CMap * map) override
  75. {
  76. EXPECT_EQ(this->map, nullptr);
  77. this->map = map;
  78. }
  79. /**
  80. * Initialize a test game with the specified map.
  81. *
  82. * This method:
  83. * - Loads the map header
  84. * - Configures player settings
  85. * - Initializes the game state
  86. * - Makes the game ready for testing
  87. *
  88. * @param mapName Name of the test map to load (default: "anything")
  89. */
  90. void startTestGame(const std::string & mapName = "anything")
  91. {
  92. StartInfo si;
  93. si.mapname = mapName;
  94. si.difficulty = 0;
  95. si.mode = EStartMode::NEW_GAME;
  96. std::unique_ptr<CMapHeader> header = mapService.loadMapHeader(ResourcePath(si.mapname));
  97. ASSERT_NE(header.get(), nullptr) << "Failed to load map header for: " << mapName;
  98. // Setup player info
  99. for(int i = 0; i < header->players.size(); i++)
  100. {
  101. const PlayerInfo & pinfo = header->players[i];
  102. if (!(pinfo.canHumanPlay || pinfo.canComputerPlay))
  103. continue;
  104. PlayerSettings & pset = si.playerInfos[PlayerColor(i)];
  105. pset.color = PlayerColor(i);
  106. pset.connectedPlayerIDs.insert(static_cast<PlayerConnectionID>(i));
  107. pset.name = "Player";
  108. pset.castle = pinfo.defaultCastle();
  109. pset.hero = pinfo.defaultHero();
  110. if(pset.hero != HeroTypeID::RANDOM && pinfo.hasCustomMainHero())
  111. {
  112. pset.hero = pinfo.mainCustomHeroId;
  113. pset.heroNameTextId = pinfo.mainCustomHeroNameTextId;
  114. pset.heroPortrait = HeroTypeID(pinfo.mainCustomHeroPortrait);
  115. }
  116. }
  117. GameRandomizer randomizer(*gameState);
  118. Load::ProgressAccumulator progressTracker;
  119. gameState->init(&mapService, &si, randomizer, progressTracker, false);
  120. ASSERT_NE(map, nullptr) << "Game state initialization failed - map is null";
  121. }
  122. protected:
  123. std::shared_ptr<CGameState> gameState;
  124. std::shared_ptr<GameEventCallbackMock> gameEventCallback;
  125. MapServiceMock mapService;
  126. ServicesMock services;
  127. CMap * map;
  128. CRandomGenerator randomGenerator;
  129. };