diff options
| author | Matthias Melcher <github@matthiasm.com> | 2023-08-15 11:36:58 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-15 11:36:58 +0200 |
| commit | 10d9010ed9a7624bebebcb0f86fc86d362672027 (patch) | |
| tree | 4303c6f36947046d2baab01f02defd0d4bdb9ab2 /test/unittest_core.cxx | |
| parent | e6440ca0a89874e2593b2dc9cf5a3b0e87df94a2 (diff) | |
Improved, yet compatible, widget callback system using macros (#729)
* adds FL/fl_callback.macros.H
* adds FL_FUNCTION_CALLBACK_n(widget, function, [type, data])
* adds FL_METHOD_CALLBACK_n(widget, class, instance, method, [type, data])
* adds FL_INLINE_CALLBACK_n(widget, [type, name, data], callback_body)
* adds `examples/callback`
* full documentation
Diffstat (limited to 'test/unittest_core.cxx')
| -rw-r--r-- | test/unittest_core.cxx | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/test/unittest_core.cxx b/test/unittest_core.cxx index 30def3782..65fd71630 100644 --- a/test/unittest_core.cxx +++ b/test/unittest_core.cxx @@ -17,8 +17,10 @@ #include "unittests.h" #include <FL/Fl_Group.H> +#include <FL/Fl_Button.H> #include <FL/Fl_Simple_Terminal.H> #include <FL/Fl_String.H> +#include <FL/fl_callback_macros.H> /* Test Fl_String constructor and assignment. */ TEST(Fl_String, Assignment) { @@ -216,6 +218,68 @@ TEST(Fl_Preferences, Strings) { return true; } +bool cb1a_ok = false, cb1b_ok = false, cb1c_ok = false; +int cb1_alloc = 0; +class MyString : public Fl_String { +public: + MyString() : Fl_String() { cb1_alloc++; } + MyString(const MyString &str) : Fl_String(str) { cb1_alloc++; } + MyString(const char *t) : Fl_String(t) { cb1_alloc++; } + ~MyString() { cb1_alloc--; } +}; +void cb1(MyString a, int b) { + cb1a_ok = true; + if (strcmp(a.c_str(),"FLTK")==0) cb1b_ok = true; + if (b==4) cb1c_ok = true; +} + +/* Test callback macros. */ +TEST(Fl_Callback_Macros, FL_FUNCTION_CALLBACK) { + Fl_Group::current(NULL); + Fl_Button *btn = new Fl_Button(10, 10, 100, 100); + FL_FUNCTION_CALLBACK_2(btn, cb1, MyString, "FLTK", int, 4); + + do { class Fl_Callback_User_Data_240 : public Fl_Callback_User_Data { + public: MyString a_; int b_; + static void cb(Fl_Widget *w, void *user_data) { + Fl_Callback_User_Data_240 *cbdata = (Fl_Callback_User_Data_240*)user_data; (void)cbdata; cb1(cbdata->a_, cbdata->b_); }; Fl_Callback_User_Data_240(MyString a, int b) : a_(a), b_(b) { } }; btn->callback(Fl_Callback_User_Data_240::cb, new Fl_Callback_User_Data_240("FLTK", 4), true); } while(0); + + btn->do_callback(); + delete btn; + EXPECT_TRUE(cb1a_ok); // callback called + EXPECT_TRUE(cb1b_ok); // string stored correctly + EXPECT_TRUE(cb1c_ok); // integer stored correctly + EXPECT_TRUE(cb1_alloc==0); // string destroyed correctly (allocated as often as deallocated) + return true; +} + +TEST(Fl_Callback_Macros, FL_METHOD_CALLBACK) { + Fl_Group::current(NULL); + Fl_String *str = new Fl_String("FLTK"); + Fl_Button *btn = new Fl_Button(10, 10, 100, 100); + FL_METHOD_CALLBACK_2(btn, Fl_String, str, insert, int, 2, const char*, "XX"); + btn->do_callback(); + EXPECT_STREQ(str->c_str(), "FLXXTK"); + delete btn; + delete str; + return true; +} + +int cb3a = 0, cb3b = 0; +TEST(Fl_Callback_Macros, FL_INLINE_CALLBACK) { + Fl_Group::current(NULL); + Fl_Button *btn = new Fl_Button(10, 10, 100, 100); + FL_INLINE_CALLBACK_2(btn, + int, a, 42, int, b, 16, + { cb3a = a; cb3b = b; } + ); + btn->do_callback(); + EXPECT_EQ(cb3a, 42); + EXPECT_EQ(cb3b, 16); + delete btn; + return true; +} + // //------- test aspects of the FLTK core library ---------- // |
