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