diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2012-10-29 12:13:19 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2012-11-07 13:14:09 +1000 |
commit | 5c4575c63b38c294e3dac2294c30427ef52daaf6 (patch) | |
tree | 9ae65f234c1e0ee59c4cea79b8ddd61ea0974e6e | |
parent | b6013a96fce6025dce3b22772461fb809652726b (diff) |
examples: add test examples for manual XServer control
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r-- | examples/Makefile.am | 10 | ||||
-rw-r--r-- | examples/xorg-gtest-example.cpp | 102 |
2 files changed, 111 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am index df783a7..84b4dbb 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -53,11 +53,19 @@ libxorg_gtest_main_a_CPPFLAGS = \ -I$(top_srcdir) libxorg_gtest_main_a_CXXFLAGS = $(GTEST_CXXFLAGS) $(AM_CXXFLAGS) -noinst_PROGRAMS = xorg-gtest-environment-example +noinst_PROGRAMS = xorg-gtest-environment-example xorg-gtest-example if ENABLE_XORG_GTEST_TESTS TESTS = $(noinst_PROGRAMS) endif +xorg_gtest_example_SOURCES = xorg-gtest-example.cpp +xorg_gtest_example_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/include +xorg_gtest_example_LDADD = \ + libgtest.a \ + libxorg-gtest.a \ + -lpthread \ + $(X11_LIBS) \ + $(EVEMU_LIBS) xorg_gtest_environment_example_SOURCES = xorg-gtest-environment-example.cpp diff --git a/examples/xorg-gtest-example.cpp b/examples/xorg-gtest-example.cpp new file mode 100644 index 0000000..ac6dfba --- /dev/null +++ b/examples/xorg-gtest-example.cpp @@ -0,0 +1,102 @@ +#include <xorg/gtest/xorg-gtest.h> + +#include <sstream> + +#include <X11/extensions/XInput2.h> +#include <X11/Xatom.h> + +using namespace xorg::testing; + +/** + * @example 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. + */ + +/** + * Basic test, starts the server, checks it is running, then terminates it. + */ +TEST(XServer, StartServer) { + XServer server; + server.SetOption("-logfile", LOGFILE_DIR "/xserver-startserver.log"); + 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(); +} + +/** + * Start a server, check the display connection works, terminate the server + */ +TEST(XServer, DisplayConnection) { + XServer server; + server.SetOption("-logfile", LOGFILE_DIR "/xserver-display-connection.log"); + 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(); +} + +/** + * Example for a test fixture that starts a server per test. + */ +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.Start(); + + /* set up Display() */ + Test::SetDisplayString(server.GetDisplayString()); + ASSERT_NO_FATAL_FAILURE(Test::SetUp()); + } + + virtual void TearDown(void) { + ASSERT_TRUE(server.Terminate()); + server.RemoveLogFile(); + } +protected: + XServer server; +}; + +/** + * Two mostly pointless tests. Both use the same test fixture class + * above. Note that the server is restarted after each test. + */ +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(); +} |