QCMakePresetItemModelTest.cxx 4.6 KB

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