123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xunit;
- using Xunit.Abstractions;
- namespace Terminal.Gui.Views {
- public class CheckboxTests {
- readonly ITestOutputHelper output;
- public CheckboxTests (ITestOutputHelper output)
- {
- this.output = output;
- }
- [Fact]
- public void Constructors_Defaults ()
- {
- var ckb = new CheckBox ();
- Assert.True (ckb.AutoSize);
- Assert.False (ckb.Checked);
- Assert.Equal (string.Empty, ckb.Text);
- Assert.Equal ("╴ ", ckb.TextFormatter.Text);
- Assert.True (ckb.CanFocus);
- Assert.Equal (new Rect (0, 0, 2, 1), ckb.Frame);
- ckb = new CheckBox ("Test", true);
- Assert.True (ckb.AutoSize);
- Assert.True (ckb.Checked);
- Assert.Equal ("Test", ckb.Text);
- Assert.Equal ("√ Test", ckb.TextFormatter.Text);
- Assert.True (ckb.CanFocus);
- Assert.Equal (new Rect (0, 0, 6, 1), ckb.Frame);
- ckb = new CheckBox (1, 2, "Test");
- Assert.True (ckb.AutoSize);
- Assert.False (ckb.Checked);
- Assert.Equal ("Test", ckb.Text);
- Assert.Equal ("╴ Test", ckb.TextFormatter.Text);
- Assert.True (ckb.CanFocus);
- Assert.Equal (new Rect (1, 2, 6, 1), ckb.Frame);
- ckb = new CheckBox (3, 4, "Test", true);
- Assert.True (ckb.AutoSize);
- Assert.True (ckb.Checked);
- Assert.Equal ("Test", ckb.Text);
- Assert.Equal ("√ Test", ckb.TextFormatter.Text);
- Assert.True (ckb.CanFocus);
- Assert.Equal (new Rect (3, 4, 6, 1), ckb.Frame);
- }
- [Fact]
- [AutoInitShutdown]
- public void KeyBindings_Command ()
- {
- var isChecked = false;
- CheckBox ckb = new CheckBox ();
- ckb.Toggled += (e) => isChecked = true;
- Application.Top.Add (ckb);
- Application.Begin (Application.Top);
- Assert.Equal (Key.Null, ckb.HotKey);
- ckb.Text = "Test";
- Assert.Equal (Key.T, ckb.HotKey);
- Assert.False (ckb.ProcessHotKey (new KeyEvent (Key.T, new KeyModifiers ())));
- Assert.False (isChecked);
- ckb.Text = "T_est";
- Assert.Equal (Key.E, ckb.HotKey);
- Assert.True (ckb.ProcessHotKey (new KeyEvent (Key.E | Key.AltMask, new KeyModifiers () { Alt = true })));
- Assert.True (isChecked);
- isChecked = false;
- Assert.True (ckb.ProcessKey (new KeyEvent ((Key)' ', new KeyModifiers ())));
- Assert.True (isChecked);
- isChecked = false;
- Assert.True (ckb.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ())));
- Assert.True (isChecked);
- Assert.True (ckb.AutoSize);
- Application.Refresh ();
- var expected = @"
- √ Test
- ";
- var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 6, 1), pos);
- }
- [Fact, AutoInitShutdown]
- public void AutoSize_StaysVisible ()
- {
- var checkBox = new CheckBox () {
- X = 1,
- Y = Pos.Center (),
- Text = "Check this out 你"
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Title = "Test Demo 你"
- };
- win.Add (checkBox);
- Application.Top.Add (win);
- Assert.False (checkBox.IsInitialized);
- var runstate = Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- Assert.True (checkBox.IsInitialized);
- Assert.Equal (new Rect (1, 1, 19, 1), checkBox.Frame);
- Assert.Equal ("Check this out 你", checkBox.Text);
- Assert.Equal ("╴ Check this out 你", checkBox.TextFormatter.Text);
- Assert.True (checkBox.AutoSize);
- var expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ ╴ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- // Positive test
- var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- // Also Positive test
- checkBox.AutoSize = true;
- bool first = false;
- Application.RunMainLoopIteration (ref runstate, true, ref first);
- Assert.Equal (new Rect (1, 1, 19, 1), checkBox.Frame);
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ ╴ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- checkBox.Checked = true;
- Application.RunMainLoopIteration (ref runstate, true, ref first);
- Assert.Equal (new Rect (1, 1, 19, 1), checkBox.Frame);
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ √ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- checkBox.AutoSize = false;
- // It isn't auto-size so the height is guaranteed by the SetMinWidthHeight
- checkBox.Text = "Check this out 你 changed";
- Application.RunMainLoopIteration (ref runstate, true, ref first);
- Assert.Equal (new Rect (1, 1, 19, 1), checkBox.Frame);
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ √ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- checkBox.Width = 19;
- // It isn't auto-size so the height is guaranteed by the SetMinWidthHeight
- checkBox.Text = "Check this out 你 changed";
- Application.RunMainLoopIteration (ref runstate, true, ref first);
- Assert.False (checkBox.AutoSize);
- Assert.Equal (new Rect (1, 1, 19, 1), checkBox.Frame);
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ √ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- checkBox.AutoSize = true;
- Application.RunMainLoopIteration (ref runstate, true, ref first);
- Assert.Equal (new Rect (1, 1, 27, 1), checkBox.Frame);
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ √ Check this out 你 changed│
- │ │
- └────────────────────────────┘
- ";
- pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- }
- [Fact, AutoInitShutdown]
- public void TextAlignment_Left ()
- {
- var checkBox = new CheckBox () {
- X = 1,
- Y = Pos.Center (),
- Text = "Check this out 你",
- AutoSize = false,
- Width = 25
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Title = "Test Demo 你"
- };
- win.Add (checkBox);
- Application.Top.Add (win);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- Assert.Equal (TextAlignment.Left, checkBox.TextAlignment);
- Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
- Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
- Assert.Equal ("Check this out 你", checkBox.Text);
- Assert.Equal ("╴ Check this out 你", checkBox.TextFormatter.Text);
- Assert.False (checkBox.AutoSize);
- var expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ ╴ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- checkBox.Checked = true;
- Application.Refresh ();
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ √ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- }
- [Fact, AutoInitShutdown]
- public void TextAlignment_Centered ()
- {
- var checkBox = new CheckBox () {
- X = 1,
- Y = Pos.Center (),
- Text = "Check this out 你",
- TextAlignment = TextAlignment.Centered,
- AutoSize = false,
- Width = 25
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Title = "Test Demo 你"
- };
- win.Add (checkBox);
- Application.Top.Add (win);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- Assert.Equal (TextAlignment.Centered, checkBox.TextAlignment);
- Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
- Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
- Assert.Equal ("Check this out 你", checkBox.Text);
- Assert.Equal ("╴ Check this out 你", checkBox.TextFormatter.Text);
- Assert.False (checkBox.AutoSize);
- var expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ ╴ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- checkBox.Checked = true;
- Application.Refresh ();
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ √ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- }
- [Fact, AutoInitShutdown]
- public void TextAlignment_Justified ()
- {
- var checkBox = new CheckBox () {
- X = 1,
- Y = Pos.Center (),
- Text = "Check this out 你",
- TextAlignment = TextAlignment.Justified,
- AutoSize = false,
- Width = 25
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Title = "Test Demo 你"
- };
- win.Add (checkBox);
- Application.Top.Add (win);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- Assert.Equal (TextAlignment.Justified, checkBox.TextAlignment);
- Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
- Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
- Assert.Equal ("Check this out 你", checkBox.Text);
- Assert.Equal ("╴ Check this out 你", checkBox.TextFormatter.Text);
- Assert.False (checkBox.AutoSize);
- var expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ ╴ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- checkBox.Checked = true;
- Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
- Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
- Application.Refresh ();
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ √ Check this out 你 │
- │ │
- └────────────────────────────┘
- ";
- pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- }
- [Fact, AutoInitShutdown]
- public void TextAlignment_Right ()
- {
- var checkBox = new CheckBox () {
- X = 1,
- Y = Pos.Center (),
- Text = "Check this out 你",
- TextAlignment = TextAlignment.Right,
- AutoSize = false,
- Width = 25
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Title = "Test Demo 你"
- };
- win.Add (checkBox);
- Application.Top.Add (win);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- Assert.Equal (TextAlignment.Right, checkBox.TextAlignment);
- Assert.Equal (new Rect (1, 1, 25, 1), checkBox.Frame);
- Assert.Equal (new Size (25, 1), checkBox.TextFormatter.Size);
- Assert.Equal ("Check this out 你", checkBox.Text);
- Assert.Equal ("Check this out 你 ╴", checkBox.TextFormatter.Text);
- Assert.False (checkBox.AutoSize);
- var expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ Check this out 你 ╴ │
- │ │
- └────────────────────────────┘
- ";
- var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- checkBox.Checked = true;
- Application.Refresh ();
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ Check this out 你 √ │
- │ │
- └────────────────────────────┘
- ";
- pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- }
- [Fact, AutoInitShutdown]
- public void AutoSize_Stays_True_AnchorEnd_Without_HotKeySpecifier ()
- {
- var checkBox = new CheckBox () {
- Y = Pos.Center (),
- Text = "Check this out 你"
- };
- checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetTextFormatterBoundsSize ().Width);
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Title = "Test Demo 你"
- };
- win.Add (checkBox);
- Application.Top.Add (win);
- Assert.True (checkBox.AutoSize);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- var expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ ╴ Check this out 你│
- │ │
- └────────────────────────────┘
- ";
- GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.True (checkBox.AutoSize);
- checkBox.Text = "Check this out 你 changed";
- Assert.True (checkBox.AutoSize);
- Application.Refresh ();
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ ╴ Check this out 你 changed│
- │ │
- └────────────────────────────┘
- ";
- GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void AutoSize_Stays_True_AnchorEnd_With_HotKeySpecifier ()
- {
- var checkBox = new CheckBox () {
- Y = Pos.Center (),
- Text = "C_heck this out 你"
- };
- checkBox.X = Pos.AnchorEnd () - Pos.Function (() => checkBox.GetTextFormatterBoundsSize ().Width);
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Title = "Test Demo 你"
- };
- win.Add (checkBox);
- Application.Top.Add (win);
- Assert.True (checkBox.AutoSize);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- var expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ ╴ Check this out 你│
- │ │
- └────────────────────────────┘
- ";
- GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- Assert.True (checkBox.AutoSize);
- checkBox.Text = "Check this out 你 changed";
- Assert.True (checkBox.AutoSize);
- Application.Refresh ();
- expected = @"
- ┌ Test Demo 你 ──────────────┐
- │ │
- │ ╴ Check this out 你 changed│
- │ │
- └────────────────────────────┘
- ";
- GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
- }
- }
- }
|