summaryrefslogtreecommitdiff
path: root/tests/test-context.cc
blob: bd0ce2aeac6937ddc219efabb20615990f4c8680 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// vim: ts=2 sw=2 et
/*
 * These tests are of limited usefulness.  In fact, you might even say that
 * they're not really tests at all.  But I felt that it would be useful to have
 * some basic usage of most functions just to verify that things compile and
 * work generally
 */

#include <boost/test/unit_test.hpp>
#include <boost/test/test_tools.hpp>
using namespace boost::unit_test;
#include <cairomm/context.h>

#define CREATE_CONTEXT(varname) \
  Cairo::RefPtr<Cairo::Surface> surf = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, 10, 10); \
  Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surf);

void
test_dashes ()
{
  CREATE_CONTEXT(cr);
  std::valarray<double> dash_array(4);
  dash_array[0] = 0.1;
  dash_array[1] = 0.2;
  dash_array[2] = 0.04;
  dash_array[3] = 0.31;
  cr->set_dash(dash_array, 0.54);

  std::vector<double> get_array;
  double get_offset;
  cr->get_dash (get_array, get_offset);
  BOOST_CHECK (get_array[0] == dash_array[0]);
  BOOST_CHECK (get_array[1] == dash_array[1]);
  BOOST_CHECK (get_array[2] == dash_array[2]);
  BOOST_CHECK (get_array[3] == dash_array[3]);
  BOOST_CHECK (get_offset == 0.54);

  cr->unset_dash ();
  cr->get_dash (get_array, get_offset);
  BOOST_CHECK (get_array.empty ());
}

void
test_save_restore ()
{
  CREATE_CONTEXT(cr);
  cr->set_line_width (1.0);
  cr->save ();
  cr->set_line_width (4.0);
  BOOST_CHECK (cr->get_line_width () == 4.0);
  cr->restore ();
  BOOST_CHECK (cr->get_line_width () == 1.0);
}

void
test_operator ()
{
  CREATE_CONTEXT(cr);
  cr->set_operator (Cairo::OPERATOR_ATOP);
  BOOST_CHECK (cr->get_operator () == Cairo::OPERATOR_ATOP);
  cr->set_operator (Cairo::OPERATOR_CLEAR);
  BOOST_CHECK (cr->get_operator () == Cairo::OPERATOR_CLEAR);
}

void
test_source ()
{
  CREATE_CONTEXT(cr);
  Cairo::RefPtr<Cairo::Pattern> solid_pattern =
    Cairo::SolidPattern::create_rgb (1.0, 0.5, 0.25);
  Cairo::RefPtr<Cairo::Pattern> gradient_pattern =
    Cairo::LinearGradient::create (0.0, 0.0, 1.0, 1.0);

  // there doesn't seem to be any way to compare the retrieved pattern to the
  // one that was set...  for now, just excercise the function calls.
  cr->set_source (solid_pattern);
  //BOOST_CHECK (cr->get_source () == solid_pattern);

  cr->set_source (gradient_pattern);
  //BOOST_CHECK (cr->get_source () == gradient_pattern);

  cr->set_source_rgb (1.0, 0.5, 0.25);
  Cairo::RefPtr<Cairo::SolidPattern> solid =
    Cairo::RefPtr<Cairo::SolidPattern>::cast_dynamic(cr->get_source ());
  BOOST_REQUIRE (solid);
  double rx, gx, bx, ax;
  solid->get_rgba (rx, gx, bx, ax);
  BOOST_CHECK (rx == 1.0);
  BOOST_CHECK (gx == 0.5);
  BOOST_CHECK (bx == 0.25);
  cr->set_source_rgba (0.1, 0.3, 0.5, 0.7);
  solid =
    Cairo::RefPtr<Cairo::SolidPattern>::cast_dynamic(cr->get_source ());
  BOOST_REQUIRE (solid);
  solid->get_rgba (rx, gx, bx, ax);
  BOOST_CHECK (rx == 0.1);
  BOOST_CHECK (gx == 0.3);
  BOOST_CHECK (bx == 0.5);
  BOOST_CHECK (ax == 0.7);
}

void
test_tolerance ()
{
  CREATE_CONTEXT(cr);
  cr->set_tolerance (3.0);
  BOOST_CHECK (cr->get_tolerance () == 3.0);
}

void
test_antialias ()
{
  CREATE_CONTEXT(cr);
  cr->set_antialias (Cairo::ANTIALIAS_GRAY);
  BOOST_CHECK (cr->get_antialias () == Cairo::ANTIALIAS_GRAY);

  cr->set_antialias (Cairo::ANTIALIAS_SUBPIXEL);
  BOOST_CHECK (cr->get_antialias () == Cairo::ANTIALIAS_SUBPIXEL);
}

void
test_fill_rule ()
{
  CREATE_CONTEXT(cr);
  cr->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
  BOOST_CHECK (cr->get_fill_rule () == Cairo::FILL_RULE_EVEN_ODD);
  cr->set_fill_rule (Cairo::FILL_RULE_WINDING);
  BOOST_CHECK (cr->get_fill_rule () == Cairo::FILL_RULE_WINDING);
}

void
test_line_width ()
{
  CREATE_CONTEXT(cr);
  cr->set_line_width (1.0);
  BOOST_CHECK (cr->get_line_width () == 1.0);
  cr->set_line_width (4.0);
  BOOST_CHECK (cr->get_line_width () == 4.0);
}

void
test_line_cap ()
{
  CREATE_CONTEXT(cr);
  cr->set_line_cap (Cairo::LINE_CAP_BUTT);
  BOOST_CHECK (cr->get_line_cap () == Cairo::LINE_CAP_BUTT);
  cr->set_line_cap (Cairo::LINE_CAP_ROUND);
  BOOST_CHECK (cr->get_line_cap () == Cairo::LINE_CAP_ROUND);
}

void
test_line_join ()
{
  CREATE_CONTEXT(cr);
  cr->set_line_join (Cairo::LINE_JOIN_BEVEL);
  BOOST_CHECK (cr->get_line_join () == Cairo::LINE_JOIN_BEVEL);
  cr->set_line_join (Cairo::LINE_JOIN_MITER);
  BOOST_CHECK (cr->get_line_join () == Cairo::LINE_JOIN_MITER);
}

void
test_miter_limit ()
{
  CREATE_CONTEXT (cr);
  cr->set_miter_limit (1.3);
  BOOST_CHECK (cr->get_miter_limit () == 1.3);
  cr->set_miter_limit (4.12);
  BOOST_CHECK (cr->get_miter_limit () == 4.12);
}

void
test_matrix ()
{
  CREATE_CONTEXT (cr);
  //cr->transform();
  //cr->set_matrix();
  //cr->set_identity_matrix ();
}

void
test_user_device ()
{
  // scale / transform a context, and then verify that user-to-device and
  // device-to-user things work.
}

void
test_draw ()
{
  CREATE_CONTEXT (cr);
  // just call a bunch of drawing functions to excercise them a bit.  There's no
  // rhyme or reason to this, don't expect it to draw anything interesting.
  cr->begin_new_path ();
  cr->move_to (1.0, 1.0);
  cr->line_to (2.0, 2.0);
  cr->curve_to (0.5, 0.5, 0.5, 0.5, 1.0, 1.0);
  cr->arc (1.5, 0.5, 0.5, 0, 2 * M_PI);
  cr->stroke ();
  cr->arc_negative (1.5, 0.5, 0.5, 0, 2 * M_PI);
  cr->rel_move_to (0.1, 0.1);
  cr->rel_line_to (0.5, -0.5);
  cr->rel_curve_to (0.5, 0.5, 0.5, 0.5, 1.0, 1.0);
  cr->rectangle (0.0, 0.0, 1.0, 1.0);
  cr->close_path ();
  cr->paint ();
}

void
test_clip ()
{
  CREATE_CONTEXT (cr);
  cr->rectangle (0.0, 0.0, 1.0, 1.0);
  cr->clip ();
  double x1, y1, x2, y2;
  cr->get_clip_extents (x1, y1, x2, y2);
  BOOST_CHECK (x1 == 0.0);
  BOOST_CHECK (y1 == 0.0);
  BOOST_CHECK (x2 == 1.0);
  BOOST_CHECK (y2 == 1.0);
}

void
test_current_point ()
{
  CREATE_CONTEXT (cr);
  cr->move_to (2.0, 3.0);
  double x, y;
  cr->get_current_point (x, y);
  BOOST_CHECK (x == 2.0);
  BOOST_CHECK (y == 3.0);
}

test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
    test_suite* test= BOOST_TEST_SUITE( "Cairo::Context Tests" );

    test->add (BOOST_TEST_CASE (&test_dashes));
    test->add (BOOST_TEST_CASE (&test_save_restore));
    test->add (BOOST_TEST_CASE (&test_operator));
    test->add (BOOST_TEST_CASE (&test_source));
    test->add (BOOST_TEST_CASE (&test_tolerance));
    test->add (BOOST_TEST_CASE (&test_antialias));
    test->add (BOOST_TEST_CASE (&test_fill_rule));
    test->add (BOOST_TEST_CASE (&test_line_width));
    test->add (BOOST_TEST_CASE (&test_line_cap));
    test->add (BOOST_TEST_CASE (&test_line_join));
    test->add (BOOST_TEST_CASE (&test_miter_limit));
    //test->add (BOOST_TEST_CASE (&test_matrix));
    //test->add (BOOST_TEST_CASE (&test_user_device));
    test->add (BOOST_TEST_CASE (&test_draw));
    test->add (BOOST_TEST_CASE (&test_clip));
    test->add (BOOST_TEST_CASE (&test_current_point));

    return test;
}