TestFont.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. //
  2. // Test Font class testing unit
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernandez, [email protected]
  6. // Peter Dennis Bartok, [email protected]
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // (C) 2003 Ximian, Inc. http://www.ximian.com
  10. // Copyright (C) 2004-2007 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using NUnit.Framework;
  32. using System;
  33. using System.Drawing;
  34. using System.Drawing.Text;
  35. using System.Security;
  36. using System.Security.Permissions;
  37. using System.Runtime.InteropServices;
  38. namespace MonoTests.System.Drawing{
  39. [TestFixture]
  40. public class FontTest {
  41. private string name;
  42. [TestFixtureSetUp]
  43. public void FixtureSetUp ()
  44. {
  45. try {
  46. using (FontFamily ff = new FontFamily (GenericFontFamilies.Monospace)) {
  47. name = ff.Name;
  48. }
  49. }
  50. catch (ArgumentException) {
  51. Assert.Ignore ("No font family could be found.");
  52. }
  53. }
  54. // Test basic Font clone, properties and contructor
  55. [Test]
  56. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  57. public void TestClone()
  58. {
  59. Font f = new Font("Arial",12);
  60. Font f2 = (Font) f.Clone();
  61. Assert.AreEqual (f.Bold, f2.Bold, "Bold");
  62. Assert.AreEqual (f.FontFamily, f2.FontFamily, "FontFamily");
  63. Assert.AreEqual (f.GdiCharSet, f2.GdiCharSet, "GdiCharSet");
  64. Assert.AreEqual (f.GdiVerticalFont, f2.GdiVerticalFont, "GdiVerticalFont");
  65. Assert.AreEqual (f.Height, f2.Height, "Height");
  66. Assert.AreEqual (f.Italic, f2.Italic, "Italic");
  67. Assert.AreEqual (f.Name, f2.Name, "Name");
  68. Assert.AreEqual (f.Size, f2.Size, "Size");
  69. Assert.AreEqual (f.SizeInPoints, f2.SizeInPoints, "SizeInPoints");
  70. Assert.AreEqual (f.Strikeout, f2.Strikeout, "Strikeout");
  71. Assert.AreEqual (f.Style, f2.Style, "Style");
  72. Assert.AreEqual (f.Underline, f2.Underline, "Underline");
  73. Assert.AreEqual (f.Unit, f2.Unit, "Unit");
  74. }
  75. [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
  76. class LOGFONT {
  77. public int lfHeight;
  78. public int lfWidth;
  79. public int lfEscapement;
  80. public int lfOrientation;
  81. public int lfWeight;
  82. public byte lfItalic;
  83. public byte lfUnderline;
  84. public byte lfStrikeOut;
  85. public byte lfCharSet;
  86. public byte lfOutPrecision;
  87. public byte lfClipPrecision;
  88. public byte lfQuality;
  89. public byte lfPitchAndFamily;
  90. [ MarshalAs(UnmanagedType.ByValTStr, SizeConst=32) ]
  91. public string lfFaceName;
  92. }
  93. [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Auto)]
  94. struct LOGFONT_STRUCT {
  95. public int lfHeight;
  96. public int lfWidth;
  97. public int lfEscapement;
  98. public int lfOrientation;
  99. public int lfWeight;
  100. public byte lfItalic;
  101. public byte lfUnderline;
  102. public byte lfStrikeOut;
  103. public byte lfCharSet;
  104. public byte lfOutPrecision;
  105. public byte lfClipPrecision;
  106. public byte lfQuality;
  107. public byte lfPitchAndFamily;
  108. [MarshalAs (UnmanagedType.ByValTStr, SizeConst = 32)]
  109. public string lfFaceName;
  110. }
  111. [Test]
  112. [Category ("CAS")]
  113. [ExpectedException (typeof (SecurityException))]
  114. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  115. public void ToLogFont_DenyUnmanagedCode ()
  116. {
  117. Font f;
  118. LOGFONT lf;
  119. lf = new LOGFONT();
  120. f = new Font("Arial", 10);
  121. f.ToLogFont(lf);
  122. }
  123. [Test]
  124. [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
  125. public void ToLogFont_AssertUnmanagedCode ()
  126. {
  127. Font f = new Font("Arial", 10);
  128. LOGFONT lf = new LOGFONT();
  129. f.ToLogFont (lf);
  130. Assert.AreEqual (400, lf.lfWeight, "lfWeight");
  131. Assert.AreEqual (1, lf.lfCharSet, "lfCharSet");
  132. Assert.AreEqual (f.Name, lf.lfFaceName, "lfFaceName");
  133. LOGFONT_STRUCT lfs = new LOGFONT_STRUCT ();
  134. f.ToLogFont (lfs);
  135. Assert.AreEqual (0, lfs.lfWeight, "struct-lfWeight");
  136. Assert.AreEqual (0, lfs.lfCharSet, "struct-lfCharSet");
  137. Assert.AreEqual (0, lfs.lfHeight, "struct-lfHeight");
  138. Assert.AreEqual (0, lfs.lfWidth, "struct-lfWidth");
  139. Assert.AreEqual (0, lfs.lfEscapement, "struct-lfEscapement");
  140. Assert.AreEqual (0, lfs.lfOrientation, "struct-lfOrientation");
  141. Assert.AreEqual (0, lfs.lfWeight, "struct-lfWeight");
  142. Assert.AreEqual (0, lfs.lfItalic, "struct-lfItalic");
  143. Assert.AreEqual (0, lfs.lfUnderline, "struct-lfUnderline");
  144. Assert.AreEqual (0, lfs.lfStrikeOut, "struct-lfStrikeOut");
  145. Assert.AreEqual (0, lfs.lfCharSet, "struct-lfCharSet");
  146. Assert.AreEqual (0, lfs.lfOutPrecision, "struct-lfOutPrecision");
  147. Assert.AreEqual (0, lfs.lfClipPrecision, "struct-lfClipPrecision");
  148. Assert.AreEqual (0, lfs.lfQuality, "struct-lfQuality");
  149. Assert.AreEqual (0, lfs.lfPitchAndFamily, "struct-lfPitchAndFamily");
  150. Assert.IsNull (lfs.lfFaceName, "struct-lfFaceName");
  151. }
  152. [Test]
  153. [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
  154. [ExpectedException (typeof (ArgumentException))]
  155. public void ToLogFont_TooSmall ()
  156. {
  157. Font f = new Font ("Arial", 10);
  158. object o = new object ();
  159. f.ToLogFont (o);
  160. // no PInvoke conversion exists !?!?
  161. }
  162. [Test]
  163. [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
  164. public void ToLogFont_Int ()
  165. {
  166. Font f = new Font ("Arial", 10);
  167. int i = 1;
  168. f.ToLogFont (i);
  169. Assert.AreEqual (1, i);
  170. }
  171. [Test]
  172. [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
  173. [ExpectedException (typeof (AccessViolationException))]
  174. public void ToLogFont_Null ()
  175. {
  176. Font f = new Font ("Arial", 10);
  177. f.ToLogFont (null);
  178. }
  179. [Test]
  180. public void Font_StringNull_Float ()
  181. {
  182. string family = null;
  183. Font f = new Font (family, 12.5f);
  184. Assert.AreEqual (FontFamily.GenericSansSerif, f.FontFamily, "FontFamily");
  185. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  186. Assert.AreEqual (12.5f, f.Size, "Size");
  187. Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
  188. Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
  189. }
  190. [Test]
  191. public void Font_String_Float ()
  192. {
  193. Font f = new Font (name, 12.5f);
  194. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  195. Assert.IsFalse (f.Bold, "Bold");
  196. Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
  197. Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
  198. Assert.IsTrue (f.Height > 0, "Height");
  199. Assert.IsFalse (f.Italic, "Italic");
  200. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  201. Assert.AreEqual (12.5f, f.Size, "Size");
  202. Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
  203. Assert.IsFalse (f.Strikeout, "Strikeout");
  204. Assert.IsFalse (f.Underline, "Underline");
  205. Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
  206. }
  207. [Test]
  208. public void Font_String_Float_FontStyle ()
  209. {
  210. Font f = new Font (name, 12.5f, FontStyle.Bold);
  211. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  212. Assert.IsTrue (f.Bold, "Bold");
  213. Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
  214. Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
  215. Assert.IsTrue (f.Height > 0, "Height");
  216. Assert.IsFalse (f.Italic, "Italic");
  217. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  218. Assert.AreEqual (12.5f, f.Size, "Size");
  219. Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
  220. Assert.IsFalse (f.Strikeout, "Strikeout");
  221. Assert.IsFalse (f.Underline, "Underline");
  222. Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
  223. }
  224. [Test]
  225. public void Font_String_Float_FontStyle_GraphicsUnit ()
  226. {
  227. Font f = new Font (name, 12.5f, FontStyle.Italic, GraphicsUnit.Pixel);
  228. Assert.IsFalse (f.Bold, "Bold");
  229. Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
  230. Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
  231. Assert.IsTrue (f.Height > 0, "Height");
  232. Assert.IsTrue (f.Italic, "Italic");
  233. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  234. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  235. Assert.AreEqual (12.5f, f.Size, "Size");
  236. Assert.IsFalse (f.Strikeout, "Strikeout");
  237. Assert.IsFalse (f.Underline, "Underline");
  238. Assert.AreEqual (GraphicsUnit.Pixel, f.Unit, "Unit");
  239. }
  240. [Test]
  241. [ExpectedException (typeof (ArgumentException))]
  242. public void Font_String_Float_FontStyle_GraphicsUnit_Display ()
  243. {
  244. new Font (name, 12.5f, FontStyle.Italic, GraphicsUnit.Display);
  245. }
  246. [Test]
  247. public void Font_String_Float_FontStyle_GraphicsUnit_Byte ()
  248. {
  249. Font f = new Font (name, 12.5f, FontStyle.Strikeout, GraphicsUnit.Inch, Byte.MaxValue);
  250. Assert.IsFalse (f.Bold, "Bold");
  251. Assert.AreEqual (Byte.MaxValue, f.GdiCharSet, "GdiCharSet");
  252. Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
  253. Assert.IsTrue (f.Height > 0, "Height");
  254. Assert.IsFalse (f.Italic, "Italic");
  255. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  256. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  257. Assert.AreEqual (12.5f, f.Size, "Size");
  258. Assert.AreEqual (900f, f.SizeInPoints, "SizeInPoints");
  259. Assert.IsTrue (f.Strikeout, "Strikeout");
  260. Assert.IsFalse (f.Underline, "Underline");
  261. Assert.AreEqual (GraphicsUnit.Inch, f.Unit, "Unit");
  262. }
  263. [Test]
  264. public void Font_String_Float_FontStyle_GraphicsUnit_Byte_Bool ()
  265. {
  266. Font f = new Font (name, 12.5f, FontStyle.Underline, GraphicsUnit.Document, Byte.MinValue, true);
  267. Assert.IsFalse (f.Bold, "Bold");
  268. Assert.AreEqual (Byte.MinValue, f.GdiCharSet, "GdiCharSet");
  269. Assert.IsTrue (f.GdiVerticalFont, "GdiVerticalFont");
  270. Assert.IsTrue (f.Height > 0, "Height");
  271. Assert.IsFalse (f.Italic, "Italic");
  272. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  273. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  274. Assert.AreEqual (12.5f, f.Size, "Size");
  275. Assert.AreEqual (3f, f.SizeInPoints, "SizeInPoints");
  276. Assert.IsFalse (f.Strikeout, "Strikeout");
  277. Assert.IsTrue (f.Underline, "Underline");
  278. Assert.AreEqual (GraphicsUnit.Document, f.Unit, "Unit");
  279. }
  280. [Test]
  281. [ExpectedException (typeof (ArgumentNullException))]
  282. public void Font_FontFamilyNull_Float ()
  283. {
  284. FontFamily ff = null;
  285. new Font (ff, 12.5f);
  286. }
  287. [Test]
  288. [ExpectedException (typeof (NullReferenceException))]
  289. public void Font_FontNull_FontStyle ()
  290. {
  291. Font f = null;
  292. new Font (f, FontStyle.Bold);
  293. }
  294. [Test]
  295. public void Font_FontFamily_Float ()
  296. {
  297. Font f = new Font (FontFamily.GenericMonospace, 12.5f);
  298. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  299. Assert.IsFalse (f.Bold, "Bold");
  300. Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
  301. Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
  302. Assert.IsTrue (f.Height > 0, "Height");
  303. Assert.IsFalse (f.Italic, "Italic");
  304. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  305. Assert.AreEqual (12.5f, f.Size, "Size");
  306. Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
  307. Assert.IsFalse (f.Strikeout, "Strikeout");
  308. Assert.IsFalse (f.Underline, "Underline");
  309. Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
  310. }
  311. [Test]
  312. public void Font_FontFamily_Float_FontStyle ()
  313. {
  314. Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Bold);
  315. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  316. Assert.IsTrue (f.Bold, "Bold");
  317. Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
  318. Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
  319. Assert.IsTrue (f.Height > 0, "Height");
  320. Assert.IsFalse (f.Italic, "Italic");
  321. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  322. Assert.AreEqual (12.5f, f.Size, "Size");
  323. Assert.AreEqual (12.5f, f.SizeInPoints, "SizeInPoints");
  324. Assert.IsFalse (f.Strikeout, "Strikeout");
  325. Assert.IsFalse (f.Underline, "Underline");
  326. Assert.AreEqual (GraphicsUnit.Point, f.Unit, "Unit");
  327. }
  328. [Test]
  329. public void Font_FontFamily_Float_FontStyle_GraphicsUnit ()
  330. {
  331. Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Italic, GraphicsUnit.Millimeter);
  332. Assert.IsFalse (f.Bold, "Bold");
  333. Assert.AreEqual (1, f.GdiCharSet, "GdiCharSet");
  334. Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
  335. Assert.IsTrue (f.Height > 0, "Height");
  336. Assert.IsTrue (f.Italic, "Italic");
  337. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  338. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  339. Assert.AreEqual (12.5f, f.Size, "Size");
  340. Assert.AreEqual (35.43307f, f.SizeInPoints, "SizeInPoints");
  341. Assert.IsFalse (f.Strikeout, "Strikeout");
  342. Assert.IsFalse (f.Underline, "Underline");
  343. Assert.AreEqual (GraphicsUnit.Millimeter, f.Unit, "Unit");
  344. }
  345. [Test]
  346. [ExpectedException (typeof (ArgumentException))]
  347. public void Font_FontFamily_Float_FontStyle_GraphicsUnit_Display ()
  348. {
  349. new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Italic, GraphicsUnit.Display);
  350. }
  351. [Test]
  352. public void Font_FontFamily_Float_FontStyle_GraphicsUnit_Byte ()
  353. {
  354. Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Strikeout, GraphicsUnit.Inch, Byte.MaxValue);
  355. Assert.IsFalse (f.Bold, "Bold");
  356. Assert.AreEqual (Byte.MaxValue, f.GdiCharSet, "GdiCharSet");
  357. Assert.IsFalse (f.GdiVerticalFont, "GdiVerticalFont");
  358. Assert.IsTrue (f.Height > 0, "Height");
  359. Assert.IsFalse (f.Italic, "Italic");
  360. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  361. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  362. Assert.AreEqual (12.5f, f.Size, "Size");
  363. Assert.AreEqual (900f, f.SizeInPoints, "SizeInPoints");
  364. Assert.IsTrue (f.Strikeout, "Strikeout");
  365. Assert.IsFalse (f.Underline, "Underline");
  366. Assert.AreEqual (GraphicsUnit.Inch, f.Unit, "Unit");
  367. }
  368. [Test]
  369. public void Font_FontFamily_Float_FontStyle_GraphicsUnit_Byte_Bool ()
  370. {
  371. Font f = new Font (FontFamily.GenericMonospace, 12.5f, FontStyle.Underline, GraphicsUnit.Document, Byte.MinValue, true);
  372. Assert.IsFalse (f.Bold, "Bold");
  373. Assert.AreEqual (Byte.MinValue, f.GdiCharSet, "GdiCharSet");
  374. Assert.IsTrue (f.GdiVerticalFont, "GdiVerticalFont");
  375. Assert.IsTrue (f.Height > 0, "Height");
  376. Assert.IsFalse (f.Italic, "Italic");
  377. Assert.AreEqual (FontFamily.GenericMonospace, f.FontFamily, "FontFamily");
  378. Assert.AreEqual (f.Name, f.FontFamily.Name, "Name");
  379. Assert.AreEqual (12.5f, f.Size, "Size");
  380. Assert.AreEqual (3f, f.SizeInPoints, "SizeInPoints");
  381. Assert.IsFalse (f.Strikeout, "Strikeout");
  382. Assert.IsTrue (f.Underline, "Underline");
  383. Assert.AreEqual (GraphicsUnit.Document, f.Unit, "Unit");
  384. }
  385. [Test]
  386. public void Dispose_Double ()
  387. {
  388. Font f = new Font (name, 12.5f);
  389. f.Dispose ();
  390. f.Dispose ();
  391. }
  392. [Test]
  393. public void Dispose_UseAfter_Works ()
  394. {
  395. Font f = new Font (name, 12.5f);
  396. string fname = f.Name;
  397. f.Dispose ();
  398. // most properties don't throw, everything seems to be cached
  399. Assert.AreEqual (fname, f.Name, "Name");
  400. Assert.AreEqual (12.5f, f.Size, "Size");
  401. }
  402. [Test]
  403. [ExpectedException (typeof (ArgumentException))]
  404. public void Dispose_Height ()
  405. {
  406. Font f = new Font (name, 12.5f);
  407. f.Dispose ();
  408. Assert.AreEqual (0, f.Height, "Name");
  409. }
  410. [Test]
  411. [ExpectedException (typeof (ArgumentException))]
  412. public void Dispose_ToLogFont ()
  413. {
  414. Font f = new Font (name, 12.5f);
  415. f.Dispose ();
  416. LOGFONT lf = new LOGFONT();
  417. f.ToLogFont (lf);
  418. }
  419. [Test]
  420. public void Dispose_ToLogFont_LoopCharSet ()
  421. {
  422. Font f = new Font (name, 12.5f);
  423. f.Dispose ();
  424. LOGFONT lf = new LOGFONT ();
  425. for (int i = Byte.MinValue; i < Byte.MaxValue; i++) {
  426. byte b = (byte) i;
  427. lf.lfHeight = b;
  428. lf.lfWidth = b;
  429. lf.lfEscapement = b;
  430. lf.lfOrientation = b;
  431. lf.lfWeight = b;
  432. lf.lfItalic = b;
  433. lf.lfUnderline = b;
  434. lf.lfStrikeOut = b;
  435. lf.lfCharSet = b;
  436. lf.lfOutPrecision = b;
  437. lf.lfClipPrecision = b;
  438. lf.lfQuality = b;
  439. lf.lfPitchAndFamily = b;
  440. lf.lfFaceName = b.ToString ();
  441. try {
  442. f.ToLogFont (lf);
  443. }
  444. catch (ArgumentException) {
  445. Assert.AreEqual (b, lf.lfHeight, "lfHeight");
  446. Assert.AreEqual (b, lf.lfWidth, "lfWidth");
  447. Assert.AreEqual (b, lf.lfEscapement, "lfEscapement");
  448. Assert.AreEqual (b, lf.lfOrientation, "lfOrientation");
  449. Assert.AreEqual (b, lf.lfWeight, "lfWeight");
  450. Assert.AreEqual (b, lf.lfItalic, "lfItalic");
  451. Assert.AreEqual (b, lf.lfUnderline, "lfUnderline");
  452. Assert.AreEqual (b, lf.lfStrikeOut, "lfStrikeOut");
  453. // special case for 0
  454. Assert.AreEqual ((i == 0) ? (byte)1 : b, lf.lfCharSet, "lfCharSet");
  455. Assert.AreEqual (b, lf.lfOutPrecision, "lfOutPrecision");
  456. Assert.AreEqual (b, lf.lfClipPrecision, "lfClipPrecision");
  457. Assert.AreEqual (b, lf.lfQuality, "lfQuality");
  458. Assert.AreEqual (b, lf.lfPitchAndFamily, "lfPitchAndFamily");
  459. Assert.AreEqual (b.ToString (), lf.lfFaceName, "lfFaceName");
  460. }
  461. catch (Exception e) {
  462. Assert.Fail ("Unexcepted exception {0} at iteration {1}", e, i);
  463. }
  464. }
  465. }
  466. [Test]
  467. [ExpectedException (typeof (ArgumentException))]
  468. public void Dispose_ToHFont ()
  469. {
  470. Font f = new Font (name, 12.5f);
  471. f.Dispose ();
  472. f.ToHfont ();
  473. }
  474. [Test]
  475. [ExpectedException (typeof (ArgumentException))]
  476. [Category ("NotWorking")]
  477. public void UnavailableStyleException ()
  478. {
  479. // Marked NotWorking because it is dependent on what fonts/styles are available
  480. // on the OS. This test is written for Windows.
  481. Font f = new Font ("Monotype Corsiva", 8, FontStyle.Regular);
  482. }
  483. [Test]
  484. public void GetHeight_Float ()
  485. {
  486. using (Font f = new Font (name, 12.5f)) {
  487. Assert.AreEqual (0, f.GetHeight (0), "0");
  488. }
  489. }
  490. [Test]
  491. public void GetHeight_Graphics ()
  492. {
  493. using (Bitmap bmp = new Bitmap (10, 10)) {
  494. using (Graphics g = Graphics.FromImage (bmp)) {
  495. using (Font f = new Font (name, 12.5f)) {
  496. float expected = f.GetHeight (g.DpiY);
  497. Assert.AreEqual (expected, f.GetHeight (g), 0.01f, "Default");
  498. g.ScaleTransform (2, 4);
  499. Assert.AreEqual (expected, f.GetHeight (g), 0.01f, "ScaleTransform");
  500. g.PageScale = 3;
  501. Assert.AreEqual (expected, f.GetHeight (g), 0.01f, "PageScale");
  502. }
  503. }
  504. }
  505. }
  506. [Test]
  507. [ExpectedException (typeof (ArgumentNullException))]
  508. public void GetHeight_Graphics_Null ()
  509. {
  510. using (Font f = new Font (name, 12.5f)) {
  511. Assert.AreEqual (0, f.GetHeight (null), "0");
  512. }
  513. }
  514. [Test]
  515. public void FontUniqueHashCode ()
  516. {
  517. Font f1 = new Font ("Arial", 14);
  518. Font f2 = new Font ("Arial", 12);
  519. Font f3 = new Font (f1, FontStyle.Bold);
  520. Assert.IsFalse (f1.GetHashCode () == f2.GetHashCode (), "1) Fonts with different sizes should have different HashCodes");
  521. Assert.IsFalse (f1.GetHashCode () == f3.GetHashCode (), "2) Fonts with different styles should have different HashCodes");
  522. }
  523. [Test]
  524. public void GetHashCode_UnitDiffers_HashesNotEqual()
  525. {
  526. Font f1 = new Font("Arial", 8.25F, GraphicsUnit.Point);
  527. Font f2 = new Font("Arial", 8.25F, GraphicsUnit.Pixel);
  528. Assert.IsFalse(f1.GetHashCode() == f2.GetHashCode(),
  529. "Hashcodes should differ if _unit member differs");
  530. }
  531. [Test]
  532. public void GetHashCode_NameDiffers_HashesNotEqual()
  533. {
  534. Font f1 = new Font("Arial", 8.25F, GraphicsUnit.Point);
  535. Font f2 = new Font("Courier New", 8.25F, GraphicsUnit.Point);
  536. Assert.IsFalse(f1.GetHashCode() == f2.GetHashCode(),
  537. "Hashcodes should differ if _name member differs");
  538. }
  539. [Test]
  540. public void GetHashCode_StyleEqualsGdiCharSet_HashesNotEqual()
  541. {
  542. Font f1 = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
  543. Font f2 = new Font("Arial", 8.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(1)));
  544. Assert.IsFalse(f1.GetHashCode() == f2.GetHashCode(),
  545. "Hashcodes should differ if _style member differs");
  546. }
  547. }
  548. }