2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
23 #include <boost/array.hpp>
27 #include "test/TestUtils.h"
28 #include "utils/log.h"
31 #include "WestonProcess.h"
32 #include "WestonTest.h"
34 namespace xt = xbmc::test;
38 class TempFileWrapper :
43 TempFileWrapper(const CStdString &suffix);
46 void FetchDirectory(CStdString &directory);
47 void FetchFilename(CStdString &name);
53 TempFileWrapper::TempFileWrapper(const CStdString &suffix) :
54 m_file(CXBMCTestUtils::Instance().CreateTempFile(suffix))
58 TempFileWrapper::~TempFileWrapper()
60 CXBMCTestUtils::Instance().DeleteTempFile(m_file);
63 void TempFileWrapper::FetchDirectory(CStdString &directory)
65 directory = CXBMCTestUtils::Instance().TempFileDirectory(m_file);
66 /* Strip trailing "/" */
67 directory.resize(directory.size() - 1);
70 void TempFileWrapper::FetchFilename(CStdString &name)
72 CStdString path(CXBMCTestUtils::Instance().TempFilePath(m_file));
73 CStdString directory(CXBMCTestUtils::Instance().TempFileDirectory(m_file));
75 name = path.substr(directory.size());
78 class SavedTempSocket :
85 const CStdString & FetchFilename();
86 const CStdString & FetchDirectory();
90 CStdString m_filename;
91 CStdString m_directory;
94 SavedTempSocket::SavedTempSocket()
96 TempFileWrapper wrapper("");
97 wrapper.FetchDirectory(m_directory);
98 wrapper.FetchFilename(m_filename);
102 SavedTempSocket::FetchFilename()
108 SavedTempSocket::FetchDirectory()
113 template <typename Iterator>
119 SignalGuard(const Iterator &begin,
120 const Iterator &end);
127 template <typename Iterator>
128 SignalGuard<Iterator>::SignalGuard(const Iterator &begin,
132 for (Iterator it = begin;
135 sigaddset(&mask, *it);
137 if (sigprocmask(SIG_BLOCK, &mask, NULL))
139 std::stringstream ss;
140 ss << "sigprogmask: "
142 throw std::runtime_error(ss.str());
146 template <typename Iterator>
147 SignalGuard<Iterator>::~SignalGuard()
149 if (sigprocmask(SIG_UNBLOCK, &mask, NULL))
150 CLog::Log(LOGERROR, "Failed to unblock signals");
153 typedef boost::array<int, 4> SigArray;
154 SigArray BlockedSignals =
163 class WestonTest::Private
170 CStdString m_xbmcTestBase;
171 SavedTempSocket m_tempSocketName;
172 TmpEnv m_xdgRuntimeDir;
174 SignalGuard<SigArray::iterator> m_signalGuard;
176 xt::Process m_process;
179 WestonTest::WestonTest() :
184 /* We need a defined destructor, otherwise we will
185 * generate the destructors for all of the owned objects
186 * multiple times where the definitions for those
187 * destructors is unavailable */
188 WestonTest::~WestonTest()
192 WestonTest::Private::Private() :
193 m_xbmcTestBase(CXBMCTestUtils::Instance().ReferenceFilePath("")),
194 /* We want wayland (client and server) to look in our
195 * temp file directory for the socket */
196 m_xdgRuntimeDir("XDG_RUNTIME_DIR", m_tempSocketName.FetchDirectory().c_str()),
197 /* Block emission of SIGUSR2 so that we can wait on it */
198 m_signalGuard(BlockedSignals.begin(), BlockedSignals.end()),
199 m_process(m_xbmcTestBase,
200 m_tempSocketName.FetchFilename())
204 WestonTest::Private::~Private()
211 return priv->m_process.Pid();
215 WestonTest::TempSocketName()
217 return priv->m_tempSocketName.FetchFilename();
223 priv->m_process.WaitForSignal(SIGUSR2, xt::Process::DefaultProcessWaitTimeout);