summaryrefslogtreecommitdiff
path: root/include/xorg/gtest/xorg-gtest-xserver.h
blob: 03aeca45205145223402b02df42ed7b1d39cf344 (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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*******************************************************************************
 *
 * X testing environment - Google Test helper class to communicate with the
 * server
 *
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 ******************************************************************************/


#ifndef XORG_GTEST_XSERVER_H
#define XORG_GTEST_XSERVER_H

#include <gtest/gtest.h>
#include <xorg/gtest/xorg-gtest.h>
#include <X11/Xlib.h>
#include <stdexcept>

namespace xorg {
namespace testing {

/**
 * @class XIOError
 *
 * Exception thrown if the display connection encounters an IO error and
 * calls the XIOErrorHandler function.
 *
 * This exception requires an XIOErrorHandler to be registered.
 * XServer::Start() will register this error handler. For tests that do not
 * use the provided XServer object, call XServer::RegisterXIOErrorHandler()
 * instead.
 */
class XIOError : public std::runtime_error {
public:
  /** Create a new XIOError with the given message */
  XIOError(const std::string& msg) : std::runtime_error(msg) {}
};

/**
 * @class XServer xorg-gtest-xserver.h xorg/gtest/xorg-gtest-xserver.h
 *
 * Class representing the X server process.
 *
 * @code
 * XServer server;
 * server.SetOption("-logfile", "/tmp/Xserver.log");
 * server.Start();
 *
 * ...
 *
 * if (!server.Terminate()) {
 *   std::cerr << "Problem terminating server ... killing now ..." << std::endl;
 *   if (!server.Kill())
 *     std::cerr << "Problem killing server" << std::endl;
 * }
 * @endcode
 *
 * Once a XServer is started, a default XIOErrorHandler is installed and
 * subsequent IO errors on the display connection will throw an XIOError.
 */
class XServer : public xorg::testing::Process {
  public:
    XServer();
    ~XServer();

    /**
     * Start a new server. If no binary is given, the server started is the
     * default compiled-in server binary.
     *
     * @param [in] program Path to the XServer binary
     */
    virtual void Start(const std::string &program = "");

    /**
     * Terminates this server process. Will signal the server to terminate
     * multiple times before giving up.
     *
     * @param [in] timeout The timeout in millis to wait for the process to
     *                     terminate. A timeout of 0 implies not to wait but
     *                     return immediately.
     *
     * @returns true if termination succeeded and, if a timout is given, the
     *          process shut down within that timeout. false otherwise.
     */
    virtual bool Terminate(unsigned int timeout = 2000);

    /**
     * Kills the server. With a vengeance.
     *
     * @param [in] timeout The timeout in millis to wait for the process to
     *                     terminate. A timeout of 0 implies not to wait but
     *                     return immediately.
     *
     * @returns true if kill succeeded and, if a timout is given, the
     *          process shut down within that timeout. false otherwise.
     */
    virtual bool Kill(unsigned int timeout = 2000);

    /**
     * Remove the log file used by this server. By default, this function
     * only removes the log file if the server was terminated or finished
     * with an exit code of 0.
     *
     * If force is true, the log file is removed regardless of the state of
     * the server.
     *
     * @param force Force removal of the log file
     */
    void RemoveLogFile(bool force = false);

    /**
     * Waits until this server is ready to take connections.
     */
    _X_DEPRECATED void WaitForConnections(void);

    /**
     * Set the display number for this server. This number must be set
     * before the server is started to have any effect.
     * If unset, the default display number is used.
     *
     * @param [in] display_number The display number the server runs on
     */
    void SetDisplayNumber(unsigned int display_number);

    /**
     * Set the path to the server binary to be started. Optional call, if
     * not invoked the built-in default path is chosen.
     *
     * @param [in] path_to_server The path to the binary
     */
    void SetServerPath(const std::string &path_to_server);

    /**
     * Get the display number from this server. If the server was not
     * started yet, this function returns the display number the server will
     * be started on.
     *
     * @return The numeric display number this server runs on
     */
    unsigned int GetDisplayNumber(void);

    /**
     * Get the display string that may be used for XOpenDisplay to this
     * server. This string is effectively :display_number.
     *
     * @return The display string used for XOpenDisplay() to this server.
     */
    const std::string& GetDisplayString(void);

    /**
     * Get the X server version as printed into the log file, usually in the
     * form a.b.c[.d], with d being the optional part for release
     * candidates.
     *
     * @return A string representing this server's version. If the server
     *         hasn't been started yet, GetVersion() returns an empty string.
     */
    const std::string& GetVersion();

    /**
     * Get the server's log file path. If this path is empty, the server
     * will use it's built-in log file path.
     *
     * @return The log file path this server will use, is using or has used.
     */
    const std::string& GetLogFilePath();

    /**
     * Get the server's config file path. If this path is empty, the server
     * will use it's built-in config file path.
     *
     * @return The config file path this server will use, is using or has used.
     */
    const std::string& GetConfigPath();

    /**
     * Set startup options for the server.
     *
     * For arguments that do not take/need a value, use the empty string as
     * value.
     *
     * @param [in] key Commandline option
     * @param [in] value Option value (if any)
     */
    void SetOption(const std::string &key, const std::string &value = "");

    /**
     * Remove a previously set option.
     *
     * If an option was set through SetOption(), remove the option again. If
     * the specified option has never been set, do nothing.
     *
     * @param [in] option Commandline option to remove
     */
    void RemoveOption(const std::string &option);

    /**
     * Wait for a specific device to be added to the server.
     *
     * @param [in] display The X display connection
     * @param [in] name    The name of the device to wait for
     * @param [in] timeout The timeout in milliseconds
     *
     * @return Whether the device was added
     */
    static bool WaitForDevice(::Display *display, const std::string &name, time_t timeout = 1000);

    /**
     * Wait for an event on the X connection.
     *
     * @param [in] display The X display connection
     * @param [in] timeout The timeout in milliseconds
     *
     * @return Whether an event is available
     */
    static bool WaitForEvent(::Display *display, time_t timeout = 1000);

    /**
     * Wait for an event of a specific type on the X connection.
     *
     * All events preceding the matching event are discarded. If no event was found
     * before the timeout expires, all events in the queue will have been discarded.
     *
     * @param [in] display   The X display connection
     * @param [in] type      The X core protocol event type
     * @param [in] extension The X extension opcode of a generic event, or -1 for
     *                       any generic event
     * @param [in] evtype    The X extension event type of a generic event, or -1
     *                       for any event of the given extension
     * @param [in] timeout   The timeout in milliseconds
     *
     * @return Whether an event is available
     */
    static bool WaitForEventOfType(::Display *display, int type, int extension = -1, int evtype = -1, time_t timeout = 1000);

    /**
     * Install a default XIOErrorHandler. That error handler will throw an
     * xorg::testing::XIOError when encountered.
     *
     * This function is called automatically by XServer::Start(). Usually,
     * you will not need to call this function unless your test does not
     * instantiate and Start() an XServer object.
     *
     * This function will only install a new error handler if the currently
     * installed XIOErrorHandler is not the default handler used by Xlib.
     */
    static void RegisterXIOErrorHandler();

    /**
     * Install a default XErrorHandler. That error handler will cause a test
     * failure if called.
     *
     * This function is called automatically by XServer::Start(). Usually,
     * you will not need to call this function unless your test does not
     * instantiate and Start() an XServer object.
     *
     * This function will only install a new error handler if the currently
     * installed XErrorHandler is not the default handler used by Xlib.
     */
    static void RegisterXErrorHandler();

  private:
    struct Private;
    std::unique_ptr<Private> d_;

    /* Disable copy constructor, assignment operator */
    XServer(const XServer&);
    XServer& operator=(const XServer&);

    void TestStartup(void);

};
} // namespace testing
} // namespace xorg

#endif /* XORG_GTEST_XSERVER_H */