on master help sign in
unity8/tests/plugins/Unity/Indicators/UnityMenuModelStackTest.cpp
201 lines 5.6 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Nick Dedekind <nick.dedekind@canonical.com>
18 */
20#include "indicatorsmanager.h"
21#include "unitymenumodelstack.h"
23#include <QtTest>
24#include <QDebug>
25#include <unitymenumodel.h>
27#include <functional>
28#include <chrono>
30class UnityMenuModelStackTest : public QObject
32 Q_OBJECT
33private Q_SLOTS:
35 void init()
36 {
37 m_model = new UnityMenuModel();
38 m_headChanged = false;
39 m_tailChanged = false;
40 }
42 void cleanup()
43 {
44 delete m_model;
45 m_model = nullptr;
46 // send deleteLaters to avoid leaks.
47 QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
48 }
50 void testHeadOnSetHead()
51 {
52 UnityMenuModelStack stack;
53 connect(&stack, &UnityMenuModelStack::headChanged, this, &UnityMenuModelStackTest::onHeadChanged);
54 stack.setHead(m_model);
56 QCOMPARE(stack.head(), m_model);
57 QCOMPARE(m_headChanged, true);
58 }
60 void testTailOnSetHead()
61 {
62 UnityMenuModelStack stack;
63 connect(&stack, &UnityMenuModelStack::tailChanged, this, &UnityMenuModelStackTest::onTailChanged);
64 stack.setHead(m_model);
66 QCOMPARE(stack.tail(), m_model);
67 QCOMPARE(m_tailChanged, true);
68 }
70 void testPushPop_data() {
71 QTest::addColumn<int>("menuDepth");
72 QTest::addColumn<int>("subMenuCount");
73 QTest::addColumn<int>("subMenuIndex");
75 QTest::newRow("depth=0") << 0 << 1 << 0;
76 QTest::newRow("depth=1") << 1 << 1 << 0;
77 QTest::newRow("depth=8") << 8 << 2 << 1;
78 }
80 void testPushPop()
81 {
82 QFETCH(int, menuDepth);
83 QFETCH(int, subMenuCount);
84 QFETCH(int, subMenuIndex);
86 m_model->setModelData(recuseAddMenu(subMenuCount, menuDepth));
88 UnityMenuModelStack stack;
89 connect(&stack, &UnityMenuModelStack::tailChanged, this, &UnityMenuModelStackTest::onTailChanged);
91 QList<UnityMenuModel*> models;
93 int count = 0;
94 auto foreachChild = [&](UnityMenuModel* child, int childIndex) {
95 stack.push(child, childIndex);
96 QCOMPARE(stack.count(), count+1);
98 count++;
99 models << child;
100 };
101 recuseSubmenus(m_model, subMenuIndex, foreachChild);
103 QCOMPARE(stack.count(), models.count());
104 QCOMPARE(stack.count(), menuDepth+1);
105 while(stack.count() > 0) {
106 m_tailChanged = false;
108 QCOMPARE(stack.pop(), models.takeLast());
110 QCOMPARE(m_tailChanged, true);
111 if (stack.count()) {
112 QCOMPARE(stack.tail(), models.last());
113 }
114 }
115 QCOMPARE(m_tailChanged, true);
116 }
118 void testPopOnRemove_data() {
119 QTest::addColumn<int>("menuDepth");
120 QTest::addColumn<int>("subMenuCount");
121 QTest::addColumn<int>("subMenuIndex");
122 QTest::addColumn<int>("removeIndex");
123 QTest::addColumn<int>("resultCount");
125 QTest::newRow("removeIndexBefore") << 4 << 2 << 1 << 0 << 5;
126 QTest::newRow("removeCurrentIndex") << 4 << 2 << 0 << 0 << 1;
127 QTest::newRow("removeIndexAfter") << 4 << 2 << 0 << 1 << 5;
128 }
130 void testPopOnRemove()
131 {
132 QFETCH(int, menuDepth);
133 QFETCH(int, subMenuCount);
134 QFETCH(int, subMenuIndex);
135 QFETCH(int, removeIndex);
136 QFETCH(int, resultCount);
138 m_model->setModelData(recuseAddMenu(subMenuCount, menuDepth));
140 UnityMenuModelStack stack;
142 auto foreachChild = [&](UnityMenuModel* child, int childIndex) {
143 stack.push(child, childIndex);
144 };
145 recuseSubmenus(m_model, subMenuIndex, foreachChild);
147 QCOMPARE(stack.count(), menuDepth+1);
149 m_model->removeRow(removeIndex);
151 QCOMPARE(stack.count(), resultCount);
152 }
154private:
155 QVariant recuseAddMenu(int subMenuCount, int depth_remaining)
156 {
157 QVariantList rows;
159 for (int i = 0; i < subMenuCount; i ++) {
160 QVariantMap row;
161 QVariantMap rowData;
163 if (depth_remaining > 0) {
164 row["submenu"] = recuseAddMenu(subMenuCount, depth_remaining-1);
165 }
167 row["rowData"] = rowData;
169 rows << row;
170 }
172 return rows;
173 }
175 void recuseSubmenus(UnityMenuModel* model, int childIndex, std::function<void(UnityMenuModel*, int)> func) {
176 UnityMenuModel* parent = model;
177 UnityMenuModel* child = model;
179 while(child) {
180 if (func) {
181 func(child, childIndex);
182 }
184 parent = child;
185 child = qobject_cast<UnityMenuModel*>(parent->submenu(childIndex));
186 }
188 }
190private Q_SLOTS:
191 void onHeadChanged() { m_headChanged = true; }
192 void onTailChanged() { m_tailChanged = true; }
194public:
195 UnityMenuModel* m_model;
196 bool m_headChanged;
197 bool m_tailChanged;
198};
200QTEST_GUILESS_MAIN(UnityMenuModelStackTest)
201#include "UnityMenuModelStackTest.moc"