game_dispatch_test.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // game_dispatch_test.cxx
  3. // engine-test
  4. //
  5. // Created by Sam Jaffe on 7/10/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include "game/engine/game_dispatch.hpp"
  9. #include <memory>
  10. #include <game/graphics/renderer.hpp>
  11. #include <game/util/env.hpp>
  12. #include <math/vector/vector.hpp>
  13. #include <testing/xcode_gtest_helper.h>
  14. #include "game/engine/events.hpp"
  15. #include "game/engine/scene.hpp"
  16. #include "mock_renderer.h"
  17. struct mock_scene : engine::scene {
  18. mock_scene(std::shared_ptr<engine::game_dispatch> game)
  19. : engine::scene("mock", {0.f, 0.f}, game) {}
  20. mock_scene(std::shared_ptr<engine::game_dispatch> game, math::vec2 dim)
  21. : engine::scene("mock2", dim, game) {}
  22. MOCK_METHOD1(update, void(float));
  23. MOCK_METHOD0(render, void());
  24. MOCK_METHOD1(handle_key_event, void(engine::event::key_event));
  25. MOCK_METHOD1(handle_mouse_event, void(engine::event::mouse_event));
  26. };
  27. class GameDispatchTest : public testing::Test {
  28. protected:
  29. void SetUp() override;
  30. void TearDown() override;
  31. mock_scene & scene() const { return *scene_; }
  32. engine::game_dispatch & game() const { return *dispatch_; }
  33. std::shared_ptr<stub_renderer> renderer_;
  34. std::shared_ptr<engine::game_dispatch> dispatch_;
  35. std::shared_ptr<mock_scene> scene_;
  36. };
  37. void GameDispatchTest::SetUp() {
  38. env::resize_screen(math::vec2i{{100, 100}});
  39. renderer_.reset(new stub_renderer);
  40. dispatch_.reset(new engine::game_dispatch(renderer_));
  41. dispatch_->set_target_fps(env::fps::v60);
  42. scene_.reset(new mock_scene(dispatch_));
  43. game().register_scene(scene_);
  44. game().activate_scene("mock");
  45. }
  46. void GameDispatchTest::TearDown() {
  47. scene_.reset();
  48. dispatch_.reset();
  49. renderer_.reset();
  50. }
  51. using testing::_;
  52. using testing::AnyNumber;
  53. using testing::Eq;
  54. using testing::Field;
  55. using testing::FloatNear;
  56. using testing::Ge;
  57. using testing::Lt;
  58. using testing::Not;
  59. TEST_F(GameDispatchTest, ManagerIsFetchedFromRenderer) {
  60. EXPECT_THAT(&game().graphics_manager(), Eq(renderer_->manager().get()));
  61. }
  62. // TODO: Set FPS to a much smaller value for testing purposes (e.g. 512fps)
  63. TEST_F(GameDispatchTest, UpdateDispatchesToCurrentScene) {
  64. EXPECT_CALL(scene(), update(_)).Times(1);
  65. game().update();
  66. }
  67. TEST_F(GameDispatchTest, UpdateIsCappedInFrequencyByFPSParam) {
  68. auto fps60 = 1.0 / 60.0;
  69. EXPECT_CALL(scene(), update(Ge(fps60))).Times(AnyNumber());
  70. EXPECT_CALL(scene(), update(Lt(fps60))).Times(0);
  71. for (int i = 0; i < 10; i++) {
  72. game().update();
  73. }
  74. }
  75. TEST_F(GameDispatchTest, FPSCanBeChanged) {
  76. game().set_target_fps(env::fps::v120);
  77. auto fps120 = 1.0 / 120.0;
  78. EXPECT_CALL(scene(), update(Ge(fps120))).Times(AnyNumber());
  79. EXPECT_CALL(scene(), update(Lt(fps120))).Times(0);
  80. for (int i = 0; i < 10; i++) {
  81. game().update();
  82. }
  83. }
  84. TEST_F(GameDispatchTest, UpdateHasNoUpperBoundOnTime) {
  85. EXPECT_CALL(scene(), update(Ge(0.03))).Times(1);
  86. usleep(300000);
  87. game().update();
  88. }
  89. TEST_F(GameDispatchTest, SetCurrentTimestampOverridesWaiting) {
  90. auto fps60 = 1.0 / 60.0;
  91. EXPECT_CALL(scene(), update(Ge(0.03))).Times(0);
  92. EXPECT_CALL(scene(), update(FloatNear(fps60, 1E-2))).Times(1);
  93. usleep(300000);
  94. game().set_current_timestamp();
  95. game().update();
  96. }
  97. TEST_F(GameDispatchTest, RenderDispatchesToCurrentScene) {
  98. EXPECT_CALL(scene(), render()).Times(1);
  99. game().render();
  100. }
  101. using MouseEventTest = GameDispatchTest;
  102. TEST_F(MouseEventTest, DispatchesToCurrentScene) {
  103. EXPECT_CALL(scene(), handle_mouse_event(_)).Times(1);
  104. game().process_mouse_event({{0.f, 0.f}}, true);
  105. }
  106. MATCHER(WasPressed, "") {
  107. return (arg.type & engine::event::PRESSED_MASK) != 0;
  108. }
  109. TEST_F(MouseEventTest, CanPressAndRelease) {
  110. EXPECT_CALL(scene(), handle_mouse_event(WasPressed())).Times(1);
  111. game().process_mouse_event({}, true);
  112. EXPECT_CALL(scene(), handle_mouse_event(Not(WasPressed()))).Times(1);
  113. game().process_mouse_event({}, false);
  114. }
  115. TEST_F(MouseEventTest, TranslatesFrameOfRef) {
  116. // Resize the scene (not a supported operation)
  117. scene_.reset(new mock_scene(dispatch_, {10.f, 10.f}));
  118. game().register_scene(scene_);
  119. game().activate_scene("mock2");
  120. auto mouse_pos = &engine::event::mouse_event::current_mouse_position;
  121. math::vec2 const where{5.f, 5.f};
  122. EXPECT_CALL(scene(), handle_mouse_event(Field(mouse_pos, where))).Times(1);
  123. game().process_mouse_event({{50.f, 50.f}}, true);
  124. }
  125. using KeyEventTest = GameDispatchTest;
  126. TEST_F(KeyEventTest, DoesNotDispatchUnmappedKeys) {
  127. EXPECT_CALL(scene(), handle_key_event(_)).Times(0);
  128. game().process_key_event('A', true);
  129. }
  130. TEST_F(KeyEventTest, RemapsKeyWhenForwarding) {
  131. // TODO: Properly set this instead of using this garbage
  132. const_cast<engine::key_binding &>(scene().keys()).emplace('A', 'B');
  133. auto key_value = &engine::event::key_event::key;
  134. EXPECT_CALL(scene(), handle_key_event(Field(key_value, 'A'))).Times(0);
  135. EXPECT_CALL(scene(), handle_key_event(Field(key_value, 'B'))).Times(1);
  136. game().process_key_event('A', true);
  137. }