on xenial help sign in
A test that failed with the old code
commit3dbd58b1485bc53d57754138aeec850876fe6f46 authorAlbert Astals <albert.astals@canonical.com> dateTue 19 Aug 2014, 17:59 (12 years ago) parent6306f75
7 files changed+175−148
tests/mocks/Unity/fake_categories.cpp+94−136 tests/mocks/Unity/fake_categories.h+7−12 tests/mocks/Unity/fake_resultsmodel.cpp+9 tests/mocks/Unity/fake_resultsmodel.h+3 tests/mocks/Unity/fake_scope.cpp+17 tests/mocks/Unity/fake_scope.h+2 tests/qmltests/Dash/tst_GenericScopeView.qml+43
tests/mocks/Unity/fake_categories.cpp+94−136
@@ -29,25 +29,12 @@ Categories::Categories(int category_count, QObject* parent)
2929
3030int Categories::rowCount(const QModelIndex& /*parent*/) const
3131{
32− return m_category_count + m_specialCategories.count();
32+ return m_category_count;
3333}
3434
35−void Categories::addSpecialCategory(QString const& categoryId, QString const& name, QString const& icon, QString const& rawTemplate, QObject* countObject)
35+void Categories::addSpecialCategory(QString const& /*categoryId*/, QString const& /*name*/, QString const& /*icon*/, QString const& /*rawTemplate*/, QObject* /*countObject*/)
3636{
37− CategoryData catData;
38− catData.categoryId = categoryId;
39− catData.name = name;
40− catData.icon = icon;
41− catData.rawTemplate = rawTemplate;
42− catData.countObject = countObject;
43−
44− beginInsertRows(QModelIndex(), 0, 0);
45− m_specialCategories.prepend(catData);
46− endInsertRows();
47−
48− if (countObject) {
49− connect(countObject, SIGNAL(countChanged()), this, SLOT(countChanged()));
50− }
37+ qFatal("Using un-implemented Categories::addSpecialCategory");
5138}
5239
5340bool Categories::overrideCategoryJson(QString const& /* categoryId */, QString const& /* json */)
@@ -55,23 +42,6 @@ bool Categories::overrideCategoryJson(QString const& /* categoryId */, QString c
5542 qFatal("Using un-implemented Categories::overrideCategoryJson");
5643}
5744
58−void Categories::countChanged()
59−{
60− QObject* countObject = sender();
61−
62− for (int i = 0; i < m_specialCategories.count(); ++i) {
63− const CategoryData &catData = m_specialCategories.at(i);
64− if (catData.countObject == countObject) {
65− QVector<int> roles;
66− roles.append(RoleCount);
67−
68− QModelIndex changedIndex(index(i));
69− dataChanged(changedIndex, changedIndex, roles);
70− break;
71− }
72− }
73−}
74−
7545QVariant
7646Categories::data(const QModelIndex& index, int role) const
7747{
@@ -79,115 +49,74 @@ Categories::data(const QModelIndex& index, int role) const
7949 return QVariant();
8050 }
8151
82− ResultsModel *resultsModel = m_resultsModels[index.row()];
83− if (!resultsModel) {
84− resultsModel = new ResultsModel(15, index.row());
85− m_resultsModels[index.row()] = resultsModel;
86− }
87− if (index.row() < m_specialCategories.count()) {
88− const CategoryData &catData = m_specialCategories.at(index.row());
89− switch (role) {
90− case RoleCategoryId:
91− return catData.categoryId;
92− case RoleName:
93− return catData.name;
94− case RoleIcon:
95− return catData.icon;
96− case RoleRawRendererTemplate:
97− return catData.rawTemplate;
98− case RoleRenderer:
99− {
100− QVariantMap map;
101− if (index.row() % 2 == 0) {
102− map["category-layout"] = "grid";
103− } else {
104− map["category-layout"] = "carousel";
105− map["overlay"] = true;
106− }
107− map["card-size"] = "small";
108− return map;
52+ ResultsModel *resultsModel = resultModel(index.row());
53+ switch (role) {
54+ case RoleCategoryId:
55+ return QString("%1").arg(index.row());
56+ case RoleName:
57+ return QString("Category %1").arg(index.row());
58+ case RoleIcon:
59+ return "gtk-apply";
60+ case RoleRawRendererTemplate:
61+ qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
62+ return QVariant();
63+ case RoleRenderer:
64+ {
65+ QVariantMap map;
66+ if (index.row() % 2 == 0) {
67+ map["category-layout"] = "grid";
68+ } else {
69+ map["category-layout"] = "carousel";
10970 }
110− case RoleComponents:
111− {
112− QVariantMap map, artMap, attributeMap;
113− artMap["aspect-ratio"] = "1.0";
114− artMap["field"] = "art";
115− map["art"] = artMap;
116− map["title"] = "HOLA";
117− attributeMap["field"] = "attribute";
118− map["attributes"] = attributeMap;
119− return map;
71+ if (index.row() == 18) {
72+ map["category-layout"] = "horizontal-list";
73+ }
74+ if (index.row() == 19) {
75+ map["category-layout"] = "grid";
76+ map["collapsed-rows"] = 0;
77+ }
78+ map["card-size"] = "small";
79+
80+ map["category-layout"] = m_layouts.value(index.row(), map["category-layout"].toString());
81+
82+ if (map["category-layout"] == "carousel") {
83+ map["overlay"] = true;
12084 }
121− case RoleHeaderLink:
122− return QString();
123− case RoleResults:
124− return QVariant();
125− case RoleCount:
126− return catData.countObject->property("count");
127− default:
128− qFatal("Using un-implemented Categories role");
129− return QVariant();
85+
86+ return map;
13087 }
131− } else {
132− switch (role) {
133− case RoleCategoryId:
134− return QString("%1").arg(index.row());
135− case RoleName:
136− return QString("Category %1").arg(index.row());
137− case RoleIcon:
138− return "gtk-apply";
139− case RoleRawRendererTemplate:
140− qFatal("Using un-implemented RoleRawRendererTemplate Categories role");
141− return QVariant();
142− case RoleRenderer:
143− {
144− QVariantMap map;
145− if (index.row() % 2 == 0) {
146− map["category-layout"] = "grid";
147− } else {
148− map["category-layout"] = "carousel";
149− map["card-size"] = "medium";
150− map["overlay"] = true;
151− }
152− if (index.row() == 18) {
153− map["category-layout"] = "horizontal-list";
154− }
155− if (index.row() == 19) {
156− map["category-layout"] = "grid";
157− map["collapsed-rows"] = 0;
158− }
159− map["card-size"] = "small";
160− return map;
88+ case RoleComponents:
89+ {
90+ QVariantMap map, artMap, attributeMap;
91+ if (index.row() % 2 != 0) {
92+ artMap["aspect-ratio"] = QString("1.%1").arg(index.row());
93+ } else {
94+ artMap["aspect-ratio"] = "1.0";
16195 }
162− case RoleComponents:
163− {
164− QVariantMap map, artMap, attributeMap;
165− if (index.row() % 2 != 0) {
166− artMap["aspect-ratio"] = QString("1.%1").arg(index.row());
167− } else {
168− artMap["aspect-ratio"] = "1.0";
169− }
170− artMap["field"] = "art";
171− map["art"] = artMap;
172− map["title"] = "HOLA";
173− map["subtitle"] = "HOLA";
174− attributeMap["field"] = "attribute";
175− map["attributes"] = attributeMap;
176− return map;
96+ artMap["field"] = "art";
97+ map["art"] = artMap;
98+ map["title"] = "HOLA";
99+ map["subtitle"] = "HOLA";
100+ attributeMap["field"] = "attribute";
101+ map["attributes"] = attributeMap;
102+ return map;
103+ }
104+ case RoleHeaderLink:
105+ {
106+ QString res;
107+ if (index.row() == 1 || index.row() == 4) {
108+ res = QString("scope://query/1");
177109 }
178− case RoleHeaderLink:
179− if (index.row() == 1 || index.row() == 4) {
180− return QString("scope://query/1");
181− }
182− return QString();
183− case RoleResults:
184− return QVariant::fromValue(resultsModel);
185− case RoleCount:
186− return resultsModel->rowCount();
187− default:
188− qFatal("Using un-implemented Categories role");
189− return QVariant();
110+ res = m_headerLinks.value(index.row(), res);
111+ return res;
190112 }
113+ case RoleResults:
114+ return QVariant::fromValue(resultsModel);
115+ case RoleCount:
116+ return resultsModel->rowCount();
117+ default:
118+ qFatal("Using un-implemented Categories role");
119+ return QVariant();
191120 }
192121}
193122
@@ -196,3 +125,32 @@ Categories::data(int row, int role) const
196125{
197126 return data(index(row, 0), role);
198127}
128+
129+void Categories::setCount(int count)
130+{
131+ if (m_category_count != count) {
132+ beginResetModel(); // This is just for test setup so we can be lazy and reset
133+ m_category_count = count;
134+ endResetModel();
135+ }
136+}
137+
138+ResultsModel* Categories::resultModel(int row) const
139+{
140+ ResultsModel *result = m_resultsModels[row];
141+ if (!result) {
142+ result = new ResultsModel(15, row);
143+ m_resultsModels[row] = result;
144+ }
145+ return result;
146+}
147+
148+void Categories::setLayout(int row, const QString &layout)
149+{
150+ m_layouts[row] = layout;
151+}
152+
153+void Categories::setHeaderLink(int row, const QString &headerLink)
154+{
155+ m_headerLinks[row] = headerLink;
156+}
tests/mocks/Unity/fake_categories.h+7−12
@@ -39,22 +39,17 @@ public:
3939
4040 Q_INVOKABLE QVariant data(int row, int role) const;
4141
42−private Q_SLOTS:
43− void countChanged();
42+ // For testing purposes
43+ Q_INVOKABLE void setCount(int count);
44+ Q_INVOKABLE ResultsModel* resultModel(int row) const;
45+ Q_INVOKABLE void setLayout(int row, const QString &layout);
46+ Q_INVOKABLE void setHeaderLink(int row, const QString &headerLink);
4447
4548private:
4649 mutable QHash<int, ResultsModel*> m_resultsModels;
50+ QHash<int, QString> m_layouts;
51+ QHash<int, QString> m_headerLinks;
4752 int m_category_count;
48−
49− struct CategoryData {
50− QString categoryId;
51− QString name;
52− QString icon;
53− QString rawTemplate;
54− QObject* countObject;
55− };
56−
57− QList<CategoryData> m_specialCategories;
5853};
5954
6055#endif // FAKE_CATEGORIES_H
tests/mocks/Unity/fake_resultsmodel.cpp+9
@@ -44,6 +44,15 @@ void ResultsModel::setCategoryId(QString const& /*id*/)
4444 qFatal("Calling un-implemented ResultsModel::setCategoryId");
4545}
4646
47+void ResultsModel::setResultCount(int result_count)
48+{
49+ if (m_result_count != result_count) {
50+ beginResetModel(); // This is just for test setup so we can be lazy and reset
51+ m_result_count = result_count;
52+ endResetModel();
53+ }
54+}
55+
4756int ResultsModel::rowCount(const QModelIndex& parent) const
4857{
4958 Q_UNUSED(parent);
tests/mocks/Unity/fake_resultsmodel.h+3
@@ -41,6 +41,9 @@ public:
4141 /* setters */
4242 void setCategoryId(QString const& id) override;
4343
44+ // For testing purposes
45+ Q_INVOKABLE void setResultCount(int result_count);
46+
4447private:
4548 int m_result_count;
4649 int m_categoryId;
tests/mocks/Unity/fake_scope.cpp+17
@@ -143,6 +143,23 @@ void Scope::setFavorite(const bool favorite)
143143 Q_EMIT favoriteChanged();
144144 }
145145}
146+
147+void Scope::setId(const QString &id)
148+{
149+ if (id != m_id) {
150+ m_id = id;
151+ Q_EMIT idChanged();
152+ }
153+}
154+
155+void Scope::setName(const QString &name)
156+{
157+ if (name != m_name) {
158+ m_name = name;
159+ Q_EMIT nameChanged();
160+ }
161+}
162+
146163void Scope::setSearchInProgress(const bool inProg)
147164{
148165 if (inProg != m_searching) {
tests/mocks/Unity/fake_scope.h+2
@@ -56,6 +56,8 @@ public:
5656 void setFormFactor(const QString& form_factor) override;
5757 void setActive(const bool) override;
5858 void setFavorite(const bool) override;
59+ Q_INVOKABLE void setId(const QString &id); // This is not invokable in the Interface, here for testing benefits
60+ Q_INVOKABLE void setName(const QString &name); // This is not invokable in the Interface, here for testing benefits
5961 Q_INVOKABLE void setSearchInProgress(const bool inProg); // This is not invokable in the Interface, here for testing benefits
6062
6163 Q_INVOKABLE void activate(QVariant const& result) override;
tests/qmltests/Dash/tst_GenericScopeView.qml+43
@@ -41,6 +41,10 @@ Item {
4141 id: scopes
4242 }
4343
44+ MockScope {
45+ id: mockScope
46+ }
47+
4448 SignalSpy {
4549 id: spy
4650 }
@@ -390,6 +394,45 @@ Item {
390394 verify(image, "Could not find the title image.");
391395 compare(image.source, data.logo, "Title image has the wrong source");
392396 }
397+
398+ function test_seeAllTwoCategoriesScenario1() {
399+ mockScope.setId("mockScope");
400+ mockScope.setName("Mock Scope");
401+ mockScope.categories.setCount(2);
402+ mockScope.categories.resultModel(0).setResultCount(50);
403+ mockScope.categories.setLayout(1, "grid");
404+ mockScope.categories.setHeaderLink(1, "");
405+ genericScopeView.scope = mockScope;
406+ waitForRendering(genericScopeView.categoryView);
407+
408+ var category0 = findChild(genericScopeView, "dashCategory0")
409+ var seeAll0 = findChild(category0, "seeAll")
410+
411+ waitForRendering(seeAll0);
412+ verify(category0.expandable);
413+ verify(!category0.expanded);
414+
415+ mouseClick(seeAll0, seeAll0.width / 2, seeAll0.height / 2);
416+ verify(category0.expanded);
417+ tryCompare(category0, "height", category0.item.expandedHeight + seeAll0.height);
418+ tryCompare(genericScopeView.categoryView, "contentY", units.gu(13.5));
419+
420+ scrollToCategory("dashCategory1");
421+
422+ var category1 = findChild(genericScopeView, "dashCategory1")
423+ var seeAll1 = findChild(category1, "seeAll")
424+ verify(category1.expandable);
425+ verify(!category1.expanded);
426+
427+ mouseClick(seeAll1, seeAll1.width / 2, seeAll1.height / 2);
428+ verify(!category0.expanded);
429+ verify(category1.expanded);
430+ tryCompare(category1, "height", category1.item.expandedHeight + seeAll1.height);
431+ tryCompareFunction(function() {
432+ return genericScopeView.categoryView.contentY + category1.y + category1.height + genericScopeView.categoryView.pageHeader.height
433+ == genericScopeView.categoryView.contentHeight;}
434+ , true);
435+ }
393436 }
394437 }
395438}