manager_test.cxx 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // manager_test.cxx
  3. // graphics
  4. //
  5. // Created by Sam Jaffe on 6/6/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #include <gmock/gmock.h>
  9. #include "../src/helper.hpp"
  10. #include "game/graphics/manager.hpp"
  11. #include "game/graphics/material.hpp"
  12. #include "game/graphics/shader.hpp"
  13. #include "game/graphics/shader_program.hpp"
  14. #include "game/graphics/texture.hpp"
  15. using testing::AllOf;
  16. using testing::AnyNumber;
  17. using testing::Eq;
  18. using testing::Ge;
  19. using testing::Le;
  20. using testing::SizeIs;
  21. using testing::_;
  22. struct mock_manager_impl : graphics::manager {
  23. using shader = graphics::shader;
  24. using shader_program = graphics::shader_program;
  25. using texture = graphics::texture;
  26. MOCK_CONST_METHOD2(compile_shader,
  27. shader(graphics::shaders::type, std::string const &));
  28. MOCK_CONST_METHOD2(compile_program,
  29. shader_program(identity<shader>, identity<shader>));
  30. shader compile(graphics::shaders::type t, std::string const & s) const {
  31. return compile_shader(t, s);
  32. }
  33. shader_program compile(identity<shader> f, identity<shader> v) const {
  34. return compile_program(f, v);
  35. }
  36. MOCK_CONST_METHOD3(compile, texture(graphics::textures::format, math::vec2i,
  37. void const *));
  38. };
  39. template <typename T> identity<T> cast(unsigned int id) {
  40. return *reinterpret_cast<identity<T> *>(&id);
  41. }
  42. auto cast_p(unsigned int id) { return cast<graphics::shader_program>(id); }
  43. struct ManagerTest : testing::Test {
  44. void SetUp() override;
  45. mock_manager_impl mock;
  46. };
  47. void ManagerTest::SetUp() {
  48. using testing::Invoke;
  49. using testing::WithArg;
  50. // Start with a new set of IDs
  51. auto next_tex = [](math::vec2i const & sz) mutable {
  52. static unsigned int i = 0;
  53. return graphics::texture{++i, sz};
  54. };
  55. ON_CALL(mock, compile(_, _, _)).WillByDefault(WithArg<1>(Invoke(next_tex)));
  56. }
  57. TEST_F(ManagerTest, DoesNotGreedilyCompile) {
  58. EXPECT_CALL(mock, compile(_, _, _)).Times(0);
  59. EXPECT_CALL(mock, compile_shader(_, _)).Times(0);
  60. EXPECT_CALL(mock, compile_program(_, _)).Times(0);
  61. }
  62. using MaterialTest = ManagerTest;
  63. // TEST_F(MaterialTest, ThrowsExceptionIfNoTexOrUniform) {
  64. // EXPECT_CALL(mock, compile(_, _, _)).Times(AnyNumber());
  65. // // TODO (sjaffe): Throw a specific exception here, since throw; kills us
  66. // EXPECT_ANY_THROW(mock.get(cast_p(1), "", ""));
  67. //}
  68. TEST_F(MaterialTest, GeneratesUniformTexturesIfNoTexFile) {
  69. using graphics::materials::uniform;
  70. EXPECT_CALL(mock, compile(_, make_vector(1, 1), _)).Times(3);
  71. auto material_id = mock.get(cast_p(1), "", "u_normalMap");
  72. auto material = mock.get(material_id);
  73. // Ensure that the material is the 'same one' i.e.
  74. // mock.get(mock.get(id)) == mock.get(id)
  75. EXPECT_THAT(material, Eq(material_id));
  76. // Uniforms are always sized 1x1
  77. EXPECT_THAT(material.size, Eq(make_vector(1, 1)));
  78. EXPECT_THAT(material.uniforms, SizeIs(1));
  79. // Because we never initialize any textures, we are within the first three
  80. // texture ID units.
  81. EXPECT_THAT(material.uniforms[0].texture.id, AllOf(Ge(1), Le(3)));
  82. // Test the mapping from "u_normalMap" to uniform::NORMAL
  83. EXPECT_THAT(material.uniforms[0].uniform_id, Eq(uniform::NORMAL));
  84. }