MockPrefabFileIOActionValidator.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Prefab/MockPrefabFileIOActionValidator.h>
  9. #include <AzCore/IO/Path/Path.h>
  10. #include <AzCore/JSON/prettywriter.h>
  11. #include <Prefab/PrefabLoaderInterface.h>
  12. namespace UnitTest
  13. {
  14. MockPrefabFileIOActionValidator::MockPrefabFileIOActionValidator()
  15. {
  16. // Cache the existing file io instance and build our mock file io
  17. m_priorFileIO = AZ::IO::FileIOBase::GetInstance();
  18. m_fileIOMock = AZStd::make_unique<testing::NiceMock<AZ::IO::MockFileIOBase>>();
  19. // Swap out current file io instance for our mock
  20. AZ::IO::FileIOBase::SetInstance(nullptr);
  21. AZ::IO::FileIOBase::SetInstance(m_fileIOMock.get());
  22. // Setup the default returns for our mock file io calls
  23. AZ::IO::MockFileIOBase::InstallDefaultReturns(*m_fileIOMock.get());
  24. }
  25. MockPrefabFileIOActionValidator::~MockPrefabFileIOActionValidator()
  26. {
  27. // Restore our original file io instance
  28. AZ::IO::FileIOBase::SetInstance(nullptr);
  29. AZ::IO::FileIOBase::SetInstance(m_priorFileIO);
  30. }
  31. void MockPrefabFileIOActionValidator::ReadPrefabDom(
  32. AZ::IO::PathView prefabFilePath,
  33. const AzToolsFramework::Prefab::PrefabDom& prefabFileContentDom,
  34. AZ::IO::ResultCode expectedReadResultCode,
  35. AZ::IO::ResultCode expectedOpenResultCode,
  36. AZ::IO::ResultCode expectedSizeResultCode,
  37. AZ::IO::ResultCode expectedCloseResultCode)
  38. {
  39. rapidjson::StringBuffer prefabFileContentBuffer;
  40. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(prefabFileContentBuffer);
  41. prefabFileContentDom.Accept(writer);
  42. AZStd::string prefabFileContent(prefabFileContentBuffer.GetString());
  43. ReadPrefabDom(prefabFilePath, prefabFileContent,
  44. expectedReadResultCode, expectedOpenResultCode, expectedSizeResultCode, expectedCloseResultCode);
  45. }
  46. void MockPrefabFileIOActionValidator::ReadPrefabDom(
  47. AZ::IO::PathView prefabFilePath,
  48. const AZStd::string& prefabFileContent,
  49. AZ::IO::ResultCode expectedReadResultCode,
  50. AZ::IO::ResultCode expectedOpenResultCode,
  51. AZ::IO::ResultCode expectedSizeResultCode,
  52. AZ::IO::ResultCode expectedCloseResultCode)
  53. {
  54. AZ::IO::HandleType fileHandle = m_fileHandleCounter++;
  55. AZ::IO::Path prefabFullPath = AZ::Interface<AzToolsFramework::Prefab::PrefabLoaderInterface>::Get()->GetFullPath(AZStd::string_view(prefabFilePath));
  56. EXPECT_CALL(*m_fileIOMock.get(), Open(
  57. testing::StrEq(prefabFullPath.c_str()), testing::_, testing::_))
  58. .WillRepeatedly(
  59. testing::DoAll(
  60. testing::SetArgReferee<2>(fileHandle),
  61. testing::Return(AZ::IO::Result(expectedOpenResultCode))));
  62. EXPECT_CALL(*m_fileIOMock.get(), Size(fileHandle, testing::_))
  63. .WillRepeatedly(
  64. testing::DoAll(
  65. testing::SetArgReferee<1>(prefabFileContent.size()),
  66. testing::Return(AZ::IO::Result(expectedSizeResultCode))));
  67. EXPECT_CALL(*m_fileIOMock.get(), Read(fileHandle, testing::_, prefabFileContent.size(), testing::_, testing::_))
  68. .WillRepeatedly(testing::Invoke([prefabFileContent, expectedReadResultCode](AZ::IO::HandleType, void* buffer, AZ::u64, bool, AZ::u64* bytesRead)
  69. {
  70. memcpy(buffer, prefabFileContent.data(), prefabFileContent.size());
  71. *bytesRead = prefabFileContent.size();
  72. return AZ::IO::Result(expectedReadResultCode);
  73. }));
  74. EXPECT_CALL(*m_fileIOMock.get(), Close(fileHandle))
  75. .WillRepeatedly(testing::Return(AZ::IO::Result(expectedCloseResultCode)));
  76. }
  77. }