xorg-gtest 0.1
Xorg testing extension to Google Test
Loading...
Searching...
No Matches
xorg-gtest-example.cpp

These are examples for using xorg-gtest in tests and test fixtures.

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);
/* set up Display() */
Test::SetDisplayString(server.GetDisplayString());
ASSERT_NO_FATAL_FAILURE(Test::SetUp());
}
virtual void TearDown(void) {
ASSERT_TRUE(server.Terminate());
}
protected:
};
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();
}
Example for a test fixture that starts a server per test.
Definition xorg-gtest-example.cpp:60
virtual void TearDown(void)
Closes the display.
Definition xorg-gtest-example.cpp:82
virtual void SetUp(void)
Tries to connect to an X server instance.
Definition xorg-gtest-example.cpp:62
XServer server
The X server instance.
Definition xorg-gtest-example.cpp:88
enum Process::State GetState()
Return the state of the process.
Google Test fixture providing an Xlib connection to an X11 server.
Definition xorg-gtest-test.h:52
Class representing the X server process.
Definition xorg-gtest-xserver.h:80
virtual bool Terminate(unsigned int timeout=2000)
Terminates this server process.
void RemoveLogFile(bool force=false)
Remove the log file used by this server.
const std::string & GetDisplayString(void)
Get the display string that may be used for XOpenDisplay to this server.
void SetOption(const std::string &key, const std::string &value="")
Set startup options for the server.
virtual void Start(const std::string &program="")
Start a new server.