game_dispatch_test.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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", game) {}
  19. MOCK_METHOD1(update, void(float));
  20. MOCK_METHOD1(render, void(graphics::renderer &));
  21. MOCK_METHOD1(handle_key_event, void(engine::event::key_event));
  22. MOCK_METHOD1(handle_mouse_event, void(engine::event::mouse_event));
  23. };
  24. class GameDispatchTest : public testing::Test {
  25. protected:
  26. void SetUp() override;
  27. void TearDown() override;
  28. mock_scene & scene() const { return *scene_; }
  29. engine::game_dispatch & game() const { return *dispatch_; }
  30. std::shared_ptr<stub_renderer> renderer_;
  31. std::shared_ptr<engine::game_dispatch> dispatch_;
  32. std::shared_ptr<mock_scene> scene_;
  33. };
  34. void GameDispatchTest::SetUp() {
  35. env::resize_screen({{100, 100}});
  36. renderer_.reset(new stub_renderer);
  37. dispatch_.reset(new engine::game_dispatch(renderer_));
  38. scene_.reset(new mock_scene(dispatch_));
  39. game().register_scene(scene_);
  40. game().activate_scene("mock");
  41. }
  42. void GameDispatchTest::TearDown() {
  43. scene_.reset();
  44. dispatch_.reset();
  45. renderer_.reset();
  46. }
  47. using testing::AnyNumber;
  48. using testing::Eq;
  49. using testing::Field;
  50. using testing::FloatNear;
  51. using testing::Ge;
  52. using testing::Lt;
  53. using testing::_;
  54. TEST_F(GameDispatchTest, ManagerIsFetchedFromRenderer) {
  55. EXPECT_THAT(&game().graphics_manager(), Eq(renderer_->manager().get()));
  56. }
  57. // TODO: Set FPS to a much smaller value for testing purposes (e.g. 512fps)
  58. TEST_F(GameDispatchTest, UpdateDispatchesToCurrentScene) {
  59. EXPECT_CALL(scene(), update(_)).Times(1);
  60. game().update();
  61. }
  62. TEST_F(GameDispatchTest, UpdateIsCappedInFrequencyByFPSParam) {
  63. auto fps60 = 1.0 / 60.0;
  64. EXPECT_CALL(scene(), update(Ge(fps60))).Times(AnyNumber());
  65. EXPECT_CALL(scene(), update(Lt(fps60))).Times(0);
  66. for (int i = 0; i < 10; i++) {
  67. game().update();
  68. }
  69. }
  70. TEST_F(GameDispatchTest, UpdateHasNoUpperBoundOnTime) {
  71. EXPECT_CALL(scene(), update(Ge(0.03))).Times(1);
  72. usleep(300000);
  73. game().update();
  74. }
  75. TEST_F(GameDispatchTest, SetCurrentTimestampOverridesWaiting) {
  76. auto fps60 = 1.0 / 60.0;
  77. EXPECT_CALL(scene(), update(Ge(0.03))).Times(0);
  78. EXPECT_CALL(scene(), update(FloatNear(fps60, 1E-2))).Times(1);
  79. usleep(300000);
  80. game().set_current_timestamp();
  81. game().update();
  82. }
  83. TEST_F(GameDispatchTest, RenderDispatchesToCurrentScene) {
  84. EXPECT_CALL(scene(), render(_)).Times(1);
  85. game().render();
  86. }
  87. using MouseEventTest = GameDispatchTest;
  88. TEST_F(MouseEventTest, DispatchesToCurrentScene) {
  89. EXPECT_CALL(scene(), handle_mouse_event(_)).Times(1);
  90. game().process_mouse_event({{0.f, 0.f}}, true);
  91. }
  92. TEST_F(MouseEventTest, TranslatesFrameOfRef) {
  93. // TODO: Actually set the local scene size element...
  94. auto mouse_pos = &engine::event::mouse_event::current_mouse_position;
  95. auto const where = make_vector(5.f, 5.f);
  96. EXPECT_CALL(scene(), handle_mouse_event(Field(mouse_pos, where))).Times(1);
  97. game().process_mouse_event({{50.f, 50.f}}, true);
  98. }
  99. using KeyEventTest = GameDispatchTest;
  100. TEST_F(KeyEventTest, DoesNotDispatchUnmappedKeys) {
  101. EXPECT_CALL(scene(), handle_key_event(_)).Times(0);
  102. game().process_key_event('A', true);
  103. }
  104. TEST_F(KeyEventTest, RemapsKeyWhenForwarding) {
  105. // TODO: Properly set this instead of using this garbage
  106. const_cast<engine::key_binding &>(scene().keys()).emplace('A', 'B');
  107. auto key_value = &engine::event::key_event::key;
  108. EXPECT_CALL(scene(), handle_key_event(Field(key_value, 'A'))).Times(0);
  109. EXPECT_CALL(scene(), handle_key_event(Field(key_value, 'B'))).Times(1);
  110. game().process_key_event('A', true);
  111. }