FileStateCacheTests.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <native/tests/FileStateCache/FileStateCacheTests.h>
  9. #include <native/utilities/assetUtils.h>
  10. #include <native/unittests/UnitTestUtils.h>
  11. #include <AzFramework/IO/LocalFileIO.h>
  12. namespace UnitTests
  13. {
  14. using AssetFileInfo = AssetProcessor::AssetFileInfo;
  15. void FileStateCacheTests::SetUp()
  16. {
  17. m_temporarySourceDir = QDir(m_temporaryDir.path());
  18. AZ::IO::FileIOBase::SetInstance(aznew AZ::IO::LocalFileIO());
  19. m_fileStateCache = AZStd::make_unique<AssetProcessor::FileStateCache>();
  20. }
  21. void FileStateCacheTests::TearDown()
  22. {
  23. m_fileStateCache = nullptr;
  24. delete AZ::IO::FileIOBase::GetInstance();
  25. AZ::IO::FileIOBase::SetInstance(nullptr);
  26. }
  27. void FileStateCacheTests::CheckForFile(QString path, bool shouldExist)
  28. {
  29. bool exists = false;
  30. AssetProcessor::FileStateInfo fileInfo;
  31. auto* fileStateInterface = AZ::Interface<AssetProcessor::IFileStateRequests>::Get();
  32. ASSERT_NE(fileStateInterface, nullptr);
  33. exists = fileStateInterface->Exists(path);
  34. ASSERT_EQ(exists, shouldExist);
  35. exists = fileStateInterface->GetFileInfo(path, &fileInfo);
  36. ASSERT_EQ(exists, shouldExist);
  37. if (exists)
  38. {
  39. ASSERT_EQ(AssetUtilities::NormalizeFilePath(fileInfo.m_absolutePath), AssetUtilities::NormalizeFilePath(path));
  40. ASSERT_FALSE(fileInfo.m_isDirectory);
  41. ASSERT_EQ(fileInfo.m_fileSize, 0);
  42. }
  43. }
  44. TEST_F(FileStateCacheTests, QueryFile_ShouldNotExist)
  45. {
  46. QString testPath = m_temporarySourceDir.absoluteFilePath("test.txt");
  47. // Make the file but don't tell the cache about it
  48. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(testPath));
  49. CheckForFile(testPath, false);
  50. }
  51. TEST_F(FileStateCacheTests, QueryAddedFile_ShouldExist)
  52. {
  53. QString testPath = m_temporarySourceDir.absoluteFilePath("test.txt");
  54. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(testPath));
  55. m_fileStateCache->AddFile(testPath);
  56. CheckForFile(testPath, true);
  57. }
  58. TEST_F(FileStateCacheTests, QueryBulkAddedFile_ShouldExist)
  59. {
  60. QString testPath = m_temporarySourceDir.absoluteFilePath("test.txt");
  61. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(testPath));
  62. QSet<AssetFileInfo> infoSet;
  63. AssetFileInfo fileInfo;
  64. fileInfo.m_filePath = testPath;
  65. fileInfo.m_isDirectory = false;
  66. fileInfo.m_fileSize = 0;
  67. fileInfo.m_modTime = QFileInfo(testPath).lastModified();
  68. infoSet.insert(fileInfo);
  69. m_fileStateCache->AddInfoSet(infoSet);
  70. CheckForFile(testPath, true);
  71. }
  72. TEST_F(FileStateCacheTests, QueryRemovedFile_ShouldNotExist)
  73. {
  74. QString testPath = m_temporarySourceDir.absoluteFilePath("test.txt");
  75. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(testPath));
  76. m_fileStateCache->AddFile(testPath);
  77. m_fileStateCache->RemoveFile(testPath);
  78. CheckForFile(testPath, false);
  79. }
  80. TEST_F(FileStateCacheTests, AddAndRemoveFolder_ShouldAddAndRemoveSubFiles)
  81. {
  82. QDir testFolder = m_temporarySourceDir.absoluteFilePath("subfolder");
  83. QString testPath1 = testFolder.absoluteFilePath("test1.txt");
  84. QString testPath2 = testFolder.absoluteFilePath("test2.txt");
  85. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(testPath1));
  86. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(testPath2));
  87. m_fileStateCache->AddFile(testFolder.absolutePath());
  88. CheckForFile(testPath1, true);
  89. CheckForFile(testPath2, true);
  90. m_fileStateCache->RemoveFile(testFolder.absolutePath());
  91. CheckForFile(testPath1, false);
  92. CheckForFile(testPath2, false);
  93. }
  94. TEST_F(FileStateCacheTests, UpdateFileAndQuery_ShouldExist)
  95. {
  96. QString testPath = m_temporarySourceDir.absoluteFilePath("test.txt");
  97. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(testPath));
  98. QSet<AssetFileInfo> infoSet;
  99. AssetFileInfo fileInfo;
  100. fileInfo.m_filePath = testPath;
  101. fileInfo.m_isDirectory = false;
  102. fileInfo.m_fileSize = 1234; // Setting the file size to non-zero (even though the actual file is 0), UpdateFile should update this to 0 and allow CheckForFile to pass as a result
  103. fileInfo.m_modTime = QFileInfo(testPath).lastModified();
  104. infoSet.insert(fileInfo);
  105. m_fileStateCache->AddInfoSet(infoSet);
  106. m_fileStateCache->UpdateFile(testPath);
  107. CheckForFile(testPath, true);
  108. }
  109. TEST_F(FileStateCacheTests, PassthroughTest)
  110. {
  111. m_fileStateCache = nullptr; // Need to release the existing one first since only one handler can exist for the ebus
  112. m_fileStateCache = AZStd::make_unique<AssetProcessor::FileStatePassthrough>();
  113. QString testPath = m_temporarySourceDir.absoluteFilePath("test.txt");
  114. CheckForFile(testPath, false);
  115. ASSERT_TRUE(UnitTestUtils::CreateDummyFile(testPath));
  116. CheckForFile(testPath, true);
  117. }
  118. TEST_F(FileStateCacheTests, HandlesMixedSeperators)
  119. {
  120. QSet<AssetFileInfo> infoSet;
  121. AssetFileInfo fileInfo;
  122. fileInfo.m_filePath = R"(c:\some/test\file.txt)";
  123. infoSet.insert(fileInfo);
  124. m_fileStateCache->AddInfoSet(infoSet);
  125. CheckForFile(R"(c:\some\test\file.txt)", true);
  126. CheckForFile(R"(c:/some/test/file.txt)", true);
  127. }
  128. }