QCMakePresetItemModelTest.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "QCMakePresetItemModelTest.h"
  4. #include <utility>
  5. #include "QCMakePreset.h"
  6. #include "QCMakePresetItemModel.h"
  7. #include <QHash>
  8. #include <QMetaType>
  9. #include <QSignalSpy>
  10. #include <QVariant>
  11. #include <QVector>
  12. #include <QtTest>
  13. using QItemDataHash = QHash<Qt::ItemDataRole, QVariant>;
  14. void QCMakePresetItemModelTest::initTestCase()
  15. {
  16. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  17. QMetaType::registerComparators<QCMakePreset>();
  18. #endif
  19. }
  20. void QCMakePresetItemModelTest::initTestCase_data()
  21. {
  22. QTest::addColumn<QVector<QCMakePreset>>("presets");
  23. QTest::addColumn<QVector<QItemDataHash>>("data");
  24. QVector<QCMakePreset> presets{
  25. QCMakePreset{
  26. /*name=*/"no-description",
  27. /*description=*/"",
  28. /*description=*/"",
  29. /*generator=*/"",
  30. /*architecture=*/"",
  31. /*setArchitecture=*/true,
  32. /*toolset=*/"",
  33. /*setToolset=*/true,
  34. /*enabled=*/true,
  35. },
  36. QCMakePreset{
  37. /*name=*/"short-description",
  38. /*description=*/"Short Description",
  39. /*description=*/"",
  40. /*generator=*/"",
  41. /*architecture=*/"",
  42. /*setArchitecture=*/true,
  43. /*toolset=*/"",
  44. /*setToolset=*/true,
  45. /*enabled=*/true,
  46. },
  47. QCMakePreset{
  48. /*name=*/"long-description",
  49. /*description=*/"",
  50. /*description=*/"Long Description",
  51. /*generator=*/"",
  52. /*architecture=*/"",
  53. /*setArchitecture=*/true,
  54. /*toolset=*/"",
  55. /*setToolset=*/true,
  56. /*enabled=*/true,
  57. },
  58. QCMakePreset{
  59. /*name=*/"disabled",
  60. /*description=*/"",
  61. /*description=*/"",
  62. /*generator=*/"",
  63. /*architecture=*/"",
  64. /*setArchitecture=*/true,
  65. /*toolset=*/"",
  66. /*setToolset=*/true,
  67. /*enabled=*/false,
  68. },
  69. };
  70. QVector<QItemDataHash> data{
  71. QItemDataHash{
  72. { Qt::AccessibleDescriptionRole, "" },
  73. { Qt::DisplayRole, "no-description" },
  74. { Qt::ToolTipRole, "" },
  75. { Qt::UserRole, QVariant::fromValue(presets[0]) },
  76. { Qt::FontRole, QFont{} },
  77. },
  78. QItemDataHash{
  79. { Qt::AccessibleDescriptionRole, "" },
  80. { Qt::DisplayRole, "Short Description" },
  81. { Qt::ToolTipRole, "" },
  82. { Qt::UserRole, QVariant::fromValue(presets[1]) },
  83. { Qt::FontRole, QFont{} },
  84. },
  85. QItemDataHash{
  86. { Qt::AccessibleDescriptionRole, "" },
  87. { Qt::DisplayRole, "long-description" },
  88. { Qt::ToolTipRole, "Long Description" },
  89. { Qt::UserRole, QVariant::fromValue(presets[2]) },
  90. { Qt::FontRole, QFont{} },
  91. },
  92. QItemDataHash{
  93. { Qt::AccessibleDescriptionRole, "" },
  94. { Qt::DisplayRole, "disabled" },
  95. { Qt::ToolTipRole, "" },
  96. { Qt::UserRole, QVariant::fromValue(presets[3]) },
  97. { Qt::FontRole, QFont{} },
  98. },
  99. QItemDataHash{
  100. { Qt::AccessibleDescriptionRole, "separator" },
  101. { Qt::DisplayRole, QVariant{} },
  102. { Qt::ToolTipRole, QVariant{} },
  103. { Qt::UserRole, QVariant{} },
  104. { Qt::FontRole, QFont{} },
  105. },
  106. QItemDataHash{
  107. { Qt::AccessibleDescriptionRole, "" },
  108. { Qt::DisplayRole, "<custom>" },
  109. { Qt::ToolTipRole, "Specify all settings manually" },
  110. { Qt::UserRole, QVariant{} },
  111. { Qt::FontRole,
  112. []() {
  113. QFont f;
  114. f.setItalic(true);
  115. return f;
  116. }() },
  117. },
  118. };
  119. QTest::newRow("many") << presets << data;
  120. QTest::newRow("none") << QVector<QCMakePreset>{}
  121. << QVector<QItemDataHash>{ data.last() };
  122. }
  123. void QCMakePresetItemModelTest::data()
  124. {
  125. QFETCH_GLOBAL(QVector<QCMakePreset>, presets);
  126. QFETCH_GLOBAL(QVector<QItemDataHash>, data);
  127. QFETCH(Qt::ItemDataRole, role);
  128. QCMakePresetItemModel model;
  129. QSignalSpy spy1(&model, &QCMakePresetItemModel::modelAboutToBeReset);
  130. QSignalSpy spy2(&model, &QCMakePresetItemModel::modelReset);
  131. model.setPresets(presets);
  132. QCOMPARE(spy1.size(), 1);
  133. QCOMPARE(spy2.size(), 1);
  134. QVector<QVariant> expectedData(data.size());
  135. for (int i = 0; i < data.size(); ++i) {
  136. expectedData[i] = data[i][role];
  137. }
  138. auto rows = model.rowCount();
  139. QVector<QVariant> actualData(rows);
  140. for (int i = 0; i < rows; ++i) {
  141. actualData[i] = model.data(model.index(i, 0), role);
  142. }
  143. QCOMPARE(actualData, expectedData);
  144. }
  145. void QCMakePresetItemModelTest::data_data()
  146. {
  147. QTest::addColumn<Qt::ItemDataRole>("role");
  148. QTest::newRow("accessible") << Qt::AccessibleDescriptionRole;
  149. QTest::newRow("display") << Qt::DisplayRole;
  150. QTest::newRow("tooltip") << Qt::ToolTipRole;
  151. QTest::newRow("user") << Qt::UserRole;
  152. QTest::newRow("font") << Qt::FontRole;
  153. }
  154. QTEST_MAIN(QCMakePresetItemModelTest)