game_dispatch_test.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. private:
  31. std::shared_ptr<stub_renderer> renderer_;
  32. std::shared_ptr<engine::game_dispatch> dispatch_;
  33. std::shared_ptr<mock_scene> scene_;
  34. };
  35. void GameDispatchTest::SetUp() {
  36. env::resize_screen({{100, 100}});
  37. renderer_.reset(new stub_renderer);
  38. dispatch_.reset(new engine::game_dispatch(renderer_));
  39. scene_.reset(new mock_scene(dispatch_));
  40. game().register_scene(scene_);
  41. game().activate_scene("mock");
  42. }
  43. void GameDispatchTest::TearDown() {
  44. scene_.reset();
  45. dispatch_.reset();
  46. renderer_.reset();
  47. }
  48. using testing::AnyNumber;
  49. using testing::Field;
  50. using testing::Ge;
  51. using testing::Lt;
  52. using testing::_;
  53. TEST_F(GameDispatchTest, UpdateDispatchesToCurrentScene) {
  54. EXPECT_CALL(scene(), update(_)).Times(1);
  55. game().update();
  56. }
  57. TEST_F(GameDispatchTest, UpdateIsCappedInFrequencyByFPSParam) {
  58. auto fps60 = 1.0 / 60.0;
  59. EXPECT_CALL(scene(), update(Ge(fps60))).Times(AnyNumber());
  60. EXPECT_CALL(scene(), update(Lt(fps60))).Times(0);
  61. for (int i = 0; i < 10; i++) {
  62. game().update();
  63. }
  64. }
  65. TEST_F(GameDispatchTest, RenderDispatchesToCurrentScene) {
  66. EXPECT_CALL(scene(), render(_)).Times(1);
  67. game().render();
  68. }
  69. using MouseEventTest = GameDispatchTest;
  70. TEST_F(MouseEventTest, DispatchesToCurrentScene) {
  71. EXPECT_CALL(scene(), handle_mouse_event(_)).Times(1);
  72. game().process_mouse_event({{0.f, 0.f}}, true);
  73. }
  74. TEST_F(MouseEventTest, TranslatesFrameOfRef) {
  75. auto mouse_pos = &engine::event::mouse_event::current_mouse_position;
  76. auto const where = make_vector(5.f, 5.f);
  77. EXPECT_CALL(scene(), handle_mouse_event(Field(mouse_pos, where))).Times(1);
  78. game().process_mouse_event({{50.f, 50.f}}, true);
  79. }