xorg-gtest  0.1
Xorg testing extension to Google Test
xorg-gtest-example.cpp

These are examples for using xorg-gtest in tests and test fixtures.Please make sure that you have the X.org dummy display driver installed on your system and that you execute the test with root privileges.

Each of these tests starts a new server.

#include <xorg/gtest/xorg-gtest.h>
#include <sstream>
#include <X11/extensions/XInput2.h>
#include <X11/Xatom.h>
using namespace xorg::testing;
TEST(XServer, StartServer) {
XServer server;
server.SetOption("-logfile", LOGFILE_DIR "/xserver-startserver.log");
server.SetOption("-config", DUMMY_CONF_PATH);
server.Start();
ASSERT_EQ(server.GetState(), Process::RUNNING);
ASSERT_TRUE(server.Terminate());
ASSERT_EQ(server.GetState(), Process::FINISHED_SUCCESS);
/* If we get here, we were successful,so remove the log file */
server.RemoveLogFile();
}
TEST(XServer, DisplayConnection) {
XServer server;
server.SetOption("-logfile", LOGFILE_DIR "/xserver-display-connection.log");
server.SetOption("-config", DUMMY_CONF_PATH);
server.Start();
Display *dpy = XOpenDisplay(server.GetDisplayString().c_str());
ASSERT_TRUE(dpy != NULL);
/* calling Terminate isn't necessary as the destructor will do it for us,
but we do want the log file removed. That only works on terminated
servers. */
server.Terminate();
server.RemoveLogFile();
}
class ServerTest : public Test {
public:
virtual void SetUp(void) {
const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
/* Use a log file based on the test name. NOTE: test names for
parameterized tests end in /0, /1, etc. */
std::stringstream log;
log << LOGFILE_DIR "/xserver-";
log << test_info->test_case_name() << "." << test_info->name();
log << ".log";
server.SetOption("-logfile", log.str());
server.SetOption("-config", DUMMY_CONF_PATH);
server.Start();
/* set up Display() */
ASSERT_NO_FATAL_FAILURE(Test::SetUp());
}
virtual void TearDown(void) {
ASSERT_TRUE(server.Terminate());
server.RemoveLogFile();
}
protected:
XServer server;
};
TEST_F(ServerTest, DisplayWidth) {
ASSERT_GT(DisplayWidth(Display(), 0), 0);
}
TEST_F(ServerTest, DisplayHeight) {
ASSERT_GT(DisplayHeight(Display(), 0), 0);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
xorg::testing::XServer
Class representing the X server process.
Definition: xorg-gtest-xserver.h:80
xorg::testing::Process::RUNNING
@ RUNNING
The process has been started.
Definition: xorg-gtest-process.h:79
xorg::testing::Test::SetDisplayString
void SetDisplayString(const std::string &display)
Set the display string used for XOpenDisplay() and thus affects Test::Display().
xorg::testing::XServer::SetOption
void SetOption(const std::string &key, const std::string &value="")
Set startup options for the server.
xorg::testing::XServer::Start
virtual void Start(const std::string &program="")
Start a new server.
xorg::testing::Test
Google Test fixture providing an Xlib connection to an X11 server.
Definition: xorg-gtest-test.h:52
xorg::testing::XServer::RemoveLogFile
void RemoveLogFile(bool force=false)
Remove the log file used by this server.
xorg::testing::Process::FINISHED_SUCCESS
@ FINISHED_SUCCESS
The process finished with an exit code of 0.
Definition: xorg-gtest-process.h:80
xorg::testing::Process::GetState
enum Process::State GetState()
Return the state of the process.
xorg::testing::XServer::Terminate
virtual bool Terminate(unsigned int timeout=2000)
Terminates this server process.
xorg::testing::Test::SetUp
virtual void SetUp()
Tries to connect to an X server instance.
xorg::testing::XServer::GetDisplayString
const std::string & GetDisplayString(void)
Get the display string that may be used for XOpenDisplay to this server.
ServerTest
Example for a test fixture that starts a server per test.
Definition: xorg-gtest-example.cpp:60