[PATCH xorg-gtest] Make Environment API property-based.

Daniel d'Andrada daniel.dandrada at canonical.com
Mon Feb 6 03:29:46 PST 2012


Instead of shoving all parameters in the constructor.

Signed-off-by: Daniel d'Andrada <daniel.dandrada at canonical.com>
---
 include/xorg/gtest/environment.h |   64 +++++++++++++++++++++++++++++---------
 src/Makefile.am                  |    6 ++--
 src/defines.h                    |    2 +
 src/environment.cpp              |   48 ++++++++++++++++++++++------
 src/main.cpp                     |   37 +++++++++++----------
 5 files changed, 111 insertions(+), 46 deletions(-)

diff --git a/include/xorg/gtest/environment.h b/include/xorg/gtest/environment.h
index 3996507..22c82be 100644
--- a/include/xorg/gtest/environment.h
+++ b/include/xorg/gtest/environment.h
@@ -47,16 +47,11 @@ namespace testing {
  * Either associate the environment manually
  * with the overall testing framework like
  * @code
- * std::string xorg_conf_path("conf/dummy.conf");
- * std::string xorg_log_file_path("/tmp/MyDummyXorg.log");
- * int xorg_display = 133;
- * std::string server("Xorg");
- *
- * xorg::testing::Environment* environment = new xorg::testing::Environment(
- *       xorg_conf_path,
- *       server,
- *       xorg_display);
- * environment->set_log_file(xorg_log_file_path);
+ * xorg::testing::Environment* environment = new xorg::testing::Environment;
+ * environment->set_server("Xorg");
+ * environment->set_display(133);
+ * environment->set_conf_file("conf/dummy.conf");
+ * environment->set_log_file("/tmp/MyDummyXorg.log");
  * testing::AddGlobalTestEnvironment(environment);
  * @endcode
  * or link to libxorg-gtest_main.
@@ -65,12 +60,8 @@ class Environment : public ::testing::Environment {
  public:
   /**
    * Constructs an object to provide a global X server dummy environment.
-   * @param path_to_conf Path to xserver configuration.
-   * @param path_to_server Path to xserver executable.
-   * @param display Display port of dummy xserver instance.
    */
-  Environment(const std::string& path_to_conf,
-              const std::string& path_to_server = "Xorg", int display = 133);
+  Environment();
 
   virtual ~Environment();
 
@@ -87,6 +78,49 @@ class Environment : public ::testing::Environment {
    */
   const std::string& log_file() const;
 
+  /**
+   * Sets the path to the desired xserver configuration file.
+   *
+   * It will be passed on to the xserver via the command line
+   * argument "-config".
+   *
+   * @param path_conf_file Path to a Xorg X server .conf file.
+   */
+  void set_conf_file(const std::string& path_conf_file);
+
+  /**
+   * Returns the path of the xserver configuration file to be used.
+   * Its default value is [datadir]/xorg/gtest/dummy.conf
+   * @return File path of the xserver configuration currently set
+   */
+  const std::string& conf_file() const;
+
+  /**
+   * Sets the path to the xserver executable
+   * @param path_to_server Path to a xserver executable
+   */
+  void set_server(const std::string& path_to_server);
+
+  /**
+   * Returns the path where the xserver executable to be used.
+   * Its default value is "Xorg"
+   * @return Path to xserver executable.
+   */
+  const std::string& server() const;
+
+  /**
+   * Sets the display number that the xserver will use.
+   * @param diplay_num A display number.
+   */
+  void set_display(int display_num);
+
+  /**
+   * Returns the display number of the xserver instance.
+   * Its default value is 133
+   * @return Display number of the xserver.
+   */
+  int display() const;
+
  protected:
   /**
    * Starts the dummy X server.
diff --git a/src/Makefile.am b/src/Makefile.am
index b56bc8a..187dcf0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -7,7 +7,8 @@ libxorg_gtest_la_SOURCES = \
 
 libxorg_gtest_la_CPPFLAGS = \
 	$(AM_CPPFLAGS) \
-	$(GTEST_CPPFLAGS)
+	$(GTEST_CPPFLAGS) \
+	-DDUMMY_CONF_PATH="\"$(library_datadir)/dummy.conf\""
 
 libxorg_gtest_main_la_SOURCES = \
 	main.cpp
@@ -22,8 +23,7 @@ library_data_DATA = $(top_srcdir)/conf/dummy.conf
 
 libxorg_gtest_main_la_CPPFLAGS = \
 	$(AM_CPPFLAGS) \
-	$(GTEST_CPPFLAGS) \
-	-DDUMMY_CONF_PATH="\"$(library_datadir)/dummy.conf\""
+	$(GTEST_CPPFLAGS)
 
 libxorg_gtest_la_LDFLAGS = $(X11_LIBS)
 libxorg_gtest_main_la_LDFLAGS = $(X11_LIBS)
diff --git a/src/defines.h b/src/defines.h
index 6b5bcac..3bfc1da 100644
--- a/src/defines.h
+++ b/src/defines.h
@@ -2,5 +2,7 @@
 #define XORGGTEST_DEFINES
 
 #define DEFAULT_XORG_LOGFILE "/tmp/Xorg.GTest.log"
+#define DEFAULT_XORG_SERVER "Xorg"
+#define DEFAULT_DISPLAY 133
 
 #endif
diff --git a/src/environment.cpp b/src/environment.cpp
index a888454..10db3c7 100644
--- a/src/environment.cpp
+++ b/src/environment.cpp
@@ -36,22 +36,20 @@
 #include <X11/Xlib.h>
 
 struct xorg::testing::Environment::Private {
-  Private(const std::string& conf, const std::string& server, int display_num)
-      : path_to_conf(conf), path_to_log_file(DEFAULT_XORG_LOGFILE),
-        path_to_server(server), display(display_num) {
+  Private()
+      : path_to_conf(DUMMY_CONF_PATH), path_to_log_file(DEFAULT_XORG_LOGFILE),
+        path_to_server(DEFAULT_XORG_SERVER), display(DEFAULT_DISPLAY) {
   }
 
-  const std::string path_to_conf;
+  std::string path_to_conf;
   std::string path_to_log_file;
-  const std::string path_to_server;
-  const int display;
+  std::string path_to_server;
+  int display;
   Process process;
 };
 
-xorg::testing::Environment::Environment(const std::string& path_to_conf,
-                                        const std::string& path_to_server,
-                                        int display)
-    : d_(new Private(path_to_conf, path_to_server, display)) {
+xorg::testing::Environment::Environment()
+    : d_(new Private) {
 }
 
 xorg::testing::Environment::~Environment() {}
@@ -66,6 +64,36 @@ const std::string& xorg::testing::Environment::log_file() const
   return d_->path_to_log_file;
 }
 
+void xorg::testing::Environment::set_conf_file(const std::string& path_conf_file)
+{
+  d_->path_to_conf = path_conf_file;
+}
+
+const std::string& xorg::testing::Environment::conf_file() const
+{
+  return d_->path_to_conf;
+}
+
+void xorg::testing::Environment::set_server(const std::string& path_to_server)
+{
+  d_->path_to_server = path_to_server;
+}
+
+const std::string& xorg::testing::Environment::server() const
+{
+  return d_->path_to_server;
+}
+
+void xorg::testing::Environment::set_display(int display_num)
+{
+  d_->display = display_num;
+}
+
+int xorg::testing::Environment::display() const
+{
+  return d_->display;
+}
+
 void xorg::testing::Environment::SetUp() {
   static char display_string[6];
   snprintf(display_string, 6, ":%d", d_->display);
diff --git a/src/main.cpp b/src/main.cpp
index 10a94bd..7b482e7 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -30,33 +30,28 @@ namespace {
 
 int help = false;
 int no_dummy_server = false;
-int xorg_conf = false;
-int xorg_display_opt = false;
+int xorg_conf_specified = false;
+int xorg_display_specified = false;
 int xorg_logfile_specified = false;
-int server = false;
+int server_specified = false;
 
 const struct option longopts[] = {
   { "help", no_argument, &help, true, },
   { "no-dummy-server", no_argument, &no_dummy_server, true, },
-  { "xorg-conf", required_argument, &xorg_conf, true, },
-  { "xorg-display", required_argument, &xorg_display_opt, true, },
+  { "xorg-conf", required_argument, &xorg_conf_specified, true, },
+  { "xorg-display", required_argument, &xorg_display_specified, true, },
   { "xorg-logfile", required_argument, &xorg_logfile_specified, true, },
-  { "server", required_argument, &server, true, },
+  { "server", required_argument, &server_specified, true, },
   { NULL, 0, NULL, 0 }
 };
 
 } // namespace
 
 int main(int argc, char *argv[]) {
-  /* Default Xorg dummy conf path. */
-  std::string xorg_conf_path(DUMMY_CONF_PATH);
+  std::string xorg_conf_path;
   std::string xorg_log_file_path;
-
-  /* Default X display */
-  int xorg_display = 133;
-
-  /* Default Xorg executable */
-  std::string server("Xorg");
+  int xorg_display = -1;
+  std::string server;
 
   testing::InitGoogleTest(&argc, argv);
 
@@ -109,10 +104,16 @@ int main(int argc, char *argv[]) {
   }
 
   if (!no_dummy_server) {
-    xorg::testing::Environment* environment = new xorg::testing::Environment(
-        xorg_conf_path,
-        server,
-        xorg_display);
+    xorg::testing::Environment* environment = new xorg::testing::Environment;
+
+    if (xorg_conf_specified)
+      environment->set_conf_file(xorg_conf_path);
+
+    if (server_specified)
+      environment->set_server(server);
+
+    if (xorg_display_specified)
+      environment->set_display(xorg_display);
 
     if (xorg_logfile_specified)
         environment->set_log_file(xorg_log_file_path);
-- 
1.7.8.3



More information about the xorg-devel mailing list