123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- using System;
- using System.Linq;
- using Terminal.Gui;
- using Xunit;
- using Xunit.Abstractions;
- namespace UnitTests {
- public class SplitContainerTests {
- readonly ITestOutputHelper output;
- public SplitContainerTests (ITestOutputHelper output)
- {
- this.output = output;
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_Vertical ()
- {
- var splitContainer = Get11By3SplitContainer ();
- splitContainer.Redraw (splitContainer.Bounds);
- string looksLike =
- @"
- 11111│22222
- │
- │ ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Keyboard movement on splitter should have no effect if it is not focused
- splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
- splitContainer.SetNeedsDisplay ();
- splitContainer.Redraw (splitContainer.Bounds);
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_Vertical_WithBorder ()
- {
- var splitContainer = Get11By3SplitContainer (true);
- splitContainer.Redraw (splitContainer.Bounds);
- string looksLike =
- @"
- ┌────┬────┐
- │1111│2222│
- └────┴────┘";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Keyboard movement on splitter should have no effect if it is not focused
- splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
- splitContainer.SetNeedsDisplay ();
- splitContainer.Redraw (splitContainer.Bounds);
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_Vertical_Focused ()
- {
- var splitContainer = Get11By3SplitContainer ();
- SetInputFocusLine (splitContainer);
- splitContainer.Redraw (splitContainer.Bounds);
- string looksLike =
- @"
- 11111│22222
- ◊
- │ ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Now while focused move the splitter 1 unit right
- splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- 111111│2222
- ◊
- │ ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // and 2 to the left
- splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
- splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- 1111│222222
- ◊
- │ ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_Vertical_Focused_WithBorder ()
- {
- var splitContainer = Get11By3SplitContainer (true);
- SetInputFocusLine (splitContainer);
- splitContainer.Redraw (splitContainer.Bounds);
- string looksLike =
- @"
- ┌────┬────┐
- │1111◊2222│
- └────┴────┘";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Now while focused move the splitter 1 unit right
- splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- ┌─────┬───┐
- │11111◊222│
- └─────┴───┘";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // and 2 to the left
- splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
- splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- ┌───┬─────┐
- │111◊22222│
- └───┴─────┘";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_Vertical_Focused_50PercentSplit ()
- {
- var splitContainer = Get11By3SplitContainer ();
- SetInputFocusLine (splitContainer);
- splitContainer.SplitterDistance = Pos.Percent (50);
- Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
- splitContainer.Redraw (splitContainer.Bounds);
- string looksLike =
- @"
- 11111│22222
- ◊
- │ ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Now while focused move the splitter 1 unit right
- splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- 111111│2222
- ◊
- │ ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Even when moving the splitter location it should stay a Percentage based one
- Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
- // and 2 to the left
- splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
- splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- 1111│222222
- ◊
- │ ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Even when moving the splitter location it should stay a Percentage based one
- Assert.IsType<Pos.PosFactor> (splitContainer.SplitterDistance);
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_Horizontal ()
- {
- var splitContainer = Get11By3SplitContainer ();
- splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
- splitContainer.Redraw (splitContainer.Bounds);
- string looksLike =
- @"
- 11111111111
- ───────────
- 22222222222";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Keyboard movement on splitter should have no effect if it is not focused
- splitContainer.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
- splitContainer.SetNeedsDisplay ();
- splitContainer.Redraw (splitContainer.Bounds);
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_Vertical_Panel1MinSize_Absolute ()
- {
- var splitContainer = Get11By3SplitContainer ();
- SetInputFocusLine (splitContainer);
- splitContainer.Panels [0].MinSize = 6;
- // distance is too small (below 6)
- splitContainer.SplitterDistance = 2;
- // Should bound the value to the minimum distance
- Assert.Equal (6, splitContainer.SplitterDistance);
- splitContainer.Redraw (splitContainer.Bounds);
- // so should ignore the 2 distance and stick to 6
- string looksLike =
- @"
- 111111│2222
- ◊
- │ ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Keyboard movement on splitter should have no effect because it
- // would take us below the minimum splitter size
- splitContainer.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
- splitContainer.SetNeedsDisplay ();
- splitContainer.Redraw (splitContainer.Bounds);
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // but we can continue to move the splitter right if we want
- splitContainer.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
- splitContainer.SetNeedsDisplay ();
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- 1111111│222
- ◊
- │ ";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_Horizontal_Focused ()
- {
- var splitContainer = Get11By3SplitContainer ();
- splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
- SetInputFocusLine (splitContainer);
- splitContainer.Redraw (splitContainer.Bounds);
- string looksLike =
- @"
- 11111111111
- ─────◊─────
- 22222222222";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Now move splitter line down
- splitContainer.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- 11111111111
- ─────◊─────";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // And 2 up
- splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
- splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- ─────◊─────
- 22222222222";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_Horizontal_Panel1MinSize_Absolute ()
- {
- var splitContainer = Get11By3SplitContainer ();
- splitContainer.Orientation = Terminal.Gui.Graphs.Orientation.Horizontal;
- SetInputFocusLine (splitContainer);
- splitContainer.Panels [0].MinSize = 1;
- // 0 should not be allowed because it brings us below minimum size of Panel1
- splitContainer.SplitterDistance = 0;
- Assert.Equal ((Pos)1, splitContainer.SplitterDistance);
- splitContainer.Redraw (splitContainer.Bounds);
- string looksLike =
- @"
- 11111111111
- ─────◊─────
- 22222222222";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // Now move splitter line down (allowed
- splitContainer.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- 11111111111
- ─────◊─────";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- // And up 2 (only 1 is allowed because of minimum size of 1 on panel1)
- splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
- splitContainer.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
- splitContainer.Redraw (splitContainer.Bounds);
- looksLike =
- @"
- 11111111111
- ─────◊─────
- 22222222222";
- TestHelpers.AssertDriverContentsAre (looksLike, output);
- }
- [Fact, AutoInitShutdown]
- public void TestSplitContainer_CannotSetSplitterPosToFuncEtc ()
- {
- var splitContainer = Get11By3SplitContainer ();
- var ex = Assert.Throws<ArgumentException> (() => splitContainer.SplitterDistance = Pos.Right (splitContainer));
- Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property. Passed value was PosCombine", ex.Message);
- ex = Assert.Throws<ArgumentException> (() => splitContainer.SplitterDistance = Pos.Function (() => 1));
- Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property. Passed value was PosFunc", ex.Message);
- // Also not allowed because this results in a PosCombine
- ex = Assert.Throws<ArgumentException> (() => splitContainer.SplitterDistance = Pos.Percent (50) - 1);
- Assert.Equal ("Only Percent and Absolute values are supported for SplitterDistance property. Passed value was PosCombine", ex.Message);
- }
- private void SetInputFocusLine (SplitContainer splitContainer)
- {
- var line = splitContainer.Subviews [0].Subviews.OfType<LineView> ().Single ();
- line.SetFocus ();
- Assert.True (line.HasFocus);
- }
- private SplitContainer Get11By3SplitContainer (bool withBorder = false)
- {
- var container = new SplitContainer () {
- Width = 11,
- Height = 3,
- };
- if (!withBorder) {
- container.Border.BorderStyle = BorderStyle.None;
- container.Border.DrawMarginFrame = false;
- }
- container.Panels [0].Add (new Label (new string ('1', 100)));
- container.Panels [1].Add (new Label (new string ('2', 100)));
- Application.Top.Add (container);
- container.ColorScheme = new ColorScheme ();
- container.LayoutSubviews ();
- container.BeginInit ();
- container.EndInit ();
- return container;
- }
- }
- }
|