LabelTests.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. using System.ComponentModel;
  2. using Microsoft.VisualStudio.TestPlatform.ObjectModel;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewsTests;
  5. public class LabelTests (ITestOutputHelper output)
  6. {
  7. // Test that Title and Text are the same
  8. [Fact]
  9. public void Text_Mirrors_Title ()
  10. {
  11. var label = new Label ();
  12. label.Title = "Hello";
  13. Assert.Equal ("Hello", label.Title);
  14. Assert.Equal ("Hello", label.TitleTextFormatter.Text);
  15. Assert.Equal ("Hello", label.Text);
  16. Assert.Equal ("Hello", label.TextFormatter.Text);
  17. }
  18. [Fact]
  19. public void Title_Mirrors_Text ()
  20. {
  21. var label = new Label ();
  22. label.Text = "Hello";
  23. Assert.Equal ("Hello", label.Text);
  24. Assert.Equal ("Hello", label.TextFormatter.Text);
  25. Assert.Equal ("Hello", label.Title);
  26. Assert.Equal ("Hello", label.TitleTextFormatter.Text);
  27. }
  28. [Theory]
  29. [CombinatorialData]
  30. public void HotKey_Command_SetsFocus_OnNextSubview (bool hasHotKey)
  31. {
  32. var superView = new View { CanFocus = true };
  33. var label = new Label ();
  34. label.HotKey = hasHotKey ? Key.A.WithAlt : Key.Empty;
  35. var nextSubview = new View { CanFocus = true };
  36. superView.Add (label, nextSubview);
  37. superView.BeginInit ();
  38. superView.EndInit ();
  39. Assert.False (label.HasFocus);
  40. Assert.False (nextSubview.HasFocus);
  41. label.InvokeCommand (Command.HotKey);
  42. Assert.False (label.HasFocus);
  43. Assert.Equal (hasHotKey, nextSubview.HasFocus);
  44. }
  45. [Theory]
  46. [CombinatorialData]
  47. public void MouseClick_SetsFocus_OnNextSubview (bool hasHotKey)
  48. {
  49. var superView = new View { CanFocus = true, Height = 1, Width = 15 };
  50. var focusedView = new View { CanFocus = true, Width = 1, Height = 1 };
  51. var label = new Label { X = 2 };
  52. label.HotKey = hasHotKey ? Key.X.WithAlt : Key.Empty;
  53. var nextSubview = new View { CanFocus = true, X = 4, Width = 4, Height = 1 };
  54. superView.Add (focusedView, label, nextSubview);
  55. superView.BeginInit ();
  56. superView.EndInit ();
  57. Assert.False (focusedView.HasFocus);
  58. Assert.False (label.HasFocus);
  59. Assert.False (nextSubview.HasFocus);
  60. label.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked });
  61. Assert.False (label.HasFocus);
  62. Assert.Equal (hasHotKey, nextSubview.HasFocus);
  63. }
  64. [Fact]
  65. public void HotKey_Command_Does_Not_Accept ()
  66. {
  67. var label = new Label ();
  68. var accepted = false;
  69. label.Accepting += LabelOnAccept;
  70. label.InvokeCommand (Command.HotKey);
  71. Assert.False (accepted);
  72. return;
  73. void LabelOnAccept (object sender, CommandEventArgs e) { accepted = true; }
  74. }
  75. [Fact]
  76. [AutoInitShutdown]
  77. public void Text_Set_With_AnchorEnd_Works ()
  78. {
  79. var label = new Label { Y = Pos.Center (), Text = "Say Hello 你" };
  80. label.X = Pos.AnchorEnd (0) - Pos.Func (() => label.TextFormatter.Text.GetColumns ());
  81. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  82. win.Add (label);
  83. var top = new Toplevel ();
  84. top.Add (win);
  85. Application.Begin (top);
  86. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  87. var expected = @"
  88. ┌────────────────────────────┐
  89. │ │
  90. │ Say Hello 你│
  91. │ │
  92. └────────────────────────────┘
  93. ";
  94. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  95. label.Text = "Say Hello 你 changed";
  96. Application.LayoutAndDraw ();
  97. expected = @"
  98. ┌────────────────────────────┐
  99. │ │
  100. │ Say Hello 你 changed│
  101. │ │
  102. └────────────────────────────┘
  103. ";
  104. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  105. top.Dispose ();
  106. }
  107. [Fact]
  108. [AutoInitShutdown]
  109. public void Set_Text_With_Center ()
  110. {
  111. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  112. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  113. win.Add (label);
  114. var top = new Toplevel ();
  115. top.Add (win);
  116. Application.Begin (top);
  117. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  118. var expected = @"
  119. ┌────────────────────────────┐
  120. │ │
  121. │ Say Hello 你 │
  122. │ │
  123. └────────────────────────────┘
  124. ";
  125. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  126. label.Text = "Say Hello 你 changed";
  127. Application.LayoutAndDraw ();
  128. expected = @"
  129. ┌────────────────────────────┐
  130. │ │
  131. │ Say Hello 你 changed │
  132. │ │
  133. └────────────────────────────┘
  134. ";
  135. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  136. top.Dispose ();
  137. }
  138. [Fact]
  139. public void Constructors_Defaults ()
  140. {
  141. var label = new Label ();
  142. Assert.Equal (string.Empty, label.Text);
  143. Assert.Equal (Alignment.Start, label.TextAlignment);
  144. Assert.False (label.CanFocus);
  145. Assert.Equal (new (0, 0, 0, 0), label.Frame);
  146. Assert.Equal (KeyCode.Null, label.HotKey);
  147. }
  148. [Fact]
  149. [AutoInitShutdown]
  150. public void Label_Draw_Fill_Remaining ()
  151. {
  152. var tfSize = new Size (80, 1);
  153. var label = new Label { Text = "This label needs to be cleared before rewritten.", Width = tfSize.Width, Height = tfSize.Height };
  154. var tf1 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize };
  155. tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
  156. var tf2 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize, FillRemaining = true };
  157. tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
  158. var top = new Toplevel ();
  159. top.Add (label);
  160. RunState runState = Application.Begin (top);
  161. Application.RunIteration (ref runState);
  162. Assert.False (label.TextFormatter.FillRemaining);
  163. Assert.False (tf1.FillRemaining);
  164. Assert.True (tf2.FillRemaining);
  165. tf1.Draw (new (new (0, 1), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
  166. tf2.Draw (new (new (0, 2), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
  167. TestHelpers.AssertDriverContentsWithFrameAre (
  168. @"
  169. This label needs to be cleared before rewritten.
  170. This TextFormatter (tf1) without fill will not be cleared on rewritten.
  171. This TextFormatter (tf2) with fill will be cleared on rewritten. ",
  172. output
  173. );
  174. Assert.False (label.NeedsDraw);
  175. Assert.False (label.NeedsLayout);
  176. Assert.False (label.SubViewNeedsDraw);
  177. label.Text = "This label is rewritten.";
  178. Assert.True (label.NeedsDraw);
  179. Assert.True (label.NeedsLayout);
  180. //Assert.False (label.SubViewNeedsDraw);
  181. label.Draw ();
  182. tf1.Text = "This TextFormatter (tf1) is rewritten.";
  183. tf1.Draw (new (new (0, 1), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
  184. tf2.Text = "This TextFormatter (tf2) is rewritten.";
  185. tf2.Draw (new (new (0, 2), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
  186. TestHelpers.AssertDriverContentsWithFrameAre (
  187. @"
  188. This label is rewritten.
  189. This TextFormatter (tf1) is rewritten.will not be cleared on rewritten.
  190. This TextFormatter (tf2) is rewritten. ",
  191. output
  192. );
  193. top.Dispose ();
  194. }
  195. [Fact]
  196. [AutoInitShutdown]
  197. public void Label_Draw_Horizontal_Simple_Runes ()
  198. {
  199. var label = new Label { Text = "Demo Simple Rune" };
  200. var top = new Toplevel ();
  201. top.Add (label);
  202. Application.Begin (top);
  203. Application.LayoutAndDraw ();
  204. Assert.Equal (new (0, 0, 16, 1), label.Frame);
  205. var expected = @"
  206. Demo Simple Rune
  207. ";
  208. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  209. Assert.Equal (new (0, 0, 16, 1), pos);
  210. top.Dispose ();
  211. }
  212. [Fact]
  213. [AutoInitShutdown]
  214. public void Label_Draw_Vertical_Simple_Runes ()
  215. {
  216. var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "Demo Simple Rune" };
  217. var top = new Toplevel ();
  218. top.Add (label);
  219. Application.Begin (top);
  220. Application.LayoutAndDraw ();
  221. Assert.NotNull (label.Width);
  222. Assert.NotNull (label.Height);
  223. var expected = @"
  224. D
  225. e
  226. m
  227. o
  228. S
  229. i
  230. m
  231. p
  232. l
  233. e
  234. R
  235. u
  236. n
  237. e
  238. ";
  239. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  240. Assert.Equal (new (0, 0, 1, 16), pos);
  241. top.Dispose ();
  242. }
  243. [Fact]
  244. [AutoInitShutdown]
  245. public void Label_Draw_Vertical_Wide_Runes ()
  246. {
  247. var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "デモエムポンズ" };
  248. var top = new Toplevel ();
  249. top.Add (label);
  250. Application.Begin (top);
  251. Application.LayoutAndDraw ();
  252. var expected = @"
  253. ";
  254. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  255. Assert.Equal (new (0, 0, 2, 7), pos);
  256. top.Dispose ();
  257. }
  258. [Fact]
  259. public void Label_HotKeyChanged_EventFires ()
  260. {
  261. var label = new Label { Text = "Yar" };
  262. label.HotKey = 'Y';
  263. object sender = null;
  264. KeyChangedEventArgs args = null;
  265. label.HotKeyChanged += (s, e) =>
  266. {
  267. sender = s;
  268. args = e;
  269. };
  270. label.HotKey = Key.R;
  271. Assert.Same (label, sender);
  272. Assert.Equal (KeyCode.Y | KeyCode.ShiftMask, args.OldKey);
  273. Assert.Equal (Key.R, args.NewKey);
  274. }
  275. [Fact]
  276. public void Label_HotKeyChanged_EventFires_WithNone ()
  277. {
  278. var label = new Label ();
  279. object sender = null;
  280. KeyChangedEventArgs args = null;
  281. label.HotKeyChanged += (s, e) =>
  282. {
  283. sender = s;
  284. args = e;
  285. };
  286. label.HotKey = KeyCode.R;
  287. Assert.Same (label, sender);
  288. Assert.Equal (KeyCode.Null, args.OldKey);
  289. Assert.Equal (KeyCode.R, args.NewKey);
  290. }
  291. [Fact]
  292. public void TestAssignTextToLabel ()
  293. {
  294. View b = new Label { Text = "heya" };
  295. Assert.Equal ("heya", b.Text);
  296. Assert.Contains ("heya", b.TextFormatter.Text);
  297. b.Text = "heyb";
  298. Assert.Equal ("heyb", b.Text);
  299. Assert.Contains ("heyb", b.TextFormatter.Text);
  300. // with cast
  301. Assert.Equal ("heyb", ((Label)b).Text);
  302. }
  303. [Fact]
  304. [AutoInitShutdown]
  305. public void Update_Only_On_Or_After_Initialize ()
  306. {
  307. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  308. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  309. win.Add (label);
  310. var top = new Toplevel ();
  311. top.Add (win);
  312. Assert.False (label.IsInitialized);
  313. Application.Begin (top);
  314. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  315. Assert.True (label.IsInitialized);
  316. Assert.Equal ("Say Hello 你", label.Text);
  317. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  318. Assert.Equal (new (0, 0, 12, 1), label.Viewport);
  319. var expected = @"
  320. ┌────────────────────────────┐
  321. │ │
  322. │ Say Hello 你 │
  323. │ │
  324. └────────────────────────────┘
  325. ";
  326. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  327. Assert.Equal (new (0, 0, 30, 5), pos);
  328. top.Dispose ();
  329. }
  330. [Fact]
  331. [AutoInitShutdown]
  332. public void Update_Parameterless_Only_On_Or_After_Initialize ()
  333. {
  334. var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
  335. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  336. win.Add (label);
  337. var top = new Toplevel ();
  338. top.Add (win);
  339. Assert.False (label.IsInitialized);
  340. Application.Begin (top);
  341. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  342. Assert.True (label.IsInitialized);
  343. Assert.Equal ("Say Hello 你", label.Text);
  344. Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
  345. Assert.Equal (new (0, 0, 12, 1), label.Viewport);
  346. var expected = @"
  347. ┌────────────────────────────┐
  348. │ │
  349. │ Say Hello 你 │
  350. │ │
  351. └────────────────────────────┘
  352. ";
  353. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  354. Assert.Equal (new (0, 0, 30, 5), pos);
  355. top.Dispose ();
  356. }
  357. [Fact]
  358. [SetupFakeDriver]
  359. public void Full_Border ()
  360. {
  361. var label = new Label { BorderStyle = LineStyle.Single, Text = "Test" };
  362. label.BeginInit ();
  363. label.EndInit ();
  364. label.SetRelativeLayout (Application.Screen.Size);
  365. Assert.Equal (new (0, 0, 4, 1), label.Viewport);
  366. Assert.Equal (new (0, 0, 6, 3), label.Frame);
  367. label.Draw ();
  368. TestHelpers.AssertDriverContentsWithFrameAre (
  369. @"
  370. ┌┤Te├┐
  371. │Test│
  372. └────┘",
  373. output
  374. );
  375. label.Dispose ();
  376. }
  377. [Fact]
  378. [AutoInitShutdown]
  379. public void With_Top_Margin_Without_Top_Border ()
  380. {
  381. var label = new Label { Text = "Test", /*Width = 6, Height = 3,*/ BorderStyle = LineStyle.Single };
  382. label.Margin.Thickness = new (0, 1, 0, 0);
  383. label.Border.Thickness = new (1, 0, 1, 1);
  384. var top = new Toplevel ();
  385. top.Add (label);
  386. Application.Begin (top);
  387. Application.LayoutAndDraw ();
  388. Assert.Equal (new (0, 0, 6, 3), label.Frame);
  389. Assert.Equal (new (0, 0, 4, 1), label.Viewport);
  390. Application.Begin (top);
  391. TestHelpers.AssertDriverContentsWithFrameAre (
  392. @"
  393. │Test│
  394. └────┘",
  395. output
  396. );
  397. top.Dispose ();
  398. }
  399. [Fact]
  400. [AutoInitShutdown]
  401. public void Without_Top_Border ()
  402. {
  403. var label = new Label { Text = "Test", /* Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
  404. label.Border.Thickness = new (1, 0, 1, 1);
  405. var top = new Toplevel ();
  406. top.Add (label);
  407. Application.Begin (top);
  408. Application.LayoutAndDraw ();
  409. Assert.Equal (new (0, 0, 6, 2), label.Frame);
  410. Assert.Equal (new (0, 0, 4, 1), label.Viewport);
  411. Application.Begin (top);
  412. TestHelpers.AssertDriverContentsWithFrameAre (
  413. @"
  414. │Test│
  415. └────┘",
  416. output
  417. );
  418. top.Dispose ();
  419. }
  420. // These tests were formally in AutoSizetrue.cs. They are (poor) Label tests.
  421. private readonly string [] expecteds = new string [21]
  422. {
  423. @"
  424. ┌────────────────────┐
  425. │View with long text │
  426. │ │
  427. └────────────────────┘",
  428. @"
  429. ┌────────────────────┐
  430. │View with long text │
  431. │Label 0 │
  432. │Label 0 │
  433. └────────────────────┘",
  434. @"
  435. ┌────────────────────┐
  436. │View with long text │
  437. │Label 0 │
  438. │Label 1 │
  439. │Label 1 │
  440. └────────────────────┘",
  441. @"
  442. ┌────────────────────┐
  443. │View with long text │
  444. │Label 0 │
  445. │Label 1 │
  446. │Label 2 │
  447. │Label 2 │
  448. └────────────────────┘",
  449. @"
  450. ┌────────────────────┐
  451. │View with long text │
  452. │Label 0 │
  453. │Label 1 │
  454. │Label 2 │
  455. │Label 3 │
  456. │Label 3 │
  457. └────────────────────┘",
  458. @"
  459. ┌────────────────────┐
  460. │View with long text │
  461. │Label 0 │
  462. │Label 1 │
  463. │Label 2 │
  464. │Label 3 │
  465. │Label 4 │
  466. │Label 4 │
  467. └────────────────────┘",
  468. @"
  469. ┌────────────────────┐
  470. │View with long text │
  471. │Label 0 │
  472. │Label 1 │
  473. │Label 2 │
  474. │Label 3 │
  475. │Label 4 │
  476. │Label 5 │
  477. │Label 5 │
  478. └────────────────────┘",
  479. @"
  480. ┌────────────────────┐
  481. │View with long text │
  482. │Label 0 │
  483. │Label 1 │
  484. │Label 2 │
  485. │Label 3 │
  486. │Label 4 │
  487. │Label 5 │
  488. │Label 6 │
  489. │Label 6 │
  490. └────────────────────┘",
  491. @"
  492. ┌────────────────────┐
  493. │View with long text │
  494. │Label 0 │
  495. │Label 1 │
  496. │Label 2 │
  497. │Label 3 │
  498. │Label 4 │
  499. │Label 5 │
  500. │Label 6 │
  501. │Label 7 │
  502. │Label 7 │
  503. └────────────────────┘",
  504. @"
  505. ┌────────────────────┐
  506. │View with long text │
  507. │Label 0 │
  508. │Label 1 │
  509. │Label 2 │
  510. │Label 3 │
  511. │Label 4 │
  512. │Label 5 │
  513. │Label 6 │
  514. │Label 7 │
  515. │Label 8 │
  516. │Label 8 │
  517. └────────────────────┘",
  518. @"
  519. ┌────────────────────┐
  520. │View with long text │
  521. │Label 0 │
  522. │Label 1 │
  523. │Label 2 │
  524. │Label 3 │
  525. │Label 4 │
  526. │Label 5 │
  527. │Label 6 │
  528. │Label 7 │
  529. │Label 8 │
  530. │Label 9 │
  531. │Label 9 │
  532. └────────────────────┘",
  533. @"
  534. ┌────────────────────┐
  535. │View with long text │
  536. │Label 0 │
  537. │Label 1 │
  538. │Label 2 │
  539. │Label 3 │
  540. │Label 4 │
  541. │Label 5 │
  542. │Label 6 │
  543. │Label 7 │
  544. │Label 8 │
  545. │Label 9 │
  546. │Label 10 │
  547. │Label 10 │
  548. └────────────────────┘",
  549. @"
  550. ┌────────────────────┐
  551. │View with long text │
  552. │Label 0 │
  553. │Label 1 │
  554. │Label 2 │
  555. │Label 3 │
  556. │Label 4 │
  557. │Label 5 │
  558. │Label 6 │
  559. │Label 7 │
  560. │Label 8 │
  561. │Label 9 │
  562. │Label 10 │
  563. │Label 11 │
  564. │Label 11 │
  565. └────────────────────┘",
  566. @"
  567. ┌────────────────────┐
  568. │View with long text │
  569. │Label 0 │
  570. │Label 1 │
  571. │Label 2 │
  572. │Label 3 │
  573. │Label 4 │
  574. │Label 5 │
  575. │Label 6 │
  576. │Label 7 │
  577. │Label 8 │
  578. │Label 9 │
  579. │Label 10 │
  580. │Label 11 │
  581. │Label 12 │
  582. │Label 12 │
  583. └────────────────────┘",
  584. @"
  585. ┌────────────────────┐
  586. │View with long text │
  587. │Label 0 │
  588. │Label 1 │
  589. │Label 2 │
  590. │Label 3 │
  591. │Label 4 │
  592. │Label 5 │
  593. │Label 6 │
  594. │Label 7 │
  595. │Label 8 │
  596. │Label 9 │
  597. │Label 10 │
  598. │Label 11 │
  599. │Label 12 │
  600. │Label 13 │
  601. │Label 13 │
  602. └────────────────────┘",
  603. @"
  604. ┌────────────────────┐
  605. │View with long text │
  606. │Label 0 │
  607. │Label 1 │
  608. │Label 2 │
  609. │Label 3 │
  610. │Label 4 │
  611. │Label 5 │
  612. │Label 6 │
  613. │Label 7 │
  614. │Label 8 │
  615. │Label 9 │
  616. │Label 10 │
  617. │Label 11 │
  618. │Label 12 │
  619. │Label 13 │
  620. │Label 14 │
  621. │Label 14 │
  622. └────────────────────┘",
  623. @"
  624. ┌────────────────────┐
  625. │View with long text │
  626. │Label 0 │
  627. │Label 1 │
  628. │Label 2 │
  629. │Label 3 │
  630. │Label 4 │
  631. │Label 5 │
  632. │Label 6 │
  633. │Label 7 │
  634. │Label 8 │
  635. │Label 9 │
  636. │Label 10 │
  637. │Label 11 │
  638. │Label 12 │
  639. │Label 13 │
  640. │Label 14 │
  641. │Label 15 │
  642. │Label 15 │
  643. └────────────────────┘",
  644. @"
  645. ┌────────────────────┐
  646. │View with long text │
  647. │Label 0 │
  648. │Label 1 │
  649. │Label 2 │
  650. │Label 3 │
  651. │Label 4 │
  652. │Label 5 │
  653. │Label 6 │
  654. │Label 7 │
  655. │Label 8 │
  656. │Label 9 │
  657. │Label 10 │
  658. │Label 11 │
  659. │Label 12 │
  660. │Label 13 │
  661. │Label 14 │
  662. │Label 15 │
  663. │Label 16 │
  664. │Label 16 │
  665. └────────────────────┘",
  666. @"
  667. ┌────────────────────┐
  668. │View with long text │
  669. │Label 0 │
  670. │Label 1 │
  671. │Label 2 │
  672. │Label 3 │
  673. │Label 4 │
  674. │Label 5 │
  675. │Label 6 │
  676. │Label 7 │
  677. │Label 8 │
  678. │Label 9 │
  679. │Label 10 │
  680. │Label 11 │
  681. │Label 12 │
  682. │Label 13 │
  683. │Label 14 │
  684. │Label 15 │
  685. │Label 16 │
  686. │Label 17 │
  687. │Label 17 │
  688. └────────────────────┘",
  689. @"
  690. ┌────────────────────┐
  691. │View with long text │
  692. │Label 0 │
  693. │Label 1 │
  694. │Label 2 │
  695. │Label 3 │
  696. │Label 4 │
  697. │Label 5 │
  698. │Label 6 │
  699. │Label 7 │
  700. │Label 8 │
  701. │Label 9 │
  702. │Label 10 │
  703. │Label 11 │
  704. │Label 12 │
  705. │Label 13 │
  706. │Label 14 │
  707. │Label 15 │
  708. │Label 16 │
  709. │Label 17 │
  710. │Label 18 │
  711. │Label 18 │
  712. └────────────────────┘",
  713. @"
  714. ┌────────────────────┐
  715. │View with long text │
  716. │Label 0 │
  717. │Label 1 │
  718. │Label 2 │
  719. │Label 3 │
  720. │Label 4 │
  721. │Label 5 │
  722. │Label 6 │
  723. │Label 7 │
  724. │Label 8 │
  725. │Label 9 │
  726. │Label 10 │
  727. │Label 11 │
  728. │Label 12 │
  729. │Label 13 │
  730. │Label 14 │
  731. │Label 15 │
  732. │Label 16 │
  733. │Label 17 │
  734. │Label 18 │
  735. │Label 19 │
  736. │Label 19 │
  737. └────────────────────┘"
  738. };
  739. private static readonly Size _size1x1 = new (1, 1);
  740. // TODO: This is a Label test. Move to label tests if there's not already a test for this.
  741. [Fact]
  742. [AutoInitShutdown]
  743. public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
  744. {
  745. var win = new Window ();
  746. var label = new Label
  747. {
  748. Text = "This should be the last line.",
  749. ColorScheme = Colors.ColorSchemes ["Menu"],
  750. //Width = Dim.Fill (),
  751. X = 0, // keep unit test focused; don't use Center here
  752. Y = Pos.AnchorEnd (1)
  753. };
  754. win.Add (label);
  755. Toplevel top = new ();
  756. top.Add (win);
  757. RunState rs = Application.Begin (top);
  758. ((FakeDriver)Application.Driver!).SetBufferSize (40, 10);
  759. Assert.Equal (29, label.Text.Length);
  760. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  761. Assert.Equal (new (0, 0, 40, 10), win.Frame);
  762. Assert.Equal (new (0, 7, 29, 1), label.Frame);
  763. var expected = @"
  764. ┌──────────────────────────────────────┐
  765. │ │
  766. │ │
  767. │ │
  768. │ │
  769. │ │
  770. │ │
  771. │ │
  772. │This should be the last line. │
  773. └──────────────────────────────────────┘
  774. "
  775. ;
  776. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  777. Application.End (rs);
  778. top.Dispose ();
  779. }
  780. // TODO: This is a Label test. Move to label tests if there's not already a test for this.
  781. [Fact]
  782. [AutoInitShutdown]
  783. public void Bottom_Equal_Inside_Window ()
  784. {
  785. var win = new Window ();
  786. var label = new Label
  787. {
  788. Text = "This should be the last line.",
  789. ColorScheme = Colors.ColorSchemes ["Menu"],
  790. //Width = Dim.Fill (),
  791. X = 0,
  792. Y = Pos.Bottom (win)
  793. - 3 // two lines top and bottom borders more one line above the bottom border
  794. };
  795. win.Add (label);
  796. Toplevel top = new ();
  797. top.Add (win);
  798. RunState rs = Application.Begin (top);
  799. ((FakeDriver)Application.Driver!).SetBufferSize (40, 10);
  800. Assert.Equal (new (0, 0, 40, 10), top.Frame);
  801. Assert.Equal (new (0, 0, 40, 10), win.Frame);
  802. Assert.Equal (new (0, 7, 29, 1), label.Frame);
  803. var expected = @"
  804. ┌──────────────────────────────────────┐
  805. │ │
  806. │ │
  807. │ │
  808. │ │
  809. │ │
  810. │ │
  811. │ │
  812. │This should be the last line. │
  813. └──────────────────────────────────────┘
  814. ";
  815. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  816. Application.End (rs);
  817. top.Dispose ();
  818. }
  819. // TODO: This is a Dim test. Move to Dim tests.
  820. [Fact]
  821. [AutoInitShutdown]
  822. public void Dim_Subtract_Operator_With_Text ()
  823. {
  824. Toplevel top = new ();
  825. var view = new View
  826. {
  827. Text = "View with long text",
  828. X = 0,
  829. Y = 0,
  830. Width = 20,
  831. Height = 1
  832. };
  833. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  834. var count = 20;
  835. List<Label> listLabels = new ();
  836. for (var i = 0; i < count; i++)
  837. {
  838. field.Text = $"Label {i}";
  839. var label = new Label { Text = field.Text, X = 0, Y = i + 1 /*, Width = 10*/ };
  840. view.Add (label);
  841. Assert.Equal ($"Label {i}", label.Text);
  842. Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
  843. listLabels.Add (label);
  844. if (i == 0)
  845. {
  846. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  847. view.Height += 1;
  848. Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  849. }
  850. else
  851. {
  852. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  853. view.Height += 1;
  854. Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  855. }
  856. }
  857. field.KeyDown += (s, k) =>
  858. {
  859. if (k.KeyCode == KeyCode.Enter)
  860. {
  861. ((FakeDriver)Application.Driver!).SetBufferSize (22, count + 4);
  862. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  863. Assert.Equal (new (0, 0, 22, count + 4), pos);
  864. if (count > 0)
  865. {
  866. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  867. view.Remove (listLabels [count - 1]);
  868. listLabels [count - 1].Dispose ();
  869. listLabels.RemoveAt (count - 1);
  870. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  871. view.Height -= 1;
  872. count--;
  873. if (listLabels.Count > 0)
  874. {
  875. field.Text = listLabels [count - 1].Text;
  876. }
  877. else
  878. {
  879. field.Text = string.Empty;
  880. }
  881. }
  882. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  883. }
  884. };
  885. Application.Iteration += (s, a) =>
  886. {
  887. while (count > -1)
  888. {
  889. field.NewKeyDownEvent (Key.Enter);
  890. if (count == 0)
  891. {
  892. field.NewKeyDownEvent (Key.Enter);
  893. break;
  894. }
  895. }
  896. Application.RequestStop ();
  897. };
  898. var win = new Window ();
  899. win.Add (view);
  900. win.Add (field);
  901. top.Add (win);
  902. Application.Run (top);
  903. Assert.Equal (0, count);
  904. Assert.Equal (count, listLabels.Count);
  905. top.Dispose ();
  906. }
  907. // TODO: This is a Label test. Move to Label tests.
  908. [Fact]
  909. [SetupFakeDriver]
  910. public void Label_Height_Zero_Stays_Zero ()
  911. {
  912. ((FakeDriver)Application.Driver!).SetBufferSize (10, 4);
  913. var text = "Label";
  914. var label = new Label
  915. {
  916. Text = text
  917. };
  918. label.Width = Dim.Fill () - text.Length;
  919. label.Height = 0;
  920. var win = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  921. win.Add (label);
  922. win.BeginInit ();
  923. win.EndInit ();
  924. win.LayoutSubviews ();
  925. win.Draw ();
  926. Assert.Equal (5, text.Length);
  927. Assert.Equal (new (0, 0, 3, 0), label.Frame);
  928. //Assert.Equal (new (5, 1), label.TextFormatter.Size);
  929. Assert.Single (label.TextFormatter.GetLines ());
  930. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  931. var expected = @"
  932. ┌────────┐
  933. │ │
  934. │ │
  935. └────────┘
  936. ";
  937. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  938. Assert.Equal (new (0, 0, 10, 4), pos);
  939. text = "0123456789";
  940. Assert.Equal (10, text.Length);
  941. label.Width = Dim.Fill () - text.Length;
  942. win.LayoutSubviews ();
  943. win.ClearViewport ();
  944. win.Draw ();
  945. Assert.Equal (Rectangle.Empty, label.Frame);
  946. // Assert.Equal (new (5, 1), label.TextFormatter.Size);
  947. //Exception exception = Record.Exception (
  948. // () => Assert.Equal (
  949. // new List<string> { string.Empty },
  950. // label.TextFormatter.GetLines ()
  951. // )
  952. // );
  953. //Assert.Null (exception);
  954. expected = @"
  955. ┌────────┐
  956. │ │
  957. │ │
  958. └────────┘
  959. ";
  960. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  961. Assert.Equal (new (0, 0, 10, 4), pos);
  962. }
  963. [Fact]
  964. [AutoInitShutdown]
  965. public void Dim_Add_Operator_With_Text ()
  966. {
  967. Toplevel top = new ();
  968. var view = new View
  969. {
  970. Text = "View with long text",
  971. X = 0,
  972. Y = 0,
  973. Width = 20,
  974. Height = 1
  975. };
  976. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  977. var count = 0;
  978. List<Label> listLabels = new ();
  979. field.KeyDown += (s, k) =>
  980. {
  981. if (k.KeyCode == KeyCode.Enter)
  982. {
  983. ((FakeDriver)Application.Driver!).SetBufferSize (22, count + 4);
  984. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  985. Assert.Equal (new (0, 0, 22, count + 4), pos);
  986. if (count < 20)
  987. {
  988. field.Text = $"Label {count}";
  989. var label = new Label { Text = field.Text, X = 0, Y = view.Viewport.Height /*, Width = 10*/ };
  990. view.Add (label);
  991. Assert.Equal ($"Label {count}", label.Text);
  992. Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
  993. listLabels.Add (label);
  994. //if (count == 0) {
  995. // Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  996. // view.Height += 2;
  997. //} else {
  998. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  999. view.Height += 1;
  1000. //}
  1001. count++;
  1002. }
  1003. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1004. }
  1005. };
  1006. Application.Iteration += (s, a) =>
  1007. {
  1008. while (count < 21)
  1009. {
  1010. field.NewKeyDownEvent (Key.Enter);
  1011. if (count == 20)
  1012. {
  1013. field.NewKeyDownEvent (Key.Enter);
  1014. break;
  1015. }
  1016. }
  1017. Application.RequestStop ();
  1018. };
  1019. var win = new Window ();
  1020. win.Add (view);
  1021. win.Add (field);
  1022. top.Add (win);
  1023. Application.Run (top);
  1024. Assert.Equal (20, count);
  1025. Assert.Equal (count, listLabels.Count);
  1026. top.Dispose ();
  1027. }
  1028. [Fact]
  1029. [AutoInitShutdown]
  1030. public void Label_IsEmpty_False_Minimum_Height ()
  1031. {
  1032. var text = "Label";
  1033. var label = new Label
  1034. {
  1035. //Width = Dim.Fill () - text.Length,
  1036. Text = text
  1037. };
  1038. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1039. win.Add (label);
  1040. var top = new Toplevel ();
  1041. top.Add (win);
  1042. Application.Begin (top);
  1043. ((FakeDriver)Application.Driver!).SetBufferSize (10, 4);
  1044. Assert.Equal (5, text.Length);
  1045. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1046. Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
  1047. Assert.Equal (["Label"], label.TextFormatter.GetLines ());
  1048. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  1049. Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
  1050. var expected = @"
  1051. ┌────────┐
  1052. │Label │
  1053. │ │
  1054. └────────┘
  1055. ";
  1056. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1057. Assert.Equal (new (0, 0, 10, 4), pos);
  1058. text = "0123456789";
  1059. Assert.Equal (10, text.Length);
  1060. //label.Width = Dim.Fill () - text.Length;
  1061. Application.LayoutAndDraw ();
  1062. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1063. Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
  1064. Exception exception = Record.Exception (() => Assert.Single (label.TextFormatter.GetLines ()));
  1065. Assert.Null (exception);
  1066. expected = @"
  1067. ┌────────┐
  1068. │Label │
  1069. │ │
  1070. └────────┘
  1071. ";
  1072. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1073. Assert.Equal (new (0, 0, 10, 4), pos);
  1074. top.Dispose ();
  1075. }
  1076. [Fact]
  1077. [AutoInitShutdown]
  1078. public void Label_IsEmpty_False_Never_Return_Null_Lines ()
  1079. {
  1080. var text = "Label";
  1081. var label = new Label
  1082. {
  1083. //Width = Dim.Fill () - text.Length,
  1084. //Height = 1,
  1085. Text = text
  1086. };
  1087. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  1088. win.Add (label);
  1089. var top = new Toplevel ();
  1090. top.Add (win);
  1091. Application.Begin (top);
  1092. ((FakeDriver)Application.Driver!).SetBufferSize (10, 4);
  1093. Assert.Equal (5, text.Length);
  1094. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1095. Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
  1096. Assert.Equal (["Label"], label.TextFormatter.GetLines ());
  1097. Assert.Equal (new (0, 0, 10, 4), win.Frame);
  1098. Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
  1099. var expected = @"
  1100. ┌────────┐
  1101. │Label │
  1102. │ │
  1103. └────────┘
  1104. ";
  1105. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1106. Assert.Equal (new (0, 0, 10, 4), pos);
  1107. text = "0123456789";
  1108. Assert.Equal (10, text.Length);
  1109. //label.Width = Dim.Fill () - text.Length;
  1110. Application.LayoutAndDraw ();
  1111. Assert.Equal (new (0, 0, 5, 1), label.Frame);
  1112. Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
  1113. Assert.Single (label.TextFormatter.GetLines ());
  1114. expected = @"
  1115. ┌────────┐
  1116. │Label │
  1117. │ │
  1118. └────────┘
  1119. ";
  1120. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1121. Assert.Equal (new (0, 0, 10, 4), pos);
  1122. top.Dispose ();
  1123. }
  1124. [Fact]
  1125. public void Label_ResizeView_With_Dim_Absolute ()
  1126. {
  1127. var super = new View
  1128. {
  1129. Width = Dim.Fill (),
  1130. Height = Dim.Fill ()
  1131. };
  1132. var label = new Label ();
  1133. label.Text = "New text";
  1134. super.Add (label);
  1135. super.LayoutSubviews ();
  1136. Rectangle expectedLabelBounds = new (0, 0, 8, 1);
  1137. Assert.Equal (expectedLabelBounds, label.Viewport);
  1138. super.Dispose ();
  1139. }
  1140. [Fact]
  1141. public void CanFocus_False_HotKey_SetsFocus_Next ()
  1142. {
  1143. View otherView = new ()
  1144. {
  1145. Text = "otherView",
  1146. CanFocus = true
  1147. };
  1148. Label label = new ()
  1149. {
  1150. Text = "_label"
  1151. };
  1152. View nextView = new ()
  1153. {
  1154. Text = "nextView",
  1155. CanFocus = true
  1156. };
  1157. Application.Navigation = new ();
  1158. Application.Top = new ();
  1159. Application.Top.Add (otherView, label, nextView);
  1160. Application.Top.SetFocus ();
  1161. Assert.True (otherView.HasFocus);
  1162. Assert.True (Application.RaiseKeyDownEvent (label.HotKey));
  1163. Assert.False (otherView.HasFocus);
  1164. Assert.False (label.HasFocus);
  1165. Assert.True (nextView.HasFocus);
  1166. Application.Top.Dispose ();
  1167. Application.ResetState ();
  1168. }
  1169. [Fact]
  1170. public void CanFocus_False_MouseClick_SetsFocus_Next ()
  1171. {
  1172. View otherView = new () { X = 0, Y = 0, Width = 1, Height = 1, Id = "otherView", CanFocus = true };
  1173. Label label = new () { X = 0, Y = 1, Text = "_label" };
  1174. View nextView = new () { X = Pos.Right (label), Y = Pos.Top (label), Width = 1, Height = 1, Id = "nextView", CanFocus = true };
  1175. Application.Navigation = new ();
  1176. Application.Top = new ();
  1177. Application.Top.Add (otherView, label, nextView);
  1178. Application.Top.Layout ();
  1179. Application.Top.SetFocus ();
  1180. // click on label
  1181. Application.RaiseMouseEvent (new () { ScreenPosition = label.Frame.Location, Flags = MouseFlags.Button1Clicked });
  1182. Assert.False (label.HasFocus);
  1183. Assert.True (nextView.HasFocus);
  1184. Application.Top.Dispose ();
  1185. Application.ResetState ();
  1186. }
  1187. [Fact]
  1188. public void CanFocus_True_HotKey_SetsFocus ()
  1189. {
  1190. Label label = new ()
  1191. {
  1192. Text = "_label",
  1193. CanFocus = true
  1194. };
  1195. View view = new ()
  1196. {
  1197. Text = "view",
  1198. CanFocus = true
  1199. };
  1200. Application.Navigation = new ();
  1201. Application.Top = new ();
  1202. Application.Top.Add (label, view);
  1203. view.SetFocus ();
  1204. Assert.True (label.CanFocus);
  1205. Assert.False (label.HasFocus);
  1206. Assert.True (view.CanFocus);
  1207. Assert.True (view.HasFocus);
  1208. // No focused view accepts Tab, and there's no other view to focus, so OnKeyDown returns false
  1209. Assert.True (Application.RaiseKeyDownEvent (label.HotKey));
  1210. Assert.True (label.HasFocus);
  1211. Assert.False (view.HasFocus);
  1212. Application.Top.Dispose ();
  1213. Application.ResetState ();
  1214. }
  1215. [Fact]
  1216. public void CanFocus_True_MouseClick_Focuses ()
  1217. {
  1218. Application.Navigation = new ();
  1219. Label label = new ()
  1220. {
  1221. Text = "label",
  1222. X = 0,
  1223. Y = 0,
  1224. CanFocus = true
  1225. };
  1226. View otherView = new ()
  1227. {
  1228. Text = "view",
  1229. X = 0,
  1230. Y = 1,
  1231. Width = 4,
  1232. Height = 1,
  1233. CanFocus = true
  1234. };
  1235. Application.Top = new ()
  1236. {
  1237. Width = 10,
  1238. Height = 10
  1239. };
  1240. Application.Top.Add (label, otherView);
  1241. Application.Top.SetFocus ();
  1242. Application.Top.Layout ();
  1243. Assert.True (label.CanFocus);
  1244. Assert.True (label.HasFocus);
  1245. Assert.True (otherView.CanFocus);
  1246. Assert.False (otherView.HasFocus);
  1247. otherView.SetFocus ();
  1248. Assert.True (otherView.HasFocus);
  1249. // label can focus, so clicking on it set focus
  1250. Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Clicked });
  1251. Assert.True (label.HasFocus);
  1252. Assert.False (otherView.HasFocus);
  1253. // click on view
  1254. Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 1), Flags = MouseFlags.Button1Clicked });
  1255. Assert.False (label.HasFocus);
  1256. Assert.True (otherView.HasFocus);
  1257. Application.Top.Dispose ();
  1258. Application.ResetState ();
  1259. }
  1260. // https://github.com/gui-cs/Terminal.Gui/issues/3893
  1261. [Fact]
  1262. [SetupFakeDriver]
  1263. public void TestLabelUnderscoreMinus ()
  1264. {
  1265. var lbl = new Label ()
  1266. {
  1267. Text = "TextView with some more test_- text. Unicode shouldn't 𝔹Aℝ𝔽!"
  1268. };
  1269. lbl.Draw ();
  1270. }
  1271. }