TabViewTests.cs 40 KB

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