Region.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using NUnit.Framework;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using DrawingTestHelper;
  6. namespace Test.Sys.Drawing {
  7. [TestFixture]
  8. public class RegionFixture {
  9. DrawingTest t;
  10. RectangleF rect;
  11. Region r;
  12. [SetUp]
  13. public void SetUp ()
  14. {
  15. t = DrawingTest.Create (1000, 1000);
  16. rect = new RectangleF (50, 50, 50, 50);
  17. r = new Region(rect);
  18. }
  19. [Test]
  20. public void ctor_RegionData () {
  21. RegionData rgnData = r.GetRegionData ();
  22. Region r1 = new Region (rgnData);
  23. Assert.AreEqual (rgnData.Data, r1.GetRegionData ().Data);
  24. }
  25. [Test]
  26. public void ctor_GraphicsPath () {
  27. GraphicsPath path = new GraphicsPath ();
  28. path.AddRectangle (rect);
  29. Region r1 = new Region (path);
  30. r1.Xor (r);
  31. Assert.IsTrue (r1.IsEmpty (t.Graphics));
  32. }
  33. [Test]
  34. public void ctor_Rectangle () {
  35. Region r1 = new Region (new Rectangle ((int)rect.X, (int)rect.Y,
  36. (int)rect.Width, (int)rect.Height));
  37. Assert.AreEqual (r.GetRegionData ().Data,
  38. r1.GetRegionData ().Data);
  39. }
  40. [Test]
  41. public void ctor_RectangleF () {
  42. Region r1 = new Region (rect);
  43. Assert.AreEqual (r.GetRegionData ().Data,
  44. r1.GetRegionData ().Data);
  45. }
  46. [Test]
  47. public void ctor_void () {
  48. Region r1 = new Region ();
  49. Assert.IsTrue (r1.IsInfinite (t.Graphics));
  50. }
  51. [Test]
  52. public void Clone () {
  53. Region r1 = r.Clone ();
  54. Assert.IsFalse (Object.ReferenceEquals (r1, r));
  55. Assert.AreEqual (r1.GetBounds (t.Graphics), r.GetBounds (t.Graphics));
  56. r1.Xor (r);
  57. Assert.IsTrue (r1.IsEmpty (t.Graphics));
  58. Assert.IsFalse (r.IsEmpty (t.Graphics));
  59. }
  60. [Test]
  61. public void Complement () {
  62. r.Complement (new Rectangle (70, 70, 80, 20));
  63. Assert.AreEqual (new RectangleF (100, 70, 50, 20),
  64. r.GetBounds (t.Graphics));
  65. }
  66. [Test]
  67. public void Equals_Graphics () {
  68. Region r1 = new Region (rect);
  69. Assert.IsTrue (r.Equals (r1, t.Graphics));
  70. }
  71. [Test]
  72. public void Exclude () {
  73. r.Exclude (new Rectangle (10, 10, 90, 60));
  74. Assert.AreEqual (new RectangleF (50, 70, 50, 30),
  75. r.GetBounds (t.Graphics));
  76. }
  77. [Test]
  78. public void GetBounds ()
  79. {
  80. Assert.AreEqual (rect, r.GetBounds (t.Graphics));
  81. }
  82. [Test]
  83. public void GetRegionData () {
  84. byte [] actual = r.GetRegionData ().Data;
  85. byte [] expected = new byte [] {28, 0, 0, 0, 186, 15, 11, 58, 1, 16,
  86. 192, 219, 0, 0, 0, 0, 0, 0, 0, 16,
  87. 0, 0, 72, 66, 0, 0, 72, 66, 0, 0,
  88. 72, 66, 0, 0, 72, 66};
  89. Assert.AreEqual (expected, actual);
  90. }
  91. [Test]
  92. public void GetRegionScans () {
  93. Assert.AreEqual (new RectangleF [] {new Rectangle (50, 50, 50, 50)},
  94. r.GetRegionScans (new Matrix ()));
  95. r.Union (new Rectangle (100, 100, 50, 50));
  96. RectangleF [] rs = new RectangleF [] {
  97. new Rectangle (50, 50, 50, 50),
  98. new Rectangle (100, 100, 50, 50)};
  99. Assert.AreEqual (rs,
  100. r.GetRegionScans (new Matrix ()));
  101. }
  102. [Test]
  103. public void Intersect () {
  104. r.Intersect (new Rectangle (70, 70, 50, 50));
  105. Assert.AreEqual (new RectangleF (70, 70, 30, 30),
  106. r.GetBounds (t.Graphics));
  107. }
  108. [Test]
  109. public void IsEmpty () {
  110. Assert.IsFalse (r.IsEmpty (t.Graphics));
  111. r.Xor (r.Clone ());
  112. Assert.IsTrue (r.IsEmpty (t.Graphics));
  113. }
  114. [Test]
  115. public void IsInfinite () {
  116. Assert.IsFalse (r.IsInfinite (t.Graphics));
  117. r.MakeInfinite ();
  118. Assert.IsTrue (r.IsInfinite (t.Graphics));
  119. RectangleF infiniteRect = new RectangleF (-0x400000, -0x400000, 0x800000, 0x800000);
  120. Assert.AreEqual (new RectangleF [] {infiniteRect},
  121. r.GetRegionScans (new Matrix ()));
  122. r.Exclude (new Rectangle (10, 10, 10, 10));
  123. Assert.IsFalse (r.IsInfinite (t.Graphics));
  124. Assert.AreEqual (infiniteRect, r.GetBounds (t.Graphics));
  125. }
  126. [Test]
  127. public void IsVisible_int () {
  128. Rectangle rectD = new Rectangle (50, 50, 10000, 10000);
  129. r = new Region (rectD);
  130. Assert.IsTrue (r.IsVisible (rectD, t.Graphics));
  131. Assert.IsTrue (r.IsVisible (rectD.X, rectD.Y, rectD.Height, rectD.Width,
  132. t.Graphics));
  133. Assert.IsTrue (r.IsVisible (rectD));
  134. Assert.IsTrue (r.IsVisible (rectD.X, rectD.Y, rectD.Height, rectD.Width));
  135. Assert.IsFalse (r.IsVisible (new Point (rectD.Right, rectD.Bottom),
  136. t.Graphics));
  137. Assert.IsTrue (r.IsVisible (new Point (rectD.X, rectD.Y)));
  138. Assert.IsFalse (r.IsVisible (new Point (rectD.X-1, rectD.Y-1)));
  139. Assert.IsTrue (r.IsVisible (100, 100, t.Graphics));
  140. Assert.IsTrue (r.IsVisible (100, 100));
  141. }
  142. [Test]
  143. [Category ("NotWorking")]
  144. public void IsVisible_float () {
  145. t.Graphics.PageScale = 10;
  146. rect = new Rectangle (50, 50, 1000, 1000);
  147. r = new Region (rect);
  148. Assert.IsTrue (r.IsVisible (rect, t.Graphics));
  149. Assert.IsTrue (r.IsVisible (rect.X, rect.Y, rect.Height, rect.Width,
  150. t.Graphics));
  151. Assert.IsTrue (r.IsVisible (rect));
  152. Assert.IsTrue (r.IsVisible (rect.X, rect.Y, rect.Height, rect.Width));
  153. Assert.IsFalse (r.IsVisible (new PointF (rect.Right, rect.Bottom),
  154. t.Graphics));
  155. Assert.IsFalse (r.IsVisible (new PointF (rect.Right, rect.Bottom)));
  156. Assert.IsFalse (r.IsVisible (new PointF (rect.Right-0.1F,
  157. rect.Bottom-0.1F)));
  158. Assert.IsTrue (r.IsVisible (new PointF (rect.Right-1,
  159. rect.Bottom-1)));
  160. Assert.IsTrue (r.IsVisible (100.0F, 100.0F, t.Graphics));
  161. Assert.IsTrue (r.IsVisible (100.0F, 100.0F));
  162. Assert.IsTrue (r.IsVisible (new PointF (rect.X, rect.Y)));
  163. Assert.IsFalse (r.IsVisible (new PointF (rect.X-0.4F,
  164. rect.Y-0.4F)));
  165. }
  166. [Test]
  167. public void MakeEmpty () {
  168. Assert.IsFalse (r.IsEmpty (t.Graphics));
  169. r.MakeEmpty ();
  170. Assert.IsTrue (r.IsEmpty (t.Graphics));
  171. Assert.AreEqual (new RectangleF (0,0,0,0), r.GetBounds (t.Graphics));
  172. Assert.IsFalse (r.IsVisible (new Rectangle (0, 0, 0, 0)));
  173. }
  174. [Test]
  175. public void MakeInfinite () {
  176. Assert.IsFalse (r.IsInfinite (t.Graphics));
  177. r.MakeInfinite ();
  178. Assert.IsTrue (r.IsInfinite (t.Graphics));
  179. RectangleF infiniteRect = new RectangleF (-0x400000, -0x400000, 0x800000, 0x800000);
  180. Assert.AreEqual (new RectangleF [] {infiniteRect},
  181. r.GetRegionScans (new Matrix ()));
  182. }
  183. [Test]
  184. public new void ToString () {
  185. Assert.AreEqual ("System.Drawing.Region", r.ToString ());
  186. }
  187. [Test]
  188. public void Transform () {
  189. }
  190. [Test]
  191. public void Translate () {
  192. }
  193. [Test]
  194. public void Union () {
  195. r.Union (new Rectangle (70, 70, 100, 100));
  196. RectangleF [] rs = new RectangleF [] {
  197. new RectangleF (50, 50, 50, 20),
  198. new RectangleF (50, 70, 120, 30),
  199. new RectangleF (70, 100, 100, 70)};
  200. Assert.AreEqual (rs, r.GetRegionScans (new Matrix()));
  201. }
  202. [Test]
  203. public void Xor () {
  204. r.Xor (new Rectangle (0, 0, 70, 70));
  205. RectangleF [] rs = new RectangleF [] {
  206. new RectangleF (0, 0, 70, 50),
  207. new RectangleF (0, 50, 50, 20),
  208. new RectangleF (70, 50, 30, 20),
  209. new RectangleF (50, 70, 50, 30)};
  210. Assert.AreEqual (rs, r.GetRegionScans (new Matrix()));
  211. }
  212. }
  213. }