core: add cursor theme tests
authorU. Artie Eoff <ullysses.a.eoff@intel.com>
Mon, 17 Sep 2012 21:11:10 +0000 (14:11 -0700)
committerU. Artie Eoff <ullysses.a.eoff@intel.com>
Mon, 17 Sep 2012 21:11:10 +0000 (14:11 -0700)
Signed-off-by: U. Artie Eoff <ullysses.a.eoff@intel.com>
src/core/Makefile.am
src/core/test_bind_interface.cpp
src/core/test_cursor.cpp [new file with mode: 0644]

index 37592fb..32120fc 100644 (file)
@@ -2,7 +2,9 @@ INCLUDES = -I$(top_srcdir)/src
 
 lib_LTLIBRARIES = libwfits-core.la
 
-libwfits_core_la_SOURCES = test_bind_interface.cpp
+libwfits_core_la_SOURCES = \
+       test_bind_interface.cpp \
+       test_cursor.cpp
 
 libwfits_core_la_LIBADD = \
        $(top_builddir)/src/common/libwfits-common.la \
index 02c8f7f..f0500c3 100644 (file)
@@ -3,25 +3,34 @@
 #include "common/test.h"
 
 template <typename O, const wl_interface& interface, const std::string& str_interface>
-struct BindInterface {
+class BindInterface
+{
+public:
        BindInterface()
-               : display(wl_display_connect(0))
-               , object(0)
+               : display_(wl_display_connect(0))
+               , object_(0)
+               , listener_(0)
        {
-               FAIL_IF_EQUAL(display, NULL);
+               FAIL_IF_EQUAL(display_, NULL);
        }
-       
+
+       ~BindInterface()
+       {
+               wl_display_remove_global_listener(display_, listener_);
+               wl_display_disconnect(display_);
+       }
+
        void bind()
        {
-               wl_display_add_global_listener(display, &BindInterface::callback, this);
-               wl_display_iterate(display, WL_DISPLAY_READABLE);
+               listener_ = wl_display_add_global_listener(display_, &BindInterface::callback, this);
+               wl_display_iterate(display_, WL_DISPLAY_READABLE);
        }
 
        static void callback(wl_display* display, uint32_t id, const char* iface, uint32_t version, void* data)
        {
                BindInterface* bi = static_cast<BindInterface*>(data);
                if (std::string(iface) == str_interface) {
-                               bi->object = static_cast<O*>(
+                               bi->object_ = static_cast<O*>(
                                        wl_display_bind(
                                                display, id, &interface
                                        )
@@ -29,8 +38,9 @@ struct BindInterface {
                        }
        }
 
-       wl_display*     display;
-       O*              object;
+       wl_display*                     display_;
+       O*                                      object_;
+       wl_global_listener*     listener_;
 };
 
 #define BIND_TEST(name) \
@@ -39,9 +49,9 @@ struct BindInterface {
        TEST(bind_##name, "Core/BindInterface") \
        { \
                bind_##name##_interface bi; \
-               FAIL_UNLESS(bi.object == NULL); \
+               FAIL_UNLESS(bi.object_ == NULL); \
                bi.bind(); \
-               FAIL_IF(bi.object == NULL); \
+               FAIL_IF(bi.object_ == NULL); \
        }
 
 BIND_TEST(wl_compositor)
diff --git a/src/core/test_cursor.cpp b/src/core/test_cursor.cpp
new file mode 100644 (file)
index 0000000..d402086
--- /dev/null
@@ -0,0 +1,76 @@
+#include <string>
+#include <wayland-client.h>
+#include <wayland-cursor.h>
+#include "common/test.h"
+
+/** This is as defined in wayland/cursor/wayland-cursor.c **/
+struct wl_cursor_theme {
+       unsigned int cursor_count;
+       struct wl_cursor **cursors;
+       struct wl_shm *shm;
+       struct shm_pool *pool;
+       char *name;
+       int size;
+};
+
+class TestCursorTheme
+{
+public:
+       TestCursorTheme(const std::string& themeName = "default")
+               : display_(wl_display_connect(0))
+               , theme_(0)
+               , listener_(0)
+               , shm_(0)
+               , themeName_(themeName)
+       {
+               FAIL_IF_EQUAL(display_, NULL);
+       }
+
+       ~TestCursorTheme()
+       {
+               wl_cursor_theme_destroy(theme_);
+               wl_display_remove_global_listener(display_, listener_);
+               wl_display_disconnect(display_);
+       }
+
+       void run()
+       {
+               listener_ = wl_display_add_global_listener(display_, &TestCursorTheme::callback, this);
+               wl_display_iterate(display_, WL_DISPLAY_READABLE);
+       }
+
+       static void callback(wl_display* display, uint32_t id, const char* iface, uint32_t version, void* data)
+       {
+               TestCursorTheme* test = static_cast<TestCursorTheme*>(data);
+               if (std::string(iface) == "wl_shm")
+               {
+                       test->shm_ = static_cast<wl_shm*>(wl_display_bind(display, id, &wl_shm_interface));
+                       FAIL_IF_EQUAL(test->shm_, NULL);
+                       test->theme_ = wl_cursor_theme_load(test->themeName_.c_str(), 32, test->shm_);
+                       FAIL_IF_EQUAL(test->theme_, NULL);
+               }
+       }
+
+       wl_display*                     display_;
+       wl_cursor_theme*        theme_;
+       wl_global_listener* listener_;
+       wl_shm*                         shm_;
+       const std::string       themeName_;
+};
+
+TEST(load_default_theme, "Core/Cursor")
+{
+       TestCursorTheme test;
+       ASSERT(test.themeName_ == "default");
+       test.run();
+       FAIL_UNLESS_EQUAL(test.themeName_, std::string(test.theme_->name));
+}
+
+TEST(load_missing_theme, "Core/Cursor")
+{
+       TestCursorTheme test("this_theme_does_not_exist");
+       ASSERT(test.themeName_ == "this_theme_does_not_exist");
+       test.run();
+       FAIL_UNLESS_EQUAL("default", std::string(test.theme_->name));
+}
+