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/. */
5 #include <gtest/gtest.h>
16 #include <type_traits>
18 #include <CommonAPI/types.h>
19 #include <CommonAPI/AttributeExtension.h>
20 #include <CommonAPI/Runtime.h>
22 #include <CommonAPI/DBus/DBusConnection.h>
23 #include <CommonAPI/DBus/DBusProxy.h>
24 #include <CommonAPI/DBus/DBusRuntime.h>
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"
32 #include "commonapi/tests/TestInterfaceDBusProxy.h"
34 std::shared_ptr<CommonAPI::Runtime> runtime_;
35 std::shared_ptr<CommonAPI::Factory> proxyFactory;
37 class DBusProxyFactoryTest: public ::testing::Test {
39 virtual void SetUp() {
41 runtime_ = CommonAPI::Runtime::load();
44 ASSERT_TRUE((bool)runtime_);
45 CommonAPI::DBus::DBusRuntime* dbusRuntime = dynamic_cast<CommonAPI::DBus::DBusRuntime*>(&(*runtime_));
46 ASSERT_TRUE(dbusRuntime != NULL);
49 virtual void TearDown() {
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 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 ASSERT_TRUE((bool)proxyFactory);
88 auto defaultTestProxy = proxyFactory->buildProxy<commonapi::tests::TestInterfaceProxy>("local:commonapi.tests.TestInterface:commonapi.tests.TestInterface");
89 ASSERT_TRUE((bool)defaultTestProxy);
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);
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");
107 ASSERT_TRUE((bool)specificAttributeExtendedTestProxy);
109 auto attributeExtension = specificAttributeExtendedTestProxy->getTestDerivedArrayAttributeAttributeExtension();
110 ASSERT_TRUE(attributeExtension.testExtensionMethod());
113 TEST_F(DBusProxyFactoryTest, HandlesRegistrationOfStubAdapters) {
114 ASSERT_TRUE((bool)proxyFactory);
116 const std::string serviceAddress = "local:commonapi.tests.TestInterface:commonapi.tests.TestInterface";
118 auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
119 bool success = proxyFactory->registerService(myStub, serviceAddress);
120 ASSERT_TRUE(success);
122 success = proxyFactory->unregisterService("SomeOther:Unknown:Service");
123 ASSERT_FALSE(success);
125 success = proxyFactory->unregisterService(serviceAddress);
126 ASSERT_TRUE(success);
129 TEST_F(DBusProxyFactoryTest, GracefullyHandlesWrongAddresses) {
130 ASSERT_TRUE((bool)proxyFactory);
131 auto myStub = std::make_shared<commonapi::tests::TestInterfaceStubDefault>();
133 ASSERT_FALSE(proxyFactory->registerService(myStub, ""));
134 ASSERT_FALSE(proxyFactory->registerService(myStub, "too:much:stuff:here"));
138 int main(int argc, char** argv) {
139 ::testing::InitGoogleTest(&argc, argv);
140 return RUN_ALL_TESTS();