TabViewTests.cs 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. using System;
  2. using System.Globalization;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewsTests;
  6. public class TabViewTests {
  7. readonly ITestOutputHelper _output;
  8. public TabViewTests (ITestOutputHelper output) => this._output = output;
  9. TabView GetTabView () => GetTabView (out _, out _);
  10. TabView GetTabView (out Tab tab1, out Tab tab2, bool initFakeDriver = true)
  11. {
  12. if (initFakeDriver) {
  13. InitFakeDriver ();
  14. }
  15. var tv = new TabView ();
  16. tv.BeginInit ();
  17. tv.EndInit ();
  18. tv.ColorScheme = new ColorScheme ();
  19. tv.AddTab (tab1 = new Tab () { DisplayText = "Tab1", View = new TextField("hi") { Width = 2 } }, false);
  20. tv.AddTab (tab2 = new Tab () { DisplayText = "Tab2", View = new Label("hi2") }, false);
  21. return tv;
  22. }
  23. [Fact]
  24. public void AddTwoTabs_SecondIsSelected ()
  25. {
  26. InitFakeDriver ();
  27. var tv = new TabView ();
  28. Tab tab1;
  29. Tab tab2;
  30. tv.AddTab (tab1 = new Tab () { DisplayText = "Tab1", View = new TextField("hi") }, false);
  31. tv.AddTab (tab2 = new Tab () { DisplayText = "Tab1", View = new Label("hi2") }, true);
  32. Assert.Equal (2, tv.Tabs.Count);
  33. Assert.Equal (tab2, tv.SelectedTab);
  34. Application.Shutdown ();
  35. }
  36. [Fact]
  37. public void EnsureSelectedTabVisible_NullSelect ()
  38. {
  39. var tv = GetTabView ();
  40. tv.SelectedTab = null;
  41. Assert.Null (tv.SelectedTab);
  42. Assert.Equal (0, tv.TabScrollOffset);
  43. tv.EnsureSelectedTabIsVisible ();
  44. Assert.Null (tv.SelectedTab);
  45. Assert.Equal (0, tv.TabScrollOffset);
  46. Application.Shutdown ();
  47. }
  48. [Fact]
  49. public void EnsureSelectedTabVisible_MustScroll ()
  50. {
  51. var tv = GetTabView (out var tab1, out var tab2);
  52. // Make tab width small to force only one tab visible at once
  53. tv.Width = 4;
  54. tv.SelectedTab = tab1;
  55. Assert.Equal (0, tv.TabScrollOffset);
  56. tv.EnsureSelectedTabIsVisible ();
  57. Assert.Equal (0, tv.TabScrollOffset);
  58. // Asking to show tab2 should automatically move scroll offset accordingly
  59. tv.SelectedTab = tab2;
  60. Assert.Equal (1, tv.TabScrollOffset);
  61. // Shutdown must be called to safely clean up Application if Init has been called
  62. Application.Shutdown ();
  63. }
  64. [Fact]
  65. public void SelectedTabChanged_Called ()
  66. {
  67. var tv = GetTabView (out var tab1, out var tab2);
  68. tv.SelectedTab = tab1;
  69. Tab oldTab = null;
  70. Tab newTab = null;
  71. var called = 0;
  72. tv.SelectedTabChanged += (s, e) => {
  73. oldTab = e.OldTab;
  74. newTab = e.NewTab;
  75. called++;
  76. };
  77. tv.SelectedTab = tab2;
  78. Assert.Equal (1, called);
  79. Assert.Equal (tab1, oldTab);
  80. Assert.Equal (tab2, newTab);
  81. // Shutdown must be called to safely clean up Application if Init has been called
  82. Application.Shutdown ();
  83. }
  84. [Fact]
  85. public void RemoveTab_ChangesSelection ()
  86. {
  87. var tv = GetTabView (out var tab1, out var tab2);
  88. tv.SelectedTab = tab1;
  89. tv.RemoveTab (tab1);
  90. Assert.Equal (tab2, tv.SelectedTab);
  91. // Shutdown must be called to safely clean up Application if Init has been called
  92. Application.Shutdown ();
  93. }
  94. [Fact]
  95. public void RemoveTab_MultipleCalls_NotAnError ()
  96. {
  97. var tv = GetTabView (out var tab1, out var tab2);
  98. tv.SelectedTab = tab1;
  99. // Repeated calls to remove a tab that is not part of
  100. // the collection should be ignored
  101. tv.RemoveTab (tab1);
  102. tv.RemoveTab (tab1);
  103. tv.RemoveTab (tab1);
  104. tv.RemoveTab (tab1);
  105. Assert.Equal (tab2, tv.SelectedTab);
  106. // Shutdown must be called to safely clean up Application if Init has been called
  107. Application.Shutdown ();
  108. }
  109. [Fact]
  110. public void RemoveAllTabs_ClearsSelection ()
  111. {
  112. var tv = GetTabView (out var tab1, out var tab2);
  113. tv.SelectedTab = tab1;
  114. tv.RemoveTab (tab1);
  115. tv.RemoveTab (tab2);
  116. Assert.Null (tv.SelectedTab);
  117. // Shutdown must be called to safely clean up Application if Init has been called
  118. Application.Shutdown ();
  119. }
  120. [Fact]
  121. public void SwitchTabBy_NormalUsage ()
  122. {
  123. var tv = GetTabView (out var tab1, out var tab2);
  124. Tab tab3;
  125. Tab tab4;
  126. Tab tab5;
  127. tv.AddTab (tab3 = new Tab (), false);
  128. tv.AddTab (tab4 = new Tab (), false);
  129. tv.AddTab (tab5 = new Tab (), false);
  130. tv.SelectedTab = tab1;
  131. var called = 0;
  132. tv.SelectedTabChanged += (s, e) => { called++; };
  133. tv.SwitchTabBy (1);
  134. Assert.Equal (1, called);
  135. Assert.Equal (tab2, tv.SelectedTab);
  136. //reset called counter
  137. called = 0;
  138. // go right 2
  139. tv.SwitchTabBy (2);
  140. // even though we go right 2 indexes the event should only be called once
  141. Assert.Equal (1, called);
  142. Assert.Equal (tab4, tv.SelectedTab);
  143. // Shutdown must be called to safely clean up Application if Init has been called
  144. Application.Shutdown ();
  145. }
  146. [Fact]
  147. public void AddTab_SameTabMoreThanOnce ()
  148. {
  149. var tv = GetTabView (out var tab1, out var tab2);
  150. Assert.Equal (2, tv.Tabs.Count);
  151. // Tab is already part of the control so shouldn't result in duplication
  152. tv.AddTab (tab1, false);
  153. tv.AddTab (tab1, false);
  154. tv.AddTab (tab1, false);
  155. tv.AddTab (tab1, false);
  156. Assert.Equal (2, tv.Tabs.Count);
  157. // Shutdown must be called to safely clean up Application if Init has been called
  158. Application.Shutdown ();
  159. }
  160. [Fact]
  161. public void SwitchTabBy_OutOfTabsRange ()
  162. {
  163. var tv = GetTabView (out var tab1, out var tab2);
  164. tv.SelectedTab = tab1;
  165. tv.SwitchTabBy (500);
  166. Assert.Equal (tab2, tv.SelectedTab);
  167. tv.SwitchTabBy (-500);
  168. Assert.Equal (tab1, tv.SelectedTab);
  169. // Shutdown must be called to safely clean up Application if Init has been called
  170. Application.Shutdown ();
  171. }
  172. [Fact]
  173. [AutoInitShutdown]
  174. public void ShowTopLine_True_TabsOnBottom_False_TestThinTabView_WithLongNames ()
  175. {
  176. var tv = GetTabView (out var tab1, out var tab2, false);
  177. tv.Width = 10;
  178. tv.Height = 5;
  179. // Ensures that the tab bar subview gets the bounds of the parent TabView
  180. tv.LayoutSubviews ();
  181. // Test two tab names that fit
  182. tab1.DisplayText = "12";
  183. tab2.DisplayText = "13";
  184. tv.Draw ();
  185. TestHelpers.AssertDriverContentsWithFrameAre (@"
  186. ╭──┬──╮
  187. │12│13│
  188. │ ╰──┴──╮
  189. │hi │
  190. └────────┘", _output);
  191. tv.SelectedTab = tab2;
  192. tv.Draw ();
  193. TestHelpers.AssertDriverContentsWithFrameAre (@"
  194. ╭──┬──╮
  195. │12│13│
  196. ├──╯ ╰──╮
  197. │hi2 │
  198. └────────┘", _output);
  199. tv.SelectedTab = tab1;
  200. // Test first tab name too long
  201. tab1.DisplayText = "12345678910";
  202. tab2.DisplayText = "13";
  203. tv.Draw ();
  204. TestHelpers.AssertDriverContentsWithFrameAre (@"
  205. ╭───────╮
  206. │1234567│
  207. │ ╰►
  208. │hi │
  209. └────────┘", _output);
  210. //switch to tab2
  211. tv.SelectedTab = tab2;
  212. tv.Draw ();
  213. TestHelpers.AssertDriverContentsWithFrameAre (@"
  214. ╭──╮
  215. │13│
  216. ◄ ╰─────╮
  217. │hi2 │
  218. └────────┘", _output);
  219. // now make both tabs too long
  220. tab1.DisplayText = "12345678910";
  221. tab2.DisplayText = "abcdefghijklmnopq";
  222. tv.Draw ();
  223. TestHelpers.AssertDriverContentsWithFrameAre (@"
  224. ╭───────╮
  225. │abcdefg│
  226. ◄ ╰╮
  227. │hi2 │
  228. └────────┘", _output);
  229. }
  230. [Fact]
  231. [AutoInitShutdown]
  232. public void ShowTopLine_False_TabsOnBottom_False_TestThinTabView_WithLongNames ()
  233. {
  234. var tv = GetTabView (out var tab1, out var tab2, false);
  235. tv.Width = 10;
  236. tv.Height = 5;
  237. tv.Style = new TabStyle { ShowTopLine = false };
  238. tv.ApplyStyleChanges ();
  239. // Ensures that the tab bar subview gets the bounds of the parent TabView
  240. tv.LayoutSubviews ();
  241. // Test two tab names that fit
  242. tab1.DisplayText = "12";
  243. tab2.DisplayText = "13";
  244. tv.Draw ();
  245. TestHelpers.AssertDriverContentsWithFrameAre (@"
  246. │12│13│
  247. │ ╰──┴──╮
  248. │hi │
  249. │ │
  250. └────────┘", _output);
  251. tv.SelectedTab = tab2;
  252. tv.Draw ();
  253. TestHelpers.AssertDriverContentsWithFrameAre (@"
  254. │12│13│
  255. ├──╯ ╰──╮
  256. │hi2 │
  257. │ │
  258. └────────┘", _output);
  259. tv.SelectedTab = tab1;
  260. // Test first tab name too long
  261. tab1.DisplayText = "12345678910";
  262. tab2.DisplayText = "13";
  263. tv.Draw ();
  264. TestHelpers.AssertDriverContentsWithFrameAre (@"
  265. │1234567│
  266. │ ╰►
  267. │hi │
  268. │ │
  269. └────────┘", _output);
  270. //switch to tab2
  271. tv.SelectedTab = tab2;
  272. tv.Draw ();
  273. TestHelpers.AssertDriverContentsWithFrameAre (@"
  274. │13│
  275. ◄ ╰─────╮
  276. │hi2 │
  277. │ │
  278. └────────┘", _output);
  279. // now make both tabs too long
  280. tab1.DisplayText = "12345678910";
  281. tab2.DisplayText = "abcdefghijklmnopq";
  282. tv.Draw ();
  283. TestHelpers.AssertDriverContentsWithFrameAre (@"
  284. │abcdefg│
  285. ◄ ╰╮
  286. │hi2 │
  287. │ │
  288. └────────┘", _output);
  289. }
  290. [Fact]
  291. [AutoInitShutdown]
  292. public void ShowTopLine_True_TabsOnBottom_False_TestTabView_Width4 ()
  293. {
  294. var tv = GetTabView (out _, out _, false);
  295. tv.Width = 4;
  296. tv.Height = 5;
  297. tv.LayoutSubviews ();
  298. tv.Draw ();
  299. TestHelpers.AssertDriverContentsWithFrameAre (@"
  300. ╭─╮
  301. │T│
  302. │ ╰►
  303. │hi│
  304. └──┘", _output);
  305. }
  306. [Fact]
  307. [AutoInitShutdown]
  308. public void ShowTopLine_False_TabsOnBottom_False_TestTabView_Width4 ()
  309. {
  310. var tv = GetTabView (out _, out _, false);
  311. tv.Width = 4;
  312. tv.Height = 5;
  313. tv.Style = new TabStyle { ShowTopLine = false };
  314. tv.ApplyStyleChanges ();
  315. tv.LayoutSubviews ();
  316. tv.Draw ();
  317. TestHelpers.AssertDriverContentsWithFrameAre (@"
  318. │T│
  319. │ ╰►
  320. │hi│
  321. │ │
  322. └──┘", _output);
  323. }
  324. [Fact]
  325. [AutoInitShutdown]
  326. public void ShowTopLine_True_TabsOnBottom_False_TestTabView_Width3 ()
  327. {
  328. var tv = GetTabView (out _, out _, false);
  329. tv.Width = 3;
  330. tv.Height = 5;
  331. tv.LayoutSubviews ();
  332. tv.Draw ();
  333. TestHelpers.AssertDriverContentsWithFrameAre (@"
  334. ╭╮
  335. ││
  336. │╰►
  337. │h│
  338. └─┘", _output);
  339. }
  340. [Fact]
  341. [AutoInitShutdown]
  342. public void ShowTopLine_False_TabsOnBottom_False_TestTabView_Width3 ()
  343. {
  344. var tv = GetTabView (out _, out _, false);
  345. tv.Width = 3;
  346. tv.Height = 5;
  347. tv.Style = new TabStyle { ShowTopLine = false };
  348. tv.ApplyStyleChanges ();
  349. tv.LayoutSubviews ();
  350. tv.Draw ();
  351. TestHelpers.AssertDriverContentsWithFrameAre (@"
  352. ││
  353. │╰►
  354. │h│
  355. │ │
  356. └─┘", _output);
  357. }
  358. [Fact]
  359. [AutoInitShutdown]
  360. public void ShowTopLine_True_TabsOnBottom_True_TestThinTabView_WithLongNames ()
  361. {
  362. var tv = GetTabView (out var tab1, out var tab2, false);
  363. tv.Width = 10;
  364. tv.Height = 5;
  365. tv.Style = new TabStyle { TabsOnBottom = true };
  366. tv.ApplyStyleChanges ();
  367. // Ensures that the tab bar subview gets the bounds of the parent TabView
  368. tv.LayoutSubviews ();
  369. // Test two tab names that fit
  370. tab1.DisplayText = "12";
  371. tab2.DisplayText = "13";
  372. tv.Draw ();
  373. TestHelpers.AssertDriverContentsWithFrameAre (@"
  374. ┌────────┐
  375. │hi │
  376. │ ╭──┬──╯
  377. │12│13│
  378. ╰──┴──╯ ", _output);
  379. // Test first tab name too long
  380. tab1.DisplayText = "12345678910";
  381. tab2.DisplayText = "13";
  382. tv.Draw ();
  383. TestHelpers.AssertDriverContentsWithFrameAre (@"
  384. ┌────────┐
  385. │hi │
  386. │ ╭►
  387. │1234567│
  388. ╰───────╯ ", _output);
  389. //switch to tab2
  390. tv.SelectedTab = tab2;
  391. tv.Draw ();
  392. TestHelpers.AssertDriverContentsWithFrameAre (@"
  393. ┌────────┐
  394. │hi2 │
  395. ◄ ╭─────╯
  396. │13│
  397. ╰──╯ ", _output);
  398. // now make both tabs too long
  399. tab1.DisplayText = "12345678910";
  400. tab2.DisplayText = "abcdefghijklmnopq";
  401. tv.Draw ();
  402. TestHelpers.AssertDriverContentsWithFrameAre (@"
  403. ┌────────┐
  404. │hi2 │
  405. ◄ ╭╯
  406. │abcdefg│
  407. ╰───────╯ ", _output);
  408. }
  409. [Fact]
  410. [AutoInitShutdown]
  411. public void ShowTopLine_False_TabsOnBottom_True_TestThinTabView_WithLongNames ()
  412. {
  413. var tv = GetTabView (out var tab1, out var tab2, false);
  414. tv.Width = 10;
  415. tv.Height = 5;
  416. tv.Style = new TabStyle { ShowTopLine = false, TabsOnBottom = true };
  417. tv.ApplyStyleChanges ();
  418. // Ensures that the tab bar subview gets the bounds of the parent TabView
  419. tv.LayoutSubviews ();
  420. // Test two tab names that fit
  421. tab1.DisplayText = "12";
  422. tab2.DisplayText = "13";
  423. tv.Draw ();
  424. TestHelpers.AssertDriverContentsWithFrameAre (@"
  425. ┌────────┐
  426. │hi │
  427. │ │
  428. │ ╭──┬──╯
  429. │12│13│ ", _output);
  430. tv.SelectedTab = tab2;
  431. tv.Draw ();
  432. TestHelpers.AssertDriverContentsWithFrameAre (@"
  433. ┌────────┐
  434. │hi2 │
  435. │ │
  436. ├──╮ ╭──╯
  437. │12│13│ ", _output);
  438. tv.SelectedTab = tab1;
  439. // Test first tab name too long
  440. tab1.DisplayText = "12345678910";
  441. tab2.DisplayText = "13";
  442. tv.Draw ();
  443. TestHelpers.AssertDriverContentsWithFrameAre (@"
  444. ┌────────┐
  445. │hi │
  446. │ │
  447. │ ╭►
  448. │1234567│ ", _output);
  449. //switch to tab2
  450. tv.SelectedTab = tab2;
  451. tv.Draw ();
  452. TestHelpers.AssertDriverContentsWithFrameAre (@"
  453. ┌────────┐
  454. │hi2 │
  455. │ │
  456. ◄ ╭─────╯
  457. │13│ ", _output);
  458. // now make both tabs too long
  459. tab1.DisplayText = "12345678910";
  460. tab2.DisplayText = "abcdefghijklmnopq";
  461. tv.Draw ();
  462. TestHelpers.AssertDriverContentsWithFrameAre (@"
  463. ┌────────┐
  464. │hi2 │
  465. │ │
  466. ◄ ╭╯
  467. │abcdefg│ ", _output);
  468. }
  469. [Fact]
  470. [AutoInitShutdown]
  471. public void ShowTopLine_True_TabsOnBottom_True_TestTabView_Width4 ()
  472. {
  473. var tv = GetTabView (out _, out _, false);
  474. tv.Width = 4;
  475. tv.Height = 5;
  476. tv.Style = new TabStyle { TabsOnBottom = true };
  477. tv.ApplyStyleChanges ();
  478. tv.LayoutSubviews ();
  479. tv.Draw ();
  480. TestHelpers.AssertDriverContentsWithFrameAre (@"
  481. ┌──┐
  482. │hi│
  483. │ ╭►
  484. │T│
  485. ╰─╯ ", _output);
  486. }
  487. [Fact]
  488. [AutoInitShutdown]
  489. public void ShowTopLine_False_TabsOnBottom_True_TestTabView_Width4 ()
  490. {
  491. var tv = GetTabView (out _, out _, false);
  492. tv.Width = 4;
  493. tv.Height = 5;
  494. tv.Style = new TabStyle { ShowTopLine = false, TabsOnBottom = true };
  495. tv.ApplyStyleChanges ();
  496. tv.LayoutSubviews ();
  497. tv.Draw ();
  498. TestHelpers.AssertDriverContentsWithFrameAre (@"
  499. ┌──┐
  500. │hi│
  501. │ │
  502. │ ╭►
  503. │T│ ", _output);
  504. }
  505. [Fact]
  506. [AutoInitShutdown]
  507. public void ShowTopLine_True_TabsOnBottom_True_TestTabView_Width3 ()
  508. {
  509. var tv = GetTabView (out _, out _, false);
  510. tv.Width = 3;
  511. tv.Height = 5;
  512. tv.Style = new TabStyle { TabsOnBottom = true };
  513. tv.ApplyStyleChanges ();
  514. tv.LayoutSubviews ();
  515. tv.Draw ();
  516. TestHelpers.AssertDriverContentsWithFrameAre (@"
  517. ┌─┐
  518. │h│
  519. │╭►
  520. ││
  521. ╰╯ ", _output);
  522. }
  523. [Fact]
  524. [AutoInitShutdown]
  525. public void ShowTopLine_False_TabsOnBottom_True_TestTabView_Width3 ()
  526. {
  527. var tv = GetTabView (out _, out _, false);
  528. tv.Width = 3;
  529. tv.Height = 5;
  530. tv.Style = new TabStyle { ShowTopLine = false, TabsOnBottom = true };
  531. tv.ApplyStyleChanges ();
  532. tv.LayoutSubviews ();
  533. tv.Draw ();
  534. TestHelpers.AssertDriverContentsWithFrameAre (@"
  535. ┌─┐
  536. │h│
  537. │ │
  538. │╭►
  539. ││ ", _output);
  540. }
  541. [Fact]
  542. [AutoInitShutdown]
  543. public void ShowTopLine_True_TabsOnBottom_False_With_Unicode ()
  544. {
  545. var tv = GetTabView (out var tab1, out var tab2, false);
  546. tv.Width = 20;
  547. tv.Height = 5;
  548. tv.LayoutSubviews ();
  549. tab1.DisplayText = "Tab0";
  550. tab2.DisplayText = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  551. tv.Draw ();
  552. TestHelpers.AssertDriverContentsWithFrameAre (@"
  553. ╭────╮
  554. │Tab0│
  555. │ ╰─────────────►
  556. │hi │
  557. └──────────────────┘", _output);
  558. tv.SelectedTab = tab2;
  559. tv.Draw ();
  560. TestHelpers.AssertDriverContentsWithFrameAre (@"
  561. ╭──────────────╮
  562. │Les Misérables│
  563. ◄ ╰───╮
  564. │hi2 │
  565. └──────────────────┘", _output);
  566. }
  567. [Fact]
  568. [AutoInitShutdown]
  569. public void ShowTopLine_True_TabsOnBottom_True_With_Unicode ()
  570. {
  571. var tv = GetTabView (out var tab1, out var tab2, false);
  572. tv.Width = 20;
  573. tv.Height = 5;
  574. tv.Style = new TabStyle { TabsOnBottom = true };
  575. tv.ApplyStyleChanges ();
  576. tv.LayoutSubviews ();
  577. tab1.DisplayText = "Tab0";
  578. tab2.DisplayText = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  579. tv.Draw ();
  580. TestHelpers.AssertDriverContentsWithFrameAre (@"
  581. ┌──────────────────┐
  582. │hi │
  583. │ ╭─────────────►
  584. │Tab0│
  585. ╰────╯ ", _output);
  586. tv.SelectedTab = tab2;
  587. tv.Draw ();
  588. TestHelpers.AssertDriverContentsWithFrameAre (@"
  589. ┌──────────────────┐
  590. │hi2 │
  591. ◄ ╭───╯
  592. │Les Misérables│
  593. ╰──────────────╯ ", _output);
  594. }
  595. [Fact]
  596. [AutoInitShutdown]
  597. public void MouseClick_ChangesTab ()
  598. {
  599. var tv = GetTabView (out var tab1, out var tab2, false);
  600. tv.Width = 20;
  601. tv.Height = 5;
  602. tv.LayoutSubviews ();
  603. tv.Draw ();
  604. var tabRow = tv.Subviews [0];
  605. Assert.Equal ("TabRowView", tabRow.GetType ().Name);
  606. TestHelpers.AssertDriverContentsAre (@"
  607. ╭────┬────╮
  608. │Tab1│Tab2│
  609. │ ╰────┴────────╮
  610. │hi │
  611. └──────────────────┘
  612. ", _output);
  613. Tab clicked = null;
  614. tv.TabClicked += (s, e) => {
  615. clicked = e.Tab;
  616. };
  617. Application.Top.Add (tv);
  618. Application.Begin (Application.Top);
  619. MouseEventEventArgs args;
  620. // Waving mouse around does not trigger click
  621. for (int i = 0; i < 100; i++) {
  622. args = new MouseEventEventArgs (new MouseEvent {
  623. X = i,
  624. Y = 1,
  625. Flags = MouseFlags.ReportMousePosition
  626. });
  627. Application.OnMouseEvent (args);
  628. Application.Refresh ();
  629. Assert.Null (clicked);
  630. Assert.Equal (tab1, tv.SelectedTab);
  631. }
  632. args = new MouseEventEventArgs (new MouseEvent {
  633. X = 3,
  634. Y = 1,
  635. Flags = MouseFlags.Button1Clicked
  636. });
  637. Application.OnMouseEvent (args);
  638. Application.Refresh ();
  639. Assert.Equal (tab1, clicked);
  640. Assert.Equal (tab1, tv.SelectedTab);
  641. // Click to tab2
  642. args = new MouseEventEventArgs (new MouseEvent {
  643. X = 6,
  644. Y = 1,
  645. Flags = MouseFlags.Button1Clicked
  646. });
  647. Application.OnMouseEvent (args);
  648. Application.Refresh ();
  649. Assert.Equal (tab2, clicked);
  650. Assert.Equal (tab2, tv.SelectedTab);
  651. // cancel navigation
  652. tv.TabClicked += (s, e) => {
  653. clicked = e.Tab;
  654. e.MouseEvent.Handled = true;
  655. };
  656. args = new MouseEventEventArgs (new MouseEvent {
  657. X = 3,
  658. Y = 1,
  659. Flags = MouseFlags.Button1Clicked
  660. });
  661. Application.OnMouseEvent (args);
  662. Application.Refresh ();
  663. // Tab 1 was clicked but event handler blocked navigation
  664. Assert.Equal (tab1, clicked);
  665. Assert.Equal (tab2, tv.SelectedTab);
  666. args = new MouseEventEventArgs (new MouseEvent {
  667. X = 12,
  668. Y = 1,
  669. Flags = MouseFlags.Button1Clicked
  670. });
  671. Application.OnMouseEvent (args);
  672. Application.Refresh ();
  673. // Clicking beyond last tab should raise event with null Tab
  674. Assert.Null (clicked);
  675. Assert.Equal (tab2, tv.SelectedTab);
  676. }
  677. [Fact, AutoInitShutdown]
  678. public void MouseClick_Right_Left_Arrows_ChangesTab ()
  679. {
  680. var tv = GetTabView (out var tab1, out var tab2, false);
  681. tv.Width = 7;
  682. tv.Height = 5;
  683. tv.LayoutSubviews ();
  684. tv.Draw ();
  685. var tabRow = tv.Subviews [0];
  686. Assert.Equal ("TabRowView", tabRow.GetType ().Name);
  687. TestHelpers.AssertDriverContentsAre (@"
  688. ╭────╮
  689. │Tab1│
  690. │ ╰►
  691. │hi │
  692. └─────┘
  693. ", _output);
  694. Tab clicked = null;
  695. tv.TabClicked += (s, e) => {
  696. clicked = e.Tab;
  697. };
  698. Tab oldChanged = null;
  699. Tab newChanged = null;
  700. tv.SelectedTabChanged += (s, e) => {
  701. oldChanged = e.OldTab;
  702. newChanged = e.NewTab;
  703. };
  704. Application.Top.Add (tv);
  705. Application.Begin (Application.Top);
  706. // Click the right arrow
  707. var args = new MouseEventEventArgs (new MouseEvent {
  708. X = 6,
  709. Y = 2,
  710. Flags = MouseFlags.Button1Clicked
  711. });
  712. Application.OnMouseEvent (args);
  713. Application.Refresh ();
  714. Assert.Null (clicked);
  715. Assert.Equal (tab1, oldChanged);
  716. Assert.Equal (tab2, newChanged);
  717. Assert.Equal (tab2, tv.SelectedTab);
  718. TestHelpers.AssertDriverContentsAre (@"
  719. ╭────╮
  720. │Tab2│
  721. ◄ ╰╮
  722. │hi2 │
  723. └─────┘
  724. ", _output);
  725. // Click the left arrow
  726. args = new MouseEventEventArgs (new MouseEvent {
  727. X = 0,
  728. Y = 2,
  729. Flags = MouseFlags.Button1Clicked
  730. });
  731. Application.OnMouseEvent (args);
  732. Application.Refresh ();
  733. Assert.Null (clicked);
  734. Assert.Equal (tab2, oldChanged);
  735. Assert.Equal (tab1, newChanged);
  736. Assert.Equal (tab1, tv.SelectedTab);
  737. TestHelpers.AssertDriverContentsAre (@"
  738. ╭────╮
  739. │Tab1│
  740. │ ╰►
  741. │hi │
  742. └─────┘
  743. ", _output);
  744. }
  745. [Fact, AutoInitShutdown]
  746. public void MouseClick_Right_Left_Arrows_ChangesTab_With_Border ()
  747. {
  748. var tv = GetTabView (out var tab1, out var tab2, false);
  749. tv.Width = 9;
  750. tv.Height = 7;
  751. Assert.Equal (LineStyle.None, tv.BorderStyle);
  752. tv.BorderStyle = LineStyle.Single;
  753. tv.LayoutSubviews ();
  754. tv.Draw ();
  755. var tabRow = tv.Subviews [0];
  756. Assert.Equal ("TabRowView", tabRow.GetType ().Name);
  757. TestHelpers.AssertDriverContentsAre (@"
  758. ┌───────┐
  759. │╭────╮ │
  760. ││Tab1│ │
  761. ││ ╰►│
  762. ││hi ││
  763. │└─────┘│
  764. └───────┘
  765. ", _output);
  766. Tab clicked = null;
  767. tv.TabClicked += (s, e) => {
  768. clicked = e.Tab;
  769. };
  770. Tab oldChanged = null;
  771. Tab newChanged = null;
  772. tv.SelectedTabChanged += (s, e) => {
  773. oldChanged = e.OldTab;
  774. newChanged = e.NewTab;
  775. };
  776. Application.Top.Add (tv);
  777. Application.Begin (Application.Top);
  778. // Click the right arrow
  779. var args = new MouseEventEventArgs (new MouseEvent {
  780. X = 7,
  781. Y = 3,
  782. Flags = MouseFlags.Button1Clicked
  783. });
  784. Application.OnMouseEvent (args);
  785. Application.Refresh ();
  786. Assert.Null (clicked);
  787. Assert.Equal (tab1, oldChanged);
  788. Assert.Equal (tab2, newChanged);
  789. Assert.Equal (tab2, tv.SelectedTab);
  790. TestHelpers.AssertDriverContentsAre (@"
  791. ┌───────┐
  792. │╭────╮ │
  793. ││Tab2│ │
  794. │◄ ╰╮│
  795. ││hi2 ││
  796. │└─────┘│
  797. └───────┘
  798. ", _output);
  799. // Click the left arrow
  800. args = new MouseEventEventArgs (new MouseEvent {
  801. X = 1,
  802. Y = 3,
  803. Flags = MouseFlags.Button1Clicked
  804. });
  805. Application.OnMouseEvent (args);
  806. Application.Refresh ();
  807. Assert.Null (clicked);
  808. Assert.Equal (tab2, oldChanged);
  809. Assert.Equal (tab1, newChanged);
  810. Assert.Equal (tab1, tv.SelectedTab);
  811. TestHelpers.AssertDriverContentsAre (@"
  812. ┌───────┐
  813. │╭────╮ │
  814. ││Tab1│ │
  815. ││ ╰►│
  816. ││hi ││
  817. │└─────┘│
  818. └───────┘
  819. ", _output);
  820. }
  821. [Fact]
  822. public void EnsureValidScrollOffsets_TabScrollOffset ()
  823. {
  824. var tv = GetTabView (out var tab1, out var tab2);
  825. // Make tab width small to force only one tab visible at once
  826. tv.Width = 4;
  827. tv.SelectedTab = tab1;
  828. Assert.Equal (0, tv.TabScrollOffset);
  829. tv.TabScrollOffset = 10;
  830. tv.SelectedTab = tab2;
  831. Assert.Equal (1, tv.TabScrollOffset);
  832. tv.TabScrollOffset = -1;
  833. tv.SelectedTab = tab1;
  834. Assert.Equal (0, tv.TabScrollOffset);
  835. // Shutdown must be called to safely clean up Application if Init has been called
  836. Application.Shutdown ();
  837. }
  838. [Fact, AutoInitShutdown]
  839. public void ProcessKey_Down_Up_Right_Left_Home_End_PageDown_PageUp ()
  840. {
  841. var tv = GetTabView (out var tab1, out var tab2, false);
  842. tv.Width = 7;
  843. tv.Height = 5;
  844. var btn = new Button ("Ok") { Y = Pos.Bottom (tv) + 1, Width = 7 };
  845. var top = Application.Top;
  846. top.Add (tv, btn);
  847. Application.Begin (top);
  848. // Is the selected tab view hosting focused
  849. Assert.Equal (tab1, tv.SelectedTab);
  850. Assert.Equal (tv, top.Focused);
  851. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  852. Assert.Equal (tv.SelectedTab.View, top.Focused.MostFocused);
  853. // Press the cursor up key to focus the selected tab
  854. var args = new Key (Key.CursorUp);
  855. Application.OnKeyDown (args);
  856. Application.Refresh ();
  857. // Is the selected tab focused
  858. Assert.Equal (tab1, tv.SelectedTab);
  859. Assert.Equal (tv, top.Focused);
  860. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  861. Tab oldChanged = null;
  862. Tab newChanged = null;
  863. tv.SelectedTabChanged += (s, e) => {
  864. oldChanged = e.OldTab;
  865. newChanged = e.NewTab;
  866. };
  867. // Press the cursor right key to select the next tab
  868. args = new Key (Key.CursorRight);
  869. Application.OnKeyDown (args);
  870. Application.Refresh ();
  871. Assert.Equal (tab1, oldChanged);
  872. Assert.Equal (tab2, newChanged);
  873. Assert.Equal (tab2, tv.SelectedTab);
  874. Assert.Equal (tv, top.Focused);
  875. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  876. // Press the cursor down key to focused the selected tab view hosting
  877. args =new Key (Key.CursorDown);
  878. Application.OnKeyDown (args);
  879. Application.Refresh ();
  880. Assert.Equal (tab2, tv.SelectedTab);
  881. Assert.Equal (tv, top.Focused);
  882. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  883. // The tab view hosting is a label which can't be focused
  884. // and the View container is the focused one
  885. Assert.Equal (tv.Subviews [1], top.Focused.MostFocused);
  886. // Press the cursor up key to focus the selected tab
  887. args = new Key (Key.CursorUp);
  888. Application.OnKeyDown (args);
  889. Application.Refresh ();
  890. // Is the selected tab focused
  891. Assert.Equal (tab2, tv.SelectedTab);
  892. Assert.Equal (tv, top.Focused);
  893. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  894. // Press the cursor left key to select the previous tab
  895. args = new Key(Key.CursorLeft);
  896. Application.OnKeyDown (args);
  897. Application.Refresh ();
  898. Assert.Equal (tab2, oldChanged);
  899. Assert.Equal (tab1, newChanged);
  900. Assert.Equal (tab1, tv.SelectedTab);
  901. Assert.Equal (tv, top.Focused);
  902. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  903. // Press the end key to select the last tab
  904. args = new Key(Key.End);
  905. Application.OnKeyDown (args);
  906. Application.Refresh ();
  907. Assert.Equal (tab1, oldChanged);
  908. Assert.Equal (tab2, newChanged);
  909. Assert.Equal (tab2, tv.SelectedTab);
  910. Assert.Equal (tv, top.Focused);
  911. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  912. // Press the home key to select the first tab
  913. args = new Key(Key.Home);
  914. Application.OnKeyDown (args);
  915. Application.Refresh ();
  916. Assert.Equal (tab2, oldChanged);
  917. Assert.Equal (tab1, newChanged);
  918. Assert.Equal (tab1, tv.SelectedTab);
  919. Assert.Equal (tv, top.Focused);
  920. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  921. // Press the page down key to select the next set of tabs
  922. args = new Key(Key.PageDown);
  923. Application.OnKeyDown (args);
  924. Application.Refresh ();
  925. Assert.Equal (tab1, oldChanged);
  926. Assert.Equal (tab2, newChanged);
  927. Assert.Equal (tab2, tv.SelectedTab);
  928. Assert.Equal (tv, top.Focused);
  929. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  930. // Press the page up key to select the previous set of tabs
  931. args = new Key(Key.PageUp);
  932. Application.OnKeyDown (args);
  933. Application.Refresh ();
  934. Assert.Equal (tab2, oldChanged);
  935. Assert.Equal (tab1, newChanged);
  936. Assert.Equal (tab1, tv.SelectedTab);
  937. Assert.Equal (tv, top.Focused);
  938. Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
  939. }
  940. void InitFakeDriver ()
  941. {
  942. var driver = new FakeDriver ();
  943. Application.Init (driver);
  944. driver.Init ();
  945. }
  946. }