| 1 | // |
| 2 | // Unit tests for the Fast Light Tool Kit (FLTK). |
| 3 | // |
| 4 | // Copyright 1998-2022 by Bill Spitzak and others. |
| 5 | // |
| 6 | // This library is free software. Distribution and use rights are outlined in |
| 7 | // the file "COPYING" which should have been included with this file. If this |
| 8 | // file is missing or damaged, see the license at: |
| 9 | // |
| 10 | // https://www.fltk.org/COPYING.php |
| 11 | // |
| 12 | // Please see the following page on how to report bugs and issues: |
| 13 | // |
| 14 | // https://www.fltk.org/bugs.php |
| 15 | // |
| 16 | |
| 17 | #include "unittests.h" |
| 18 | |
| 19 | #include <config.h> |
| 20 | #include <FL/Fl_Box.H> |
| 21 | #include <FL/fl_draw.H> // fl_text_extents() |
| 22 | #if HAVE_GL |
| 23 | #include <FL/Fl_Gl_Window.H> |
| 24 | #endif |
| 25 | |
| 26 | #if 0 |
| 27 | |
| 28 | // not testing yet: |
| 29 | void fl_line(int x, int y, int x1, int y1) |
| 30 | void fl_line(int x, int y, int x1, int y1, int x2, int y2) |
| 31 | |
| 32 | Draw one or two lines between the given points. |
| 33 | void fl_loop(int x, int y, int x1, int y1, int x2, int y2) |
| 34 | void fl_loop(int x, int y, int x1, int y1, int x2, int y2, int x3, int y3) |
| 35 | |
| 36 | Outline a 3 or 4-sided polygon with lines. |
| 37 | void fl_polygon(int x, int y, int x1, int y1, int x2, int y2) |
| 38 | void fl_polygon(int x, int y, int x1, int y1, int x2, int y2, int x3, int y3) |
| 39 | |
| 40 | Draw vertical and horizontal lines. A vertical line is drawn first, then a horizontal, then a vertical. |
| 41 | |
| 42 | #endif |
| 43 | |
| 44 | // |
| 45 | // --- test drawing shapes that are not transformed by the drawing matrix ------ |
| 46 | // |
| 47 | |
| 48 | void draw_fast_shapes() { |
| 49 | int a = 0, b = 0, i; |
| 50 | // 1: draw a filled rectangle (fl_rectf) |
| 51 | fl_color(FL_GREEN); |
| 52 | for (i=0; i<=40; i++) { fl_point(a+i, b); fl_point(a+i, b+20); } |
| 53 | for (i=0; i<=20; i++) { fl_point(a, b+i); fl_point(a+40, b+i); } |
| 54 | fl_color(FL_RED); |
| 55 | for (i=1; i<=39; i++) { fl_point(a+i, b+1); fl_point(a+i, b+19); } |
| 56 | for (i=1; i<=19; i++) { fl_point(a+1, b+i); fl_point(a+39, b+i); } |
| 57 | fl_color(FL_BLACK); |
| 58 | fl_rectf(a+1, b+1, 39, 19); |
| 59 | // 2: draw a one units wide frame |
| 60 | b+=24; |
| 61 | fl_color(FL_GREEN); |
| 62 | for (i=0; i<=40; i++) { fl_point(a+i, b); fl_point(a+i, b+20); } |
| 63 | for (i=0; i<=20; i++) { fl_point(a, b+i); fl_point(a+40, b+i); } |
| 64 | fl_color(FL_GREEN); |
| 65 | for (i=2; i<=38; i++) { fl_point(a+i, b+2); fl_point(a+i, b+18); } |
| 66 | for (i=2; i<=18; i++) { fl_point(a+2, b+i); fl_point(a+38, b+i); } |
| 67 | fl_color(FL_RED); |
| 68 | for (i=1; i<=39; i++) { fl_point(a+i, b+1); fl_point(a+i, b+19); } |
| 69 | for (i=1; i<=19; i++) { fl_point(a+1, b+i); fl_point(a+39, b+i); } |
| 70 | fl_color(FL_BLACK); |
| 71 | fl_rect(a+1, b+1, 39, 19); |
| 72 | // 3: draw a three units wide frame |
| 73 | b+=24; |
| 74 | fl_color(FL_GREEN); |
| 75 | fl_rect(a, b, 41, 21); |
| 76 | fl_rect(a+4, b+4, 33, 13); |
| 77 | fl_color(FL_RED); |
| 78 | fl_rect(a+1, b+1, 39, 19); |
| 79 | fl_rect(a+3, b+3, 35, 15); |
| 80 | fl_color(FL_BLACK); |
| 81 | fl_line_style(FL_SOLID, 3); |
| 82 | fl_rect(a+2, b+2, 37, 17); |
| 83 | fl_line_style(FL_SOLID, 1); |
| 84 | // 4: draw fl_xyline |
| 85 | b+=24; |
| 86 | fl_color(FL_GREEN); |
| 87 | fl_rect(a, b+8, 41, 3); // single line |
| 88 | fl_rect(a+45, b, 41, 3); // horizontal, then vertical line |
| 89 | fl_rect(a+83, b, 3, 21); |
| 90 | fl_rect(a+90, b, 21, 3); // horizontal, vertical, horizontal line |
| 91 | fl_rect(a+109, b, 3, 21); |
| 92 | fl_rect(a+109, b+18, 21, 3); |
| 93 | fl_color(FL_RED); |
| 94 | fl_rectf(a+1, b+9, 39, 1); // single line |
| 95 | fl_rectf(a+46, b+1, 39, 1); // two lines |
| 96 | fl_rectf(a+84, b+1, 1, 19); |
| 97 | fl_rectf(a+91, b+1, 20, 1); // three lines |
| 98 | fl_rectf(a+110, b+1, 1, 19); |
| 99 | fl_rectf(a+110, b+19, 19, 1); // three lines |
| 100 | fl_color(FL_BLACK); |
| 101 | fl_xyline(a+1, b+9, a+39); |
| 102 | fl_xyline(a+46, b+1, a+84, b+19); |
| 103 | fl_xyline(a+91, b+1, a+110, b+19, a+128); |
| 104 | b+=24; |
| 105 | fl_color(FL_GREEN); |
| 106 | fl_rect(a, b+7, 41, 5); // single line |
| 107 | fl_rect(a+45, b, 41, 5); // horizontal, then vertical line |
| 108 | fl_rect(a+81, b, 5, 21); |
| 109 | fl_rect(a+90, b, 22, 5); // horizontal, vertical, horizontal line |
| 110 | fl_rect(a+108, b, 5, 21); |
| 111 | fl_rect(a+108, b+16, 22, 5); |
| 112 | fl_color(FL_RED); |
| 113 | fl_rectf(a+1, b+8, 39, 3); // single line |
| 114 | fl_rectf(a+46, b+1, 39, 3); // two lines |
| 115 | fl_rectf(a+82, b+1, 3, 19); |
| 116 | fl_rectf(a+91, b+1, 20, 3); // three lines |
| 117 | fl_rectf(a+109, b+1, 3, 19); |
| 118 | fl_rectf(a+109, b+17, 20, 3); // three lines |
| 119 | fl_color(FL_BLACK); |
| 120 | fl_line_style(FL_SOLID, 3); |
| 121 | fl_xyline(a+1, b+9, a+39); |
| 122 | fl_xyline(a+46, b+2, a+83, b+19); |
| 123 | fl_xyline(a+91, b+2, a+110, b+18, a+128); |
| 124 | fl_line_style(FL_SOLID, 1); |
| 125 | // 5: draw fl_xyline |
| 126 | b+=24; |
| 127 | fl_color(FL_GREEN); |
| 128 | fl_rect(a+9, b, 3, 21); // single line |
| 129 | fl_rect(a+45, b, 3, 21); // horizontal, then vertical line |
| 130 | fl_rect(a+45, b+18, 41, 3); |
| 131 | fl_rect(a+90, b, 3, 11); // horizontal, vertical, horizontal line |
| 132 | fl_rect(a+90, b+9, 40, 3); |
| 133 | fl_rect(a+127, b+9, 3, 12); |
| 134 | fl_color(FL_RED); |
| 135 | fl_rectf(a+10, b+1, 1, 19); // single line |
| 136 | fl_rectf(a+46, b+1, 1, 19); // two lines |
| 137 | fl_rectf(a+46, b+19, 39, 1); |
| 138 | fl_rectf(a+91, b+1, 1, 10); // three lines |
| 139 | fl_rectf(a+91, b+10, 38, 1); |
| 140 | fl_rectf(a+128, b+10, 1, 10); // three lines |
| 141 | fl_color(FL_BLACK); |
| 142 | fl_yxline(a+10, b+1, b+19); |
| 143 | fl_yxline(a+46, b+1, b+19, a+84); |
| 144 | fl_yxline(a+91, b+1, b+10, a+128, b+19); |
| 145 | b+=24; |
| 146 | fl_color(FL_GREEN); |
| 147 | fl_rect(a+8, b, 5, 21); // single line |
| 148 | fl_rect(a+45, b, 5, 21); // horizontal, then vertical line |
| 149 | fl_rect(a+45, b+16, 41, 5); |
| 150 | fl_rect(a+90, b, 5, 11); // horizontal, vertical, horizontal line |
| 151 | fl_rect(a+90, b+8, 40, 5); |
| 152 | fl_rect(a+125, b+8, 5, 13); |
| 153 | fl_color(FL_RED); |
| 154 | fl_rectf(a+9, b+1, 3, 19); // single line |
| 155 | fl_rectf(a+46, b+1, 3, 19); // two lines |
| 156 | fl_rectf(a+46, b+17, 39, 3); |
| 157 | fl_rectf(a+91, b+1, 3, 10); // three lines |
| 158 | fl_rectf(a+91, b+9, 38, 3); |
| 159 | fl_rectf(a+126, b+9, 3, 11); // three lines |
| 160 | fl_color(FL_BLACK); |
| 161 | fl_line_style(FL_SOLID, 3); |
| 162 | fl_yxline(a+10, b+1, b+19); |
| 163 | fl_yxline(a+47, b+1, b+18, a+84); |
| 164 | fl_yxline(a+92, b+1, b+10, a+127, b+19); |
| 165 | fl_line_style(FL_SOLID, 1); |
| 166 | // 6: fast diagonal lines |
| 167 | b+=24; |
| 168 | fl_color(FL_GREEN); |
| 169 | fl_point(a, b); fl_point(a+1, b); fl_point(a, b+1); |
| 170 | fl_point(a+20, b+20); fl_point(a+19, b+20); fl_point(a+20, b+19); |
| 171 | fl_color(FL_RED); |
| 172 | fl_point(a+1, b+1); fl_point(a+19, b+19); |
| 173 | fl_color(FL_BLACK); |
| 174 | fl_line(a+1, b+1, a+19, b+19); |
| 175 | fl_color(FL_GREEN); |
| 176 | fl_point(a+25+1, b); fl_point(a+25, b+1); fl_point(a+25+4, b); fl_point(a+25, b+4); |
| 177 | fl_point(a+25+20, b+19); fl_point(a+25+19, b+20); fl_point(a+25+16, b+20); fl_point(a+25+20, b+16); |
| 178 | fl_color(FL_RED); |
| 179 | fl_point(a+25+2, b+2); fl_point(a+25+18, b+18); |
| 180 | fl_color(FL_BLACK); |
| 181 | fl_line_style(FL_SOLID, 5); |
| 182 | fl_line(a+25+1, b+1, a+25+19, b+19); |
| 183 | fl_line(a+50+1, b+1, a+50+20, b+20, a+50+39, b+1); |
| 184 | fl_line_style(FL_SOLID, 1); |
| 185 | } |
| 186 | |
| 187 | #if HAVE_GL |
| 188 | |
| 189 | class Ut_GL_Rect_Test : public Fl_Gl_Window { |
| 190 | public: |
| 191 | Ut_GL_Rect_Test(int x, int y, int w, int h) |
| 192 | : Fl_Gl_Window(x, y, w, h) { |
| 193 | box(FL_FLAT_BOX); |
| 194 | } |
| 195 | void draw() FL_OVERRIDE { |
| 196 | draw_begin(); |
| 197 | fl_color(color()); |
| 198 | fl_rectf(0, 0, w(), h()); |
| 199 | draw_fast_shapes(); |
| 200 | draw_end(); |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | #endif |
| 205 | |
| 206 | class Ut_Native_Rect_Test : public Fl_Window { |
| 207 | public: |
| 208 | Ut_Native_Rect_Test(int x, int y, int w, int h) |
| 209 | : Fl_Window(x, y, w, h) { |
| 210 | box(FL_FLAT_BOX); |
| 211 | end(); |
| 212 | } |
| 213 | void draw() FL_OVERRIDE { |
| 214 | Fl_Window::draw(); |
| 215 | draw_fast_shapes(); |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | class Ut_Rect_Test : public Fl_Group { // 520 x 365 |
| 220 | public: |
| 221 | static Fl_Widget *create() { |
| 222 | return new Ut_Rect_Test(UT_TESTAREA_X, UT_TESTAREA_Y, UT_TESTAREA_W, UT_TESTAREA_H); |
| 223 | } |
| 224 | Ut_Rect_Test(int x, int y, int w, int h) |
| 225 | : Fl_Group(x, y, w, h) { |
| 226 | label("Testing FLTK fast shape calls.\n" |
| 227 | "These calls draw horizontal and vertical lines, frames, and rectangles.\n\n" |
| 228 | "No red pixels should be visible. " |
| 229 | "If you see bright red lines, or if parts of the green frames are hidden, " |
| 230 | "drawing alignment is off."); |
| 231 | align(FL_ALIGN_INSIDE|FL_ALIGN_BOTTOM|FL_ALIGN_LEFT|FL_ALIGN_WRAP); |
| 232 | box(FL_BORDER_BOX); |
| 233 | |
| 234 | int a = x+16, b = y+34; |
| 235 | Fl_Box *t = new Fl_Box(a, b-24, 80, 18, "native"); |
| 236 | t->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); |
| 237 | |
| 238 | /* NativeRectTest *nr = */ new Ut_Native_Rect_Test(a+23, b-1, 200, 200); |
| 239 | |
| 240 | t = new Fl_Box(a, b, 18, 18, "1"); |
| 241 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 242 | t->tooltip(// Title: |
| 243 | "Testing filled rectangle alignment.\n\n" |
| 244 | // Description: |
| 245 | "This draws a black rectangle, surrounded by a green frame.\n\n" |
| 246 | // Things to look out for: |
| 247 | "If green pixels are missing, filled rectangles draw too big (see fl_rectf).\n\n" |
| 248 | "If red pixels are showing, filled rectangles are drawn too small." |
| 249 | ); |
| 250 | b+=24; |
| 251 | t = new Fl_Box(a, b, 18, 18, "2"); |
| 252 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 253 | t->tooltip(// Title: |
| 254 | "Testing rectangle frame alignment.\n\n" |
| 255 | // Description: |
| 256 | "This draws a black frame, surrounded on the inside and outside by a green frame.\n\n" |
| 257 | // Things to look out for: |
| 258 | "If green pixels are missing or red pixels are showing, rectangular frame drawing schould be adjusted (see fl_rect).\n\n" |
| 259 | "If red pixels show in the corners of the frame in hidpi mode, line endings should be adjusted." |
| 260 | ); |
| 261 | b+=24; |
| 262 | t = new Fl_Box(a, b, 18, 18, "3"); |
| 263 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 264 | t->tooltip(// Title: |
| 265 | "Testing scaled frame alignment.\n\n" |
| 266 | // Description: |
| 267 | "This draws a 3 units wide black frame, surrounded on the inside and outside by a green frame.\n\n" |
| 268 | // Things to look out for: |
| 269 | "If green pixels are missing or red pixels are showing, line width schould be adjusted (see fl_line_style).\n\n" |
| 270 | "If red pixels show in the corners of the frame in hidpi mode, line endings should be adjusted." |
| 271 | ); |
| 272 | b+=24; |
| 273 | t = new Fl_Box(a, b, 18, 18, "4"); |
| 274 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 275 | t->tooltip(// Title: |
| 276 | "Testing fl_xyline.\n\n" |
| 277 | // Description: |
| 278 | "This draws 3 versions of fl_xyline surronded with a green outline.\n\n" |
| 279 | // Things to look out for: |
| 280 | "If green pixels are missing or red pixels are showing, fl_xyline must be adjusted." |
| 281 | ); |
| 282 | b+=48; |
| 283 | t = new Fl_Box(a, b, 18, 18, "5"); |
| 284 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 285 | t->tooltip(// Title: |
| 286 | "Testing fl_yxline.\n\n" |
| 287 | // Description: |
| 288 | "This draws 3 versions of fl_yxline surronded with a green outline.\n\n" |
| 289 | // Things to look out for: |
| 290 | "If green pixels are missing or red pixels are showing, fl_yxline must be adjusted." |
| 291 | ); |
| 292 | b+=48; |
| 293 | t = new Fl_Box(a, b, 18, 18, "6"); |
| 294 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 295 | t->tooltip(// Title: |
| 296 | "Testing fl_line(int...).\n\n" |
| 297 | // Description: |
| 298 | "This draws 2 lines at differnet widths, and one connected line.\n\n" |
| 299 | // Things to look out for: |
| 300 | "Green and red pixels mark the beginning and end of single lines." |
| 301 | "The line caps should be flat, the joints should be of type \"miter\"." |
| 302 | ); |
| 303 | |
| 304 | #if HAVE_GL |
| 305 | |
| 306 | a = x+16+250, b = y+34; |
| 307 | t = new Fl_Box(a, b-24, 80, 18, "OpenGL"); |
| 308 | t->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); |
| 309 | |
| 310 | /*GLRectTest *glr = */ new Ut_GL_Rect_Test(a+31, b-1, 200, 200); |
| 311 | |
| 312 | t = new Fl_Box(a, b, 26, 18, "1a"); |
| 313 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 314 | t->tooltip(// Title: |
| 315 | "Testing filled rectangle alignment.\n\n" |
| 316 | // Description: |
| 317 | "This draws a black rectangle, surrounded by a green frame.\n\n" |
| 318 | // Things to look out for: |
| 319 | "If green pixels are missing, filled rectangles draw too big (see fl_rectf).\n\n" |
| 320 | "If red pixels are showing, filled rectangles are drawn too small." |
| 321 | ); |
| 322 | |
| 323 | b+=24; |
| 324 | t = new Fl_Box(a, b, 26, 18, "2a"); |
| 325 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 326 | t->tooltip(// Title: |
| 327 | "Testing rectangle frame alignment.\n\n" |
| 328 | // Description: |
| 329 | "This draws a black frame, surrounded on the inside and outside by a green frame.\n\n" |
| 330 | // Things to look out for: |
| 331 | "If green pixels are missing or red pixels are showing, rectangular frame drawing schould be adjusted (see fl_rect).\n\n" |
| 332 | "If red pixels show in the corners of the frame in hidpi mode, line endings should be adjusted." |
| 333 | ); |
| 334 | |
| 335 | b+=24; |
| 336 | t = new Fl_Box(a, b, 26, 18, "3a"); |
| 337 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 338 | t->tooltip(// Title: |
| 339 | "Testing scaled frame alignment.\n\n" |
| 340 | // Description: |
| 341 | "This draws a 3 units wide black frame, surrounded on the inside and outside by a green frame.\n\n" |
| 342 | // Things to look out for: |
| 343 | "If green pixels are missing or red pixels are showing, line width schould be adjusted (see fl_line_style).\n\n" |
| 344 | "If red pixels show in the corners of the frame in hidpi mode, line endings should be adjusted." |
| 345 | ); |
| 346 | b+=24; |
| 347 | t = new Fl_Box(a, b, 26, 18, "4a"); |
| 348 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 349 | t->tooltip(// Title: |
| 350 | "Testing fl_xyline.\n\n" |
| 351 | // Description: |
| 352 | "This draws 3 versions of fl_xyline surronded with a green outline.\n\n" |
| 353 | // Things to look out for: |
| 354 | "If green pixels are missing or red pixels are showing, fl_xyline must be adjusted." |
| 355 | ); |
| 356 | b+=48; |
| 357 | t = new Fl_Box(a, b, 26, 18, "5a"); |
| 358 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 359 | t->tooltip(// Title: |
| 360 | "Testing fl_yxline.\n\n" |
| 361 | // Description: |
| 362 | "This draws 3 versions of fl_yxline surronded with a green outline.\n\n" |
| 363 | // Things to look out for: |
| 364 | "If green pixels are missing or red pixels are showing, fl_yxline must be adjusted." |
| 365 | ); |
| 366 | b+=48; |
| 367 | t = new Fl_Box(a, b, 26, 18, "6a"); |
| 368 | t->box(FL_ROUNDED_BOX); t->color(FL_YELLOW); |
| 369 | t->tooltip(// Title: |
| 370 | "Testing fl_line(int...).\n\n" |
| 371 | // Description: |
| 372 | "This draws 2 lines at differnet widths, and one connected line.\n\n" |
| 373 | // Things to look out for: |
| 374 | "Green and red pixels mark the beginning and end of single lines." |
| 375 | "The line caps should be flat, the joints should be of type \"miter\"." |
| 376 | ); |
| 377 | #endif |
| 378 | |
| 379 | t = new Fl_Box(x+w-1,y+h-1, 1, 1); |
| 380 | resizable(t); |
| 381 | } |
| 382 | }; |
| 383 | |
| 384 | UnitTest rects(UT_TEST_FAST_SHAPES, "Fast Shapes", Ut_Rect_Test::create); |