on xenial help sign in
added test for cached model
commit74f208a6b268090cd2a504f7d8a479a84e1b2336 authorNick Dedekind <nicholas.dedekind@gmail.com> dateTue 19 Aug 2014, 11:56 (12 years ago) parent0b65bb2
4 files changed+132−2
plugins/Unity/Indicators/unitymenumodelcache.cpp+8−2 plugins/Unity/Indicators/unitymenumodelcache.h+2 tests/qmltests/CMakeLists.txt+1 tests/qmltests/Panel/Indicators/tst_CachedUnityMenuModel.qml+121
plugins/Unity/Indicators/unitymenumodelcache.cpp+8−2
@@ -21,6 +21,7 @@
2121#include <unitymenumodel.h>
2222
2323#include <QQmlEngine>
24+#include <QDebug>
2425
2526UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
2627 : QObject(parent)
@@ -52,13 +53,18 @@ void UnityMenuModelCache::ref(UnityMenuModel* menuModel)
5253void UnityMenuModelCache::deref(UnityMenuModel* menuModel)
5354{
5455 if (m_refs.contains(menuModel)) {
55− if (m_refs[menuModel]-- == 0) {
56+ if (--m_refs[menuModel] == 0) {
5657 m_refs.remove(menuModel);
5758 QList<QString> keys = m_menuModels.keys(menuModel);
5859 Q_FOREACH(const QString& key, keys) {
5960 m_menuModels.remove(key);
6061 }
61− delete menuModel;
62+ menuModel->deleteLater();
6263 }
6364 }
6465}
66+
67+bool UnityMenuModelCache::contains(UnityMenuModel* menuModel)
68+{
69+ return m_refs.contains(menuModel);
70+}
plugins/Unity/Indicators/unitymenumodelcache.h+2
@@ -40,6 +40,8 @@ public:
4040 Q_INVOKABLE void ref(UnityMenuModel* menuModel);
4141 Q_INVOKABLE void deref(UnityMenuModel* menuModel);
4242
43+ Q_INVOKABLE bool contains(UnityMenuModel* menuModel);
44+
4345private:
4446 QHash<QString, UnityMenuModel*> m_menuModels;
4547 QHash<UnityMenuModel*, uint> m_refs;
tests/qmltests/CMakeLists.txt+1
@@ -72,6 +72,7 @@ add_qml_test(Panel Panel)
7272add_qml_test(Panel SearchIndicator)
7373add_qml_test(Panel/Indicators DefaultIndicatorWidget ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests/mocks/QMenuModel")
7474add_qml_test(Panel/Indicators DefaultIndicatorPage ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests/mocks/QMenuModel")
75+add_qml_test(Panel/Indicators CachedUnityMenuModel IMPORT_PATHS ${CMAKE_BINARY_DIR}/tests/mocks ${qmltest_DEFAULT_IMPORT_PATHS} ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests/mocks/QMenuModel")
7576# These MenuItemFactory tests need the test/mocks/ to come before plugins/
7677add_qml_test(Panel/Indicators MenuItemFactory IMPORT_PATHS ${CMAKE_BINARY_DIR}/tests/mocks ${qmltest_DEFAULT_IMPORT_PATHS})
7778add_qml_test(Panel/Indicators MessageMenuItemFactory IMPORT_PATHS ${CMAKE_BINARY_DIR}/tests/mocks ${qmltest_DEFAULT_IMPORT_PATHS})
tests/qmltests/Panel/Indicators/tst_CachedUnityMenuModel.qmlnew file+121
@@ -0,0 +1,121 @@
1+/*
2+ * Copyright 2014 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+
17+import QtQuick 2.0
18+import QtTest 1.0
19+import Unity.Test 0.1 as UT
20+import QMenuModel 0.1
21+import Unity.Indicators 0.1 as Indicators
22+
23+Item {
24+ id: root
25+ width: units.gu(40)
26+ height: units.gu(70)
27+
28+ Component {
29+ id: model
30+ Indicators.CachedUnityMenuModel {}
31+ }
32+
33+ UT.UnityTestCase {
34+ name: "CachedUnityMenuModel"
35+ when: windowShown
36+
37+ function test_createDifferent() {
38+ var cachedObject = model.createObject(root,
39+ {
40+ "busName": "com.canonical.test1",
41+ "menuObjectPath": "com/canonical/test1",
42+ "actionsObjectPath": "com/canonical/test1/actions"
43+ });
44+
45+ var cachedObject2 = model.createObject(root,
46+ {
47+ "busName": "com.canonical.test2",
48+ "menuObjectPath": "com/canonical/test2",
49+ "actionsObjectPath": "com/canonical/test2/actions"
50+ });
51+
52+ verify(cachedObject.model !== cachedObject2.model);
53+
54+ cachedObject.destroy();
55+ cachedObject2.destroy();
56+ }
57+
58+ function test_createSame() {
59+ var cachedObject = model.createObject(root,
60+ {
61+ "busName": "com.canonical.test1",
62+ "menuObjectPath": "com/canonical/test1",
63+ "actionsObjectPath": "com/canonical/test1/actions"
64+ });
65+
66+ var cachedObject2 = model.createObject(root,
67+ {
68+ "busName": "com.canonical.test1",
69+ "menuObjectPath": "com/canonical/test1",
70+ "actionsObjectPath": "com/canonical/test1/actions"
71+ });
72+
73+ verify(cachedObject.model === cachedObject2.model);
74+
75+ cachedObject.destroy();
76+ cachedObject2.destroy();
77+ }
78+
79+ function test_destroyAllDeletesModel() {
80+ var cachedObject = model.createObject(root,
81+ {
82+ "busName": "com.canonical.test1",
83+ "menuObjectPath": "com/canonical/test1",
84+ "actionsObjectPath": "com/canonical/test1/actions"
85+ });
86+
87+ var cachedObject2 = model.createObject(root,
88+ {
89+ "busName": "com.canonical.test1",
90+ "menuObjectPath": "com/canonical/test1",
91+ "actionsObjectPath": "com/canonical/test1/actions"
92+ });
93+
94+ var menuModel = cachedObject.model;
95+ cachedObject.destroy();
96+ cachedObject2.destroy();
97+ tryCompareFunction(function() { return Indicators.UnityMenuModelCache.contains(menuModel) }, false);
98+ }
99+
100+ function test_destroyPartialKeepsModel() {
101+ var cachedObject = model.createObject(root,
102+ {
103+ "busName": "com.canonical.test1",
104+ "menuObjectPath": "com/canonical/test1",
105+ "actionsObjectPath": "com/canonical/test1/actions"
106+ });
107+
108+ var cachedObject2 = model.createObject(root,
109+ {
110+ "busName": "com.canonical.test1",
111+ "menuObjectPath": "com/canonical/test1",
112+ "actionsObjectPath": "com/canonical/test1/actions"
113+ });
114+
115+ var menuModel = cachedObject.model;
116+ cachedObject.destroy();
117+ wait(1);
118+ compare(Indicators.UnityMenuModelCache.contains(menuModel), true);
119+ }
120+ }
121+}