File: fixture.h

package info (click to toggle)
dbus-cpp 5.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,208 kB
  • sloc: cpp: 9,427; ansic: 2,012; xml: 1,156; makefile: 14; sh: 2
file content (187 lines) | stat: -rw-r--r-- 5,212 bytes parent folder | download | duplicates (3)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright © 2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */
#ifndef CORE_DBUS_FIXTURE_H_
#define CORE_DBUS_FIXTURE_H_

#include <core/dbus/visibility.h>

#include <chrono>
#include <memory>
#include <string>

namespace core
{
namespace dbus
{
class Bus;

/**
 * @brief The Fixture class provides private session and system bus instances for testing purposes.
 */
class ORG_FREEDESKTOP_DBUS_DLL_PUBLIC Fixture
{
public:
    /** @brief Fractional seconds. */
    typedef std::chrono::duration<double> Seconds;

    /**
      * @brief default_daemon_timeout after which the dbus daemons will be killed.
      */
    static Seconds& default_daemon_timeout();

    /**
     * @brief default_session_bus_config_file provides the filename of the default session
     * bus configuration file.
     */
    static const std::string& default_session_bus_config_file();

    /**
     * @brief default_system_bus_config_file provides the filename of the default system
     * bus configuration file.
     */
    static const std::string& default_system_bus_config_file();

    /**
     * @brief Constructs a fixture instance with the two given configuration files.
     *
     * Any test running within the scope of this fixture will access the private
     * session- and system-bus instances setup by this class.
     *
     * @param session_bus_config_file Path to the session-bus configuration file.
     * @param system_bus_config_file Path to the system-bus configuration file.
     */
    Fixture(const std::string& session_bus_config_file,
            const std::string& system_bus_config_file);

    virtual ~Fixture();

    /** 
     * @brief session_bus_address returns the address of the private session bus instance. 
     */
    std::string session_bus_address();

    /**
     * @brief create_connection_to_session_bus creates a new connection to the testing session bus instance.
     */
    std::shared_ptr<Bus> create_connection_to_session_bus();

    /** 
     * @brief session_bus_address returns the address of the private session bus instance. 
     */
    std::string system_bus_address();

    /**
     * @brief create_connection_to_system_bus creates a new connection to the testing system bus instance.
     */
    std::shared_ptr<Bus> create_connection_to_system_bus();

private:
    struct Private;
    std::unique_ptr<Private> d;
};
}
}

#if defined(CORE_DBUS_ENABLE_GOOGLE_TEST_FIXTURE)
#include <gtest/gtest.h>
namespace core
{
namespace dbus
{
namespace testing
{
/**
 * @brief The Fixture class provides a Google Test fixture for running
 * tests in a private dbus environment.
 */
class Fixture : public ::testing::Test
{
public:

    /**
     * @brief default_session_bus_config_file provides the filename of the default session
     * bus configuration file.
     */
    inline static std::string& default_session_bus_config_file()
    {
        static std::string s{core::dbus::Fixture::default_session_bus_config_file()};
        return s;
    }

    /**
     * @brief default_system_bus_config_file provides the filename of the default system
     * bus configuration file.
     */
    inline static std::string& default_system_bus_config_file()
    {
        static std::string s{core::dbus::Fixture::default_system_bus_config_file()};
        return s;
    }

    /**
     * @brief Constructs an instance and sets up connections to the private
     * session and system bus.
     *
     * @throw std::runtime_error in case of issues.
     */
    inline Fixture() :
        fixture(default_session_bus_config_file(),
                default_system_bus_config_file())
    {
    }

    /** 
     * @brief session_bus_address returns the address of the private session bus instance. 
     */
    inline std::string session_bus_address()
    {
        return fixture.session_bus_address();
    }

    /**
     * @brief session_bus provides access to the private session bus.
     */
    inline std::shared_ptr<Bus> session_bus()
    {
        return fixture.create_connection_to_session_bus();
    }

    /** 
     * @brief system_bus_address returns the address of the private session bus instance. 
     */
    inline std::string system_bus_address()
    {
        return fixture.system_bus_address();
    }

    /**
     * @brief system_bus provides access to the private system bus.
     */
    inline std::shared_ptr<Bus> system_bus()
    {
        return fixture.create_connection_to_system_bus();
    }

private:
    core::dbus::Fixture fixture;
};
}
}
}
#endif // CORE_DBUS_ENABLE_GOOGLE_TEST_FIXTURE
#endif // CORE_DBUS_FIXTURE_H_