summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/clientapi.h25
-rw-r--r--tests/clientapitest.cpp36
2 files changed, 20 insertions, 41 deletions
diff --git a/common/clientapi.h b/common/clientapi.h
index 4948c59..748baa6 100644
--- a/common/clientapi.h
+++ b/common/clientapi.h
@@ -108,7 +108,7 @@ public:
108 108
109class FacadeFactory { 109class FacadeFactory {
110public: 110public:
111 typedef std::function<void*(bool &externallyManaged)> FactoryFunction; 111 typedef std::function<std::shared_ptr<void>()> FactoryFunction;
112 112
113 //FIXME: proper singleton implementation 113 //FIXME: proper singleton implementation
114 static FacadeFactory &instance() 114 static FacadeFactory &instance()
@@ -126,17 +126,13 @@ public:
126 void registerFacade(const QByteArray &resource) 126 void registerFacade(const QByteArray &resource)
127 { 127 {
128 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>(); 128 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>();
129 mFacadeRegistry.insert(key(resource, typeName), [](bool &externallyManaged){ return new Facade; }); 129 mFacadeRegistry.insert(key(resource, typeName), [](){ return std::shared_ptr<Facade>(new Facade); });
130 } 130 }
131 131
132 /* 132 /*
133 * Allows the registrar to register a specific instance. 133 * Allows the registrar to register a specific instance.
134 * 134 *
135 * Primarily for testing. 135 * Primarily for testing.
136 * The facade factory takes ovnership of the pointer and typically deletes the instance via shared pointer.
137 * Supplied factory functions should therefore always return a new pointer (i.e. via clone())
138 *
139 * FIXME the factory function should really be returning QSharedPointer<void>, which doesn't work (std::shared_pointer<void> would though). That way i.e. a test could keep the object alive until it's done. As a workaround the factory function can define wether it manages the lifetime of the facade itself.
140 */ 136 */
141 template<class DomainType, class Facade> 137 template<class DomainType, class Facade>
142 void registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction) 138 void registerFacade(const QByteArray &resource, const FactoryFunction &customFactoryFunction)
@@ -155,25 +151,12 @@ public:
155 mFacadeRegistry.clear(); 151 mFacadeRegistry.clear();
156 } 152 }
157 153
158 static void doNothingDeleter(void *)
159 {
160 qWarning() << "Do nothing";
161 }
162
163 template<class DomainType> 154 template<class DomainType>
164 std::shared_ptr<StoreFacade<DomainType> > getFacade(const QByteArray &resource) 155 std::shared_ptr<StoreFacade<DomainType> > getFacade(const QByteArray &resource)
165 { 156 {
166 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>(); 157 const QByteArray typeName = ApplicationDomain::getTypeName<DomainType>();
167 auto factoryFunction = mFacadeRegistry.value(key(resource, typeName)); 158 if (auto factoryFunction = mFacadeRegistry.value(key(resource, typeName))) {
168 if (factoryFunction) { 159 return std::static_pointer_cast<StoreFacade<DomainType> >(factoryFunction());
169 bool externallyManaged = false;
170 auto ptr = static_cast<StoreFacade<DomainType>* >(factoryFunction(externallyManaged));
171 if (externallyManaged) {
172 //Allows tests to manage the lifetime of injected facades themselves
173 return std::shared_ptr<StoreFacade<DomainType> >(ptr, doNothingDeleter);
174 } else {
175 return std::shared_ptr<StoreFacade<DomainType> >(ptr);
176 }
177 } 160 }
178 qWarning() << "Failed to find facade for resource: " << resource << " and type: " << typeName; 161 qWarning() << "Failed to find facade for resource: " << resource << " and type: " << typeName;
179 return std::shared_ptr<StoreFacade<DomainType> >(); 162 return std::shared_ptr<StoreFacade<DomainType> >();
diff --git a/tests/clientapitest.cpp b/tests/clientapitest.cpp
index 30c52fc..081e6ad 100644
--- a/tests/clientapitest.cpp
+++ b/tests/clientapitest.cpp
@@ -106,13 +106,12 @@ private Q_SLOTS:
106 106
107 void testLoad() 107 void testLoad()
108 { 108 {
109 DummyResourceFacade facade; 109 auto facade = std::make_shared<DummyResourceFacade>();
110 facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); 110 facade->results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>());
111 111
112 Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource", 112 Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource",
113 [&facade](bool &externallyManaged) { 113 [facade]() {
114 externallyManaged = true; 114 return facade;
115 return &facade;
116 } 115 }
117 ); 116 );
118 117
@@ -127,13 +126,12 @@ private Q_SLOTS:
127 126
128 void testLiveQuery() 127 void testLiveQuery()
129 { 128 {
130 DummyResourceFacade facade; 129 auto facade = std::make_shared<DummyResourceFacade>();
131 facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); 130 facade->results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>());
132 131
133 Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource", 132 Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource",
134 [&facade](bool &externallyManaged){ 133 [facade](){
135 externallyManaged = true; 134 return facade;
136 return &facade;
137 } 135 }
138 ); 136 );
139 137
@@ -146,22 +144,20 @@ private Q_SLOTS:
146 QCOMPARE(result.size(), 1); 144 QCOMPARE(result.size(), 1);
147 145
148 //Enter a second result 146 //Enter a second result
149 facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id2", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); 147 facade->results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id2", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>());
150 qWarning() << &facade; 148 QVERIFY(facade->notifier);
151 QVERIFY(facade.notifier); 149 facade->notifier->revisionChanged(2);
152 facade.notifier->revisionChanged(2);
153 QTRY_COMPARE(result.size(), 2); 150 QTRY_COMPARE(result.size(), 2);
154 } 151 }
155 152
156 void testQueryLifetime() 153 void testQueryLifetime()
157 { 154 {
158 DummyResourceFacade facade; 155 auto facade = std::make_shared<DummyResourceFacade>();
159 facade.results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>()); 156 facade->results << QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("resource", "id", 0, QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor>());
160 157
161 Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource", 158 Akonadi2::FacadeFactory::instance().registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>("dummyresource",
162 [&facade](bool &externallyManaged){ 159 [facade](){
163 externallyManaged = true; 160 return facade;
164 return &facade;
165 } 161 }
166 ); 162 );
167 163
@@ -175,7 +171,7 @@ private Q_SLOTS:
175 QCOMPARE(result.size(), 1); 171 QCOMPARE(result.size(), 1);
176 } 172 }
177 //It's running in a separate thread, so we have to wait for a moment. 173 //It's running in a separate thread, so we have to wait for a moment.
178 QTRY_VERIFY(!facade.capturedResultProvider); 174 QTRY_VERIFY(!facade->capturedResultProvider);
179 } 175 }
180 176
181}; 177};