on xenial help sign in
Fix real world submenus (e.g. kate) not getting their first item selected on open

MenuBar.qml: remove unused function MenuPopup.qml: Select first item when one arrives if there's no currentItem instead of on completion unitymenumodel.cpp: trick rowCount() to simulate "real world late" menus tst_MenuPopup.qml: adapt to unitymenumodel.cpp changes (and make the Up test actually press Up and not Down)

commitac33d0a294b4f558aaf92721466696b095cdaa92 authorAlbert Astals Cid <albert.astals@canonical.com> dateWed 22 Feb 2017, 13:59 (10 years ago) parent879cd78
5 files changed+53−23
qml/ApplicationMenus/MenuBar.qml−4 qml/ApplicationMenus/MenuPopup.qml+6−1 tests/mocks/QMenuModel/unitymenumodel.cpp+23−4 tests/mocks/QMenuModel/unitymenumodel.h+7 tests/qmltests/ApplicationMenus/tst_MenuPopup.qml+17−14
qml/ApplicationMenus/MenuBar.qml−4
@@ -36,10 +36,6 @@ Item {
3636 implicitWidth: row.width
3737 height: parent.height
3838
39− function select(index) {
40− d.select(index);
41− }
42−
4339 function dismiss() {
4440 d.dismissAll();
4541 }
qml/ApplicationMenus/MenuPopup.qml+6−1
@@ -285,6 +285,12 @@ UbuntuShape {
285285 Repeater {
286286 id: repeater
287287
288+ onCountChanged: {
289+ if (!d.currentItem && count > 0) {
290+ root.select(0);
291+ }
292+ }
293+
288294 Loader {
289295 id: loader
290296 objectName: root.objectName + "-item" + __ownIndex
@@ -483,7 +489,6 @@ UbuntuShape {
483489 onChildActivated: childActivated();
484490 }
485491
486− Component.onCompleted: item.select(0);
487492 onVisibleChanged: if (visible) { item.select(0); }
488493 }
489494 }
tests/mocks/QMenuModel/unitymenumodel.cpp+23−4
@@ -18,6 +18,8 @@
1818
1919#include "unitymenumodel.h"
2020
21+#include <QTimer>
22+
2123enum MenuRoles {
2224 LabelRole = Qt::DisplayRole + 1,
2325 SensitiveRole,
@@ -35,7 +37,8 @@ enum MenuRoles {
3537};
3638
3739UnityMenuModel::UnityMenuModel(QObject *parent)
38−: QAbstractListModel(parent)
40+ : QAbstractListModel(parent)
41+ , m_rowCountStatus(NoRequestMade)
3942{
4043}
4144
@@ -60,7 +63,7 @@ void UnityMenuModel::setModelData(const QVariant& data)
6063
6164void UnityMenuModel::insertRow(int row, const QVariant& data)
6265{
63− row = qMin(row, rowCount());
66+ row = qMin(row, m_modelData.count());
6467
6568 beginInsertRows(QModelIndex(), row, row);
6669
@@ -71,12 +74,12 @@ void UnityMenuModel::insertRow(int row, const QVariant& data)
7174
7275void UnityMenuModel::appendRow(const QVariant& data)
7376{
74− insertRow(rowCount(), data);
77+ insertRow(m_modelData.count(), data);
7578}
7679
7780void UnityMenuModel::removeRow(int row)
7881{
79− if (row < 0 || rowCount() <= row) {
82+ if (row < 0 || m_modelData.count() <= row) {
8083 return;
8184 }
8285
@@ -133,6 +136,22 @@ QString UnityMenuModel::nameOwner() const
133136
134137int UnityMenuModel::rowCount(const QModelIndex&) const
135138{
139+ // Fake the rowCount to be 0 for a while (100ms)
140+ // This emulates menus in real world that don't load immediately
141+ if (m_rowCountStatus == TimerRunning)
142+ return 0;
143+
144+ if (m_rowCountStatus == NoRequestMade) {
145+ UnityMenuModel *that = const_cast<UnityMenuModel*>(this);
146+ that->m_rowCountStatus = TimerRunning;
147+ QTimer::singleShot(100, that, [that] {
148+ that->beginInsertRows(QModelIndex(), 0, that->m_modelData.count() - 1);
149+ that->m_rowCountStatus = TimerFinished;
150+ that->endInsertRows();
151+ });
152+ return 0;
153+ }
154+
136155 return m_modelData.count();
137156}
138157
tests/mocks/QMenuModel/unitymenumodel.h+7
@@ -101,6 +101,13 @@ private:
101101 QByteArray m_busName;
102102 QVariantMap m_actions;
103103 QByteArray m_menuObjectPath;
104+
105+ enum RowCountStatus {
106+ NoRequestMade,
107+ TimerRunning,
108+ TimerFinished
109+ };
110+ RowCountStatus m_rowCountStatus;
104111};
105112
106113#endif // MOCK_UNITYMENUMODEL_H
tests/qmltests/ApplicationMenus/tst_MenuPopup.qml+17−14
@@ -159,9 +159,6 @@ Item {
159159
160160 var priv = findInvisibleChild(menu, "d");
161161
162− keyClick(Qt.Key_Down, Qt.NoModifier);
163− compare(priv.currentItem, item0, "CurrentItem should have moved to item 0");
164−
165162 keyClick(Qt.Key_Down, Qt.NoModifier);
166163 compare(priv.currentItem, item1, "CurrentItem should have moved to item 1");
167164
@@ -170,6 +167,9 @@ Item {
170167
171168 keyClick(Qt.Key_Down, Qt.NoModifier);
172169 compare(priv.currentItem, item0, "CurrentItem should have moved to item 0");
170+
171+ keyClick(Qt.Key_Down, Qt.NoModifier);
172+ compare(priv.currentItem, item1, "CurrentItem should have moved to item 1");
173173 }
174174
175175 function test_keyboardNavigation_UpKeySelectsAndOpensPreviousMenuItemAndRotates() {
@@ -181,17 +181,17 @@ Item {
181181
182182 var priv = findInvisibleChild(menu, "d");
183183
184− keyClick(Qt.Key_Down, Qt.NoModifier);
185− compare(priv.currentItem, item0, "CurrentItem should have moved to item 2");
184+ keyClick(Qt.Key_Up, Qt.NoModifier);
185+ compare(priv.currentItem, item2, "CurrentItem should have moved to item 2");
186186
187− keyClick(Qt.Key_Down, Qt.NoModifier);
187+ keyClick(Qt.Key_Up, Qt.NoModifier);
188188 compare(priv.currentItem, item1, "CurrentItem should have moved to item 1");
189189
190− keyClick(Qt.Key_Down, Qt.NoModifier);
191− compare(priv.currentItem, item2, "CurrentItem should have moved to item 0");
190+ keyClick(Qt.Key_Up, Qt.NoModifier);
191+ compare(priv.currentItem, item0, "CurrentItem should have moved to item 0");
192192
193− keyClick(Qt.Key_Down, Qt.NoModifier);
194− compare(priv.currentItem, item0, "CurrentItem should have moved to item 2");
193+ keyClick(Qt.Key_Up, Qt.NoModifier);
194+ compare(priv.currentItem, item2, "CurrentItem should have moved to item 2");
195195 }
196196
197197 function test_keyboardNavigation_RightKeyEntersSubMenu() {
@@ -217,7 +217,7 @@ Item {
217217
218218 var menuItem = findChild(menu, "menu-item0"); verify(menuItem);
219219 mouseClick(menuItem, menuItem.width/2, menuItem.height/2);
220− tryCompareFunction(function() { return menuItem.popup !== null && menuItem.popup.visible }, true);
220+ tryCompareFunction(function() { return menuItem.popup !== null && findInvisibleChild(menuItem.popup, "d").currentItem !== null }, true);
221221
222222 keyClick(Qt.Key_Left, Qt.NoModifier);
223223 tryCompareFunction(function() { return menuItem.popup !== null && menuItem.popup.visible }, false);
@@ -245,13 +245,16 @@ Item {
245245
246246 menu.unityMenuModel.modelData = differentSizesMenu;
247247
248− waitForRendering(menu);
248+ // Wait for the two items to be there
249+ tryCompareFunction(function() { return findChild(menu, "menu-item1") !== null; }, true);
249250 var longWidth = menu.width;
251+
252+ // Now pop one item and make sure it's smaller
250253 differentSizesMenu.pop();
251254 menu.unityMenuModel.modelData = differentSizesMenu;
252255
253− waitForRendering(menu);
254− verify(menu.width < longWidth);
256+ tryCompareFunction(function() { return findChild(menu, "menu-item0") !== null; }, true);
257+ tryCompareFunction(function() { return menu.width < longWidth; }, true);
255258 }
256259
257260 function test_minimumWidth() {