[PATCH xorg-gtest 11/11] examples: add test examples for manual XServer control

Peter Hutterer peter.hutterer at who-t.net
Mon Oct 29 18:38:33 PDT 2012


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 examples/Makefile.am            |  10 +++-
 examples/xorg-gtest-example.cpp | 102 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 1 deletion(-)
 create mode 100644 examples/xorg-gtest-example.cpp

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();
+}
-- 
1.7.11.7



More information about the xorg-devel mailing list