2 * Copyright (C) 2013 Canonical, Ltd.
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.
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.
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/>.
17 * Nick Dedekind <nick.dedekind@canonical.com>
20#include "indicatorsmanager.h"
21#include "unitymenumodelstack.h"
25#include <unitymenumodel.h>
30class UnityMenuModelStackTest : public QObject
37 m_model = new UnityMenuModel();
38 m_headChanged = false;
39 m_tailChanged = false;
48 void testHeadOnSetHead()
50 UnityMenuModelStack stack;
51 connect(&stack, &UnityMenuModelStack::headChanged, this, &UnityMenuModelStackTest::onHeadChanged);
52 stack.setHead(m_model);
54 QCOMPARE(stack.head(), m_model);
55 QCOMPARE(m_headChanged, true);
58 void testTailOnSetHead()
60 UnityMenuModelStack stack;
61 connect(&stack, &UnityMenuModelStack::tailChanged, this, &UnityMenuModelStackTest::onTailChanged);
62 stack.setHead(m_model);
64 QCOMPARE(stack.tail(), m_model);
65 QCOMPARE(m_tailChanged, true);
68 void testPushPop_data() {
69 QTest::addColumn<int>("menuDepth");
70 QTest::addColumn<int>("subMenuCount");
71 QTest::addColumn<int>("subMenuIndex");
73 QTest::newRow("depth=0") << 0 << 1 << 0;
74 QTest::newRow("depth=1") << 1 << 1 << 0;
75 QTest::newRow("depth=8") << 8 << 2 << 1;
80 QFETCH(int, menuDepth);
81 QFETCH(int, subMenuCount);
82 QFETCH(int, subMenuIndex);
84 m_model->setModelData(recuseAddMenu(subMenuCount, menuDepth));
86 UnityMenuModelStack stack;
87 connect(&stack, &UnityMenuModelStack::tailChanged, this, &UnityMenuModelStackTest::onTailChanged);
89 QList<UnityMenuModel*> models;
92 auto foreachChild = [&](UnityMenuModel* child, int childIndex) {
93 stack.push(child, childIndex);
94 QCOMPARE(stack.count(), count+1);
99 recuseSubmenus(m_model, subMenuIndex, foreachChild);
101 QCOMPARE(stack.count(), models.count());
102 QCOMPARE(stack.count(), menuDepth+1);
103 while(stack.count() > 0) {
104 m_tailChanged = false;
106 QCOMPARE(stack.pop(), models.takeLast());
108 QCOMPARE(m_tailChanged, true);
110 QCOMPARE(stack.tail(), models.last());
113 QCOMPARE(m_tailChanged, true);
116 void testPopOnRemove_data() {
117 QTest::addColumn<int>("menuDepth");
118 QTest::addColumn<int>("subMenuCount");
119 QTest::addColumn<int>("subMenuIndex");
120 QTest::addColumn<int>("removeIndex");
121 QTest::addColumn<int>("resultCount");
123 QTest::newRow("removeIndexBefore") << 4 << 2 << 1 << 0 << 5;
124 QTest::newRow("removeCurrentIndex") << 4 << 2 << 0 << 0 << 1;
125 QTest::newRow("removeIndexAfter") << 4 << 2 << 0 << 1 << 5;
128 void testPopOnRemove()
130 QFETCH(int, menuDepth);
131 QFETCH(int, subMenuCount);
132 QFETCH(int, subMenuIndex);
133 QFETCH(int, removeIndex);
134 QFETCH(int, resultCount);
136 m_model->setModelData(recuseAddMenu(subMenuCount, menuDepth));
138 UnityMenuModelStack stack;
140 auto foreachChild = [&](UnityMenuModel* child, int childIndex) {
141 stack.push(child, childIndex);
143 recuseSubmenus(m_model, subMenuIndex, foreachChild);
145 QCOMPARE(stack.count(), menuDepth+1);
147 m_model->removeRow(removeIndex);
149 waitFor([&stack, resultCount]() { return stack.count() == resultCount; }, 1000);
150 QCOMPARE(stack.count(), resultCount);
154 bool waitFor(std::function<bool()> functor, int ms) {
158 while(!functor() && timer.elapsed() < ms) { QTest::qWait(10); }
162 QVariant recuseAddMenu(int subMenuCount, int depth_remaining)
166 for (int i = 0; i < subMenuCount; i ++) {
170 if (depth_remaining > 0) {
171 row["submenu"] = recuseAddMenu(subMenuCount, depth_remaining-1);
174 row["rowData"] = rowData;
182 void recuseSubmenus(UnityMenuModel* model, int childIndex, std::function<void(UnityMenuModel*, int)> func) {
183 UnityMenuModel* parent = model;
184 UnityMenuModel* child = model;
188 func(child, childIndex);
192 child = qobject_cast<UnityMenuModel*>(parent->submenu(childIndex));
198 void onHeadChanged() { m_headChanged = true; }
199 void onTailChanged() { m_tailChanged = true; }
202 UnityMenuModel* m_model;
207QTEST_GUILESS_MAIN(UnityMenuModelStackTest)
208#include "UnityMenuModelStackTest.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list