TabViewTests.cs 39 KB

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