summaryrefslogtreecommitdiff
path: root/src/test/DBusFactoryTest.cpp
blob: 866130af6df0d91b652b11d0745d6990543010b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (C) 2013-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <gtest/gtest.h>

#include <cassert>
#include <cstdint>
#include <iostream>
#include <fstream>
#include <functional>
#include <memory>
#include <stdint.h>
#include <string>
#include <utility>
#include <tuple>
#include <type_traits>

#include <CommonAPI/CommonAPI.hpp>

#define COMMONAPI_INTERNAL_COMPILATION

#include <CommonAPI/Utils.hpp>

#include <CommonAPI/DBus/DBusConnection.hpp>
#include <CommonAPI/DBus/DBusProxy.hpp>
#include <CommonAPI/DBus/DBusUtils.hpp>

#include "commonapi/tests/PredefinedTypeCollection.hpp"
#include "commonapi/tests/DerivedTypeCollection.hpp"
#include "v1/commonapi/tests/TestInterfaceProxy.hpp"
#include "v1/commonapi/tests/TestInterfaceStubDefault.hpp"
#include "v1/commonapi/tests/TestInterfaceDBusStubAdapter.hpp"

#include "v1/commonapi/tests/TestInterfaceDBusProxy.hpp"

#define VERSION v1_0

static const char DBUS_CONFIG_SUFFIX[] = "_dbus.conf";

static const std::string fileString =
""
"[factory$session]\n"
"dbus_bustype=session\n"
"[factory$system]\n"
"dbus_bustype=system\n"
"";

class Environment: public ::testing::Environment {
public:
    virtual ~Environment() {
    }

    virtual void SetUp() {
        CommonAPI::Runtime::setProperty("LibraryBase", "fakeGlueCode");
    }

    virtual void TearDown() {
    }
};

class DBusProxyFactoryTest: public ::testing::Test {
 protected:
    virtual void SetUp() {
        runtime_ = CommonAPI::Runtime::get();
        ASSERT_TRUE((bool)runtime_);

#ifdef WIN32
        configFileName_ = _pgmptr;
#else
        char cCurrentPath[FILENAME_MAX];
        if(getcwd(cCurrentPath, sizeof(cCurrentPath)) == NULL) {
            std::perror("DBusProxyFactoryTest::SetUp");
        }
        configFileName_ = cCurrentPath;
#endif

        configFileName_ += DBUS_CONFIG_SUFFIX;
        std::ofstream configFile(configFileName_);
        ASSERT_TRUE(configFile.is_open());
        configFile << fileString;
        configFile.close();
    }

    virtual void TearDown() {
        usleep(30000);
        std::remove(configFileName_.c_str());
    }

    std::string configFileName_;
    std::shared_ptr<CommonAPI::Runtime> runtime_;
};



namespace myExtensions {

template<typename _AttributeType>
class AttributeTestExtension: public CommonAPI::AttributeExtension<_AttributeType> {
    typedef CommonAPI::AttributeExtension<_AttributeType> __baseClass_t;

public:
    typedef typename _AttributeType::ValueType ValueType;
    typedef typename _AttributeType::AttributeAsyncCallback AttributeAsyncCallback;

    AttributeTestExtension(_AttributeType& baseAttribute) :
                    CommonAPI::AttributeExtension<_AttributeType>(baseAttribute) {}

   ~AttributeTestExtension() {}

   bool testExtensionMethod() const {
       return true;
   }
};

} // namespace myExtensions

//####################################################################################################################

TEST_F(DBusProxyFactoryTest, DBusFactoryCanBeCreated) {
}

TEST_F(DBusProxyFactoryTest, CreatesDefaultTestProxy) {
    auto defaultTestProxy = runtime_->buildProxy<VERSION::commonapi::tests::TestInterfaceProxy>("local", "commonapi.tests.TestInterface");
    ASSERT_TRUE((bool)defaultTestProxy);
}

TEST_F(DBusProxyFactoryTest, CreatesDefaultExtendedTestProxy) {
    auto defaultTestProxy = runtime_->buildProxyWithDefaultAttributeExtension<VERSION::commonapi::tests::TestInterfaceProxy, myExtensions::AttributeTestExtension>("local", "commonapi.tests.TestInterface");
    ASSERT_TRUE((bool)defaultTestProxy);

    auto attributeExtension = defaultTestProxy->getTestDerivedArrayAttributeAttributeExtension();
    ASSERT_TRUE(attributeExtension.testExtensionMethod());
}

TEST_F(DBusProxyFactoryTest, CreatesIndividuallyExtendedTestProxy) {
    auto specificAttributeExtendedTestProxy = runtime_->buildProxy<
        VERSION::commonapi::tests::TestInterfaceProxy,
        VERSION::commonapi::tests::TestInterfaceExtensions::TestDerivedArrayAttributeAttributeExtension<myExtensions::AttributeTestExtension> >
            ("local", "commonapi.tests.TestInterface");

    ASSERT_TRUE((bool)specificAttributeExtendedTestProxy);

    auto attributeExtension = specificAttributeExtendedTestProxy->getTestDerivedArrayAttributeAttributeExtension();
    ASSERT_TRUE(attributeExtension.testExtensionMethod());
}

TEST_F(DBusProxyFactoryTest, CreateNamedFactory) {
}

#ifndef __NO_MAIN__
int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    ::testing::AddGlobalTestEnvironment(new Environment());
    return RUN_ALL_TESTS();
}
#endif