on xenial help sign in
merge with sharedunitymenumodel branch
6b61029Nick Dedekind
plugins/Unity/Indicators/unitymenumodelcache.cpp+5−4
@@ -22,14 +22,14 @@
22
22
23#include <QQmlEngine>
23#include <QQmlEngine>
24
24
25−UnityMenuModelCache* UnityMenuModelCache::theCache = nullptr;
25+QPointer<UnityMenuModelCache> UnityMenuModelCache::theCache = nullptr;
26
26
27UnityMenuModelCache* UnityMenuModelCache::singleton()
27UnityMenuModelCache* UnityMenuModelCache::singleton()
28{
28{
29− if (!theCache) {
29+ if (theCache.isNull()) {
30 theCache = new UnityMenuModelCache();
30 theCache = new UnityMenuModelCache();
31 }
31 }
32− return theCache;
32+ return theCache.data();
33}
33}
34
34
35UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
35UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
@@ -50,8 +50,9 @@ QSharedPointer<UnityMenuModel> UnityMenuModelCache::model(const QByteArray& path
50 QMutableHashIterator<QByteArray, QWeakPointer<UnityMenuModel>> iter(m_registry);
50 QMutableHashIterator<QByteArray, QWeakPointer<UnityMenuModel>> iter(m_registry);
51 while(iter.hasNext()) {
51 while(iter.hasNext()) {
52 auto keyVal = iter.next();
52 auto keyVal = iter.next();
53− if (keyVal.value().toStrongRef().isNull()) {
53+ if (keyVal.value().isNull()) {
54 iter.remove();
54 iter.remove();
55+ break;
55 }
56 }
56 }
57 }
57 });
58 });
plugins/Unity/Indicators/unitymenumodelcache.h+2−1
@@ -24,6 +24,7 @@
24
24
25#include <QObject>
25#include <QObject>
26#include <QHash>
26#include <QHash>
27+#include <QPointer>
27#include <QWeakPointer>
28#include <QWeakPointer>
28
29
29class UnityMenuModel;
30class UnityMenuModel;
@@ -43,7 +44,7 @@ public:
43
44
44protected:
45protected:
45 QHash<QByteArray, QWeakPointer<UnityMenuModel>> m_registry;
46 QHash<QByteArray, QWeakPointer<UnityMenuModel>> m_registry;
46− static UnityMenuModelCache* theCache;
47+ static QPointer<UnityMenuModelCache> theCache;
47};
48};
48
49
49#endif // UNITYMENUMODELCACHE_H
50#endif // UNITYMENUMODELCACHE_H
tests/mocks/Unity/Indicators/fakeunitymenumodelcache.cpp+3−3
@@ -17,14 +17,14 @@
17#include "fakeunitymenumodelcache.h"
17#include "fakeunitymenumodelcache.h"
18#include <unitymenumodel.h>
18#include <unitymenumodel.h>
19
19
20−FakeUnityMenuModelCache* FakeUnityMenuModelCache::theFakeCache = nullptr;
20+QPointer<FakeUnityMenuModelCache> FakeUnityMenuModelCache::theFakeCache = nullptr;
21
21
22FakeUnityMenuModelCache* FakeUnityMenuModelCache::singleton()
22FakeUnityMenuModelCache* FakeUnityMenuModelCache::singleton()
23{
23{
24− if (!theFakeCache) {
24+ if (theFakeCache.isNull()) {
25 theFakeCache = new FakeUnityMenuModelCache();
25 theFakeCache = new FakeUnityMenuModelCache();
26 }
26 }
27− return theFakeCache;
27+ return theFakeCache.data();
28}
28}
29
29
30FakeUnityMenuModelCache::FakeUnityMenuModelCache(QObject* parent)
30FakeUnityMenuModelCache::FakeUnityMenuModelCache(QObject* parent)
tests/mocks/Unity/Indicators/fakeunitymenumodelcache.h+1−2
@@ -19,7 +19,6 @@
19
19
20#include "unitymenumodelcache.h"
20#include "unitymenumodelcache.h"
21
21
22−#include <QHash>
23#include <QVariantMap>
22#include <QVariantMap>
24
23
25class FakeUnityMenuModelCache : public UnityMenuModelCache
24class FakeUnityMenuModelCache : public UnityMenuModelCache
@@ -39,7 +38,7 @@ public:
39 Q_INVOKABLE QVariant getCachedModelData(const QByteArray& path);
38 Q_INVOKABLE QVariant getCachedModelData(const QByteArray& path);
40
39
41private:
40private:
42− static FakeUnityMenuModelCache* theFakeCache;
41+ static QPointer<FakeUnityMenuModelCache> theFakeCache;
43 QHash<QByteArray, QSharedPointer<UnityMenuModel>> m_models;
42 QHash<QByteArray, QSharedPointer<UnityMenuModel>> m_models;
44};
43};
45
44
tests/plugins/Unity/Indicators/sharedunitymenumodeltest.cpp+10
@@ -86,6 +86,16 @@ private Q_SLOTS:
86 QCOMPARE(model2->model()->menuObjectPath(), QByteArray("/com/canonical/LP1328646"));
86 QCOMPARE(model2->model()->menuObjectPath(), QByteArray("/com/canonical/LP1328646"));
87 }
87 }
88
88
89+ // Tests that the cache is recreated if deleted.
90+ void testDeletedCache()
91+ {
92+ QSharedPointer<SharedUnityMenuModel> model1(createFullModel("test1"));
93+
94+ QCOMPARE(UnityMenuModelCache::singleton()->contains("/com/canonical/test1"), true);
95+ delete UnityMenuModelCache::singleton();
96+ QCOMPARE(UnityMenuModelCache::singleton()->contains("/com/canonical/test1"), false);
97+ }
98+
89};
99};
90
100
91QTEST_GUILESS_MAIN(SharedUnityMenuModelTest)
101QTEST_GUILESS_MAIN(SharedUnityMenuModelTest)