[niepce] Added a test for the ConfigDataBinder



commit f6de48913194db483c35203ab9a43dce3e6db2c3
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Jun 22 11:26:43 2013 -0400

    Added a test for the ConfigDataBinder

 .gitignore                                 |    1 +
 src/fwk/toolkit/Makefile.am                |   11 +++
 src/fwk/toolkit/application.cpp            |    2 +-
 src/fwk/toolkit/configuration.cpp          |   19 ++++--
 src/fwk/toolkit/configuration.hpp          |   10 ++-
 src/fwk/toolkit/t/testconfigdatabinder.cpp |  107 ++++++++++++++++++++++++++++
 6 files changed, 139 insertions(+), 11 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index f8abf1f..13cb90e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -47,6 +47,7 @@ src/fwk/utils/testpathutils
 src/fwk/utils/teststringutils
 src/fwk/utils/testufrawmeta
 src/fwk/utils/testxmp
+src/fwk/toolkit/testconfigdatabinder
 src/libraryclient/test_worker
 src/engine/db/test_library
 src/engine/db/test_filebundle
diff --git a/src/fwk/toolkit/Makefile.am b/src/fwk/toolkit/Makefile.am
index a2f76a1..bc6d20f 100644
--- a/src/fwk/toolkit/Makefile.am
+++ b/src/fwk/toolkit/Makefile.am
@@ -14,6 +14,17 @@ noinst_LIBRARIES = libniepceframework.a
 noinst_HEADERS =   \
          goocanvas_proxy_header.hpp
 
+TESTS = testconfigdatabinder
+
+check_PROGRAMS = testconfigdatabinder
+
+testconfigdatabinder_SOURCES = t/testconfigdatabinder.cpp
+testconfigdatabinder_LDADD = libniepceframework.a \
+       ../base/libfwkbase.a \
+       ../utils/libniepceutils.a \
+        @LIBGLIBMM_LIBS@
+
+
 libniepceframework_a_SOURCES = configuration.hpp configuration.cpp \
        application.hpp application.cpp \
        dialog.hpp dialog.cpp \
diff --git a/src/fwk/toolkit/application.cpp b/src/fwk/toolkit/application.cpp
index 88c07ed..1c8c98d 100644
--- a/src/fwk/toolkit/application.cpp
+++ b/src/fwk/toolkit/application.cpp
@@ -39,7 +39,7 @@ Application::Ptr Application::m_application;
 
 Application::Application(int & argc, char** &argv, const char* app_id,
                          const char * name)
-    : m_config(name)
+    : m_config(Configuration::make_config_path(name))
     , m_refUIManager(Gtk::UIManager::create())
     , m_module_manager(new ModuleManager())
     , m_gtkapp(Gtk::Application::create(argc, argv, app_id))
diff --git a/src/fwk/toolkit/configuration.cpp b/src/fwk/toolkit/configuration.cpp
index 8620587..2da9471 100644
--- a/src/fwk/toolkit/configuration.cpp
+++ b/src/fwk/toolkit/configuration.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - framework/configuration.cpp
  *
- * Copyright (C) 2007-2008 Hubert Figuiere
+ * Copyright (C) 2007-2013 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -36,13 +36,20 @@
 
 namespace fwk {
 
-Configuration::Configuration(const Glib::ustring & file)
-    : m_root("main")
+
+Glib::ustring Configuration::make_config_path(const Glib::ustring & file)
 {
-    m_filename = Glib::build_filename(Glib::get_user_config_dir(),
+    Glib::ustring filename = Glib::build_filename(Glib::get_user_config_dir(),
                                       file);
-    m_filename = Glib::build_filename(m_filename, "config");
-    fwk::ensure_path_for_file(m_filename);
+    filename = Glib::build_filename(filename, "config");
+    fwk::ensure_path_for_file(filename);
+    return filename;
+}
+
+Configuration::Configuration(const Glib::ustring & file)
+    : m_filename(file)
+    , m_root("main")
+{
     try {
         m_keyfile.load_from_file(m_filename);
     }
diff --git a/src/fwk/toolkit/configuration.hpp b/src/fwk/toolkit/configuration.hpp
index e6f0328..69f5b5a 100644
--- a/src/fwk/toolkit/configuration.hpp
+++ b/src/fwk/toolkit/configuration.hpp
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/toolkit/configuration.hpp
  *
- * Copyright (C) 2007-2009 Hubert Figuiere
+ * Copyright (C) 2007-2013 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -31,14 +31,16 @@ class Configuration
 public:
     Configuration(const Glib::ustring & file);
     ~Configuration();
-    
+
+    static Glib::ustring make_config_path(const Glib::ustring & file);
+
     bool hasKey(const Glib::ustring & key) const;
     const Glib::ustring getValue(const Glib::ustring & key,
                                  const Glib::ustring & def) const;
-    
+
     void setValue(const Glib::ustring & key, const Glib::ustring & value);
 private:
-    
+
     void save();
 
     Glib::ustring          m_filename;
diff --git a/src/fwk/toolkit/t/testconfigdatabinder.cpp b/src/fwk/toolkit/t/testconfigdatabinder.cpp
new file mode 100644
index 0000000..bbf3814
--- /dev/null
+++ b/src/fwk/toolkit/t/testconfigdatabinder.cpp
@@ -0,0 +1,107 @@
+/*
+ * niepce - fwk/toolkit/testdatabinder.cpp
+ *
+ * Copyright (C) 2013 Hubert Figuiere
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+/** @brief unit test for files */
+
+#include <boost/test/minimal.hpp>
+
+#include <glibmm/init.h>
+#include <glibmm/object.h>
+#include <glibmm/property.h>
+#include <giomm/file.h>
+
+#include "fwk/toolkit/configdatabinder.hpp"
+
+class PropertyFixture
+       : Glib::Object
+{
+public:
+       PropertyFixture()
+               : Glib::ObjectBase(typeid(PropertyFixture))
+               , Glib::Object()
+               , str_property(*this, "string")
+               , int_property(*this, "int")
+               , bool_property(*this, "bool")
+               {
+               }
+
+       Glib::Property<Glib::ustring> str_property;
+       Glib::Property<int> int_property;
+       Glib::Property<bool> bool_property;
+};
+
+int test_main( int, char *[] )             // note the name!
+{
+       Glib::init();
+
+       PropertyFixture fixture;
+
+       // get tmp file
+       Glib::ustring cfg_file("/tmp/tmp-cfg.ini");
+       Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(cfg_file);
+
+       {
+               fwk::Configuration cfg(cfg_file);
+
+               {
+                       // the binder only write to the preference when
+                       // it is destroyed.
+                       fwk::ConfigDataBinder<int>
+                               binder_int(fixture.int_property.get_proxy(),
+                                          cfg, "int");
+
+                       fixture.int_property = 1;
+               }
+               BOOST_CHECK(file->query_exists());
+
+               BOOST_CHECK(cfg.hasKey("int"));
+               Glib::ustring val = cfg.getValue("int", "0");
+               BOOST_CHECK(val == "1");
+
+               {
+                       fwk::ConfigDataBinder<Glib::ustring>
+                               binder_str(fixture.str_property.get_proxy(),
+                                          cfg, "string");
+                       fixture.str_property = "foo";
+               }
+               BOOST_CHECK(file->query_exists());
+
+               BOOST_CHECK(cfg.hasKey("string"));
+               val = cfg.getValue("string", "");
+               BOOST_CHECK(val == "foo");
+
+               {
+                       fwk::ConfigDataBinder<bool>
+                               binder_bool(fixture.bool_property.get_proxy(),
+                                          cfg, "bool");
+                       fixture.bool_property = true;
+               }
+               BOOST_CHECK(file->query_exists());
+
+               BOOST_CHECK(cfg.hasKey("bool"));
+               val = cfg.getValue("bool", "");
+               BOOST_CHECK(val == "1");
+       }
+
+
+       file->remove();
+
+       BOOST_CHECK(!file->query_exists());
+
+       return 0;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]