TextTests.cs 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. ๏ปฟusing System.Text;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace UnitTests.ViewBaseTests;
  5. /// <summary>
  6. /// Tests of the <see cref="View.Text"/> and <see cref="View.TextFormatter"/> properties.
  7. /// </summary>
  8. public class TextTests (ITestOutputHelper output)
  9. {
  10. [Fact]
  11. [SetupFakeApplication]
  12. public void Setting_With_Height_Horizontal ()
  13. {
  14. var top = new View { Width = 25, Height = 25 };
  15. top.App = ApplicationImpl.Instance;
  16. var label = new Label { Text = "Hello", /* Width = 10, Height = 2, */ ValidatePosDim = true };
  17. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  18. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  19. top.Add (label, viewX, viewY);
  20. top.Layout ();
  21. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  22. top.LayoutSubViews ();
  23. top.Draw ();
  24. var expected = @"
  25. HelloX
  26. Y
  27. ";
  28. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  29. label.Width = 10;
  30. label.Height = 2;
  31. Assert.Equal (new (0, 0, 10, 2), label.Frame);
  32. top.LayoutSubViews ();
  33. top.SetClipToScreen ();
  34. top.Draw ();
  35. expected = @"
  36. Hello X
  37. Y
  38. ";
  39. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  40. }
  41. [Fact]
  42. [AutoInitShutdown]
  43. public void Setting_With_Height_Vertical ()
  44. {
  45. // BUGBUG: Label is Width = Dim.Auto (), Height = Dim.Auto (), so Width & Height are ignored
  46. var label = new Label
  47. { /*Width = 2, Height = 10, */
  48. TextDirection = TextDirection.TopBottom_LeftRight, ValidatePosDim = true
  49. };
  50. var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
  51. var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
  52. var top = new Runnable ();
  53. top.Add (label, viewX, viewY);
  54. SessionToken rs = Application.Begin (top);
  55. label.Text = "Hello";
  56. AutoInitShutdownAttribute.RunIteration ();
  57. Assert.Equal (new (0, 0, 1, 5), label.Frame);
  58. var expected = @"
  59. HX
  60. e
  61. l
  62. l
  63. o
  64. Y
  65. ";
  66. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  67. label.Width = 2;
  68. label.Height = 10;
  69. AutoInitShutdownAttribute.RunIteration ();
  70. Assert.Equal (new (0, 0, 2, 10), label.Frame);
  71. expected = @"
  72. H X
  73. e
  74. l
  75. l
  76. o
  77. Y
  78. "
  79. ;
  80. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  81. Application.End (rs);
  82. top.Dispose ();
  83. }
  84. [Fact]
  85. [AutoInitShutdown]
  86. public void TextDirection_Toggle ()
  87. {
  88. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  89. var view = new View ();
  90. win.Add (view);
  91. var top = new Runnable ();
  92. top.Add (win);
  93. SessionToken rs = Application.Begin (top);
  94. Application.Driver!.SetScreenSize (15, 15);
  95. Application.LayoutAndDraw ();
  96. Assert.Equal (new (0, 0, 15, 15), win.Frame);
  97. Assert.Equal (new (0, 0, 15, 15), win.Margin!.Frame);
  98. Assert.Equal (new (0, 0, 15, 15), win.Border!.Frame);
  99. Assert.Equal (new (1, 1, 13, 13), win.Padding.Frame);
  100. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  101. Assert.Equal (Rectangle.Empty, view.Frame);
  102. Assert.Equal ("Absolute(0)", view.X.ToString ());
  103. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  104. Assert.Equal ("Absolute(0)", view.Width.ToString ());
  105. Assert.Equal ("Absolute(0)", view.Height.ToString ());
  106. var expected = @"
  107. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  108. โ”‚ โ”‚
  109. โ”‚ โ”‚
  110. โ”‚ โ”‚
  111. โ”‚ โ”‚
  112. โ”‚ โ”‚
  113. โ”‚ โ”‚
  114. โ”‚ โ”‚
  115. โ”‚ โ”‚
  116. โ”‚ โ”‚
  117. โ”‚ โ”‚
  118. โ”‚ โ”‚
  119. โ”‚ โ”‚
  120. โ”‚ โ”‚
  121. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  122. ";
  123. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  124. view.Text = "Hello World";
  125. view.Width = 11;
  126. view.Height = 1;
  127. AutoInitShutdownAttribute.RunIteration ();
  128. Assert.Equal (new (0, 0, 11, 1), view.Frame);
  129. Assert.Equal ("Absolute(0)", view.X.ToString ());
  130. Assert.Equal ("Absolute(0)", view.Y.ToString ());
  131. Assert.Equal ("Absolute(11)", view.Width.ToString ());
  132. Assert.Equal ("Absolute(1)", view.Height.ToString ());
  133. expected = @"
  134. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  135. โ”‚Hello World โ”‚
  136. โ”‚ โ”‚
  137. โ”‚ โ”‚
  138. โ”‚ โ”‚
  139. โ”‚ โ”‚
  140. โ”‚ โ”‚
  141. โ”‚ โ”‚
  142. โ”‚ โ”‚
  143. โ”‚ โ”‚
  144. โ”‚ โ”‚
  145. โ”‚ โ”‚
  146. โ”‚ โ”‚
  147. โ”‚ โ”‚
  148. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  149. ";
  150. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  151. view.Width = Dim.Auto ();
  152. view.Height = Dim.Auto ();
  153. view.Text = "Hello Worlds";
  154. AutoInitShutdownAttribute.RunIteration ();
  155. int len = "Hello Worlds".Length;
  156. Assert.Equal (12, len);
  157. Assert.Equal (new (0, 0, len, 1), view.Frame);
  158. expected = @"
  159. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  160. โ”‚Hello Worlds โ”‚
  161. โ”‚ โ”‚
  162. โ”‚ โ”‚
  163. โ”‚ โ”‚
  164. โ”‚ โ”‚
  165. โ”‚ โ”‚
  166. โ”‚ โ”‚
  167. โ”‚ โ”‚
  168. โ”‚ โ”‚
  169. โ”‚ โ”‚
  170. โ”‚ โ”‚
  171. โ”‚ โ”‚
  172. โ”‚ โ”‚
  173. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  174. ";
  175. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  176. view.TextDirection = TextDirection.TopBottom_LeftRight;
  177. AutoInitShutdownAttribute.RunIteration ();
  178. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  179. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  180. expected = @"
  181. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  182. โ”‚H โ”‚
  183. โ”‚e โ”‚
  184. โ”‚l โ”‚
  185. โ”‚l โ”‚
  186. โ”‚o โ”‚
  187. โ”‚ โ”‚
  188. โ”‚W โ”‚
  189. โ”‚o โ”‚
  190. โ”‚r โ”‚
  191. โ”‚l โ”‚
  192. โ”‚d โ”‚
  193. โ”‚s โ”‚
  194. โ”‚ โ”‚
  195. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  196. ";
  197. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  198. // Setting to false causes Width and Height to be set to the current ContentSize
  199. view.Width = 1;
  200. view.Height = 12;
  201. AutoInitShutdownAttribute.RunIteration ();
  202. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  203. view.Width = 12;
  204. view.Height = 1;
  205. view.TextFormatter.ConstrainToSize = new (12, 1);
  206. AutoInitShutdownAttribute.RunIteration ();
  207. Assert.Equal (new (12, 1), view.TextFormatter.ConstrainToSize);
  208. Assert.Equal (new (0, 0, 12, 1), view.Frame);
  209. top.ClearViewport ();
  210. view.SetNeedsDraw ();
  211. view.Draw ();
  212. expected = @" HelloWorlds";
  213. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  214. AutoInitShutdownAttribute.RunIteration ();
  215. // TextDirection.TopBottom_LeftRight - Height of 1 and Width of 12 means
  216. // that the text will be spread "vertically" across 1 line.
  217. // Hence no space.
  218. expected = @"
  219. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  220. โ”‚HelloWorlds โ”‚
  221. โ”‚ โ”‚
  222. โ”‚ โ”‚
  223. โ”‚ โ”‚
  224. โ”‚ โ”‚
  225. โ”‚ โ”‚
  226. โ”‚ โ”‚
  227. โ”‚ โ”‚
  228. โ”‚ โ”‚
  229. โ”‚ โ”‚
  230. โ”‚ โ”‚
  231. โ”‚ โ”‚
  232. โ”‚ โ”‚
  233. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  234. ";
  235. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  236. view.PreserveTrailingSpaces = true;
  237. AutoInitShutdownAttribute.RunIteration ();
  238. Assert.Equal (new (0, 0, 12, 1), view.Frame);
  239. expected = @"
  240. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  241. โ”‚Hello Worlds โ”‚
  242. โ”‚ โ”‚
  243. โ”‚ โ”‚
  244. โ”‚ โ”‚
  245. โ”‚ โ”‚
  246. โ”‚ โ”‚
  247. โ”‚ โ”‚
  248. โ”‚ โ”‚
  249. โ”‚ โ”‚
  250. โ”‚ โ”‚
  251. โ”‚ โ”‚
  252. โ”‚ โ”‚
  253. โ”‚ โ”‚
  254. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  255. ";
  256. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  257. view.PreserveTrailingSpaces = false;
  258. Rectangle f = view.Frame;
  259. view.Width = f.Height;
  260. view.Height = f.Width;
  261. view.TextDirection = TextDirection.TopBottom_LeftRight;
  262. AutoInitShutdownAttribute.RunIteration ();
  263. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  264. expected = @"
  265. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  266. โ”‚H โ”‚
  267. โ”‚e โ”‚
  268. โ”‚l โ”‚
  269. โ”‚l โ”‚
  270. โ”‚o โ”‚
  271. โ”‚ โ”‚
  272. โ”‚W โ”‚
  273. โ”‚o โ”‚
  274. โ”‚r โ”‚
  275. โ”‚l โ”‚
  276. โ”‚d โ”‚
  277. โ”‚s โ”‚
  278. โ”‚ โ”‚
  279. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  280. ";
  281. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  282. view.Width = Dim.Auto ();
  283. view.Height = Dim.Auto ();
  284. AutoInitShutdownAttribute.RunIteration ();
  285. Assert.Equal (new (0, 0, 1, 12), view.Frame);
  286. expected = @"
  287. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  288. โ”‚H โ”‚
  289. โ”‚e โ”‚
  290. โ”‚l โ”‚
  291. โ”‚l โ”‚
  292. โ”‚o โ”‚
  293. โ”‚ โ”‚
  294. โ”‚W โ”‚
  295. โ”‚o โ”‚
  296. โ”‚r โ”‚
  297. โ”‚l โ”‚
  298. โ”‚d โ”‚
  299. โ”‚s โ”‚
  300. โ”‚ โ”‚
  301. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  302. ";
  303. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  304. Application.End (rs);
  305. top.Dispose ();
  306. }
  307. [Fact]
  308. [AutoInitShutdown]
  309. public void View_IsEmpty_False_Minimum_Width ()
  310. {
  311. var text = "Views";
  312. var view = new View
  313. {
  314. TextDirection = TextDirection.TopBottom_LeftRight,
  315. Height = Dim.Fill () - text.Length,
  316. Text = text
  317. };
  318. view.Width = Dim.Auto ();
  319. view.Height = Dim.Auto ();
  320. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  321. win.Add (view);
  322. var top = new Runnable ();
  323. top.Add (win);
  324. SessionToken rs = Application.Begin (top);
  325. Application.Driver!.SetScreenSize (4, 10);
  326. Application.LayoutAndDraw ();
  327. Assert.Equal (5, text.Length);
  328. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  329. Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
  330. Assert.Equal (new () { "Views" }, view.TextFormatter.GetLines ());
  331. Assert.Equal (new (0, 0, 4, 10), win.Frame);
  332. Assert.Equal (new (0, 0, 4, 10), Application.TopRunnableView.Frame);
  333. var expected = @"
  334. โ”Œโ”€โ”€โ”
  335. โ”‚V โ”‚
  336. โ”‚i โ”‚
  337. โ”‚e โ”‚
  338. โ”‚w โ”‚
  339. โ”‚s โ”‚
  340. โ”‚ โ”‚
  341. โ”‚ โ”‚
  342. โ”‚ โ”‚
  343. โ””โ”€โ”€โ”˜
  344. ";
  345. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  346. Assert.Equal (new (0, 0, 4, 10), pos);
  347. text = "0123456789";
  348. Assert.Equal (10, text.Length);
  349. //view.Height = Dim.Fill () - text.Length;
  350. AutoInitShutdownAttribute.RunIteration ();
  351. Assert.Equal (new (0, 0, 1, 5), view.Frame);
  352. Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
  353. Exception exception = Record.Exception (() => Assert.Single (view.TextFormatter.GetLines ()));
  354. Assert.Null (exception);
  355. expected = @"
  356. โ”Œโ”€โ”€โ”
  357. โ”‚V โ”‚
  358. โ”‚i โ”‚
  359. โ”‚e โ”‚
  360. โ”‚w โ”‚
  361. โ”‚s โ”‚
  362. โ”‚ โ”‚
  363. โ”‚ โ”‚
  364. โ”‚ โ”‚
  365. โ””โ”€โ”€โ”˜
  366. ";
  367. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  368. Assert.Equal (new (0, 0, 4, 10), pos);
  369. top.Dispose ();
  370. }
  371. [Fact]
  372. [SetupFakeApplication]
  373. public void DimAuto_Vertical_TextDirection_Wide_Rune ()
  374. {
  375. var text = "็•ŒView";
  376. var view = new View
  377. {
  378. App = ApplicationImpl.Instance,
  379. TextDirection = TextDirection.TopBottom_LeftRight,
  380. Text = text,
  381. Width = Dim.Auto (),
  382. Height = Dim.Auto ()
  383. };
  384. view.SetRelativeLayout (new (4, 10));
  385. Assert.Equal (5, text.Length);
  386. // Vertical text - 2 wide, 5 down
  387. Assert.Equal (new (0, 0, 2, 5), view.Frame);
  388. Assert.Equal (new (2, 5), view.TextFormatter.ConstrainToSize);
  389. Assert.Equal (new () { "็•ŒView" }, view.TextFormatter.GetLines ());
  390. view.Draw ();
  391. var expected = @"
  392. ็•Œ
  393. V
  394. i
  395. e
  396. w ";
  397. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  398. }
  399. [Fact]
  400. [AutoInitShutdown]
  401. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  402. {
  403. var text = $"0123456789{Environment.NewLine}01234567891";
  404. var horizontalView = new View
  405. {
  406. Width = Dim.Auto (),
  407. Height = Dim.Auto (),
  408. Text = text
  409. };
  410. var verticalView = new View
  411. {
  412. Width = Dim.Auto (),
  413. Height = Dim.Auto (),
  414. Y = 3,
  415. //Height = 11,
  416. //Width = 2,
  417. Text = text,
  418. TextDirection = TextDirection.TopBottom_LeftRight
  419. };
  420. var win = new Window
  421. {
  422. Width = Dim.Fill (),
  423. Height = Dim.Fill (),
  424. Text = "Window"
  425. };
  426. win.Add (horizontalView, verticalView);
  427. var top = new Runnable ();
  428. top.Add (win);
  429. SessionToken rs = Application.Begin (top);
  430. Application.Driver!.SetScreenSize (20, 20);
  431. Application.LayoutAndDraw ();
  432. Assert.Equal (new (0, 0, 11, 2), horizontalView.Frame);
  433. Assert.Equal (new (0, 3, 2, 11), verticalView.Frame);
  434. var expected = @"
  435. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  436. โ”‚0123456789 โ”‚
  437. โ”‚01234567891 โ”‚
  438. โ”‚ โ”‚
  439. โ”‚00 โ”‚
  440. โ”‚11 โ”‚
  441. โ”‚22 โ”‚
  442. โ”‚33 โ”‚
  443. โ”‚44 โ”‚
  444. โ”‚55 โ”‚
  445. โ”‚66 โ”‚
  446. โ”‚77 โ”‚
  447. โ”‚88 โ”‚
  448. โ”‚99 โ”‚
  449. โ”‚ 1 โ”‚
  450. โ”‚ โ”‚
  451. โ”‚ โ”‚
  452. โ”‚ โ”‚
  453. โ”‚ โ”‚
  454. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  455. ";
  456. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  457. verticalView.Text = $"ๆœ€ๅˆใฎ่กŒ{Environment.NewLine}ไบŒ่กŒ็›ฎ";
  458. AutoInitShutdownAttribute.RunIteration ();
  459. Assert.Equal (new (0, 3, 4, 4), verticalView.Frame);
  460. expected = @"
  461. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  462. โ”‚0123456789 โ”‚
  463. โ”‚01234567891 โ”‚
  464. โ”‚ โ”‚
  465. โ”‚ๆœ€ไบŒ โ”‚
  466. โ”‚ๅˆ่กŒ โ”‚
  467. โ”‚ใฎ็›ฎ โ”‚
  468. โ”‚่กŒ โ”‚
  469. โ”‚ โ”‚
  470. โ”‚ โ”‚
  471. โ”‚ โ”‚
  472. โ”‚ โ”‚
  473. โ”‚ โ”‚
  474. โ”‚ โ”‚
  475. โ”‚ โ”‚
  476. โ”‚ โ”‚
  477. โ”‚ โ”‚
  478. โ”‚ โ”‚
  479. โ”‚ โ”‚
  480. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  481. ";
  482. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  483. Application.End (rs);
  484. top.Dispose ();
  485. }
  486. [Fact]
  487. [AutoInitShutdown]
  488. public void Width_Height_Stay_True_If_TextFormatter_Size_Fit ()
  489. {
  490. var text = "Finish ็ต‚";
  491. var horizontalView = new View
  492. {
  493. Id = "horizontalView",
  494. Width = Dim.Auto (), Height = Dim.Auto (), Text = text
  495. };
  496. var verticalView = new View
  497. {
  498. Id = "verticalView",
  499. Y = 3,
  500. Width = Dim.Auto (),
  501. Height = Dim.Auto (),
  502. Text = text,
  503. TextDirection = TextDirection.TopBottom_LeftRight
  504. };
  505. var win = new Window { Id = "win", Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
  506. win.Add (horizontalView, verticalView);
  507. var top = new Runnable ();
  508. top.Add (win);
  509. SessionToken rs = Application.Begin (top);
  510. Application.Driver!.SetScreenSize (22, 22);
  511. Application.LayoutAndDraw ();
  512. Assert.Equal (new (text.GetColumns (), 1), horizontalView.TextFormatter.ConstrainToSize);
  513. Assert.Equal (new (2, 8), verticalView.TextFormatter.ConstrainToSize);
  514. //Assert.Equal (new (0, 0, 10, 1), horizontalView.Frame);
  515. //Assert.Equal (new (0, 3, 10, 9), verticalView.Frame);
  516. var expected = @"
  517. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  518. โ”‚Finish ็ต‚ โ”‚
  519. โ”‚ โ”‚
  520. โ”‚ โ”‚
  521. โ”‚F โ”‚
  522. โ”‚i โ”‚
  523. โ”‚n โ”‚
  524. โ”‚i โ”‚
  525. โ”‚s โ”‚
  526. โ”‚h โ”‚
  527. โ”‚ โ”‚
  528. โ”‚็ต‚ โ”‚
  529. โ”‚ โ”‚
  530. โ”‚ โ”‚
  531. โ”‚ โ”‚
  532. โ”‚ โ”‚
  533. โ”‚ โ”‚
  534. โ”‚ โ”‚
  535. โ”‚ โ”‚
  536. โ”‚ โ”‚
  537. โ”‚ โ”‚
  538. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  539. ";
  540. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  541. verticalView.Text = "ๆœ€ๅˆใฎ่กŒไบŒ่กŒ็›ฎ";
  542. AutoInitShutdownAttribute.RunIteration ();
  543. // height was initialized with 8 and can only grow or keep initial value
  544. Assert.Equal (new (0, 3, 2, 7), verticalView.Frame);
  545. expected = @"
  546. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  547. โ”‚Finish ็ต‚ โ”‚
  548. โ”‚ โ”‚
  549. โ”‚ โ”‚
  550. โ”‚ๆœ€ โ”‚
  551. โ”‚ๅˆ โ”‚
  552. โ”‚ใฎ โ”‚
  553. โ”‚่กŒ โ”‚
  554. โ”‚ไบŒ โ”‚
  555. โ”‚่กŒ โ”‚
  556. โ”‚็›ฎ โ”‚
  557. โ”‚ โ”‚
  558. โ”‚ โ”‚
  559. โ”‚ โ”‚
  560. โ”‚ โ”‚
  561. โ”‚ โ”‚
  562. โ”‚ โ”‚
  563. โ”‚ โ”‚
  564. โ”‚ โ”‚
  565. โ”‚ โ”‚
  566. โ”‚ โ”‚
  567. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  568. ";
  569. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  570. Application.End (rs);
  571. top.Dispose ();
  572. }
  573. [Fact]
  574. [AutoInitShutdown]
  575. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  576. {
  577. var lbl = new Label { Text = "123" };
  578. var top = new Runnable ();
  579. top.Add (lbl);
  580. SessionToken rs = Application.Begin (top);
  581. AutoInitShutdownAttribute.RunIteration ();
  582. Assert.Equal (new (0, 0, 3, 1), lbl.Frame);
  583. Assert.Equal ("123 ", GetContents ());
  584. lbl.Text = "12";
  585. lbl.Layout ();
  586. Assert.Equal (new (0, 0, 2, 1), lbl.Frame);
  587. Assert.Equal (new (0, 0, 2, 1), lbl.NeedsDrawRect);
  588. Assert.Equal (new (0, 0, 80, 25), lbl.SuperView.NeedsDrawRect);
  589. Assert.True (lbl.SuperView.NeedsLayout);
  590. AutoInitShutdownAttribute.RunIteration ();
  591. Assert.Equal ("12 ", GetContents ());
  592. string GetContents ()
  593. {
  594. var sb = new StringBuilder ();
  595. for (var i = 0; i < 4; i++)
  596. {
  597. sb.Append (Application.Driver?.Contents! [0, i].Grapheme);
  598. }
  599. return sb.ToString ();
  600. }
  601. Application.End (rs);
  602. top.Dispose ();
  603. }
  604. [Theory]
  605. [AutoInitShutdown]
  606. [InlineData (true)]
  607. [InlineData (false)]
  608. public void View_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
  609. {
  610. var text = "Hello World";
  611. var width = 20;
  612. var lblLeft = new View
  613. {
  614. Text = text,
  615. Width = width,
  616. Height = 1
  617. };
  618. if (autoSize)
  619. {
  620. lblLeft.Width = Dim.Auto ();
  621. lblLeft.Height = Dim.Auto ();
  622. }
  623. var lblCenter = new View
  624. {
  625. Text = text,
  626. Y = 1,
  627. Width = width,
  628. Height = 1,
  629. TextAlignment = Alignment.Center
  630. };
  631. if (autoSize)
  632. {
  633. lblCenter.Width = Dim.Auto ();
  634. lblCenter.Height = Dim.Auto ();
  635. }
  636. var lblRight = new View
  637. {
  638. Text = text,
  639. Y = 2,
  640. Width = width,
  641. Height = 1,
  642. TextAlignment = Alignment.End
  643. };
  644. if (autoSize)
  645. {
  646. lblRight.Width = Dim.Auto ();
  647. lblRight.Height = Dim.Auto ();
  648. }
  649. var lblJust = new View
  650. {
  651. Text = text,
  652. Y = 3,
  653. Width = width,
  654. Height = 1,
  655. TextAlignment = Alignment.Fill
  656. };
  657. if (autoSize)
  658. {
  659. lblJust.Width = Dim.Auto ();
  660. lblJust.Height = Dim.Auto ();
  661. }
  662. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), BorderStyle = LineStyle.Single };
  663. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  664. var top = new Runnable ();
  665. top.Add (frame);
  666. Application.Begin (top);
  667. Application.Driver!.SetScreenSize (width + 2, 6);
  668. Application.LayoutAndDraw ();
  669. // frame.Width is width + border wide (20 + 2) and 6 high
  670. if (autoSize)
  671. {
  672. Size expectedSize = new (11, 1);
  673. Assert.Equal (expectedSize, lblLeft.TextFormatter.ConstrainToSize);
  674. Assert.Equal (expectedSize, lblCenter.TextFormatter.ConstrainToSize);
  675. Assert.Equal (expectedSize, lblRight.TextFormatter.ConstrainToSize);
  676. Assert.Equal (expectedSize, lblJust.TextFormatter.ConstrainToSize);
  677. }
  678. else
  679. {
  680. Size expectedSize = new (width, 1);
  681. Assert.Equal (expectedSize, lblLeft.TextFormatter.ConstrainToSize);
  682. Assert.Equal (expectedSize, lblCenter.TextFormatter.ConstrainToSize);
  683. Assert.Equal (expectedSize, lblRight.TextFormatter.ConstrainToSize);
  684. Assert.Equal (expectedSize, lblJust.TextFormatter.ConstrainToSize);
  685. }
  686. Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
  687. string expected;
  688. if (autoSize)
  689. {
  690. expected = @"
  691. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  692. โ”‚Hello World โ”‚
  693. โ”‚Hello World โ”‚
  694. โ”‚Hello World โ”‚
  695. โ”‚Hello World โ”‚
  696. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  697. ";
  698. }
  699. else
  700. {
  701. expected = @"
  702. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  703. โ”‚Hello World โ”‚
  704. โ”‚ Hello World โ”‚
  705. โ”‚ Hello Worldโ”‚
  706. โ”‚Hello Worldโ”‚
  707. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  708. ";
  709. }
  710. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  711. Assert.Equal (new (0, 0, width + 2, 6), pos);
  712. top.Dispose ();
  713. }
  714. [Theory]
  715. [AutoInitShutdown]
  716. [InlineData (true)]
  717. [InlineData (false)]
  718. public void View_Draw_Vertical_Simple_TextAlignments (bool autoSize)
  719. {
  720. var text = "Hello World";
  721. var height = 20;
  722. var lblLeft = new View
  723. {
  724. Text = text,
  725. Width = 1,
  726. Height = height,
  727. TextDirection = TextDirection.TopBottom_LeftRight
  728. };
  729. if (autoSize)
  730. {
  731. lblLeft.Width = Dim.Auto ();
  732. lblLeft.Height = Dim.Auto ();
  733. }
  734. var lblCenter = new View
  735. {
  736. Text = text,
  737. X = 2,
  738. Width = 1,
  739. Height = height,
  740. TextDirection = TextDirection.TopBottom_LeftRight,
  741. VerticalTextAlignment = Alignment.Center
  742. };
  743. if (autoSize)
  744. {
  745. lblCenter.Width = Dim.Auto ();
  746. lblCenter.Height = Dim.Auto ();
  747. }
  748. var lblRight = new View
  749. {
  750. Text = text,
  751. X = 4,
  752. Width = 1,
  753. Height = height,
  754. TextDirection = TextDirection.TopBottom_LeftRight,
  755. VerticalTextAlignment = Alignment.End
  756. };
  757. if (autoSize)
  758. {
  759. lblRight.Width = Dim.Auto ();
  760. lblRight.Height = Dim.Auto ();
  761. }
  762. var lblJust = new View
  763. {
  764. Text = text,
  765. X = 6,
  766. Width = 1,
  767. Height = height,
  768. TextDirection = TextDirection.TopBottom_LeftRight,
  769. VerticalTextAlignment = Alignment.Fill
  770. };
  771. if (autoSize)
  772. {
  773. lblJust.Width = Dim.Auto ();
  774. lblJust.Height = Dim.Auto ();
  775. }
  776. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), BorderStyle = LineStyle.Single };
  777. frame.Add (lblLeft, lblCenter, lblRight, lblJust);
  778. var top = new Runnable ();
  779. top.Add (frame);
  780. Application.Begin (top);
  781. Application.Driver!.SetScreenSize (9, height + 2);
  782. Application.LayoutAndDraw ();
  783. if (autoSize)
  784. {
  785. Assert.Equal (new (1, 11), lblLeft.TextFormatter.ConstrainToSize);
  786. Assert.Equal (new (1, 11), lblCenter.TextFormatter.ConstrainToSize);
  787. Assert.Equal (new (1, 11), lblRight.TextFormatter.ConstrainToSize);
  788. Assert.Equal (new (1, 11), lblJust.TextFormatter.ConstrainToSize);
  789. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  790. }
  791. else
  792. {
  793. Assert.Equal (new (1, height), lblLeft.TextFormatter.ConstrainToSize);
  794. Assert.Equal (new (1, height), lblCenter.TextFormatter.ConstrainToSize);
  795. Assert.Equal (new (1, height), lblRight.TextFormatter.ConstrainToSize);
  796. Assert.Equal (new (1, height), lblJust.TextFormatter.ConstrainToSize);
  797. Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
  798. }
  799. string expected;
  800. if (autoSize)
  801. {
  802. expected = @"
  803. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  804. โ”‚H H H Hโ”‚
  805. โ”‚e e e eโ”‚
  806. โ”‚l l l lโ”‚
  807. โ”‚l l l lโ”‚
  808. โ”‚o o o oโ”‚
  809. โ”‚ โ”‚
  810. โ”‚W W W Wโ”‚
  811. โ”‚o o o oโ”‚
  812. โ”‚r r r rโ”‚
  813. โ”‚l l l lโ”‚
  814. โ”‚d d d dโ”‚
  815. โ”‚ โ”‚
  816. โ”‚ โ”‚
  817. โ”‚ โ”‚
  818. โ”‚ โ”‚
  819. โ”‚ โ”‚
  820. โ”‚ โ”‚
  821. โ”‚ โ”‚
  822. โ”‚ โ”‚
  823. โ”‚ โ”‚
  824. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  825. ";
  826. }
  827. else
  828. {
  829. expected = @"
  830. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  831. โ”‚H Hโ”‚
  832. โ”‚e eโ”‚
  833. โ”‚l lโ”‚
  834. โ”‚l lโ”‚
  835. โ”‚o H oโ”‚
  836. โ”‚ e โ”‚
  837. โ”‚W l โ”‚
  838. โ”‚o l โ”‚
  839. โ”‚r o โ”‚
  840. โ”‚l H โ”‚
  841. โ”‚d W e โ”‚
  842. โ”‚ o l โ”‚
  843. โ”‚ r l โ”‚
  844. โ”‚ l o โ”‚
  845. โ”‚ d โ”‚
  846. โ”‚ W Wโ”‚
  847. โ”‚ o oโ”‚
  848. โ”‚ r rโ”‚
  849. โ”‚ l lโ”‚
  850. โ”‚ d dโ”‚
  851. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  852. ";
  853. }
  854. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  855. Assert.Equal (new (0, 0, 9, height + 2), pos);
  856. top.Dispose ();
  857. }
  858. [Fact]
  859. [SetupFakeApplication]
  860. public void Narrow_Wide_Runes ()
  861. {
  862. Application.Driver!.SetScreenSize (32, 32);
  863. var top = new View { Width = 32, Height = 32 };
  864. top.App = ApplicationImpl.Instance;
  865. var text = $"First line{Environment.NewLine}Second line";
  866. var horizontalView = new View { Width = 20, Height = 1, Text = text };
  867. // Autosize is off, so we have to explicitly set TextFormatter.Size
  868. horizontalView.TextFormatter.ConstrainToSize = new (20, 1);
  869. var verticalView = new View
  870. {
  871. Y = 3,
  872. Height = 20,
  873. Width = 1,
  874. Text = text,
  875. TextDirection = TextDirection.TopBottom_LeftRight
  876. };
  877. // Autosize is off, so we have to explicitly set TextFormatter.Size
  878. verticalView.TextFormatter.ConstrainToSize = new (1, 20);
  879. var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window", BorderStyle = LineStyle.Single };
  880. frame.Add (horizontalView, verticalView);
  881. top.Add (frame);
  882. top.BeginInit ();
  883. top.EndInit ();
  884. Assert.Equal (new (0, 0, 20, 1), horizontalView.Frame);
  885. Assert.Equal (new (0, 3, 1, 20), verticalView.Frame);
  886. top.Draw ();
  887. var expected = @"
  888. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  889. โ”‚First line Second li โ”‚
  890. โ”‚ โ”‚
  891. โ”‚ โ”‚
  892. โ”‚F โ”‚
  893. โ”‚i โ”‚
  894. โ”‚r โ”‚
  895. โ”‚s โ”‚
  896. โ”‚t โ”‚
  897. โ”‚ โ”‚
  898. โ”‚l โ”‚
  899. โ”‚i โ”‚
  900. โ”‚n โ”‚
  901. โ”‚e โ”‚
  902. โ”‚ โ”‚
  903. โ”‚S โ”‚
  904. โ”‚e โ”‚
  905. โ”‚c โ”‚
  906. โ”‚o โ”‚
  907. โ”‚n โ”‚
  908. โ”‚d โ”‚
  909. โ”‚ โ”‚
  910. โ”‚l โ”‚
  911. โ”‚i โ”‚
  912. โ”‚ โ”‚
  913. โ”‚ โ”‚
  914. โ”‚ โ”‚
  915. โ”‚ โ”‚
  916. โ”‚ โ”‚
  917. โ”‚ โ”‚
  918. โ”‚ โ”‚
  919. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  920. ";
  921. Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  922. verticalView.Text = $"ๆœ€ๅˆใฎ่กŒ{Environment.NewLine}ไบŒ่กŒ็›ฎ";
  923. Assert.True (verticalView.TextFormatter.NeedsFormat);
  924. // Autosize is off, so we have to explicitly set TextFormatter.Size
  925. // We know these glpyhs are 2 cols wide, so we need to widen the view
  926. verticalView.Width = 2;
  927. verticalView.TextFormatter.ConstrainToSize = new (2, 20);
  928. Assert.True (verticalView.TextFormatter.NeedsFormat);
  929. top.SetClipToScreen ();
  930. top.Draw ();
  931. Assert.Equal (new (0, 3, 2, 20), verticalView.Frame);
  932. expected = @"
  933. โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  934. โ”‚First line Second li โ”‚
  935. โ”‚ โ”‚
  936. โ”‚ โ”‚
  937. โ”‚ๆœ€ โ”‚
  938. โ”‚ๅˆ โ”‚
  939. โ”‚ใฎ โ”‚
  940. โ”‚่กŒ โ”‚
  941. โ”‚ โ”‚
  942. โ”‚ไบŒ โ”‚
  943. โ”‚่กŒ โ”‚
  944. โ”‚็›ฎ โ”‚
  945. โ”‚ โ”‚
  946. โ”‚ โ”‚
  947. โ”‚ โ”‚
  948. โ”‚ โ”‚
  949. โ”‚ โ”‚
  950. โ”‚ โ”‚
  951. โ”‚ โ”‚
  952. โ”‚ โ”‚
  953. โ”‚ โ”‚
  954. โ”‚ โ”‚
  955. โ”‚ โ”‚
  956. โ”‚ โ”‚
  957. โ”‚ โ”‚
  958. โ”‚ โ”‚
  959. โ”‚ โ”‚
  960. โ”‚ โ”‚
  961. โ”‚ โ”‚
  962. โ”‚ โ”‚
  963. โ”‚ โ”‚
  964. โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  965. ";
  966. pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  967. }
  968. [Fact]
  969. [SetupFakeApplication]
  970. public void SetText_RendersCorrectly ()
  971. {
  972. View view;
  973. var text = "test";
  974. view = new Label { App = ApplicationImpl.Instance, Text = text };
  975. view.BeginInit ();
  976. view.EndInit ();
  977. view.Draw ();
  978. DriverAssert.AssertDriverContentsWithFrameAre (text, output);
  979. }
  980. }