tests: fix blocking semantic in DBusProxyTest
[profile/ivi/common-api-dbus-runtime.git] / src / test / DBusCommunicationTest.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
8 #include <gtest/gtest.h>
9
10 #include <cassert>
11 #include <cstdint>
12 #include <iostream>
13 #include <functional>
14 #include <memory>
15 #include <stdint.h>
16 #include <string>
17 #include <utility>
18 #include <tuple>
19 #include <type_traits>
20
21 #include <CommonAPI/types.h>
22 #include <CommonAPI/AttributeExtension.h>
23 #include <CommonAPI/Runtime.h>
24
25 #include <CommonAPI/DBus/DBusConnection.h>
26 #include <CommonAPI/DBus/DBusProxy.h>
27 #include <CommonAPI/DBus/DBusRuntime.h>
28
29 #include "commonapi/tests/PredefinedTypeCollection.h"
30 #include "commonapi/tests/DerivedTypeCollection.h"
31 #include "commonapi/tests/TestInterfaceProxy.h"
32 #include "commonapi/tests/TestInterfaceStubDefault.h"
33 #include "commonapi/tests/TestInterfaceDBusStubAdapter.h"
34
35 #include "commonapi/tests/TestInterfaceDBusProxy.h"
36
37
38 namespace myExtensions {
39
40 template<typename _AttributeType>
41 class AttributeTestExtension: public CommonAPI::AttributeExtension<_AttributeType> {
42     typedef CommonAPI::AttributeExtension<_AttributeType> __baseClass_t;
43
44 public:
45     typedef typename _AttributeType::ValueType ValueType;
46     typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;
47
48     AttributeTestExtension(_AttributeType& baseAttribute) :
49                     CommonAPI::AttributeExtension<_AttributeType>(baseAttribute) {}
50
51    ~AttributeTestExtension() {}
52
53    bool testExtensionMethod() const {
54        return true;
55    }
56 };
57
58 } // namespace myExtensions
59
60
61 class DBusCommunicationTest: public ::testing::Test {
62  protected:
63     virtual void SetUp() {
64         runtime_ = CommonAPI::Runtime::load();
65         ASSERT_TRUE((bool)runtime_);
66         CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime_));
67         ASSERT_TRUE(dbusRuntime != NULL);
68
69         proxyFactory_ = runtime_->createFactory();
70         ASSERT_TRUE((bool)proxyFactory_);
71         stubFactory_ = runtime_->createFactory();
72         ASSERT_TRUE((bool)stubFactory_);
73     }
74
75     virtual void TearDown() {
76     }
77
78     std::shared_ptr<CommonAPI::Runtime> runtime_;
79     std::shared_ptr<CommonAPI::Factory> proxyFactory_;
80     std::shared_ptr<CommonAPI::Factory> stubFactory_;
81
82     static const std::string serviceAddress_;
83     static const std::string nonstandardAddress_;
84 };
85
86 const std::string DBusCommunicationTest::serviceAddress_ = "local:commonapi.tests.TestInterface:commonapi.tests.TestInterface";
87 const std::string DBusCommunicationTest::nonstandardAddress_ = "local:non.standard.ServiceName:non.standard.participand.ID";
88
89
90
91 TEST_F(DBusCommunicationTest, RemoteMethodCallSucceeds) {
92     auto defaultTestProxy = proxyFactory_->buildProxy<commonapi::tests::TestInterfaceProxy>(serviceAddress_);
93     ASSERT_TRUE((bool)defaultTestProxy);
94
95     auto stub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
96     bool success = stubFactory_->registerService(stub, serviceAddress_);
97     ASSERT_TRUE(success);
98
99     uint32_t v1 = 5;
100     std::string v2 = "Ciao ;)";
101     CommonAPI::CallStatus stat;
102     defaultTestProxy->testVoidPredefinedTypeMethod(v1, v2, stat);
103
104     ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
105 }
106
107
108 TEST_F(DBusCommunicationTest, RemoteMethodCallWithNonstandardAddressSucceeds) {
109     auto defaultTestProxy = proxyFactory_->buildProxy<commonapi::tests::TestInterfaceProxy>(nonstandardAddress_);
110     ASSERT_TRUE((bool)defaultTestProxy);
111
112     auto stub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
113     bool success = stubFactory_->registerService(stub, nonstandardAddress_);
114     ASSERT_TRUE(success);
115
116     uint32_t v1 = 5;
117     std::string v2 = "Hai :)";
118     CommonAPI::CallStatus stat;
119     defaultTestProxy->testVoidPredefinedTypeMethod(v1, v2, stat);
120
121     ASSERT_EQ(stat, CommonAPI::CallStatus::SUCCESS);
122 }
123
124
125 int main(int argc, char** argv) {
126     ::testing::InitGoogleTest(&argc, argv);
127     return RUN_ALL_TESTS();
128 }