PictureBoxTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. //
  2. // PictureBoxTest.cs: Test cases for PictureBox.
  3. //
  4. // Author:
  5. // Ritvik Mayank ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc. (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Threading;
  14. using System.Windows.Forms;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Windows.Forms
  17. {
  18. [TestFixture]
  19. public class PictureBoxTest : TestHelper
  20. {
  21. [Test]
  22. public void PictureBoxPropertyTest ()
  23. {
  24. Form myForm = new Form ();
  25. myForm.ShowInTaskbar = false;
  26. PictureBox myPicBox = new PictureBox ();
  27. myForm.Controls.Add (myPicBox);
  28. // B
  29. Assert.AreEqual (BorderStyle.None, myPicBox.BorderStyle, "#B1");
  30. myPicBox.BorderStyle = BorderStyle.Fixed3D;
  31. Assert.AreEqual (BorderStyle.Fixed3D, myPicBox.BorderStyle, "#B2");
  32. // P
  33. Assert.AreEqual (PictureBoxSizeMode.Normal, myPicBox.SizeMode, "#P1");
  34. myPicBox.SizeMode = PictureBoxSizeMode.AutoSize;
  35. Assert.AreEqual (PictureBoxSizeMode.AutoSize, myPicBox.SizeMode, "#P2");
  36. myForm.Dispose ();
  37. }
  38. #if NET_2_0
  39. [Test]
  40. [Category ("NotWorking")]
  41. public void ImageLocation_Async ()
  42. {
  43. Form f = new Form ();
  44. PictureBox pb = new PictureBox ();
  45. f.Controls.Add (pb);
  46. f.Show ();
  47. Assert.IsNull (pb.ImageLocation, "#A");
  48. pb.ImageLocation = "M.gif";
  49. Application.DoEvents ();
  50. Assert.AreEqual ("M.gif", pb.ImageLocation, "#B1");
  51. Assert.AreSame (pb.InitialImage, pb.Image, "#B2");
  52. using (Stream s = this.GetType ().Assembly.GetManifestResourceStream ("32x32.ico")) {
  53. pb.Image = Image.FromStream (s);
  54. }
  55. Application.DoEvents ();
  56. Assert.AreEqual ("M.gif", pb.ImageLocation, "#C1");
  57. Assert.IsNotNull (pb.Image, "#C2");
  58. Assert.AreEqual (60, pb.Image.Height, "#C3");
  59. Assert.AreEqual (150, pb.Image.Width, "#C4");
  60. pb.ImageLocation = null;
  61. Application.DoEvents ();
  62. Assert.IsNull (pb.ImageLocation, "#D1");
  63. Assert.IsNull (pb.Image, "#D2");
  64. pb.ImageLocation = "M.gif";
  65. Application.DoEvents ();
  66. Assert.AreEqual ("M.gif", pb.ImageLocation, "#E1");
  67. Assert.IsNull (pb.Image, "#E2");
  68. pb.Load ();
  69. Application.DoEvents ();
  70. Assert.AreEqual ("M.gif", pb.ImageLocation, "#F1");
  71. Assert.IsNotNull (pb.Image, "#F2");
  72. Assert.AreEqual (60, pb.Image.Height, "#F3");
  73. Assert.AreEqual (150, pb.Image.Width, "#F4");
  74. pb.ImageLocation = null;
  75. Application.DoEvents ();
  76. Assert.IsNull (pb.ImageLocation, "#G1");
  77. Assert.IsNull (pb.Image, "#G2");
  78. pb.ImageLocation = "M.gif";
  79. pb.Load ();
  80. pb.ImageLocation = "XYZ.gif";
  81. Application.DoEvents ();
  82. Assert.AreEqual ("XYZ.gif", pb.ImageLocation, "#H1");
  83. Assert.IsNotNull (pb.Image, "#H2");
  84. Assert.AreEqual (60, pb.Image.Height, "#H3");
  85. Assert.AreEqual (150, pb.Image.Width, "#H4");
  86. pb.ImageLocation = string.Empty;
  87. Application.DoEvents ();
  88. Assert.AreEqual (string.Empty, pb.ImageLocation, "#I1");
  89. Assert.IsNull (pb.Image, "#I2");
  90. using (Stream s = this.GetType ().Assembly.GetManifestResourceStream ("32x32.ico")) {
  91. pb.Image = Image.FromStream (s);
  92. }
  93. Application.DoEvents ();
  94. Assert.AreEqual (string.Empty, pb.ImageLocation, "#J1");
  95. Assert.IsNotNull (pb.Image, "#J2");
  96. Assert.AreEqual (96, pb.Image.Height, "#J3");
  97. Assert.AreEqual (96, pb.Image.Width, "#J4");
  98. pb.Load ("M.gif");
  99. Application.DoEvents ();
  100. Assert.AreEqual ("M.gif", pb.ImageLocation, "#K1");
  101. Assert.IsNotNull (pb.Image, "#K2");
  102. Assert.AreEqual (60, pb.Image.Height, "#K3");
  103. Assert.AreEqual (150, pb.Image.Width, "#K4");
  104. pb.ImageLocation = null;
  105. Application.DoEvents ();
  106. Assert.IsNull (pb.ImageLocation, "#L1");
  107. Assert.IsNull (pb.Image, "#L2");
  108. f.Dispose ();
  109. }
  110. [Test]
  111. public void ImageLocation_Sync ()
  112. {
  113. Form f = new Form ();
  114. PictureBox pb = new PictureBox ();
  115. pb.WaitOnLoad = true;
  116. f.Controls.Add (pb);
  117. f.Show ();
  118. Assert.IsNull (pb.ImageLocation, "#A");
  119. pb.ImageLocation = "M.gif";
  120. Assert.AreEqual ("M.gif", pb.ImageLocation, "#B1");
  121. Assert.IsNotNull (pb.Image, "#B2");
  122. Assert.AreEqual (60, pb.Image.Height, "#B3");
  123. Assert.AreEqual (150, pb.Image.Width, "#B4");
  124. using (Stream s = this.GetType ().Assembly.GetManifestResourceStream ("32x32.ico")) {
  125. pb.Image = Image.FromStream (s);
  126. }
  127. Assert.AreEqual ("M.gif", pb.ImageLocation, "#C1");
  128. Assert.IsNotNull (pb.Image, "#C2");
  129. Assert.AreEqual (96, pb.Image.Height, "#C3");
  130. Assert.AreEqual (96, pb.Image.Width, "#C4");
  131. pb.ImageLocation = null;
  132. Assert.IsNull (pb.ImageLocation, "#D1");
  133. Assert.IsNotNull (pb.Image, "#D2");
  134. Assert.AreEqual (96, pb.Image.Height, "#D3");
  135. Assert.AreEqual (96, pb.Image.Width, "#D4");
  136. pb.ImageLocation = "M.gif";
  137. Assert.AreEqual ("M.gif", pb.ImageLocation, "#E1");
  138. Assert.IsNotNull (pb.Image, "#E2");
  139. Assert.AreEqual (60, pb.Image.Height, "#E3");
  140. Assert.AreEqual (150, pb.Image.Width, "#E4");
  141. pb.Load ();
  142. Assert.AreEqual ("M.gif", pb.ImageLocation, "#F1");
  143. Assert.IsNotNull (pb.Image, "#F2");
  144. Assert.AreEqual (60, pb.Image.Height, "#F3");
  145. Assert.AreEqual (150, pb.Image.Width, "#F4");
  146. pb.ImageLocation = null;
  147. Assert.IsNull (pb.ImageLocation, "#G1");
  148. Assert.IsNull (pb.Image, "#G2");
  149. using (Stream s = this.GetType ().Assembly.GetManifestResourceStream ("32x32.ico")) {
  150. pb.Image = Image.FromStream (s);
  151. }
  152. Assert.IsNull (pb.ImageLocation, "#H1");
  153. Assert.IsNotNull (pb.Image, "#H2");
  154. Assert.AreEqual (96, pb.Image.Height, "#H3");
  155. Assert.AreEqual (96, pb.Image.Width, "#H4");
  156. pb.Load ("M.gif");
  157. Assert.AreEqual ("M.gif", pb.ImageLocation, "#I1");
  158. Assert.IsNotNull (pb.Image, "#I2");
  159. Assert.AreEqual (60, pb.Image.Height, "#I3");
  160. Assert.AreEqual (150, pb.Image.Width, "#I4");
  161. pb.ImageLocation = string.Empty;
  162. Assert.AreEqual (string.Empty, pb.ImageLocation, "#J1");
  163. Assert.IsNull (pb.Image, "#J2");
  164. pb.ImageLocation = "M.gif";
  165. Assert.AreEqual ("M.gif", pb.ImageLocation, "#K1");
  166. Assert.IsNotNull (pb.Image, "#K2");
  167. Assert.AreEqual (60, pb.Image.Height, "#K3");
  168. Assert.AreEqual (150, pb.Image.Width, "#K4");
  169. try {
  170. pb.ImageLocation = "XYZ.gif";
  171. Assert.Fail ("#L1");
  172. } catch (FileNotFoundException ex) {
  173. Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#L2");
  174. Assert.IsNull (ex.InnerException, "#L3");
  175. Assert.IsNotNull (ex.Message, "#L4");
  176. }
  177. Assert.AreEqual ("XYZ.gif", pb.ImageLocation, "#M1");
  178. Assert.IsNotNull (pb.Image, "#M2");
  179. Assert.AreEqual (60, pb.Image.Height, "#M3");
  180. Assert.AreEqual (150, pb.Image.Width, "#M4");
  181. f.Dispose ();
  182. }
  183. #endif
  184. [Test]
  185. public void ImagePropertyTest ()
  186. {
  187. PictureBox myPicBox = new PictureBox ();
  188. // I
  189. Assert.IsNull (myPicBox.Image, "#1");
  190. Image myImage = Image.FromFile ("M.gif");
  191. myPicBox.Image = myImage;
  192. Assert.AreSame (myImage, myPicBox.Image, "#2");
  193. Assert.AreEqual (60, myPicBox.Image.Height, "#3");
  194. Assert.AreEqual (150, myPicBox.Image.Width, "#4");
  195. myPicBox.Image = null;
  196. Assert.IsNull (myPicBox.Image, "#5");
  197. myPicBox.Image = null;
  198. Assert.IsNull (myPicBox.Image, "#6");
  199. }
  200. #if NET_2_0
  201. [Test] // Load ()
  202. public void Load_ImageLocation_Empty ()
  203. {
  204. PictureBox pb = new PictureBox ();
  205. pb.ImageLocation = string.Empty;
  206. try {
  207. pb.Load ();
  208. Assert.Fail ("#1");
  209. } catch (InvalidOperationException ex) {
  210. // ImageLocation must be set
  211. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  212. Assert.IsNull (ex.InnerException, "#3");
  213. Assert.IsNotNull (ex.Message, "#4");
  214. }
  215. }
  216. [Test] // Load ()
  217. public void Load_ImageLocation_Null ()
  218. {
  219. PictureBox pb = new PictureBox ();
  220. pb.ImageLocation = null;
  221. try {
  222. pb.Load ();
  223. Assert.Fail ("#1");
  224. } catch (InvalidOperationException ex) {
  225. // ImageLocation must be set
  226. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  227. Assert.IsNull (ex.InnerException, "#3");
  228. Assert.IsNotNull (ex.Message, "#4");
  229. }
  230. }
  231. [Test] // Load (String)
  232. public void Load2_Url_Empty ()
  233. {
  234. PictureBox pb = new PictureBox ();
  235. try {
  236. pb.Load (string.Empty);
  237. Assert.Fail ("#1");
  238. } catch (InvalidOperationException ex) {
  239. // ImageLocation must be set
  240. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  241. Assert.IsNull (ex.InnerException, "#3");
  242. Assert.IsNotNull (ex.Message, "#4");
  243. }
  244. }
  245. [Test] // Load (String)
  246. public void Load2_Url_Null ()
  247. {
  248. PictureBox pb = new PictureBox ();
  249. try {
  250. pb.Load ((string) null);
  251. Assert.Fail ("#1");
  252. } catch (InvalidOperationException ex) {
  253. // ImageLocation must be set
  254. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  255. Assert.IsNull (ex.InnerException, "#3");
  256. Assert.IsNotNull (ex.Message, "#4");
  257. }
  258. }
  259. [Test] // LoadAsync ()
  260. public void LoadAsync1_ImageLocation_Empty ()
  261. {
  262. PictureBox pb = new PictureBox ();
  263. pb.ImageLocation = string.Empty;
  264. try {
  265. pb.LoadAsync ();
  266. Assert.Fail ("#1");
  267. } catch (InvalidOperationException ex) {
  268. // ImageLocation must be set
  269. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  270. Assert.IsNull (ex.InnerException, "#3");
  271. Assert.IsNotNull (ex.Message, "#4");
  272. }
  273. }
  274. [Test] // LoadAsync ()
  275. public void LoadAsync1_ImageLocation_Null ()
  276. {
  277. PictureBox pb = new PictureBox ();
  278. pb.ImageLocation = null;
  279. try {
  280. pb.LoadAsync ();
  281. Assert.Fail ("#1");
  282. } catch (InvalidOperationException ex) {
  283. // ImageLocation must be set
  284. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  285. Assert.IsNull (ex.InnerException, "#3");
  286. Assert.IsNotNull (ex.Message, "#4");
  287. }
  288. }
  289. [Test] // LoadAsync (String)
  290. public void LoadASync2_Url_Empty ()
  291. {
  292. PictureBox pb = new PictureBox ();
  293. try {
  294. pb.LoadAsync (string.Empty);
  295. Assert.Fail ("#1");
  296. } catch (InvalidOperationException ex) {
  297. // ImageLocation must be set
  298. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  299. Assert.IsNull (ex.InnerException, "#3");
  300. Assert.IsNotNull (ex.Message, "#4");
  301. }
  302. }
  303. [Test] // LoadAsync (String)
  304. public void LoadAsync2_Url_Null ()
  305. {
  306. PictureBox pb = new PictureBox ();
  307. try {
  308. pb.LoadAsync ((string) null);
  309. Assert.Fail ("#1");
  310. } catch (InvalidOperationException ex) {
  311. // ImageLocation must be set
  312. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  313. Assert.IsNull (ex.InnerException, "#3");
  314. Assert.IsNotNull (ex.Message, "#4");
  315. }
  316. }
  317. #endif
  318. [Test]
  319. public void ToStringMethodTest ()
  320. {
  321. PictureBox myPicBox = new PictureBox ();
  322. Assert.AreEqual ("System.Windows.Forms.PictureBox, SizeMode: Normal", myPicBox.ToString (), "#T1");
  323. }
  324. #if NET_2_0
  325. [Test]
  326. public void Defaults ()
  327. {
  328. PictureBox pb = new PictureBox ();
  329. Assert.IsNotNull (pb.ErrorImage, "A1");
  330. Assert.AreEqual (false, pb.WaitOnLoad, "A2");
  331. Assert.AreEqual (false, pb.AutoSize, "A3");
  332. pb.SizeMode = PictureBoxSizeMode.AutoSize;
  333. Assert.AreEqual (true, pb.AutoSize, "A4");
  334. }
  335. #endif
  336. [TestFixture]
  337. public class PictureBoxSizeModeEventClass : TestHelper
  338. {
  339. static bool eventhandled = false;
  340. public static void SizeMode_EventHandler (object sender, EventArgs e)
  341. {
  342. eventhandled = true;
  343. }
  344. [Test]
  345. public void PictureBoxEventTest ()
  346. {
  347. Form myForm = new Form ();
  348. myForm.ShowInTaskbar = false;
  349. PictureBox myPicBox = new PictureBox ();
  350. myForm.Controls.Add (myPicBox);
  351. myPicBox.SizeModeChanged += new EventHandler (SizeMode_EventHandler);
  352. myPicBox.SizeMode = PictureBoxSizeMode.AutoSize;
  353. Assert.AreEqual (true, eventhandled, "#SM1");
  354. eventhandled = false;
  355. myPicBox.SizeMode = PictureBoxSizeMode.CenterImage;
  356. Assert.AreEqual (true, eventhandled, "#SM2");
  357. eventhandled = false;
  358. myPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
  359. Assert.AreEqual (true, eventhandled, "#SM3");
  360. myForm.Dispose ();
  361. }
  362. }
  363. }
  364. }