boundingbox.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/graphics/boundingbox.cpp
  3. // Purpose: wxGCDC bounding box unit tests
  4. // Author: Vadim Zeitlin / Maarten Spoek / Toni Ruža
  5. // Created: 2011-01-36
  6. // RCS-ID: $Id$
  7. // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // (c) 2014 Toni Ruža <toni.ruza@gmail.com>
  9. ///////////////////////////////////////////////////////////////////////////////
  10. // ----------------------------------------------------------------------------
  11. // headers
  12. // ----------------------------------------------------------------------------
  13. #include "testprec.h"
  14. #ifdef __BORLANDC__
  15. #pragma hdrstop
  16. #endif
  17. #include "wx/bitmap.h"
  18. #include "wx/dcmemory.h"
  19. #include "wx/dcgraph.h"
  20. #include "wx/icon.h"
  21. #include "wx/colour.h"
  22. #include "wx/gdicmn.h"
  23. // ----------------------------------------------------------------------------
  24. // test class
  25. // ----------------------------------------------------------------------------
  26. class GCDCBoundingBoxTestCase : public CppUnit::TestCase
  27. {
  28. public:
  29. GCDCBoundingBoxTestCase()
  30. {
  31. m_bmp.Create(100, 100);
  32. m_dc.SelectObject(m_bmp);
  33. m_gcdc = new wxGCDC(m_dc);
  34. }
  35. ~GCDCBoundingBoxTestCase()
  36. {
  37. delete m_gcdc;
  38. m_dc.SelectObject(wxNullBitmap);
  39. m_bmp = wxNullBitmap;
  40. }
  41. virtual void setUp()
  42. {
  43. m_gcdc->ResetBoundingBox();
  44. }
  45. private:
  46. wxBitmap m_bmp;
  47. wxMemoryDC m_dc;
  48. wxGCDC *m_gcdc;
  49. void AssertBox(int minX, int minY, int width, int height, int margin = 0)
  50. {
  51. int maxX = minX + width;
  52. int maxY = minY + height;
  53. // Allow for a margin of error due to different implementation
  54. // specificities regarding drawing paths.
  55. if ( margin )
  56. {
  57. #define WX_ASSERT_CLOSE(expected, actual, delta) \
  58. WX_ASSERT_MESSAGE(("%d != %d", actual, expected), \
  59. abs(actual - expected) <= delta)
  60. WX_ASSERT_CLOSE(minX, m_gcdc->MinX(), margin);
  61. WX_ASSERT_CLOSE(minY, m_gcdc->MinY(), margin);
  62. WX_ASSERT_CLOSE(maxX, m_gcdc->MaxX(), margin);
  63. WX_ASSERT_CLOSE(maxY, m_gcdc->MaxY(), margin);
  64. #undef WX_ASSERT_CLOSE
  65. }
  66. else
  67. {
  68. CPPUNIT_ASSERT_EQUAL(minX, m_gcdc->MinX());
  69. CPPUNIT_ASSERT_EQUAL(minY, m_gcdc->MinY());
  70. CPPUNIT_ASSERT_EQUAL(maxX, m_gcdc->MaxX());
  71. CPPUNIT_ASSERT_EQUAL(maxY, m_gcdc->MaxY());
  72. }
  73. }
  74. CPPUNIT_TEST_SUITE( GCDCBoundingBoxTestCase );
  75. CPPUNIT_TEST( DrawBitmap );
  76. CPPUNIT_TEST( DrawIcon );
  77. CPPUNIT_TEST( DrawLine );
  78. CPPUNIT_TEST( CrossHair );
  79. CPPUNIT_TEST( DrawArc );
  80. CPPUNIT_TEST( DrawEllipticArc );
  81. CPPUNIT_TEST( DrawPoint );
  82. CPPUNIT_TEST( DrawLines );
  83. #if wxUSE_SPLINES
  84. CPPUNIT_TEST( DrawSpline );
  85. #endif
  86. CPPUNIT_TEST( DrawPolygon );
  87. CPPUNIT_TEST( DrawPolyPolygon );
  88. CPPUNIT_TEST( DrawRectangle );
  89. CPPUNIT_TEST( DrawRoundedRectangle );
  90. CPPUNIT_TEST( DrawEllipse );
  91. CPPUNIT_TEST( Blit );
  92. CPPUNIT_TEST( StretchBlit );
  93. CPPUNIT_TEST( DrawRotatedText );
  94. CPPUNIT_TEST( DrawText );
  95. CPPUNIT_TEST( GradientFillLinear );
  96. CPPUNIT_TEST( GradientFillConcentric );
  97. CPPUNIT_TEST( DrawCheckMark );
  98. CPPUNIT_TEST_SUITE_END();
  99. void DrawBitmap();
  100. void DrawIcon();
  101. void DrawLine();
  102. void CrossHair();
  103. void DrawArc();
  104. void DrawEllipticArc();
  105. void DrawPoint();
  106. void DrawLines();
  107. #if wxUSE_SPLINES
  108. void DrawSpline();
  109. #endif
  110. void DrawPolygon();
  111. void DrawPolyPolygon();
  112. void DrawRectangle();
  113. void DrawRoundedRectangle();
  114. void DrawEllipse();
  115. void Blit();
  116. void StretchBlit();
  117. void DrawRotatedText();
  118. void DrawText();
  119. void GradientFillLinear();
  120. void GradientFillConcentric();
  121. void DrawCheckMark();
  122. DECLARE_NO_COPY_CLASS(GCDCBoundingBoxTestCase)
  123. };
  124. // register in the unnamed registry so that these tests are run by default
  125. CPPUNIT_TEST_SUITE_REGISTRATION( GCDCBoundingBoxTestCase );
  126. // also include in it's own registry so that these tests can be run alone
  127. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GCDCBoundingBoxTestCase, "GCDCBoundingBoxTestCase" );
  128. void GCDCBoundingBoxTestCase::DrawBitmap()
  129. {
  130. wxBitmap bitmap;
  131. bitmap.Create(12, 12);
  132. m_gcdc->DrawBitmap(bitmap, 5, 5);
  133. AssertBox(5, 5, 12, 12);
  134. }
  135. void GCDCBoundingBoxTestCase::DrawIcon()
  136. {
  137. wxBitmap bitmap;
  138. bitmap.Create(16, 16);
  139. wxIcon icon;
  140. icon.CopyFromBitmap(bitmap);
  141. m_gcdc->DrawIcon(icon, 42, 42);
  142. AssertBox(42, 42, 16, 16);
  143. }
  144. void GCDCBoundingBoxTestCase::DrawLine()
  145. {
  146. m_gcdc->DrawLine(10, 10, 20, 15);
  147. AssertBox(10, 10, 10, 5);
  148. }
  149. void GCDCBoundingBoxTestCase::CrossHair()
  150. {
  151. int w, h;
  152. m_gcdc->GetSize(&w, &h);
  153. m_gcdc->CrossHair(33, 33);
  154. AssertBox(0, 0, w, h);
  155. }
  156. void GCDCBoundingBoxTestCase::DrawArc()
  157. {
  158. m_gcdc->DrawArc(25, 30, 15, 40, 25, 40); // quarter circle
  159. AssertBox(15, 30, 10, 10, 3);
  160. }
  161. void GCDCBoundingBoxTestCase::DrawEllipticArc()
  162. {
  163. m_gcdc->DrawEllipticArc(40, 50, 30, 20, 0, 180); // half circle
  164. AssertBox(40, 50, 30, 10, 3);
  165. }
  166. void GCDCBoundingBoxTestCase::DrawPoint()
  167. {
  168. m_gcdc->DrawPoint(20, 20);
  169. AssertBox(20, 20, 1, 1);
  170. }
  171. void GCDCBoundingBoxTestCase::DrawLines()
  172. {
  173. wxPoint points[4];
  174. points[0] = wxPoint(10, 20);
  175. points[1] = wxPoint(20, 10);
  176. points[2] = wxPoint(30, 20);
  177. points[3] = wxPoint(20, 30);
  178. m_gcdc->DrawLines(4, points, 7, 8);
  179. AssertBox(17, 18, 20, 20);
  180. }
  181. #if wxUSE_SPLINES
  182. void GCDCBoundingBoxTestCase::DrawSpline()
  183. {
  184. wxPoint points[3];
  185. points[0] = wxPoint(10, 30);
  186. points[1] = wxPoint(20, 20);
  187. points[2] = wxPoint(40, 50);
  188. m_gcdc->DrawSpline(3, points);
  189. AssertBox(10, 20, 30, 30, 5);
  190. }
  191. #endif // wxUSE_SPLINES
  192. void GCDCBoundingBoxTestCase::DrawPolygon()
  193. {
  194. wxPoint points[3];
  195. points[0] = wxPoint(10, 30);
  196. points[1] = wxPoint(20, 10);
  197. points[2] = wxPoint(30, 30);
  198. m_gcdc->DrawPolygon(3, points, -5, -7);
  199. AssertBox(5, 3, 20, 20);
  200. }
  201. void GCDCBoundingBoxTestCase::DrawPolyPolygon()
  202. {
  203. int lenghts[2];
  204. lenghts[0] = 3;
  205. lenghts[1] = 3;
  206. wxPoint points[6];
  207. points[0] = wxPoint(10, 30);
  208. points[1] = wxPoint(20, 10);
  209. points[2] = wxPoint(30, 30);
  210. points[3] = wxPoint(20, 60);
  211. points[4] = wxPoint(30, 40);
  212. points[5] = wxPoint(40, 60);
  213. m_gcdc->DrawPolyPolygon(2, lenghts, points, 12, 5);
  214. AssertBox(22, 15, 30, 50, 4);
  215. }
  216. void GCDCBoundingBoxTestCase::DrawRectangle()
  217. {
  218. m_gcdc->DrawRectangle(2, 2, 12, 12);
  219. AssertBox(2, 2, 12, 12);
  220. }
  221. void GCDCBoundingBoxTestCase::DrawRoundedRectangle()
  222. {
  223. m_gcdc->DrawRoundedRectangle(27, 27, 12, 12, 2);
  224. AssertBox(27, 27, 12, 12);
  225. }
  226. void GCDCBoundingBoxTestCase::DrawEllipse()
  227. {
  228. m_gcdc->DrawEllipse(54, 45, 23, 12);
  229. AssertBox(54, 45, 23, 12);
  230. }
  231. void GCDCBoundingBoxTestCase::Blit()
  232. {
  233. wxBitmap bitmap;
  234. bitmap.Create(20, 20);
  235. wxMemoryDC dc(bitmap);
  236. m_gcdc->Blit(20, 10, 12, 7, &dc, 0, 0);
  237. AssertBox(20, 10, 12, 7);
  238. dc.SelectObject(wxNullBitmap);
  239. }
  240. void GCDCBoundingBoxTestCase::StretchBlit()
  241. {
  242. wxBitmap bitmap;
  243. bitmap.Create(20, 20);
  244. wxMemoryDC dc(bitmap);
  245. m_gcdc->StretchBlit(30, 50, 5, 5, &dc, 0, 0, 12, 4);
  246. AssertBox(30, 50, 5, 5);
  247. dc.SelectObject(wxNullBitmap);
  248. }
  249. void GCDCBoundingBoxTestCase::DrawRotatedText()
  250. {
  251. wxString text("vertical");
  252. wxCoord w, h;
  253. m_gcdc->GetTextExtent(text, &w, &h);
  254. m_gcdc->DrawRotatedText(text, 43, 22, -90);
  255. AssertBox(43 - h, 22, h, w, 3);
  256. }
  257. void GCDCBoundingBoxTestCase::DrawText()
  258. {
  259. wxString text("H");
  260. wxCoord w, h;
  261. m_gcdc->GetTextExtent(text, &w, &h);
  262. m_gcdc->DrawText(text, 3, 3);
  263. AssertBox(3, 3, w, h, 3);
  264. }
  265. void GCDCBoundingBoxTestCase::GradientFillLinear()
  266. {
  267. wxRect rect(16, 16, 30, 40);
  268. m_gcdc->GradientFillLinear(rect, *wxWHITE, *wxBLACK, wxNORTH);
  269. AssertBox(16, 16, 30, 40);
  270. }
  271. void GCDCBoundingBoxTestCase::GradientFillConcentric()
  272. {
  273. wxRect rect(6, 6, 30, 40);
  274. m_gcdc->GradientFillConcentric(rect, *wxWHITE, *wxBLACK, wxPoint(10, 10));
  275. AssertBox(6, 6, 30, 40);
  276. }
  277. void GCDCBoundingBoxTestCase::DrawCheckMark()
  278. {
  279. m_gcdc->DrawCheckMark(32, 24, 16, 16);
  280. AssertBox(32, 24, 16, 16);
  281. }