LabelTests.cs 46 KB

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