aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/test/base.c
blob: c7115282b24b81779818e2820d6ca0d1888375e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
}