Reorganise to remove redundant folder
[profile/ivi/common-api-dbus-runtime.git] / src / test / DBusStubAdapterTest.cpp
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #include <CommonAPI/DBus/DBusFunctionalHash.h>
5 #include <CommonAPI/DBus/DBusStubAdapterHelper.h>
6
7 #include <cassert>
8 #include <iostream>
9 #include <tuple>
10
11 static uint32_t dispatchedMessageCount;
12
13 class TestStubRemoteEventHandler {
14  public:
15     virtual ~TestStubRemoteEventHandler() { }
16
17     virtual bool onRemoteSetTestAttribute(int32_t testValue) = 0;
18
19     virtual void onRemoteTestAttributeChanged() = 0;
20 };
21
22 class TestStubAdapter: virtual public CommonAPI::StubAdapter {
23  public:
24     virtual void fireTestAttributeChanged(const int32_t& testValue)  = 0;
25 };
26
27 class TestStub: public CommonAPI::Stub<TestStubAdapter, TestStubRemoteEventHandler> {
28  public:
29     TestStub(): remoteEventHandler_(this) {
30     }
31
32     virtual TestStubRemoteEventHandler* initStubAdapter(const std::shared_ptr<TestStubAdapter>& stubAdapter) {
33         return &remoteEventHandler_;
34     }
35
36     void getEmptyResponse() {
37         std::cout << "onGetEmptyResponse()\n";
38         dispatchedMessageCount++;
39     }
40
41     void getDeepCopy(int32_t inValue, int32_t& outValue) {
42         outValue = inValue;
43         std::cout << "getDeepCopy(inValue=" << inValue << ", outValue=" << outValue << ")\n";
44         dispatchedMessageCount++;
45     }
46
47     void getDeepCopies(std::vector<int32_t> inValues, std::vector<int32_t>& outValues) {
48         outValues = inValues;
49         std::cout << "getDeepCopies(inValues=" << inValues.size() << ", outValues=" << outValues.size() << ")\n";
50         dispatchedMessageCount++;
51     }
52
53     void getShallowCopy(int32_t inValue, int32_t& outValue) {
54         outValue = inValue;
55         std::cout << "getShallowCopy(inValue=" << inValue << ", outValue=" << outValue << ")\n";
56         dispatchedMessageCount++;
57     }
58
59     void getShallowCopies(std::vector<int32_t> inValues, std::vector<int32_t>& outValues) {
60         outValues = inValues;
61         std::cout << "getShallowCopies(inValues=" << inValues.size() << ", outValues=" << outValues.size() << ")\n";
62         dispatchedMessageCount++;
63     }
64
65     const int32_t& getTestAttribute() {
66         return testAttributeValue_;
67     }
68
69  private:
70     class RemoteEventHandler: public TestStubRemoteEventHandler {
71      public:
72         RemoteEventHandler(TestStub* stub): stub_(stub) {
73         }
74
75         virtual bool onRemoteSetTestAttribute(int32_t testValue) {
76             std::cout << "RemoteEventHandler::onRemoteSetTestAttribute(" << testValue << "): oldValue = " << stub_->testAttributeValue_ << "\n";
77             const bool valueChanged = (stub_->testAttributeValue_ != testValue);
78             stub_->testAttributeValue_ = testValue;
79             return valueChanged;
80         }
81
82         virtual void onRemoteTestAttributeChanged() {
83             std::cout << "RemoteEventHandler::onRemoteTestAttributeChanged()\n";
84         }
85
86      private:
87         TestStub* stub_;
88     };
89
90     RemoteEventHandler remoteEventHandler_;
91     int32_t testAttributeValue_;
92 };
93
94 typedef CommonAPI::DBus::DBusStubAdapterHelper<TestStub> TestStubAdapterHelper;
95
96 class TestDBusStubAdapter: public TestStubAdapter,  public TestStubAdapterHelper {
97  public:
98     TestDBusStubAdapter(const std::string& dbusBusName,
99                     const std::string& dbusObjectPath,
100                     const std::shared_ptr<CommonAPI::DBus::DBusConnection>& dbusConnection,
101                     const std::shared_ptr<TestStub>& testStub) :
102                     TestStubAdapterHelper(
103                                     dbusBusName,
104                                     dbusObjectPath,
105                                     "org.genivi.CommonAPI.DBus.TestInterface",
106                                     dbusConnection,
107                                     testStub) {
108     }
109
110     virtual void fireTestAttributeChanged(const int32_t& testValue) {
111         std::cout << "TestDBusStubAdapter::fireTestAttributeChanged(" << testValue << ")\n";
112     }
113
114  protected:
115     virtual const char* getMethodsDBusIntrospectionXmlData() const;
116 };
117
118 const char* TestDBusStubAdapter::getMethodsDBusIntrospectionXmlData() const {
119     return "<method name=\"GetEmptyResponse\">\n"
120            "</method>\n"
121            "<method name=\"GetDeepCopy\">\n"
122                     "<arg type=\"i\" name=\"int32InValue\" direction=\"in\"/>\n"
123                     "<arg type=\"i\" name=\"int32OutValue\" direction=\"out\"/>\n"
124            "</method>\n"
125            "<method name=\"GetDeepCopies\">\n"
126                     "<arg type=\"ai\" name=\"int32InValues\" direction=\"in\"/>\n"
127                     "<arg type=\"ai\" name=\"int32OutValues\" direction=\"out\"/>\n"
128            "</method>\n"
129            "<method name=\"GetShallowCopy\">\n"
130                     "<arg type=\"i\" name=\"int32InValue\" direction=\"in\"/>\n"
131                     "<arg type=\"i\" name=\"int32OutValue\" direction=\"out\"/>\n"
132            "</method>\n"
133            "<method name=\"GetShallowCopies\">\n"
134                     "<arg type=\"ai\" name=\"int32InValues\" direction=\"in\"/>\n"
135                     "<arg type=\"ai\" name=\"int32OutValues\" direction=\"out\"/>\n"
136            "</method>"
137            "<method name=\"SetTestAttribute\">\n"
138                     "<arg type=\"i\" name=\"value\" direction=\"in\"/>\n"
139                     "<arg type=\"i\" name=\"value\" direction=\"out\"/>\n"
140            "</method>"
141            ;
142 }
143
144 namespace {
145 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
146                     TestStub,
147                     std::tuple<>,
148                     std::tuple<> > getEmptyResponseStubDispatcher(&TestStub::getEmptyResponse, "");
149 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
150                     TestStub,
151                     std::tuple<int32_t>,
152                     std::tuple<int32_t> > getDeepCopyStubDispatcher(&TestStub::getDeepCopy, "i");
153 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
154                     TestStub,
155                     std::tuple<std::vector<int32_t>>,
156                     std::tuple<std::vector<int32_t>> > getDeepCopiesStubDispatcher(&TestStub::getDeepCopies, "ai");
157 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
158                     TestStub,
159                     std::tuple<int32_t>,
160                     std::tuple<int32_t> > getShallowCopyStubDispatcher(&TestStub::getShallowCopy, "i");
161 static CommonAPI::DBus::DBusMethodWithReplyStubDispatcher<
162                     TestStub,
163                     std::tuple<std::vector<int32_t>>,
164                     std::tuple<std::vector<int32_t>> > getShallowCopiesStubDispatcher(&TestStub::getShallowCopies, "ai");
165 static CommonAPI::DBus::DBusSetObservableAttributeStubDispatcher<TestStub, int32_t> setTestAttributeStubDispatcher(
166                                     &TestStub::getTestAttribute,
167                                     &TestStubRemoteEventHandler::onRemoteSetTestAttribute,
168                                     &TestStubRemoteEventHandler::onRemoteTestAttributeChanged,
169                                     &TestStubAdapter::fireTestAttributeChanged,
170                                     "i");
171 }
172
173 template<>
174 const TestStubAdapterHelper::StubDispatcherTable TestStubAdapterHelper::stubDispatcherTable_ = {
175      { {"GetEmptyResponse", ""}, &getEmptyResponseStubDispatcher },
176      { {"GetDeepCopy", "i"}, &getDeepCopyStubDispatcher },
177      { {"GetDeepCopies", "ai"}, &getDeepCopiesStubDispatcher },
178      { {"GetShallowCopy", "i"}, &getShallowCopyStubDispatcher },
179      { {"GetShallowCopies", "ai"}, &getShallowCopiesStubDispatcher },
180      { {"SetTestAttribute", "i"}, &setTestAttributeStubDispatcher }
181 };
182
183 int main(void) {
184     auto dbusConnection = CommonAPI::DBus::DBusConnection::getSessionBus();
185
186     if (!dbusConnection->isConnected())
187         dbusConnection->connect();
188
189     assert(dbusConnection->isConnected());
190
191     const bool serviceNameAcquired = dbusConnection->requestServiceNameAndBlock(
192                     "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter");
193     assert(serviceNameAcquired);
194
195     auto testStub = std::make_shared<TestStub>();
196     auto testStubAdapter = std::make_shared<TestDBusStubAdapter>(
197                     "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter",
198                     "/common/api/dbus/TestDBusInterfaceAdapter",
199                     dbusConnection,
200                     testStub);
201     testStubAdapter->init();
202
203     auto dbusMessageCall = CommonAPI::DBus::DBusMessage::createMethodCall(
204                     "org.genivi.CommonAPI.DBus.TestDBusInterfaceAdapter",
205                     testStubAdapter->getObjectPath().c_str(),
206                     testStubAdapter->getServiceId().c_str(),
207                     "GetEmptyResponse");
208
209     const bool messageSent = dbusConnection->sendDBusMessage(dbusMessageCall);
210     assert(messageSent);
211
212     for (int i = 0; i < 10; i++)
213         dbusConnection->readWriteDispatch(100);
214
215 //    while (dbusConnection->readWriteDispatch(100))
216 //        ;
217
218     assert(dispatchedMessageCount > 0);
219
220     return 0;
221 }