~ubuntu-branches/ubuntu/trusty/compiz/trusty

« back to all changes in this revision

Viewing changes to tests/xorg-gtest/src/compiz_xorg_gtest_main.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2013-08-22 06:58:07 UTC
  • mto: This revision was merged to the branch mainline in revision 3352.
  • Revision ID: package-import@ubuntu.com-20130822065807-17nlzez0d30y09so
Tags: upstream-0.9.10+13.10.20130822
ImportĀ upstreamĀ versionĀ 0.9.10+13.10.20130822

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Compiz XOrg GTest Decoration Pixmap Protocol Integration Tests
 
3
 *
 
4
 * Copyright (C) 2013 Sam Spilsbury.
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 *
 
20
 * Authored By:
 
21
 * Sam Spilsbury <smspillaz@gmail.com>
 
22
 */
 
23
 
 
24
#include <csignal>
 
25
 
 
26
#include <sstream>
 
27
#include <stdexcept>
 
28
 
 
29
#include <gtest/gtest.h>
 
30
#include "xorg/gtest/xorg-gtest-environment.h"
 
31
 
 
32
#include <X11/Xlib.h>
 
33
 
 
34
namespace compiz
 
35
{
 
36
namespace testing
 
37
{
 
38
class XorgEnvironment :
 
39
    public xorg::testing::Environment
 
40
{
 
41
    public:
 
42
 
 
43
        virtual void SetUp ()
 
44
        {
 
45
            /* Another hack - if the server fails
 
46
             * to start then just set another display
 
47
             * number until it does */
 
48
            const int MaxConnections = 255;
 
49
            int displayNumber = 0;
 
50
            bool serverRunningOnDisplay = true;
 
51
 
 
52
            while (serverRunningOnDisplay &&
 
53
                   displayNumber < MaxConnections)
 
54
            {
 
55
                std::stringstream ss;
 
56
                ss << ":" << displayNumber;
 
57
                Display *check = XOpenDisplay (ss.str ().c_str ());
 
58
 
 
59
                if (!check)
 
60
                    serverRunningOnDisplay = false;
 
61
                else
 
62
                {
 
63
                    XCloseDisplay (check);
 
64
                    ++displayNumber;
 
65
                }
 
66
            }
 
67
 
 
68
            if (displayNumber == MaxConnections)
 
69
                throw std::runtime_error ("couldn't find a socket "
 
70
                                          "to launch on");
 
71
 
 
72
            std::stringstream logFile;
 
73
            logFile << "/tmp/Compiz.Xorg.GTest." << displayNumber << ".log";
 
74
 
 
75
            SetDisplayNumber (displayNumber);
 
76
            SetLogFile (logFile.str ());
 
77
            xorg::testing::Environment::SetUp ();
 
78
        }
 
79
};
 
80
}
 
81
}
 
82
 
 
83
/* X testing environment - Google Test environment feat. dummy x server
 
84
 * Copyright (C) 2011, 2012 Canonical Ltd.
 
85
 */
 
86
compiz::testing::XorgEnvironment *environment = NULL;
 
87
 
 
88
namespace
 
89
{
 
90
 
 
91
void SignalHandler (int signum)
 
92
{
 
93
    if (environment)
 
94
        environment->Kill ();
 
95
 
 
96
    /* This will call the default handler because we used SA_RESETHAND */
 
97
    raise (signum);
 
98
}
 
99
 
 
100
void SetupSignalHandlers ()
 
101
{
 
102
    static const int signals[] =
 
103
    {
 
104
        SIGHUP,
 
105
        SIGTERM,
 
106
        SIGQUIT,
 
107
        SIGILL,
 
108
        SIGABRT,
 
109
        SIGFPE,
 
110
        SIGSEGV,
 
111
        SIGPIPE,
 
112
        SIGALRM,
 
113
        SIGTERM,
 
114
        SIGUSR1,
 
115
        SIGUSR2,
 
116
        SIGBUS,
 
117
        SIGPOLL,
 
118
        SIGPROF,
 
119
        SIGSYS,
 
120
        SIGTRAP,
 
121
        SIGVTALRM,
 
122
        SIGXCPU,
 
123
        SIGXFSZ,
 
124
        SIGIOT,
 
125
        SIGSTKFLT,
 
126
        SIGIO,
 
127
        SIGPWR,
 
128
        SIGUNUSED,
 
129
    };
 
130
 
 
131
    struct sigaction action;
 
132
    action.sa_handler = SignalHandler;
 
133
    sigemptyset(&action.sa_mask);
 
134
    action.sa_flags = SA_RESETHAND;
 
135
 
 
136
    for (unsigned i = 0; i < sizeof(signals) / sizeof(signals[0]); ++i)
 
137
        if (sigaction(signals[i], &action, NULL))
 
138
            std::cerr << "Warning: Failed to set signal handler for signal "
 
139
                      << signals[i] << "\n";
 
140
}
 
141
}
 
142
 
 
143
int main (int argc, char **argv)
 
144
{
 
145
    ::testing::InitGoogleTest (&argc, argv);
 
146
 
 
147
    SetupSignalHandlers ();
 
148
 
 
149
    environment = new compiz::testing::XorgEnvironment ();
 
150
    ::testing::AddGlobalTestEnvironment (environment);
 
151
 
 
152
    return RUN_ALL_TESTS ();
 
153
}
 
154