aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/test/base.c
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/d2tk/test/base.c')
-rw-r--r--subprojects/d2tk/test/base.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/subprojects/d2tk/test/base.c b/subprojects/d2tk/test/base.c
new file mode 100644
index 0000000..c711528
--- /dev/null
+++ b/subprojects/d2tk/test/base.c
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2018-2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
+ *
+ * This is free software: you can redistribute it and/or modify
+ * it under the terms of the Artistic License 2.0 as published by
+ * The Perl Foundation.
+ *
+ * This source 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
+ * Artistic License 2.0 for more details.
+ *
+ * You should have received a copy of the Artistic License 2.0
+ * along the source as a COPYING file. If not, obtain it from
+ * http://www.perlfoundation.org/artistic_license_2_0.
+ */
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+
+#include <d2tk/base.h>
+#include <d2tk/hash.h>
+#include "mock.h"
+
+static void
+_expose_hot(d2tk_base_t *base, const d2tk_rect_t *rect, unsigned n, unsigned p)
+{
+ D2TK_BASE_TABLE(rect, n, 1, tab)
+ {
+ const d2tk_rect_t *bnd = d2tk_table_get_rect(tab);
+ const unsigned k = d2tk_table_get_index(tab);
+ const d2tk_id_t id = D2TK_ID_IDX(k);
+
+ const d2tk_state_t state = d2tk_base_button(base, id, bnd);
+
+ if(k == p) // this element must be of state OVER and HOT
+ {
+ if(p == 0) // first element additionally has FOCUS
+ {
+ assert(state == (D2TK_STATE_OVER | D2TK_STATE_FOCUS | D2TK_STATE_HOT) );
+ }
+ else
+ {
+ assert(state == (D2TK_STATE_OVER | D2TK_STATE_HOT) );
+ }
+ }
+ else if(k == 0) // first element always has FOCUS
+ {
+ assert(state == D2TK_STATE_FOCUS);
+ }
+ else // all other elements have no state
+ {
+ assert(state == D2TK_STATE_NONE);
+ }
+ }
+}
+
+static void
+_test_hot()
+{
+#define N 4
+ d2tk_mock_ctx_t ctx = {
+ .check = NULL
+ };
+
+ d2tk_base_t *base = d2tk_base_new(&d2tk_mock_driver_lazy, &ctx);
+ assert(base);
+
+ d2tk_base_set_dimensions(base, DIM_W, DIM_H);
+ const d2tk_rect_t rect = D2TK_RECT(0, 0, DIM_W, DIM_H);
+
+ for(unsigned p = 0; p < N; p++)
+ {
+ const float p5 = p + 0.5f;
+ const d2tk_coord_t mx = DIM_W / N * p5;
+ const d2tk_coord_t my = DIM_H / 2;
+
+ d2tk_base_set_mouse_pos(base, mx, my);
+
+ d2tk_base_pre(base);
+
+ _expose_hot(base, &rect, N, p);
+
+ d2tk_base_post(base);
+ }
+
+ // trigger garbage collector
+ for(unsigned i = 0; i < 0x400; i++)
+ {
+ d2tk_base_pre(base);
+ d2tk_base_post(base);
+ }
+
+ d2tk_base_free(base);
+#undef N
+}
+
+int
+main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
+{
+ _test_hot();
+
+ return EXIT_SUCCESS;
+}