summaryrefslogtreecommitdiff
path: root/tests/diagram.c
blob: 8af61b1ef08ecb44081f2d7c55254226e14f193c (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * diagram.c
 *
 * This file is a part of logdiag.
 * Copyright Přemysl Janouch 2011
 *
 * See the file LICENSE for licensing information.
 *
 */

#include <liblogdiag/liblogdiag.h>

typedef struct
{
	LdDiagram *diagram;
}
Diagram;

static void
diagram_setup (Diagram *fixture, gconstpointer test_data)
{
	fixture->diagram = ld_diagram_new ();
}

static void
diagram_teardown (Diagram *fixture, gconstpointer test_data)
{
	g_object_unref (fixture->diagram);
}

static void
diagram_test_history (Diagram *fixture, gconstpointer user_data)
{
	const gdouble start_x = 1;
	const guint move_length = 3;
	LdDiagramObject *object;
	guint i;
	gdouble x;

	object = ld_diagram_object_new (NULL);
	g_object_set (object, "x", start_x, NULL);

	/* Link an object with the diagram. */
	ld_diagram_insert_object (fixture->diagram, object, 0);
	g_assert (ld_diagram_can_undo (fixture->diagram) != FALSE);

	/* Create some object actions to undo. */
	for (i = 0; i++ < move_length;)
		g_object_set (object, "x", start_x + i, NULL);

	/* Undo them and check the state. */
	for (i = move_length; i--;)
	{
		g_assert (ld_diagram_can_undo (fixture->diagram) != FALSE);
		ld_diagram_undo (fixture->diagram);

		g_object_get (object, "x", &x, NULL);
		g_assert_cmpfloat (x, ==, start_x + i);
	}

	/* Redo them and check the state. */
	for (i = 0; i++ < move_length;)
	{
		g_assert (ld_diagram_can_redo (fixture->diagram) != FALSE);
		ld_diagram_redo (fixture->diagram);

		g_object_get (object, "x", &x, NULL);
		g_assert_cmpfloat (x, ==, start_x + i);
	}

	g_object_unref (object);
}

static void
diagram_test_history_grouping (Diagram *fixture, gconstpointer user_data)
{
	const LdPoint start_position = {1, 3};
	const guint move_length = 3;
	LdDiagramObject *object;
	guint i;
	gdouble x, y;

	object = ld_diagram_object_new (NULL);
	g_object_set (object,
		"x", start_position.x,
		"y", start_position.y,
		NULL);

	/* Create a single user action. */
	ld_diagram_begin_user_action (fixture->diagram);
	ld_diagram_insert_object (fixture->diagram, object, 0);
	for (i = 0; i++ < move_length;)
	{
		ld_diagram_begin_user_action (fixture->diagram);
		g_object_set (object,
			"x", start_position.x + i,
			"y", start_position.y + i,
			NULL);
		ld_diagram_end_user_action (fixture->diagram);
	}
	ld_diagram_end_user_action (fixture->diagram);

	/* Undo the action. */
	g_assert (ld_diagram_get_objects (fixture->diagram) != NULL);
	g_assert (ld_diagram_can_undo (fixture->diagram) != FALSE);
	ld_diagram_undo (fixture->diagram);

	/* Check that it has been undone correctly. */
	g_assert (ld_diagram_get_objects (fixture->diagram) == NULL);
	g_assert (ld_diagram_can_undo (fixture->diagram) == FALSE);

	g_object_get (object, "x", &x, "y", &y, NULL);
	g_assert_cmpfloat (x, ==, start_position.x);
	g_assert_cmpfloat (y, ==, start_position.y);

	/* Redo the action. */
	g_assert (ld_diagram_can_redo (fixture->diagram) != FALSE);
	ld_diagram_redo (fixture->diagram);

	/* Check that it has been redone correctly. */
	g_object_get (object, "x", &x, "y", &y, NULL);
	g_assert_cmpfloat (x, ==, start_position.x + move_length);
	g_assert_cmpfloat (y, ==, start_position.y + move_length);

	g_object_unref (object);
}

int
main (int argc, char *argv[])
{
	gtk_test_init (&argc, &argv, NULL);

	/* History. */
	g_test_add ("/diagram/history", Diagram, NULL,
		diagram_setup, diagram_test_history,
		diagram_teardown);
	g_test_add ("/diagram/history-grouping", Diagram, NULL,
		diagram_setup, diagram_test_history_grouping,
		diagram_teardown);

	return g_test_run ();
}