12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178 |
- using System;
- using System.Globalization;
- using Xunit;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewsTests;
- public class TabViewTests {
- readonly ITestOutputHelper _output;
- public TabViewTests (ITestOutputHelper output) => this._output = output;
- TabView GetTabView () => GetTabView (out _, out _);
- TabView GetTabView (out Tab tab1, out Tab tab2, bool initFakeDriver = true)
- {
- if (initFakeDriver) {
- InitFakeDriver ();
- }
- var tv = new TabView ();
- tv.BeginInit ();
- tv.EndInit ();
- tv.ColorScheme = new ColorScheme ();
- tv.AddTab (tab1 = new Tab () { DisplayText = "Tab1", View = new TextField("hi") { Width = 2 } }, false);
- tv.AddTab (tab2 = new Tab () { DisplayText = "Tab2", View = new Label("hi2") }, false);
- return tv;
- }
- [Fact]
- public void AddTwoTabs_SecondIsSelected ()
- {
- InitFakeDriver ();
- var tv = new TabView ();
- Tab tab1;
- Tab tab2;
- tv.AddTab (tab1 = new Tab () { DisplayText = "Tab1", View = new TextField("hi") }, false);
- tv.AddTab (tab2 = new Tab () { DisplayText = "Tab1", View = new Label("hi2") }, true);
- Assert.Equal (2, tv.Tabs.Count);
- Assert.Equal (tab2, tv.SelectedTab);
- Application.Shutdown ();
- }
- [Fact]
- public void EnsureSelectedTabVisible_NullSelect ()
- {
- var tv = GetTabView ();
- tv.SelectedTab = null;
- Assert.Null (tv.SelectedTab);
- Assert.Equal (0, tv.TabScrollOffset);
- tv.EnsureSelectedTabIsVisible ();
- Assert.Null (tv.SelectedTab);
- Assert.Equal (0, tv.TabScrollOffset);
- Application.Shutdown ();
- }
- [Fact]
- public void EnsureSelectedTabVisible_MustScroll ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- // Make tab width small to force only one tab visible at once
- tv.Width = 4;
- tv.SelectedTab = tab1;
- Assert.Equal (0, tv.TabScrollOffset);
- tv.EnsureSelectedTabIsVisible ();
- Assert.Equal (0, tv.TabScrollOffset);
- // Asking to show tab2 should automatically move scroll offset accordingly
- tv.SelectedTab = tab2;
- Assert.Equal (1, tv.TabScrollOffset);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void SelectedTabChanged_Called ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- tv.SelectedTab = tab1;
- Tab oldTab = null;
- Tab newTab = null;
- var called = 0;
- tv.SelectedTabChanged += (s, e) => {
- oldTab = e.OldTab;
- newTab = e.NewTab;
- called++;
- };
- tv.SelectedTab = tab2;
- Assert.Equal (1, called);
- Assert.Equal (tab1, oldTab);
- Assert.Equal (tab2, newTab);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void RemoveTab_ChangesSelection ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- tv.SelectedTab = tab1;
- tv.RemoveTab (tab1);
- Assert.Equal (tab2, tv.SelectedTab);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void RemoveTab_MultipleCalls_NotAnError ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- tv.SelectedTab = tab1;
- // Repeated calls to remove a tab that is not part of
- // the collection should be ignored
- tv.RemoveTab (tab1);
- tv.RemoveTab (tab1);
- tv.RemoveTab (tab1);
- tv.RemoveTab (tab1);
- Assert.Equal (tab2, tv.SelectedTab);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void RemoveAllTabs_ClearsSelection ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- tv.SelectedTab = tab1;
- tv.RemoveTab (tab1);
- tv.RemoveTab (tab2);
- Assert.Null (tv.SelectedTab);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void SwitchTabBy_NormalUsage ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- Tab tab3;
- Tab tab4;
- Tab tab5;
- tv.AddTab (tab3 = new Tab (), false);
- tv.AddTab (tab4 = new Tab (), false);
- tv.AddTab (tab5 = new Tab (), false);
- tv.SelectedTab = tab1;
- var called = 0;
- tv.SelectedTabChanged += (s, e) => { called++; };
- tv.SwitchTabBy (1);
- Assert.Equal (1, called);
- Assert.Equal (tab2, tv.SelectedTab);
- //reset called counter
- called = 0;
- // go right 2
- tv.SwitchTabBy (2);
- // even though we go right 2 indexes the event should only be called once
- Assert.Equal (1, called);
- Assert.Equal (tab4, tv.SelectedTab);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void AddTab_SameTabMoreThanOnce ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- Assert.Equal (2, tv.Tabs.Count);
- // Tab is already part of the control so shouldn't result in duplication
- tv.AddTab (tab1, false);
- tv.AddTab (tab1, false);
- tv.AddTab (tab1, false);
- tv.AddTab (tab1, false);
- Assert.Equal (2, tv.Tabs.Count);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void SwitchTabBy_OutOfTabsRange ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- tv.SelectedTab = tab1;
- tv.SwitchTabBy (500);
- Assert.Equal (tab2, tv.SelectedTab);
- tv.SwitchTabBy (-500);
- Assert.Equal (tab1, tv.SelectedTab);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_False_TestThinTabView_WithLongNames ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 10;
- tv.Height = 5;
- // Ensures that the tab bar subview gets the bounds of the parent TabView
- tv.LayoutSubviews ();
- // Test two tab names that fit
- tab1.DisplayText = "12";
- tab2.DisplayText = "13";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╭──┬──╮
- │12│13│
- │ ╰──┴──╮
- │hi │
- └────────┘", _output);
- tv.SelectedTab = tab2;
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╭──┬──╮
- │12│13│
- ├──╯ ╰──╮
- │hi2 │
- └────────┘", _output);
- tv.SelectedTab = tab1;
- // Test first tab name too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "13";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╭───────╮
- │1234567│
- │ ╰►
- │hi │
- └────────┘", _output);
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╭──╮
- │13│
- ◄ ╰─────╮
- │hi2 │
- └────────┘", _output);
- // now make both tabs too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "abcdefghijklmnopq";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╭───────╮
- │abcdefg│
- ◄ ╰╮
- │hi2 │
- └────────┘", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_False_TestThinTabView_WithLongNames ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 10;
- tv.Height = 5;
- tv.Style = new TabStyle { ShowTopLine = false };
- tv.ApplyStyleChanges ();
- // Ensures that the tab bar subview gets the bounds of the parent TabView
- tv.LayoutSubviews ();
- // Test two tab names that fit
- tab1.DisplayText = "12";
- tab2.DisplayText = "13";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- │12│13│
- │ ╰──┴──╮
- │hi │
- │ │
- └────────┘", _output);
- tv.SelectedTab = tab2;
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- │12│13│
- ├──╯ ╰──╮
- │hi2 │
- │ │
- └────────┘", _output);
- tv.SelectedTab = tab1;
- // Test first tab name too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "13";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- │1234567│
- │ ╰►
- │hi │
- │ │
- └────────┘", _output);
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- │13│
- ◄ ╰─────╮
- │hi2 │
- │ │
- └────────┘", _output);
- // now make both tabs too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "abcdefghijklmnopq";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- │abcdefg│
- ◄ ╰╮
- │hi2 │
- │ │
- └────────┘", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_False_TestTabView_Width4 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 4;
- tv.Height = 5;
- tv.LayoutSubviews ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╭─╮
- │T│
- │ ╰►
- │hi│
- └──┘", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_False_TestTabView_Width4 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 4;
- tv.Height = 5;
- tv.Style = new TabStyle { ShowTopLine = false };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- │T│
- │ ╰►
- │hi│
- │ │
- └──┘", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_False_TestTabView_Width3 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 3;
- tv.Height = 5;
- tv.LayoutSubviews ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╭╮
- ││
- │╰►
- │h│
- └─┘", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_False_TestTabView_Width3 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 3;
- tv.Height = 5;
- tv.Style = new TabStyle { ShowTopLine = false };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ││
- │╰►
- │h│
- │ │
- └─┘", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_True_TestThinTabView_WithLongNames ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 10;
- tv.Height = 5;
- tv.Style = new TabStyle { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- // Ensures that the tab bar subview gets the bounds of the parent TabView
- tv.LayoutSubviews ();
- // Test two tab names that fit
- tab1.DisplayText = "12";
- tab2.DisplayText = "13";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi │
- │ ╭──┬──╯
- │12│13│
- ╰──┴──╯ ", _output);
- // Test first tab name too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "13";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi │
- │ ╭►
- │1234567│
- ╰───────╯ ", _output);
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- ◄ ╭─────╯
- │13│
- ╰──╯ ", _output);
- // now make both tabs too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "abcdefghijklmnopq";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- ◄ ╭╯
- │abcdefg│
- ╰───────╯ ", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_True_TestThinTabView_WithLongNames ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 10;
- tv.Height = 5;
- tv.Style = new TabStyle { ShowTopLine = false, TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- // Ensures that the tab bar subview gets the bounds of the parent TabView
- tv.LayoutSubviews ();
- // Test two tab names that fit
- tab1.DisplayText = "12";
- tab2.DisplayText = "13";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi │
- │ │
- │ ╭──┬──╯
- │12│13│ ", _output);
- tv.SelectedTab = tab2;
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- │ │
- ├──╮ ╭──╯
- │12│13│ ", _output);
- tv.SelectedTab = tab1;
- // Test first tab name too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "13";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi │
- │ │
- │ ╭►
- │1234567│ ", _output);
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- │ │
- ◄ ╭─────╯
- │13│ ", _output);
- // now make both tabs too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "abcdefghijklmnopq";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- │ │
- ◄ ╭╯
- │abcdefg│ ", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_True_TestTabView_Width4 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 4;
- tv.Height = 5;
- tv.Style = new TabStyle { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──┐
- │hi│
- │ ╭►
- │T│
- ╰─╯ ", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_True_TestTabView_Width4 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 4;
- tv.Height = 5;
- tv.Style = new TabStyle { ShowTopLine = false, TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──┐
- │hi│
- │ │
- │ ╭►
- │T│ ", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_True_TestTabView_Width3 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 3;
- tv.Height = 5;
- tv.Style = new TabStyle { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌─┐
- │h│
- │╭►
- ││
- ╰╯ ", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_True_TestTabView_Width3 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 3;
- tv.Height = 5;
- tv.Style = new TabStyle { ShowTopLine = false, TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌─┐
- │h│
- │ │
- │╭►
- ││ ", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_False_With_Unicode ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 20;
- tv.Height = 5;
- tv.LayoutSubviews ();
- tab1.DisplayText = "Tab0";
- tab2.DisplayText = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╭────╮
- │Tab0│
- │ ╰─────────────►
- │hi │
- └──────────────────┘", _output);
- tv.SelectedTab = tab2;
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ╭──────────────╮
- │Les Misérables│
- ◄ ╰───╮
- │hi2 │
- └──────────────────┘", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_True_With_Unicode ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 20;
- tv.Height = 5;
- tv.Style = new TabStyle { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tab1.DisplayText = "Tab0";
- tab2.DisplayText = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────┐
- │hi │
- │ ╭─────────────►
- │Tab0│
- ╰────╯ ", _output);
- tv.SelectedTab = tab2;
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────┐
- │hi2 │
- ◄ ╭───╯
- │Les Misérables│
- ╰──────────────╯ ", _output);
- }
- [Fact]
- [AutoInitShutdown]
- public void MouseClick_ChangesTab ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 20;
- tv.Height = 5;
- tv.LayoutSubviews ();
- tv.Draw ();
- var tabRow = tv.Subviews [0];
- Assert.Equal ("TabRowView", tabRow.GetType ().Name);
- TestHelpers.AssertDriverContentsAre (@"
- ╭────┬────╮
- │Tab1│Tab2│
- │ ╰────┴────────╮
- │hi │
- └──────────────────┘
- ", _output);
- Tab clicked = null;
- tv.TabClicked += (s, e) => {
- clicked = e.Tab;
- };
- Application.Top.Add (tv);
- Application.Begin (Application.Top);
- MouseEventEventArgs args;
- // Waving mouse around does not trigger click
- for (int i = 0; i < 100; i++) {
- args = new MouseEventEventArgs (new MouseEvent {
- X = i,
- Y = 1,
- Flags = MouseFlags.ReportMousePosition
- });
- Application.OnMouseEvent (args);
- Application.Refresh ();
- Assert.Null (clicked);
- Assert.Equal (tab1, tv.SelectedTab);
- }
- args = new MouseEventEventArgs (new MouseEvent {
- X = 3,
- Y = 1,
- Flags = MouseFlags.Button1Clicked
- });
- Application.OnMouseEvent (args);
- Application.Refresh ();
- Assert.Equal (tab1, clicked);
- Assert.Equal (tab1, tv.SelectedTab);
- // Click to tab2
- args = new MouseEventEventArgs (new MouseEvent {
- X = 6,
- Y = 1,
- Flags = MouseFlags.Button1Clicked
- });
- Application.OnMouseEvent (args);
- Application.Refresh ();
- Assert.Equal (tab2, clicked);
- Assert.Equal (tab2, tv.SelectedTab);
- // cancel navigation
- tv.TabClicked += (s, e) => {
- clicked = e.Tab;
- e.MouseEvent.Handled = true;
- };
- args = new MouseEventEventArgs (new MouseEvent {
- X = 3,
- Y = 1,
- Flags = MouseFlags.Button1Clicked
- });
- Application.OnMouseEvent (args);
- Application.Refresh ();
- // Tab 1 was clicked but event handler blocked navigation
- Assert.Equal (tab1, clicked);
- Assert.Equal (tab2, tv.SelectedTab);
- args = new MouseEventEventArgs (new MouseEvent {
- X = 12,
- Y = 1,
- Flags = MouseFlags.Button1Clicked
- });
- Application.OnMouseEvent (args);
- Application.Refresh ();
- // Clicking beyond last tab should raise event with null Tab
- Assert.Null (clicked);
- Assert.Equal (tab2, tv.SelectedTab);
- }
- [Fact, AutoInitShutdown]
- public void MouseClick_Right_Left_Arrows_ChangesTab ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 7;
- tv.Height = 5;
- tv.LayoutSubviews ();
- tv.Draw ();
- var tabRow = tv.Subviews [0];
- Assert.Equal ("TabRowView", tabRow.GetType ().Name);
- TestHelpers.AssertDriverContentsAre (@"
- ╭────╮
- │Tab1│
- │ ╰►
- │hi │
- └─────┘
- ", _output);
- Tab clicked = null;
- tv.TabClicked += (s, e) => {
- clicked = e.Tab;
- };
- Tab oldChanged = null;
- Tab newChanged = null;
- tv.SelectedTabChanged += (s, e) => {
- oldChanged = e.OldTab;
- newChanged = e.NewTab;
- };
- Application.Top.Add (tv);
- Application.Begin (Application.Top);
- // Click the right arrow
- var args = new MouseEventEventArgs (new MouseEvent {
- X = 6,
- Y = 2,
- Flags = MouseFlags.Button1Clicked
- });
- Application.OnMouseEvent (args);
- Application.Refresh ();
- Assert.Null (clicked);
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (@"
- ╭────╮
- │Tab2│
- ◄ ╰╮
- │hi2 │
- └─────┘
- ", _output);
- // Click the left arrow
- args = new MouseEventEventArgs (new MouseEvent {
- X = 0,
- Y = 2,
- Flags = MouseFlags.Button1Clicked
- });
- Application.OnMouseEvent (args);
- Application.Refresh ();
- Assert.Null (clicked);
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (@"
- ╭────╮
- │Tab1│
- │ ╰►
- │hi │
- └─────┘
- ", _output);
- }
- [Fact, AutoInitShutdown]
- public void MouseClick_Right_Left_Arrows_ChangesTab_With_Border ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 9;
- tv.Height = 7;
- Assert.Equal (LineStyle.None, tv.BorderStyle);
- tv.BorderStyle = LineStyle.Single;
- tv.LayoutSubviews ();
- tv.Draw ();
- var tabRow = tv.Subviews [0];
- Assert.Equal ("TabRowView", tabRow.GetType ().Name);
- TestHelpers.AssertDriverContentsAre (@"
- ┌───────┐
- │╭────╮ │
- ││Tab1│ │
- ││ ╰►│
- ││hi ││
- │└─────┘│
- └───────┘
- ", _output);
- Tab clicked = null;
- tv.TabClicked += (s, e) => {
- clicked = e.Tab;
- };
- Tab oldChanged = null;
- Tab newChanged = null;
- tv.SelectedTabChanged += (s, e) => {
- oldChanged = e.OldTab;
- newChanged = e.NewTab;
- };
- Application.Top.Add (tv);
- Application.Begin (Application.Top);
- // Click the right arrow
- var args = new MouseEventEventArgs (new MouseEvent {
- X = 7,
- Y = 3,
- Flags = MouseFlags.Button1Clicked
- });
- Application.OnMouseEvent (args);
- Application.Refresh ();
- Assert.Null (clicked);
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (@"
- ┌───────┐
- │╭────╮ │
- ││Tab2│ │
- │◄ ╰╮│
- ││hi2 ││
- │└─────┘│
- └───────┘
- ", _output);
- // Click the left arrow
- args = new MouseEventEventArgs (new MouseEvent {
- X = 1,
- Y = 3,
- Flags = MouseFlags.Button1Clicked
- });
- Application.OnMouseEvent (args);
- Application.Refresh ();
- Assert.Null (clicked);
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (@"
- ┌───────┐
- │╭────╮ │
- ││Tab1│ │
- ││ ╰►│
- ││hi ││
- │└─────┘│
- └───────┘
- ", _output);
- }
- [Fact]
- public void EnsureValidScrollOffsets_TabScrollOffset ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- // Make tab width small to force only one tab visible at once
- tv.Width = 4;
- tv.SelectedTab = tab1;
- Assert.Equal (0, tv.TabScrollOffset);
- tv.TabScrollOffset = 10;
- tv.SelectedTab = tab2;
- Assert.Equal (1, tv.TabScrollOffset);
- tv.TabScrollOffset = -1;
- tv.SelectedTab = tab1;
- Assert.Equal (0, tv.TabScrollOffset);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void ProcessKey_Down_Up_Right_Left_Home_End_PageDown_PageUp ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 7;
- tv.Height = 5;
- var btn = new Button ("Ok") { Y = Pos.Bottom (tv) + 1, Width = 7 };
- var top = Application.Top;
- top.Add (tv, btn);
- Application.Begin (top);
- // Is the selected tab view hosting focused
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- Assert.Equal (tv.SelectedTab.View, top.Focused.MostFocused);
- // Press the cursor up key to focus the selected tab
- var args = new Key (Key.CursorUp);
- Application.OnKeyDown (args);
- Application.Refresh ();
- // Is the selected tab focused
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- Tab oldChanged = null;
- Tab newChanged = null;
- tv.SelectedTabChanged += (s, e) => {
- oldChanged = e.OldTab;
- newChanged = e.NewTab;
- };
- // Press the cursor right key to select the next tab
- args = new Key (Key.CursorRight);
- Application.OnKeyDown (args);
- Application.Refresh ();
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the cursor down key to focused the selected tab view hosting
- args =new Key (Key.CursorDown);
- Application.OnKeyDown (args);
- Application.Refresh ();
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // The tab view hosting is a label which can't be focused
- // and the View container is the focused one
- Assert.Equal (tv.Subviews [1], top.Focused.MostFocused);
- // Press the cursor up key to focus the selected tab
- args = new Key (Key.CursorUp);
- Application.OnKeyDown (args);
- Application.Refresh ();
- // Is the selected tab focused
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the cursor left key to select the previous tab
- args = new Key(Key.CursorLeft);
- Application.OnKeyDown (args);
- Application.Refresh ();
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the end key to select the last tab
- args = new Key(Key.End);
- Application.OnKeyDown (args);
- Application.Refresh ();
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the home key to select the first tab
- args = new Key(Key.Home);
- Application.OnKeyDown (args);
- Application.Refresh ();
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the page down key to select the next set of tabs
- args = new Key(Key.PageDown);
- Application.OnKeyDown (args);
- Application.Refresh ();
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the page up key to select the previous set of tabs
- args = new Key(Key.PageUp);
- Application.OnKeyDown (args);
- Application.Refresh ();
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- }
- void InitFakeDriver ()
- {
- var driver = new FakeDriver ();
- Application.Init (driver);
- driver.Init ();
- }
- }
|