summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeGUI/QCMakePresetItemModelTest.cxx
blob: 97dbb30fd230905443e7b458006c2d29bf3459cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "QCMakePresetItemModelTest.h"

#include <utility>

#include "QCMakePreset.h"
#include "QCMakePresetItemModel.h"
#include <QHash>
#include <QMetaType>
#include <QSignalSpy>
#include <QVariant>
#include <QVector>
#include <QtTest>

using QItemDataHash = QHash<Qt::ItemDataRole, QVariant>;

void QCMakePresetItemModelTest::initTestCase()
{
  QMetaType::registerComparators<QCMakePreset>();
}

void QCMakePresetItemModelTest::initTestCase_data()
{
  QTest::addColumn<QVector<QCMakePreset>>("presets");
  QTest::addColumn<QVector<QItemDataHash>>("data");

  QVector<QCMakePreset> presets{
    QCMakePreset{
      /*name=*/"no-description",
      /*description=*/"",
      /*description=*/"",
      /*generator=*/"",
      /*architecture=*/"",
      /*setArchitecture=*/true,
      /*toolset=*/"",
      /*setToolset=*/true,
      /*enabled=*/true,
    },
    QCMakePreset{
      /*name=*/"short-description",
      /*description=*/"Short Description",
      /*description=*/"",
      /*generator=*/"",
      /*architecture=*/"",
      /*setArchitecture=*/true,
      /*toolset=*/"",
      /*setToolset=*/true,
      /*enabled=*/true,
    },
    QCMakePreset{
      /*name=*/"long-description",
      /*description=*/"",
      /*description=*/"Long Description",
      /*generator=*/"",
      /*architecture=*/"",
      /*setArchitecture=*/true,
      /*toolset=*/"",
      /*setToolset=*/true,
      /*enabled=*/true,
    },
    QCMakePreset{
      /*name=*/"disabled",
      /*description=*/"",
      /*description=*/"",
      /*generator=*/"",
      /*architecture=*/"",
      /*setArchitecture=*/true,
      /*toolset=*/"",
      /*setToolset=*/true,
      /*enabled=*/false,
    },
  };
  QVector<QItemDataHash> data{
    QItemDataHash{
      { Qt::AccessibleDescriptionRole, "" },
      { Qt::DisplayRole, "no-description" },
      { Qt::ToolTipRole, "" },
      { Qt::UserRole, QVariant::fromValue(presets[0]) },
      { Qt::FontRole, QFont{} },
    },
    QItemDataHash{
      { Qt::AccessibleDescriptionRole, "" },
      { Qt::DisplayRole, "Short Description" },
      { Qt::ToolTipRole, "" },
      { Qt::UserRole, QVariant::fromValue(presets[1]) },
      { Qt::FontRole, QFont{} },
    },
    QItemDataHash{
      { Qt::AccessibleDescriptionRole, "" },
      { Qt::DisplayRole, "long-description" },
      { Qt::ToolTipRole, "Long Description" },
      { Qt::UserRole, QVariant::fromValue(presets[2]) },
      { Qt::FontRole, QFont{} },
    },
    QItemDataHash{
      { Qt::AccessibleDescriptionRole, "" },
      { Qt::DisplayRole, "disabled" },
      { Qt::ToolTipRole, "" },
      { Qt::UserRole, QVariant::fromValue(presets[3]) },
      { Qt::FontRole, QFont{} },
    },
    QItemDataHash{
      { Qt::AccessibleDescriptionRole, "separator" },
      { Qt::DisplayRole, QVariant{} },
      { Qt::ToolTipRole, QVariant{} },
      { Qt::UserRole, QVariant{} },
      { Qt::FontRole, QFont{} },
    },
    QItemDataHash{
      { Qt::AccessibleDescriptionRole, "" },
      { Qt::DisplayRole, "<custom>" },
      { Qt::ToolTipRole, "Specify all settings manually" },
      { Qt::UserRole, QVariant{} },
      { Qt::FontRole,
        []() {
          QFont f;
          f.setItalic(true);
          return f;
        }() },
    },
  };
  QTest::newRow("many") << presets << data;
  QTest::newRow("none") << QVector<QCMakePreset>{}
                        << QVector<QItemDataHash>{ data.last() };
}

void QCMakePresetItemModelTest::data()
{
  QFETCH_GLOBAL(QVector<QCMakePreset>, presets);
  QFETCH_GLOBAL(QVector<QItemDataHash>, data);
  QFETCH(Qt::ItemDataRole, role);

  QCMakePresetItemModel model;
  QSignalSpy spy1(&model, &QCMakePresetItemModel::modelAboutToBeReset);
  QSignalSpy spy2(&model, &QCMakePresetItemModel::modelReset);
  model.setPresets(presets);
  QCOMPARE(spy1.size(), 1);
  QCOMPARE(spy2.size(), 1);

  QVector<QVariant> expectedData(data.size());
  for (int i = 0; i < data.size(); ++i) {
    expectedData[i] = data[i][role];
  }

  auto rows = model.rowCount();
  QVector<QVariant> actualData(rows);
  for (int i = 0; i < rows; ++i) {
    actualData[i] = model.data(model.index(i, 0), role);
  }

  QCOMPARE(actualData, expectedData);
}

void QCMakePresetItemModelTest::data_data()
{
  QTest::addColumn<Qt::ItemDataRole>("role");

  QTest::newRow("accessible") << Qt::AccessibleDescriptionRole;
  QTest::newRow("display") << Qt::DisplayRole;
  QTest::newRow("tooltip") << Qt::ToolTipRole;
  QTest::newRow("user") << Qt::UserRole;
  QTest::newRow("font") << Qt::FontRole;
}

QTEST_MAIN(QCMakePresetItemModelTest)