UtilsTests.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <AzCore/UnitTest/TestTypes.h>
  9. #include <Application.h>
  10. #include <ProjectUtils.h>
  11. #include <ProjectManagerDefs.h>
  12. #include <ProjectManager_Test_Traits_Platform.h>
  13. #include <QFile>
  14. #include <QTextStream>
  15. #include <QTimer>
  16. #include <QApplication>
  17. #include <QKeyEvent>
  18. #include <QDir>
  19. namespace O3DE::ProjectManager
  20. {
  21. namespace ProjectUtils
  22. {
  23. class ProjectManagerUtilsTests
  24. : public ::UnitTest::LeakDetectionFixture
  25. {
  26. public:
  27. ProjectManagerUtilsTests()
  28. {
  29. m_projectAPath = QDir(m_testFolder.GetDirectory()).path() + QDir::separator() + "ProjectA";
  30. m_projectBPath = QDir(m_testFolder.GetDirectory()).path() + QDir::separator() + "ProjectB";
  31. // Replaces first 'A' with 'B'
  32. m_projectABuildPath = QString("%1%2%3").arg(m_projectAPath, QDir::separator(), ProjectBuildDirectoryName);
  33. m_projectBBuildPath = QString("%1%2%3").arg(m_projectBPath, QDir::separator(), ProjectBuildDirectoryName);
  34. QDir dir;
  35. dir.mkpath(m_projectABuildPath);
  36. dir.mkdir(m_projectBPath);
  37. m_projectAOrigFilePath = QString("%1%2%3").arg(m_projectAPath, QDir::separator(), "origFile.txt");
  38. m_projectBOrigFilePath = QString("%1%2%3").arg(m_projectBPath, QDir::separator(), "origFile.txt");
  39. QFile origFile(m_projectAOrigFilePath);
  40. if (origFile.open(QIODevice::ReadWrite))
  41. {
  42. QTextStream stream(&origFile);
  43. stream << "orig" << Qt::endl;
  44. origFile.close();
  45. }
  46. m_projectAReplaceFilePath = QString("%1%2%3").arg(m_projectAPath, QDir::separator(), "replaceFile.txt");
  47. m_projectBReplaceFilePath = QString("%1%2%3").arg(m_projectBPath, QDir::separator(), "replaceFile.txt");
  48. QFile replaceFile(m_projectAReplaceFilePath);
  49. if (replaceFile.open(QIODevice::ReadWrite))
  50. {
  51. QTextStream stream(&replaceFile);
  52. stream << "replace" << Qt::endl;
  53. replaceFile.close();
  54. }
  55. m_projectABuildFilePath = QString("%1%2%3").arg(m_projectABuildPath, QDir::separator(), "build.obj");
  56. m_projectBBuildFilePath = QString("%1%2%3").arg(m_projectBBuildPath, QDir::separator(), "build.obj");
  57. QFile buildFile(m_projectABuildFilePath);
  58. if (buildFile.open(QIODevice::ReadWrite))
  59. {
  60. QTextStream stream(&buildFile);
  61. stream << "x0FFFFFFFF" << Qt::endl;
  62. buildFile.close();
  63. }
  64. }
  65. ~ProjectManagerUtilsTests()
  66. {
  67. QDir dirA(m_projectAPath);
  68. dirA.removeRecursively();
  69. QDir dirB(m_projectBPath);
  70. dirB.removeRecursively();
  71. }
  72. QString m_projectAPath;
  73. QString m_projectAOrigFilePath;
  74. QString m_projectAReplaceFilePath;
  75. QString m_projectABuildPath;
  76. QString m_projectABuildFilePath;
  77. QString m_projectBPath;
  78. QString m_projectBOrigFilePath;
  79. QString m_projectBReplaceFilePath;
  80. QString m_projectBBuildPath;
  81. QString m_projectBBuildFilePath;
  82. AZ::Test::ScopedAutoTempDirectory m_testFolder;
  83. };
  84. #if AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  85. TEST_F(ProjectManagerUtilsTests, DISABLED_MoveProject_MovesExpectedFiles)
  86. #else
  87. TEST_F(ProjectManagerUtilsTests, MoveProject_MovesExpectedFiles)
  88. #endif // !AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  89. {
  90. EXPECT_TRUE(MoveProject(
  91. m_projectAPath,
  92. m_projectBPath, nullptr,
  93. true, /*displayProgress=*/ false));
  94. QFileInfo origFile(m_projectAOrigFilePath);
  95. EXPECT_FALSE(origFile.exists());
  96. QFileInfo replaceFile(m_projectAReplaceFilePath);
  97. EXPECT_FALSE(replaceFile.exists());
  98. QFileInfo origFileMoved(m_projectBOrigFilePath);
  99. EXPECT_TRUE(origFileMoved.exists() && origFileMoved.isFile());
  100. QFileInfo replaceFileMoved(m_projectBReplaceFilePath);
  101. EXPECT_TRUE(replaceFileMoved.exists() && replaceFileMoved.isFile());
  102. }
  103. #if AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  104. TEST_F(ProjectManagerUtilsTests, DISABLED_MoveProject_DoesntMoveBuild)
  105. #else
  106. TEST_F(ProjectManagerUtilsTests, MoveProject_DoesntMoveBuild)
  107. #endif // !AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  108. {
  109. EXPECT_TRUE(MoveProject(
  110. m_projectAPath,
  111. m_projectBPath, nullptr,
  112. true, /*displayProgress=*/ false));
  113. QFileInfo origFile(m_projectAOrigFilePath);
  114. EXPECT_FALSE(origFile.exists());
  115. QFileInfo origFileMoved(m_projectBOrigFilePath);
  116. EXPECT_TRUE(origFileMoved.exists() && origFileMoved.isFile());
  117. QDir buildDir(m_projectBBuildPath);
  118. EXPECT_FALSE(buildDir.exists());
  119. }
  120. #if AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  121. TEST_F(ProjectManagerUtilsTests, DISABLED_CopyProject_CopiesExpectedFiles)
  122. #else
  123. TEST_F(ProjectManagerUtilsTests, CopyProject_CopiesExpectedFiles)
  124. #endif // !AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  125. {
  126. EXPECT_TRUE(CopyProject(
  127. m_projectAPath,
  128. m_projectBPath, nullptr,
  129. true, /*displayProgress=*/ false));
  130. QFileInfo origFile(m_projectAOrigFilePath);
  131. EXPECT_TRUE(origFile.exists());
  132. QFileInfo replaceFile(m_projectAReplaceFilePath);
  133. EXPECT_TRUE(replaceFile.exists());
  134. QFileInfo origFileMoved(m_projectBOrigFilePath);
  135. EXPECT_TRUE(origFileMoved.exists() && origFileMoved.isFile());
  136. QFileInfo replaceFileMoved(m_projectBReplaceFilePath);
  137. EXPECT_TRUE(replaceFileMoved.exists() && replaceFileMoved.isFile());
  138. }
  139. #if AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  140. TEST_F(ProjectManagerUtilsTests, DISABLED_CopyProject_DoesntCopyBuild)
  141. #else
  142. TEST_F(ProjectManagerUtilsTests, CopyProject_DoesntCopyBuild)
  143. #endif // !AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  144. {
  145. EXPECT_TRUE(CopyProject(
  146. m_projectAPath,
  147. m_projectBPath, nullptr,
  148. true, /*displayProgress=*/ false));
  149. QFileInfo origFile(m_projectAOrigFilePath);
  150. EXPECT_TRUE(origFile.exists());
  151. QFileInfo origFileMoved(m_projectBOrigFilePath);
  152. EXPECT_TRUE(origFileMoved.exists() && origFileMoved.isFile());
  153. QDir buildDir(m_projectBBuildPath);
  154. EXPECT_FALSE(buildDir.exists());
  155. }
  156. #if AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  157. TEST_F(ProjectManagerUtilsTests, DISABLED_ReplaceFile_Succeeds)
  158. #else
  159. TEST_F(ProjectManagerUtilsTests, ReplaceFile_Succeeds)
  160. #endif // !AZ_TRAIT_DISABLE_FAILED_PROJECT_MANAGER_TESTS
  161. {
  162. EXPECT_TRUE(ReplaceProjectFile(m_projectAOrigFilePath, m_projectAReplaceFilePath, nullptr, false));
  163. QFile origFile(m_projectAOrigFilePath);
  164. EXPECT_TRUE(origFile.open(QIODevice::ReadOnly));
  165. {
  166. QTextStream stream(&origFile);
  167. QString line = stream.readLine();
  168. EXPECT_EQ(line, "replace");
  169. origFile.close();
  170. }
  171. }
  172. } // namespace ProjectUtils
  173. } // namespace O3DE::ProjectManager