Skip to content

Commit e4cb4b9

Browse files
committed
Working on fixing tests
1 parent be4474a commit e4cb4b9

File tree

2 files changed

+90
-19
lines changed

2 files changed

+90
-19
lines changed

Terminal.Gui/Views/SplitContainer.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private void Setup ()
188188

189189
splitterPanels[0].Height = Dim.Fill ();
190190
splitterPanels[0].Width = new Dim.DimFunc (() =>
191-
splitterDistance.Anchor (Bounds.Width)) - 1;
191+
splitterDistance.Anchor (Bounds.Width));
192192

193193
splitterPanels[1].X = Pos.Right (splitterLine);
194194
splitterPanels[1].Y = 0;
@@ -322,15 +322,20 @@ public SplitContainerLineView (SplitContainer parent)
322322
LayoutStarted += (e) => {
323323
moveRuneRenderLocation = null;
324324
if (Orientation == Orientation.Horizontal) {
325-
StartingAnchor = Driver.LeftTee;
326-
EndingAnchor = Driver.RightTee;
325+
StartingAnchor = ParentHasBorder () ? Driver.LeftTee : (Rune?)null;
326+
EndingAnchor = ParentHasBorder () ? Driver.RightTee : (Rune?)null;
327327
} else {
328-
StartingAnchor = Driver.TopTee;
329-
EndingAnchor = Driver.BottomTee;
328+
StartingAnchor = ParentHasBorder () ? Driver.TopTee : (Rune?)null;
329+
EndingAnchor = ParentHasBorder () ? Driver.BottomTee : (Rune?)null;
330330
}
331331
};
332332
}
333333

334+
private bool ParentHasBorder ()
335+
{
336+
return parent.Border != null && parent.Border.BorderStyle != BorderStyle.None;
337+
}
338+
334339
public override bool ProcessKey (KeyEvent kb)
335340
{
336341
if (!CanFocus || !HasFocus) {

UnitTests/SplitContainerTests.cs

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using Terminal.Gui;
34
using Xunit;
45
using Xunit.Abstractions;
@@ -33,13 +34,33 @@ public void TestSplitContainer_Vertical ()
3334
splitContainer.Redraw (splitContainer.Bounds);
3435
TestHelpers.AssertDriverContentsAre (looksLike, output);
3536

37+
}
38+
[Fact, AutoInitShutdown]
39+
public void TestSplitContainer_Vertical_WithBorder ()
40+
{
41+
var splitContainer = Get11By3SplitContainer (true);
42+
splitContainer.Redraw (splitContainer.Bounds);
43+
44+
string looksLike =
45+
@"
46+
┌────┬────┐
47+
│1111│2222│
48+
└────┴────┘";
49+
TestHelpers.AssertDriverContentsAre (looksLike, output);
50+
51+
// Keyboard movement on splitter should have no effect if it is not focused
52+
splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
53+
splitContainer.SetNeedsDisplay ();
54+
splitContainer.Redraw (splitContainer.Bounds);
55+
TestHelpers.AssertDriverContentsAre (looksLike, output);
56+
3657
}
3758
[Fact, AutoInitShutdown]
3859
public void TestSplitContainer_Vertical_Focused ()
3960
{
4061
var splitContainer = Get11By3SplitContainer ();
41-
splitContainer.EnsureFocus ();
42-
splitContainer.FocusFirst ();
62+
SetInputFocusLine (splitContainer);
63+
4364
splitContainer.Redraw (splitContainer.Bounds);
4465

4566
string looksLike =
@@ -74,12 +95,52 @@ public void TestSplitContainer_Vertical_Focused ()
7495
TestHelpers.AssertDriverContentsAre (looksLike, output);
7596
}
7697

98+
[Fact, AutoInitShutdown]
99+
public void TestSplitContainer_Vertical_Focused_WithBorder ()
100+
{
101+
var splitContainer = Get11By3SplitContainer (true);
102+
SetInputFocusLine (splitContainer);
103+
104+
splitContainer.Redraw (splitContainer.Bounds);
105+
106+
string looksLike =
107+
@"
108+
┌────┬────┐
109+
│1111◊2222│
110+
└────┴────┘";
111+
TestHelpers.AssertDriverContentsAre (looksLike, output);
112+
113+
// Now while focused move the splitter 1 unit right
114+
splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
115+
splitContainer.Redraw (splitContainer.Bounds);
116+
117+
looksLike =
118+
@"
119+
┌─────┬───┐
120+
│11111◊222│
121+
└─────┴───┘";
122+
TestHelpers.AssertDriverContentsAre (looksLike, output);
123+
124+
125+
// and 2 to the left
126+
splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
127+
splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
128+
splitContainer.Redraw (splitContainer.Bounds);
129+
130+
looksLike =
131+
@"
132+
┌───┬─────┐
133+
│111◊22222│
134+
└───┴─────┘";
135+
TestHelpers.AssertDriverContentsAre (looksLike, output);
136+
}
137+
138+
77139
[Fact, AutoInitShutdown]
78140
public void TestSplitContainer_Vertical_Focused_50PercentSplit ()
79141
{
80142
var splitContainer = Get11By3SplitContainer ();
81-
splitContainer.EnsureFocus ();
82-
splitContainer.FocusFirst ();
143+
SetInputFocusLine (splitContainer);
83144
splitContainer.SplitterDistance = Pos.Percent (50);
84145
Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
85146
splitContainer.Redraw (splitContainer.Bounds);
@@ -147,9 +208,7 @@ public void TestSplitContainer_Horizontal ()
147208
public void TestSplitContainer_Vertical_Panel1MinSize_Absolute ()
148209
{
149210
var splitContainer = Get11By3SplitContainer ();
150-
151-
splitContainer.EnsureFocus ();
152-
splitContainer.FocusFirst ();
211+
SetInputFocusLine (splitContainer);
153212
splitContainer.Panels [0].MinSize = 6;
154213

155214
// distance is too small (below 6)
@@ -195,8 +254,7 @@ public void TestSplitContainer_Horizontal_Focused ()
195254
var splitContainer = Get11By3SplitContainer ();
196255

197256
splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
198-
splitContainer.EnsureFocus ();
199-
splitContainer.FocusFirst ();
257+
SetInputFocusLine (splitContainer);
200258

201259
splitContainer.Redraw (splitContainer.Bounds);
202260

@@ -235,8 +293,7 @@ public void TestSplitContainer_Horizontal_Panel1MinSize_Absolute ()
235293
var splitContainer = Get11By3SplitContainer ();
236294

237295
splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
238-
splitContainer.EnsureFocus ();
239-
splitContainer.FocusFirst ();
296+
SetInputFocusLine (splitContainer);
240297
splitContainer.Panels [0].MinSize = 1;
241298

242299
// 0 should not be allowed because it brings us below minimum size of Panel1
@@ -291,15 +348,24 @@ public void TestSplitContainer_CannotSetSplitterPosToFuncEtc ()
291348
Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property. Passed value was PosCombine", ex.Message);
292349
}
293350

294-
private SplitContainer Get11By3SplitContainer ()
351+
private void SetInputFocusLine (SplitContainer splitContainer)
352+
{
353+
var line = splitContainer.Subviews [0].Subviews.OfType<LineView> ().Single ();
354+
line.SetFocus ();
355+
Assert.True (line.HasFocus);
356+
}
357+
358+
private SplitContainer Get11By3SplitContainer (bool withBorder = false)
295359
{
296360
var container = new SplitContainer () {
297361
Width = 11,
298362
Height = 3,
299363
};
300364

301-
container.Border.DrawMarginFrame = false;
302-
container.Border.BorderStyle = BorderStyle.None;
365+
if (!withBorder) {
366+
container.Border.BorderStyle = BorderStyle.None;
367+
container.Border.DrawMarginFrame = false;
368+
}
303369

304370
container.Panels [0].Add (new Label (new string ('1', 100)));
305371
container.Panels [1].Add (new Label (new string ('2', 100)));

0 commit comments

Comments
 (0)