| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- //
- // manager_test.cxx
- // graphics
- //
- // Created by Sam Jaffe on 6/6/19.
- // Copyright © 2019 Sam Jaffe. All rights reserved.
- //
- #include <gmock/gmock.h>
- #include "../src/helper.hpp"
- #include "game/graphics/manager.hpp"
- #include "game/graphics/material.hpp"
- #include "game/graphics/shader.hpp"
- #include "game/graphics/shader_program.hpp"
- #include "game/graphics/texture.hpp"
- using testing::AllOf;
- using testing::AnyNumber;
- using testing::Eq;
- using testing::Ge;
- using testing::Le;
- using testing::SizeIs;
- using testing::_;
- struct mock_manager_impl : graphics::manager {
- using shader = graphics::shader;
- using shader_program = graphics::shader_program;
- using texture = graphics::texture;
- MOCK_CONST_METHOD2(compile_shader,
- shader(graphics::shaders::type, std::string const &));
- MOCK_CONST_METHOD2(compile_program,
- shader_program(identity<shader>, identity<shader>));
- shader compile(graphics::shaders::type t, std::string const & s) const {
- return compile_shader(t, s);
- }
- shader_program compile(identity<shader> f, identity<shader> v) const {
- return compile_program(f, v);
- }
- MOCK_CONST_METHOD3(compile, texture(graphics::textures::format, math::vec2i,
- void const *));
- };
- template <typename T> identity<T> cast(unsigned int id) {
- return *reinterpret_cast<identity<T> *>(&id);
- }
- auto cast_p(unsigned int id) { return cast<graphics::shader_program>(id); }
- struct ManagerTest : testing::Test {
- void SetUp() override;
- mock_manager_impl mock;
- };
- void ManagerTest::SetUp() {
- using testing::Invoke;
- using testing::WithArg;
- // Start with a new set of IDs
- auto next_tex = [](math::vec2i const & sz) mutable {
- static unsigned int i = 0;
- return graphics::texture{++i, sz};
- };
- ON_CALL(mock, compile(_, _, _)).WillByDefault(WithArg<1>(Invoke(next_tex)));
- }
- TEST_F(ManagerTest, DoesNotGreedilyCompile) {
- EXPECT_CALL(mock, compile(_, _, _)).Times(0);
- EXPECT_CALL(mock, compile_shader(_, _)).Times(0);
- EXPECT_CALL(mock, compile_program(_, _)).Times(0);
- }
- using MaterialTest = ManagerTest;
- // TEST_F(MaterialTest, ThrowsExceptionIfNoTexOrUniform) {
- // EXPECT_CALL(mock, compile(_, _, _)).Times(AnyNumber());
- // // TODO (sjaffe): Throw a specific exception here, since throw; kills us
- // EXPECT_ANY_THROW(mock.get(cast_p(1), "", ""));
- //}
- TEST_F(MaterialTest, GeneratesUniformTexturesIfNoTexFile) {
- using graphics::materials::uniform;
- EXPECT_CALL(mock, compile(_, make_vector(1, 1), _)).Times(3);
- auto material_id = mock.get(cast_p(1), "", "u_normalMap");
- auto material = mock.get(material_id);
- // Ensure that the material is the 'same one' i.e.
- // mock.get(mock.get(id)) == mock.get(id)
- EXPECT_THAT(material, Eq(material_id));
- // Uniforms are always sized 1x1
- EXPECT_THAT(material.size, Eq(make_vector(1, 1)));
- EXPECT_THAT(material.uniforms, SizeIs(1));
- // Because we never initialize any textures, we are within the first three
- // texture ID units.
- EXPECT_THAT(material.uniforms[0].texture.id, AllOf(Ge(1), Le(3)));
- // Test the mapping from "u_normalMap" to uniform::NORMAL
- EXPECT_THAT(material.uniforms[0].uniform_id, Eq(uniform::NORMAL));
- }
|