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/. */
8 #include <gtest/gtest.h>
19 #include <type_traits>
21 #include <CommonAPI/types.h>
22 #include <CommonAPI/AttributeExtension.h>
23 #include <CommonAPI/Runtime.h>
25 #include <CommonAPI/DBus/DBusConnection.h>
26 #include <CommonAPI/DBus/DBusProxy.h>
27 #include <CommonAPI/DBus/DBusRuntime.h>
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"
35 #include "commonapi/tests/TestInterfaceDBusProxy.h"
38 class DBusProxyFactoryTest: public ::testing::Test {
40 virtual void SetUp() {
41 runtime_ = CommonAPI::Runtime::load();
42 ASSERT_TRUE((bool)runtime_);
43 CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime_));
44 ASSERT_TRUE(dbusRuntime != NULL);
47 virtual void TearDown() {
50 std::shared_ptr<CommonAPI::Runtime> runtime_;
55 namespace myExtensions {
57 template<typename _AttributeType>
58 class AttributeTestExtension: public CommonAPI::AttributeExtension<_AttributeType> {
59 typedef CommonAPI::AttributeExtension<_AttributeType> __baseClass_t;
62 typedef typename _AttributeType::ValueType ValueType;
63 typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;
65 AttributeTestExtension(_AttributeType& baseAttribute) :
66 CommonAPI::AttributeExtension<_AttributeType>(baseAttribute) {}
68 ~AttributeTestExtension() {}
70 bool testExtensionMethod() const {
75 } // namespace myExtensions
77 //####################################################################################################################
79 TEST_F(DBusProxyFactoryTest, DBusFactoryCanBeCreated) {
80 std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime_->createFactory();
81 ASSERT_TRUE((bool)proxyFactory);
82 CommonAPI::DBus::DBusFactory* dbusProxyFactory = dynamic_cast<CommonAPI::DBus::DBusFactory*>(&(*proxyFactory));
83 ASSERT_TRUE(dbusProxyFactory != NULL);
86 TEST_F(DBusProxyFactoryTest, CreatesDefaultTestProxy) {
87 std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime_->createFactory();
88 ASSERT_TRUE((bool)proxyFactory);
89 auto defaultTestProxy = proxyFactory->buildProxy<commonapi::tests::TestInterfaceProxy>("local:commonapi.tests.TestInterface:commonapi.tests.TestInterface");
90 ASSERT_TRUE((bool)defaultTestProxy);
93 TEST_F(DBusProxyFactoryTest, CreatesDefaultExtendedTestProxy) {
94 std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime_->createFactory();
95 ASSERT_TRUE((bool)proxyFactory);
96 auto defaultTestProxy = proxyFactory->buildProxyWithDefaultAttributeExtension<
97 commonapi::tests::TestInterfaceProxy,
98 myExtensions::AttributeTestExtension>("local:commonapi.tests.TestInterface:commonapi.tests.TestInterface");
99 ASSERT_TRUE((bool)defaultTestProxy);
102 TEST_F(DBusProxyFactoryTest, CreatesIndividuallyExtendedTestProxy) {
103 std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime_->createFactory();
104 ASSERT_TRUE((bool)proxyFactory);
105 auto specificAttributeExtendedTestProxy = proxyFactory->buildProxy<
106 commonapi::tests::TestInterfaceProxy,
107 commonapi::tests::TestInterfaceExtensions::TestDerivedArrayAttributeAttributeExtension<myExtensions::AttributeTestExtension> >
108 ("local:commonapi.tests.TestInterface:commonapi.tests.TestInterface");
110 ASSERT_TRUE((bool)specificAttributeExtendedTestProxy);
112 auto attributeExtension = specificAttributeExtendedTestProxy->getTestDerivedArrayAttributeAttributeExtension();
113 ASSERT_TRUE(attributeExtension.testExtensionMethod());
116 TEST_F(DBusProxyFactoryTest, HandlesRegistrationOfStubAdapters) {
117 std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime_->createFactory();
118 ASSERT_TRUE((bool)proxyFactory);
120 const std::string serviceAddress = "local:commonapi.tests.TestInterface:commonapi.tests.TestInterface";
122 auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
123 bool success = proxyFactory->registerService(myStub, serviceAddress);
124 ASSERT_TRUE(success);
126 success = proxyFactory->unregisterService("SomeOther:Unknown:Service");
127 ASSERT_FALSE(success);
129 success = proxyFactory->unregisterService(serviceAddress);
130 ASSERT_TRUE(success);
133 TEST_F(DBusProxyFactoryTest, GracefullyHandlesWrongAddresses) {
134 std::shared_ptr<CommonAPI::Factory> proxyFactory = runtime_->createFactory();
135 ASSERT_TRUE((bool)proxyFactory);
136 auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
138 ASSERT_FALSE(proxyFactory->registerService(myStub, ""));
139 ASSERT_FALSE(proxyFactory->registerService(myStub, "too:much:stuff:here"));
143 int main(int argc, char** argv) {
144 ::testing::InitGoogleTest(&argc, argv);
145 return RUN_ALL_TESTS();