on xenial help sign in
added test for cached model
74f208aNick Dedekind
plugins/Unity/Indicators/unitymenumodelcache.cpp+8−2
@@ -21,6 +21,7 @@
21#include <unitymenumodel.h>
21#include <unitymenumodel.h>
22
22
23#include <QQmlEngine>
23#include <QQmlEngine>
24+#include <QDebug>
24
25
25UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
26UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
26 : QObject(parent)
27 : QObject(parent)
@@ -52,13 +53,18 @@ void UnityMenuModelCache::ref(UnityMenuModel* menuModel)
52void UnityMenuModelCache::deref(UnityMenuModel* menuModel)
53void UnityMenuModelCache::deref(UnityMenuModel* menuModel)
53{
54{
54 if (m_refs.contains(menuModel)) {
55 if (m_refs.contains(menuModel)) {
55− if (m_refs[menuModel]-- == 0) {
56+ if (--m_refs[menuModel] == 0) {
56 m_refs.remove(menuModel);
57 m_refs.remove(menuModel);
57 QList<QString> keys = m_menuModels.keys(menuModel);
58 QList<QString> keys = m_menuModels.keys(menuModel);
58 Q_FOREACH(const QString& key, keys) {
59 Q_FOREACH(const QString& key, keys) {
59 m_menuModels.remove(key);
60 m_menuModels.remove(key);
60 }
61 }
61− delete menuModel;
62+ menuModel->deleteLater();
62 }
63 }
63 }
64 }
64}
65}
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:
40 Q_INVOKABLE void ref(UnityMenuModel* menuModel);
40 Q_INVOKABLE void ref(UnityMenuModel* menuModel);
41 Q_INVOKABLE void deref(UnityMenuModel* menuModel);
41 Q_INVOKABLE void deref(UnityMenuModel* menuModel);
42
42
43+ Q_INVOKABLE bool contains(UnityMenuModel* menuModel);
44+
43private:
45private:
44 QHash<QString, UnityMenuModel*> m_menuModels;
46 QHash<QString, UnityMenuModel*> m_menuModels;
45 QHash<UnityMenuModel*, uint> m_refs;
47 QHash<UnityMenuModel*, uint> m_refs;
tests/qmltests/CMakeLists.txt+1
@@ -72,6 +72,7 @@ add_qml_test(Panel Panel)
72add_qml_test(Panel SearchIndicator)
72add_qml_test(Panel SearchIndicator)
73add_qml_test(Panel/Indicators DefaultIndicatorWidget ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests/mocks/QMenuModel")
73add_qml_test(Panel/Indicators DefaultIndicatorWidget ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests/mocks/QMenuModel")
74add_qml_test(Panel/Indicators DefaultIndicatorPage ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests/mocks/QMenuModel")
74add_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")
75# These MenuItemFactory tests need the test/mocks/ to come before plugins/
76# These MenuItemFactory tests need the test/mocks/ to come before plugins/
76add_qml_test(Panel/Indicators MenuItemFactory IMPORT_PATHS ${CMAKE_BINARY_DIR}/tests/mocks ${qmltest_DEFAULT_IMPORT_PATHS})
77add_qml_test(Panel/Indicators MenuItemFactory IMPORT_PATHS ${CMAKE_BINARY_DIR}/tests/mocks ${qmltest_DEFAULT_IMPORT_PATHS})
77add_qml_test(Panel/Indicators MessageMenuItemFactory IMPORT_PATHS ${CMAKE_BINARY_DIR}/tests/mocks ${qmltest_DEFAULT_IMPORT_PATHS})
78add_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+}