manager_test.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. class ManagerTest : public testing::Test {
  44. public:
  45. void SetUp() override;
  46. mock_manager_impl mock;
  47. private:
  48. struct {
  49. graphics::texture operator()(math::vec2i const & sz) { return {++i, sz}; }
  50. unsigned int i{0};
  51. } next_tex;
  52. };
  53. void ManagerTest::SetUp() {
  54. using testing::Invoke;
  55. using testing::WithArg;
  56. // Start with a new set of IDs
  57. ON_CALL(mock, compile(_, _, _))
  58. .WillByDefault(WithArg<1>(Invoke(std::ref(next_tex))));
  59. }
  60. TEST_F(ManagerTest, DoesNotGreedilyCompile) {
  61. EXPECT_CALL(mock, compile(_, _, _)).Times(0);
  62. EXPECT_CALL(mock, compile_shader(_, _)).Times(0);
  63. EXPECT_CALL(mock, compile_program(_, _)).Times(0);
  64. }
  65. using MaterialTest = ManagerTest;
  66. // TEST_F(MaterialTest, ThrowsExceptionIfNoTexOrUniform) {
  67. // EXPECT_CALL(mock, compile(_, _, _)).Times(AnyNumber());
  68. // // TODO (sjaffe): Throw a specific exception here, since throw; kills us
  69. // EXPECT_ANY_THROW(mock.get(cast_p(1), "", ""));
  70. //}
  71. TEST_F(MaterialTest, GeneratesUniformTexturesIfNoTexFile) {
  72. using graphics::materials::uniform;
  73. EXPECT_CALL(mock, compile(_, make_vector(1, 1), _)).Times(3);
  74. EXPECT_NO_THROW(mock.get(cast_p(1), "", "u_normalMap"));
  75. }
  76. TEST_F(MaterialTest, CreatedMaterialCanBeRefetched) {
  77. using graphics::materials::uniform;
  78. EXPECT_CALL(mock, compile(_, make_vector(1, 1), _)).Times(3);
  79. auto material_id = mock.get(cast_p(1), "", "u_normalMap");
  80. auto material = mock.get(material_id);
  81. // Ensure that the material is the 'same one' i.e.
  82. // mock.get(mock.get(id)) == mock.get(id)
  83. EXPECT_THAT(material, Eq(material_id));
  84. }
  85. TEST_F(MaterialTest, UniformMaterialIsOneByOne) {
  86. using graphics::materials::uniform;
  87. EXPECT_CALL(mock, compile(_, make_vector(1, 1), _)).Times(3);
  88. auto material = mock.get(mock.get(cast_p(1), "", "u_normalMap"));
  89. // Uniforms are always sized 1x1
  90. EXPECT_THAT(material.size, Eq(make_vector(1, 1)));
  91. }
  92. TEST_F(MaterialTest, UniformMaterialHasDataBindingToNormalTex) {
  93. using graphics::materials::uniform;
  94. EXPECT_CALL(mock, compile(_, make_vector(1, 1), _)).Times(3);
  95. auto material = mock.get(mock.get(cast_p(1), "", "u_normalMap"));
  96. EXPECT_THAT(material.uniforms, SizeIs(1));
  97. // Because we never initialize any textures, we are within the first three
  98. // texture ID units.
  99. EXPECT_THAT(material.uniforms[0].texture.id, AllOf(Ge(1), Le(3)));
  100. // Test the mapping from "u_normalMap" to uniform::NORMAL
  101. EXPECT_THAT(material.uniforms[0].uniform_id, Eq(uniform::NORMAL));
  102. }