You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.2KB

  1. //
  2. // "$Id: unittests.cxx 7903 2010-11-28 21:06:39Z matt $"
  3. //
  4. // Unit tests for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. // Fltk unit tests
  28. // v0.1 - Greg combines Matthias + Ian's tests
  29. // v0.2 - Ian's 02/12/09 fixes applied
  30. // v0.3 - Fixes to circle desc, augmented extent tests, fixed indents, added show(argc,argv)
  31. // v1.0 - Submit for svn
  32. // v1.1 - Matthias seperated all tests into multiple source files for hopefully easier handling
  33. #include <FL/Fl.H>
  34. #include <FL/Fl_Double_Window.H>
  35. #include <FL/Fl_Hold_Browser.H>
  36. #include <FL/Fl_Help_View.H>
  37. #include <FL/Fl_Group.H>
  38. #include <FL/Fl_Box.H>
  39. #include <FL/fl_draw.H> // fl_text_extents()
  40. // WINDOW/WIDGET SIZES
  41. #define MAINWIN_W 700 // main window w()
  42. #define MAINWIN_H 400 // main window h()
  43. #define BROWSER_X 10 // browser x()
  44. #define BROWSER_Y 25 // browser y()
  45. #define BROWSER_W 150 // browser w()
  46. #define BROWSER_H MAINWIN_H-35 // browser h()
  47. #define TESTAREA_X (BROWSER_W + 20) // test area x()
  48. #define TESTAREA_Y 25 // test area y()
  49. #define TESTAREA_W (MAINWIN_W - BROWSER_W - 30) // test area w()
  50. #define TESTAREA_H BROWSER_H // test area h()
  51. typedef void (*UnitTestCallback)(const char*,Fl_Group*);
  52. class MainWindow *mainwin = 0;
  53. Fl_Hold_Browser *browser = 0;
  54. // This class helps to automagically register a new test with the unittest app.
  55. // Please see the examples on how this is used.
  56. class UnitTest {
  57. public:
  58. UnitTest(const char *label, Fl_Widget* (*create)()) :
  59. fWidget(0L)
  60. {
  61. fLabel = strdup(label);
  62. fCreate = create;
  63. add(this);
  64. }
  65. ~UnitTest() {
  66. delete fWidget;
  67. free(fLabel);
  68. }
  69. const char *label() {
  70. return fLabel;
  71. }
  72. void create() {
  73. fWidget = fCreate();
  74. if (fWidget) fWidget->hide();
  75. }
  76. void show() {
  77. if (fWidget) fWidget->show();
  78. }
  79. void hide() {
  80. if (fWidget) fWidget->hide();
  81. }
  82. static int numTest() { return nTest; }
  83. static UnitTest *test(int i) { return fTest[i]; }
  84. private:
  85. char *fLabel;
  86. Fl_Widget *(*fCreate)();
  87. Fl_Widget *fWidget;
  88. static void add(UnitTest *t) {
  89. fTest[nTest] = t;
  90. nTest++;
  91. }
  92. static int nTest;
  93. static UnitTest *fTest[200];
  94. };
  95. int UnitTest::nTest = 0;
  96. UnitTest *UnitTest::fTest[];
  97. // The main window needs an additional drawing feature in order to support
  98. // the viewport alignment test.
  99. class MainWindow : public Fl_Double_Window {
  100. public:
  101. MainWindow(int w, int h, const char *l=0L) :
  102. Fl_Double_Window(w, h, l),
  103. fTestAlignment(0)
  104. { }
  105. // this code is used by the viewport alignment test
  106. void drawAlignmentIndicators() {
  107. const int sze = 16;
  108. // top left corner
  109. fl_color(FL_GREEN); fl_yxline(0, sze, 0, sze);
  110. fl_color(FL_RED); fl_yxline(-1, sze, -1, sze);
  111. fl_color(FL_WHITE); fl_rectf(3, 3, sze-2, sze-2);
  112. fl_color(FL_BLACK); fl_rect(3, 3, sze-2, sze-2);
  113. // bottom left corner
  114. fl_color(FL_GREEN); fl_yxline(0, h()-sze-1, h()-1, sze);
  115. fl_color(FL_RED); fl_yxline(-1, h()-sze-1, h(), sze);
  116. fl_color(FL_WHITE); fl_rectf(3, h()-sze-1, sze-2, sze-2);
  117. fl_color(FL_BLACK); fl_rect(3, h()-sze-1, sze-2, sze-2);
  118. // bottom right corner
  119. fl_color(FL_GREEN); fl_yxline(w()-1, h()-sze-1, h()-1, w()-sze-1);
  120. fl_color(FL_RED); fl_yxline(w(), h()-sze-1, h(), w()-sze-1);
  121. fl_color(FL_WHITE); fl_rectf(w()-sze-1, h()-sze-1, sze-2, sze-2);
  122. fl_color(FL_BLACK); fl_rect(w()-sze-1, h()-sze-1, sze-2, sze-2);
  123. // top right corner
  124. fl_color(FL_GREEN); fl_yxline(w()-1, sze, 0, w()-sze-1);
  125. fl_color(FL_RED); fl_yxline(w(), sze, -1, w()-sze-1);
  126. fl_color(FL_WHITE); fl_rectf(w()-sze-1, 3, sze-2, sze-2);
  127. fl_color(FL_BLACK); fl_rect(w()-sze-1, 3, sze-2, sze-2);
  128. }
  129. void draw() {
  130. Fl_Double_Window::draw();
  131. if (fTestAlignment) {
  132. drawAlignmentIndicators();
  133. }
  134. }
  135. void testAlignment(int v) {
  136. fTestAlignment = v;
  137. redraw();
  138. }
  139. int fTestAlignment;
  140. };
  141. //------- include the various unit tests as inline code -------
  142. #include "unittest_about.cxx"
  143. #include "unittest_points.cxx"
  144. #include "unittest_lines.cxx"
  145. #include "unittest_rects.cxx"
  146. #include "unittest_circles.cxx"
  147. #include "unittest_text.cxx"
  148. #include "unittest_images.cxx"
  149. #include "unittest_viewport.cxx"
  150. #include "unittest_scrollbarsize.cxx"
  151. // callback whenever the browser value changes
  152. void Browser_CB(Fl_Widget*, void*) {
  153. for ( int t=1; t<=browser->size(); t++ ) {
  154. UnitTest *ti = (UnitTest*)browser->data(t);
  155. if ( browser->selected(t) ) {
  156. ti->show();
  157. } else {
  158. ti->hide();
  159. }
  160. }
  161. }
  162. // this is the main call. It creates the window and adds all previously
  163. // registered tests to the browser widget.
  164. int main(int argc, char **argv) {
  165. Fl::args(argc,argv);
  166. Fl::visual(FL_RGB);
  167. mainwin = new MainWindow(MAINWIN_W, MAINWIN_H, "Fltk Unit Tests");
  168. browser = new Fl_Hold_Browser(BROWSER_X, BROWSER_Y, BROWSER_W, BROWSER_H, "Unit Tests");
  169. browser->align(FL_ALIGN_TOP|FL_ALIGN_LEFT);
  170. browser->when(FL_WHEN_CHANGED);
  171. browser->callback(Browser_CB);
  172. int i, n = UnitTest::numTest();
  173. for (i=0; i<n; i++) {
  174. UnitTest *t = UnitTest::test(i);
  175. mainwin->begin();
  176. t->create();
  177. mainwin->end();
  178. browser->add(t->label(), (void*)t);
  179. }
  180. /////
  181. mainwin->resizable(mainwin);
  182. mainwin->show(argc,argv);
  183. // Select first test in browser, and show that test.
  184. browser->select(1);
  185. Browser_CB(browser,0);
  186. return(Fl::run());
  187. }
  188. //
  189. // End of "$Id: unittests.cxx 7903 2010-11-28 21:06:39Z matt $".
  190. //