game_dispatch_test.cxx 4.8 KB

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