Skip to content

Commit fc6256e

Browse files
author
Vitalii Arteev
committed
Add auxiliary API for Proxy
1 parent 63e4de6 commit fc6256e

File tree

5 files changed

+206
-5
lines changed

5 files changed

+206
-5
lines changed

src/ipc/ipc-common/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ facelift_add_library(FaceliftIPCCommonLib
2323
IPCServiceAdapterBase.h
2424
NewIPCServiceAdapterBase.h
2525
IPCAttachedPropertyFactory.h
26+
observer.h
2627
HEADERS_NO_MOC
2728
AppendDBUSSignatureFunction.h
2829
ipc-serialization.h

src/ipc/ipc-common/IPCProxyBase.h

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "IPCProxyBaseBase.h"
3535
#include "IPCProxyBinderBase.h"
3636

37-
3837
#if defined(FaceliftIPCCommonLib_LIBRARY)
3938
# define FaceliftIPCCommonLib_EXPORT Q_DECL_EXPORT
4039
#else

src/ipc/ipc-common/observer.h

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**********************************************************************
2+
**
3+
** Copyright (C) 2020 Luxoft Sweden AB
4+
**
5+
** This file is part of the FaceLift project
6+
**
7+
** Permission is hereby granted, freIPCServiceAdapterBasee of charge, to any person
8+
** obtaining a copy of this software and associated documentation files
9+
** (the "Software"), to deal in the Software without restriction,
10+
** including without limitation the rights to use, copy, modify, merge,
11+
** publish, distribute, sublicense, and/or sell copies of the Software,
12+
** and to permit persons to whom the Software is furnished to do so,
13+
** subject to the following conditions:
14+
**
15+
** The above copyright notice and this permission notice shall be
16+
** included in all copies or substantial portions of the Software.
17+
**
18+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20+
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22+
** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23+
** ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24+
** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
** SOFTWARE.
26+
**
27+
** SPDX-License-Identifier: MIT
28+
**
29+
**********************************************************************/
30+
31+
#pragma once
32+
33+
#include <memory>
34+
#include <QVector>
35+
#include <QObject>
36+
37+
namespace facelift {
38+
39+
class IObserver : public QObject
40+
{
41+
Q_OBJECT
42+
public:
43+
virtual void onReadyChanged(const std::shared_ptr<QMetaObject::Connection> &connection) = 0;
44+
};
45+
46+
class IsReadyObserver: public QObject
47+
{
48+
Q_OBJECT
49+
QVector< QPointer<IObserver> > m_observers{};
50+
51+
public:
52+
IsReadyObserver() {}
53+
54+
template<typename T, typename F>
55+
void watch(T const *object, F signal)
56+
{
57+
QObject::connect(object, std::move(signal), this, &IsReadyObserver::readyChanged);
58+
}
59+
60+
// Set observers
61+
void setObservers(const QVector< QPointer<IObserver> > &observers) {
62+
m_observers.clear();
63+
for (auto observer: observers) {
64+
Q_ASSERT (observer != nullptr);
65+
m_observers.push_back(observer);
66+
auto connection = std::make_shared<QMetaObject::Connection>();
67+
*connection = QObject::connect(this, &IsReadyObserver::readyChanged, observer, [observer, connection](){
68+
observer->onReadyChanged( connection );
69+
});
70+
}
71+
}
72+
73+
// Get observers
74+
const QVector< QPointer<IObserver> > &getObservers() const {
75+
return m_observers;
76+
}
77+
78+
Q_SIGNAL void readyChanged();
79+
80+
void onReadyChanged() {
81+
emit readyChanged();
82+
}
83+
};
84+
85+
// Single-time observer which will unregister itself when done
86+
template<typename T>
87+
class SingleTimeObserver : public IObserver
88+
{
89+
T m_function;
90+
91+
public:
92+
explicit SingleTimeObserver(T function) : m_function{function} {}
93+
~SingleTimeObserver() = default;
94+
95+
void onReadyChanged(const std::shared_ptr<QMetaObject::Connection> &connection) override {
96+
m_function();
97+
QObject::disconnect(*connection);
98+
}
99+
};
100+
101+
// Standard observer which will work for each signal
102+
template<typename T>
103+
class StandartObserver : public IObserver
104+
{
105+
T m_function;
106+
107+
public:
108+
explicit StandartObserver(T function) : m_function{function} {}
109+
~StandartObserver() = default;
110+
111+
void onReadyChanged(const std::shared_ptr<QMetaObject::Connection> &) override {
112+
m_function();
113+
}
114+
};
115+
116+
}

tests/unittest/CMakeLists.txt

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,34 @@ if(${GTEST_FOUND})
66
add_library(GTest::GMock UNKNOWN IMPORTED)
77
set_target_properties(GTest::GMock PROPERTIES IMPORTED_LOCATION ${GMOCK_LIBRARY})
88
else()
9-
message(WARNING "Google test/mock not found.")
9+
message(ERROR "Google test/mock not found.")
1010
endif()
1111

12+
find_package(Qt5 COMPONENTS Test REQUIRED)
13+
if(${QT5TEST_NOTFOUND})
14+
message(ERROR "Required package Qt5Test not found.")
15+
endif()
1216
find_package(Threads REQUIRED)
13-
set(FACELIFT_GTEST_LIBRARIES ${GTEST_BOTH_LIBRARIES} GTest::GMock Threads::Threads)
17+
if(${THREADS_NOTFOUND})
18+
message(ERROR "Required package Threads not found.")
19+
endif()
20+
set(FACELIFT_GTEST_LIBRARIES ${GTEST_BOTH_LIBRARIES} GTest::GMock Qt5::Test Threads::Threads)
1421
include_directories(${GTEST_INCLUDE_DIRS})
1522

1623
facelift_add_test(UnitTests
1724
SOURCES FaceliftUtilsTest.cpp
18-
LINK_LIBRARIES ${FACELIFT_GTEST_LIBRARIES} FaceliftCommonLib)
25+
LINK_LIBRARIES
26+
${FACELIFT_GTEST_LIBRARIES}
27+
FaceliftCommonLib
28+
)
1929

30+
facelift_add_test(UnitTestsObserver
31+
SOURCES FaceliftObserverTest.cpp
32+
LINK_LIBRARIES
33+
${FACELIFT_GTEST_LIBRARIES}
34+
FaceliftIPCCommonLib
35+
)
2036
else()
21-
message(WARNING "Required package google test not found!")
37+
message(ERROR "Required package google test not found!")
2238
endif()
2339

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <gtest/gtest.h>
2+
#include "IPCProxyBase.h"
3+
#include "InterfaceBase.h"
4+
#include <QSignalSpy>
5+
#include "observer.h"
6+
7+
namespace {
8+
9+
using namespace facelift;
10+
11+
class Counter
12+
{
13+
public:
14+
Counter() { m_value = 0; }
15+
16+
void incValue() {
17+
++m_value;
18+
}
19+
int getValue() const {
20+
return m_value;
21+
}
22+
private:
23+
int m_value;
24+
};
25+
26+
class IPCProxyBaseTest : public ::testing::Test
27+
{
28+
public:
29+
Counter c1;
30+
Counter c2;
31+
32+
StandartObserver < std::function<void()> > obs1 { std::bind(&Counter::incValue, &c1) };
33+
SingleTimeObserver< std::function<void()> > obs2 { std::bind(&Counter::incValue, &c2) };
34+
35+
IPCProxyBase<InterfaceBase> proxyBase{nullptr};
36+
IsReadyObserver readyObserver{};
37+
38+
~IPCProxyBaseTest() {}
39+
};
40+
41+
TEST_F(IPCProxyBaseTest, testObservers)
42+
{
43+
readyObserver.watch( &proxyBase, &IPCProxyBase<InterfaceBase>::readyChanged );
44+
// Set observers
45+
const auto expected = QVector< QPointer<IObserver> >{ &obs1, &obs2 };
46+
readyObserver.setObservers(expected);
47+
48+
// Check values before calling a signal
49+
ASSERT_EQ(c1.getValue(), 0); // for StandartObserver
50+
ASSERT_EQ(c2.getValue(), 0); // for SingleTimeObserver
51+
52+
// Check handle of signal
53+
QSignalSpy spy(&readyObserver, &IsReadyObserver::readyChanged );
54+
ASSERT_EQ( spy.isValid(), true);
55+
spy.clear();
56+
57+
// Generate signal
58+
ASSERT_EQ(spy.count(), 0);
59+
proxyBase.readyChanged();
60+
proxyBase.readyChanged();
61+
proxyBase.readyChanged();
62+
ASSERT_EQ(spy.count(), 3);
63+
64+
// Check values after signal call
65+
ASSERT_EQ(c1.getValue(), 3); // for StandartObserver
66+
ASSERT_EQ(c2.getValue(), 1); // for SingleTimeObserver
67+
}
68+
69+
} // end namespace

0 commit comments

Comments
 (0)