Reorganise to remove redundant folder
[profile/ivi/common-api-dbus-runtime.git] / src / test / DBusFactoryTest.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
5 #include <gtest/gtest.h>
6
7 #include <cassert>
8 #include <cstdint>
9 #include <iostream>
10 #include <functional>
11 #include <memory>
12 #include <stdint.h>
13 #include <string>
14 #include <utility>
15 #include <tuple>
16 #include <type_traits>
17
18 #include <CommonAPI/types.h>
19 #include <CommonAPI/AttributeExtension.h>
20 #include <CommonAPI/Runtime.h>
21
22 #include <CommonAPI/DBus/DBusConnection.h>
23 #include <CommonAPI/DBus/DBusProxy.h>
24 #include <CommonAPI/DBus/DBusRuntime.h>
25
26 #include "commonapi/tests/PredefinedTypeCollection.h"
27 #include "commonapi/tests/DerivedTypeCollection.h"
28 #include "commonapi/tests/TestInterfaceProxy.h"
29 #include "commonapi/tests/TestInterfaceStubDefault.h"
30 #include "commonapi/tests/TestInterfaceDBusStubAdapter.h"
31
32 #include "commonapi/tests/TestInterfaceDBusProxy.h"
33
34 std::shared_ptr<CommonAPI::Runtime> runtime_;
35 std::shared_ptr<CommonAPI::Factory> proxyFactory;
36
37 class DBusProxyFactoryTest: public ::testing::Test {
38  protected:
39     virtual void SetUp() {
40          if(!runtime_) {
41              runtime_ = CommonAPI::Runtime::load();
42
43          }
44          ASSERT_TRUE((bool)runtime_);
45          CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime_));
46          ASSERT_TRUE(dbusRuntime != NULL);
47     }
48
49     virtual void TearDown() {
50     }
51 };
52
53
54
55 namespace myExtensions {
56
57 template<typename _AttributeType>
58 class AttributeTestExtension: public CommonAPI::AttributeExtension<_AttributeType> {
59     typedef CommonAPI::AttributeExtension<_AttributeType> __baseClass_t;
60
61 public:
62     typedef typename _AttributeType::ValueType ValueType;
63     typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;
64
65     AttributeTestExtension(_AttributeType& baseAttribute) :
66                     CommonAPI::AttributeExtension<_AttributeType>(baseAttribute) {}
67
68    ~AttributeTestExtension() {}
69
70    bool testExtensionMethod() const {
71        return true;
72    }
73 };
74
75 } // namespace myExtensions
76
77 //####################################################################################################################
78
79 TEST_F(DBusProxyFactoryTest, DBusFactoryCanBeCreated) {
80     proxyFactory = runtime_->createFactory();
81     ASSERT_TRUE((bool)proxyFactory);
82     CommonAPI::DBus::DBusFactory* dbusProxyFactory = dynamic_cast<CommonAPI::DBus::DBusFactory*>(&(*proxyFactory));
83     ASSERT_TRUE(dbusProxyFactory != NULL);
84 }
85
86 TEST_F(DBusProxyFactoryTest, CreatesDefaultTestProxy) {
87     ASSERT_TRUE((bool)proxyFactory);
88     auto defaultTestProxy = proxyFactory->buildProxy<commonapi::tests::TestInterfaceProxy>("local:commonapi.tests.TestInterface:commonapi.tests.TestInterface");
89     ASSERT_TRUE((bool)defaultTestProxy);
90 }
91
92 TEST_F(DBusProxyFactoryTest, CreatesDefaultExtendedTestProxy) {
93     ASSERT_TRUE((bool)proxyFactory);
94     auto defaultTestProxy = proxyFactory->buildProxyWithDefaultAttributeExtension<
95                     commonapi::tests::TestInterfaceProxy,
96                     myExtensions::AttributeTestExtension>("local:commonapi.tests.TestInterface:commonapi.tests.TestInterface");
97     ASSERT_TRUE((bool)defaultTestProxy);
98 }
99
100 TEST_F(DBusProxyFactoryTest, CreatesIndividuallyExtendedTestProxy) {
101     ASSERT_TRUE((bool)proxyFactory);
102     auto specificAttributeExtendedTestProxy = proxyFactory->buildProxy<
103                     commonapi::tests::TestInterfaceProxy,
104                     commonapi::tests::TestInterfaceExtensions::TestDerivedArrayAttributeAttributeExtension<myExtensions::AttributeTestExtension> >
105             ("local:commonapi.tests.TestInterface:commonapi.tests.TestInterface");
106
107     ASSERT_TRUE((bool)specificAttributeExtendedTestProxy);
108
109     auto attributeExtension = specificAttributeExtendedTestProxy->getTestDerivedArrayAttributeAttributeExtension();
110     ASSERT_TRUE(attributeExtension.testExtensionMethod());
111 }
112
113 TEST_F(DBusProxyFactoryTest, HandlesRegistrationOfStubAdapters) {
114     ASSERT_TRUE((bool)proxyFactory);
115
116     const std::string serviceAddress = "local:commonapi.tests.TestInterface:commonapi.tests.TestInterface";
117
118     auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
119     bool success = proxyFactory->registerService(myStub, serviceAddress);
120     ASSERT_TRUE(success);
121
122     success = proxyFactory->unregisterService("SomeOther:Unknown:Service");
123     ASSERT_FALSE(success);
124
125     success = proxyFactory->unregisterService(serviceAddress);
126     ASSERT_TRUE(success);
127 }
128
129 TEST_F(DBusProxyFactoryTest, GracefullyHandlesWrongAddresses) {
130     ASSERT_TRUE((bool)proxyFactory);
131     auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
132
133     ASSERT_FALSE(proxyFactory->registerService(myStub, ""));
134     ASSERT_FALSE(proxyFactory->registerService(myStub, "too:much:stuff:here"));
135 }
136
137
138 int main(int argc, char** argv) {
139     ::testing::InitGoogleTest(&argc, argv);
140     return RUN_ALL_TESTS();
141 }