tests: fix blocking semantic in DBusProxyTest
[profile/ivi/common-api-dbus-runtime.git] / src / test / DBusMultipleConnectionTest.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 <gtest/gtest.h>
8
9 #include <cassert>
10 #include <cstdint>
11 #include <iostream>
12 #include <functional>
13 #include <memory>
14 #include <stdint.h>
15 #include <string>
16 #include <utility>
17 #include <tuple>
18 #include <type_traits>
19
20 #include <CommonAPI/types.h>
21 #include <CommonAPI/Runtime.h>
22
23 #include "commonapi/tests/TestInterfaceProxy.h"
24 #include "commonapi/tests/TestInterfaceStubDefault.h"
25
26
27 const std::string serviceAddress = "local:commonapi.tests.TestInterface:commonapi.tests.TestInterface";
28
29 class DBusMultipleConnectionTest: public ::testing::Test {
30  protected:
31     virtual void SetUp() {
32         proxyFactory = CommonAPI::Runtime::load()->createFactory();
33         stubFactory = CommonAPI::Runtime::load()->createFactory();
34         ASSERT_TRUE((bool)proxyFactory);
35         ASSERT_TRUE((bool)stubFactory);
36
37         stub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
38         bool success = stubFactory->registerService(stub, serviceAddress);
39         ASSERT_TRUE(success);
40
41         proxy = proxyFactory->buildProxy<commonapi::tests::TestInterfaceProxy>(serviceAddress);
42         ASSERT_TRUE((bool)proxy);
43     }
44
45     virtual void TearDown() {
46     }
47
48     std::shared_ptr<CommonAPI::Factory> proxyFactory;
49     std::shared_ptr<CommonAPI::Factory> stubFactory;
50     std::shared_ptr<commonapi::tests::TestInterfaceStubDefault> stub;
51     std::shared_ptr<commonapi::tests::TestInterfaceProxy<> > proxy;
52
53 };
54
55
56 TEST_F(DBusMultipleConnectionTest, RemoteMethodCall) {
57     uint32_t v1 = 5;
58     std::string v2 = "Hai :)";
59     CommonAPI::CallStatus stat;
60     proxy->testVoidPredefinedTypeMethod(v1, v2, stat);
61     ASSERT_EQ(CommonAPI::CallStatus::SUCCESS, stat);
62 }
63
64 TEST_F(DBusMultipleConnectionTest, Broadcast) {
65     uint32_t v1 = 5;
66     uint32_t v3 = 0;
67     std::string v2 = "Hai :)";
68
69     std::promise<bool> promise;
70     auto future = promise.get_future();
71
72     proxy->getTestPredefinedTypeBroadcastEvent().subscribe([&](
73                     const uint32_t intVal, const std::string& strVal) {
74         v3 = intVal;
75         promise.set_value(true);
76     });
77
78     stub->fireTestPredefinedTypeBroadcastEvent(v1, v2);
79
80     ASSERT_TRUE(future.get());
81     ASSERT_EQ(v1, v3);
82 }
83
84 TEST_F(DBusMultipleConnectionTest, SetAttribute) {
85     uint32_t v1 = 5;
86     uint32_t v2;
87     CommonAPI::CallStatus stat;
88     proxy->getTestPredefinedTypeAttributeAttribute().setValue(v1, stat, v2);
89     ASSERT_EQ(CommonAPI::CallStatus::SUCCESS, stat);
90     ASSERT_EQ(v1, v2);
91 }
92
93 TEST_F(DBusMultipleConnectionTest, SetAttributeBroadcast) {
94     uint32_t v1 = 6;
95     uint32_t v2;
96     uint32_t v3 = 0;
97
98     std::promise<bool> promise;
99     auto future = promise.get_future();
100
101     proxy->getTestPredefinedTypeAttributeAttribute().getChangedEvent().subscribe([&](
102                     const uint32_t intVal) {
103         v3 = intVal;
104         promise.set_value(true);
105     });
106
107     CommonAPI::CallStatus stat;
108     proxy->getTestPredefinedTypeAttributeAttribute().setValue(v1, stat, v2);
109     ASSERT_EQ(CommonAPI::CallStatus::SUCCESS, stat);
110     ASSERT_EQ(v1, v2);
111
112     ASSERT_TRUE(future.get());
113     ASSERT_EQ(v1, v3);
114
115 }
116
117
118 TEST_F(DBusMultipleConnectionTest, GetAttribute) {
119     uint32_t v1;
120     CommonAPI::CallStatus stat = proxy->getTestPredefinedTypeAttributeAttribute().getValue(v1);
121     ASSERT_EQ(CommonAPI::CallStatus::SUCCESS, stat);
122 }
123
124
125 int main(int argc, char** argv) {
126     ::testing::InitGoogleTest(&argc, argv);
127     return RUN_ALL_TESTS();
128 }
129