UtilsTests.cpp 8.0 KB

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