resourceTest.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "unitTesting.h"
  23. #include "platform/platform.h"
  24. #include "core/util/fourcc.h"
  25. #include "console/console.h"
  26. #include "core/resourceManager.h"
  27. static bool destructorCalled;
  28. struct TestResource
  29. {
  30. public:
  31. U64 values[4] = { 0,0,0,0 };
  32. virtual ~TestResource() { destructorCalled = true; }
  33. static Resource< TestResource > load(const Torque::Path& path);
  34. static ResourceRegisterPostLoadSignal< TestResource > _smAutoLoad;
  35. static void _onTestLoaded(Resource< TestResource >& test);
  36. };
  37. template<> ResourceBase::Signature Resource<TestResource>::signature()
  38. {
  39. return MakeFourCC('T', 'E', 'S', 'T'); // Direct Draw Surface
  40. }
  41. template<> void* Resource<TestResource>::create(const Torque::Path& path)
  42. {
  43. TestResource* testRes = new TestResource;
  44. return testRes;
  45. }
  46. Resource<TestResource> TestResource::load(const Torque::Path& path)
  47. {
  48. Resource<TestResource> testRes = ResourceManager::get().load(path);
  49. return testRes;
  50. }
  51. ResourceRegisterPostLoadSignal< TestResource > TestResource::_smAutoLoad(&TestResource::_onTestLoaded);
  52. void TestResource::_onTestLoaded(Resource<TestResource>& test)
  53. {
  54. test->values[0] = 1;
  55. test->values[1] = 1;
  56. test->values[2] = 1;
  57. test->values[3] = 1;
  58. }
  59. TEST(ResourceManagerTests, All_Resource_Functionality)
  60. {
  61. if (true)
  62. {
  63. Resource<TestResource> testRes;
  64. testRes = TestResource::load("empty");
  65. EXPECT_EQ(testRes.signature(), MakeFourCC('T', 'E', 'S', 'T')) << "Incorrect resource type returned!";
  66. EXPECT_EQ(testRes.getPath(), "empty") << "Wrong path!";
  67. U64 postLoadValues[4];
  68. postLoadValues[0] = 1;
  69. postLoadValues[1] = 1;
  70. postLoadValues[2] = 1;
  71. postLoadValues[3] = 1;
  72. for (U32 i = 0; i < 4; i++)
  73. {
  74. EXPECT_EQ(testRes->values[i], postLoadValues[i]) << "Array not equal";
  75. }
  76. EXPECT_EQ(destructorCalled, false) << "Destructor true should be false";
  77. }
  78. EXPECT_EQ(destructorCalled, true) << "Destructor false should be true";
  79. }