Files
Terminal.Gui/Tests/UnitTests/Views/ButtonTests.cs
Thomas Nind 51dda7e69f Fixes #3947 Adds Fake driver and fixes fluent tests (iteration-zero) (#4225)
* Consider width2 chars that are not IsBmp

* Apply same fix in WindowsDriver

* Explicitly use type of local variable

* Revert changes to WindowsDriver

* Assume we are running in a terminal that supports true color by default unless user explicitly forces 16

* Switch to SetAttribute and WriteConsole instead of WriteConsoleOutput for 16 color mode

* Fix some cursor issues (WIP)

* Remove concept of 'dirty rows' from v2 as its never actually used

* Remove damageRegion as it does nothing

* Make string builder to console writing simpler

* Radically simplify Write method

* Simplify conditional logic

* Simplify restoring cursor position

* Reference local variable for console buffer

* Reduce calls to ConsoleWrite by accumulating till attribute changes

* When resizing v2 16 color mode on windows, recreate the back buffer to match its size

* Fixes for VTS enabled

* Fix _lastSize never being assigned

* Fixes VTS for Force16Colors

* Fixes force16Colors in VTS

* Fixes escape sequences always echoing in non-VTS

* Force Force16Colors in non-VTS. It have a bug in adding a newline in the last line

* WIP Add base class for NetOutput

* Abstract away how we change attribute

* WIP - Make WindowsOutput use base class

* WIP working to fix set cursor position

* Remove commented out code

* Fixes legacy output mode

* Fixes size with no alt buffer supported on VTS and size restore after maximized.

* Fix set cursor which also fixes the broken surrogate pairs

* Add force parameter

* Fixes an issue that only happens with Windows Terminal when paste surrogate pairs by press Ctrl+V

* In Windows escape sequences must be sent during the lifetime of the console which is created in input handle

* Ensure flush the input buffer before reset the console

* Flush input buffer before reset console in v2win

* Fixes issue in v2net not being refreshing the menu bar at start

* Only force layout and draw on size changed.

* Fix v2net issue not draw first line by forcing set cursor position

* Set _lastCursorPosition nullable and remove bool force from set cursor position

* Remove force parameter

* Add v2 version of fake driver attribute

* Make direct replacement and wire up window resizing events

* Update casts to use V2 fake driver instead

* Adjust interfaces to expose less internals

* Fix not raising iteration event in v2

* WIP investigate what it takes to do resize and redraw using TextAlignment_Centered as example

* Sketch adding component factory

* Create relevant fake component factories

* Add window size monitor into factory

* Fake size monitor injecting

* Add helper for faking console resize in AutoInitShutdown tests

* Fix size setting in FakeDriverV2

* Switch to new method

* Fix IsLegacy becoming false when using blank constructor

* Fix for Ready not being raised when showing same top twice also fixes garbage collection issue if running millions of top levels

* Fix tests

* Remove auto init

* Restore conditional compilation stuff

* Restore 'if running unit tests' logic

* Check only for the output being specific classes for the suppression

* Fix ShadowView blowing up with index out of bounds error

* Fix resize in fluent tests

* Fix for people using Iteration call directly

* Fix more calls to iteration to use
        AutoInitShutdownAttribute.RunIteration ();

* Add comment

* Remove assumption that Run with prior view not disposed should throw

* Fix timings in Dialog_Opened_From_Another_Dialog

* Fix Zero_Buttons_Works

* Standardize and fix Button_IsDefault_True_Return_His_Index_On_Accepting

* Fix iteration counts on MessageBoxTests

* Fix WizartTests and DrawTests_Ruler

* Implement SendKeys into ConsoleDriverFacade

* Fix SendKeys in console driver facade such that FileDialogTests works
Fix when Clip is null in popover

* Add missing dispose call to test

* Fix support for Esc in facade SendKeys

* Fix AutocompleteTests

* Fix various tests

* Replace LayoutAndDraw with run iteration

* Fix draw issues

* fix draw order

* Fix run iteration calls

* Fix unit tests

* Fix SendKeys in facade.

* Manipulate upper and lower cases.

* Add IsValidInput method to the interface.

* Fix SendKeys scenario

* Fixes surrogate pairs in the label

* Make tests more sensible - they are testing draw functionality.  Callbacks do not need to happen in Iteration method

* Fix tests and harden cleanup in AutoInitShutdownAttribute v2 lifecycle dispose

* Delete extra create input call

* Fix mocks and order of exceptions thrown in Run when things are not initialized

* Revert use of `MapConsoleKeyInfoToKeyCode`

* Ignore casing as it is not what test is really about

* Clear application top and top levels before each auto init shutdown test

* Fix for unstable tests

* Restore actually working SendKeys code

* option to pass logger in fluent ctor

* restore ToArray

* Fix SendKeys method and add extension to unit test

* Leverage the EscSeqUtils.MapConsoleKeyInfo method to avoid duplicate code

* Remove unnecessary hack

* Using only KeyCode for rKeys

* Recover modifier keys in surrogate pairs

* Reformat

* Remove iteration limit for benchmarking in v2

* remove iteration delay to identify bugs

* Remove nudge to unique key and make Then run on UI thread

* fix fluid assertions

* Ensure UI operations all happen on UI thread

* Add explicit error for WaitIteration during an invoke

* Remove timeout added for debug

* Catch failing asserts better

* Fix screenshot

* Fix null ref

* Fix race condition in processing input

* Test fixing

* Standardize asserts

* Remove calls to layout and draw, remove pointless lock and enable reading Cancelled from Dialog even if it is disposed

* fix bad merge

* Make logs access threadsafe

* add extra wait to remove race between iteration end and assert

* Code cleanup

* Remove test for crash on access Cancelled after dispose as this is no longer a restriction

* Change resize console to run on UI thread - fixing race condition with redrawing

* Restore original frame rate after test

* Restore nudge to unique key

* Code Cleanup

* Fix for cascading failures when an assert fails in a specific test

* fix for bad merge

* Address PR feedback

* Move classes to seperate files and add xmldoc

* xml doc warnings

* More xml comments docs

* Fix spelling

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2025-09-10 10:01:57 -06:00

688 lines
21 KiB
C#

using UnitTests;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewsTests;
public class ButtonTests (ITestOutputHelper output)
{
// Test that Title and Text are the same
[Fact]
public void Text_Mirrors_Title ()
{
var view = new Button ();
view.Title = "Hello";
Assert.Equal ("Hello", view.Title);
Assert.Equal ("Hello", view.TitleTextFormatter.Text);
Assert.Equal ("Hello", view.Text);
Assert.Equal ($"{Glyphs.LeftBracket} Hello {Glyphs.RightBracket}", view.TextFormatter.Text);
view.Dispose ();
}
[Fact]
public void Title_Mirrors_Text ()
{
var view = new Button ();
view.Text = "Hello";
Assert.Equal ("Hello", view.Text);
Assert.Equal ($"{Glyphs.LeftBracket} Hello {Glyphs.RightBracket}", view.TextFormatter.Text);
Assert.Equal ("Hello", view.Title);
Assert.Equal ("Hello", view.TitleTextFormatter.Text);
view.Dispose ();
}
[Theory]
[InlineData ("01234", 0, 0, 0, 0)]
[InlineData ("01234", 1, 0, 1, 0)]
[InlineData ("01234", 0, 1, 0, 1)]
[InlineData ("01234", 1, 1, 1, 1)]
[InlineData ("01234", 10, 1, 10, 1)]
[InlineData ("01234", 10, 3, 10, 3)]
[InlineData ("0_1234", 0, 0, 0, 0)]
[InlineData ("0_1234", 1, 0, 1, 0)]
[InlineData ("0_1234", 0, 1, 0, 1)]
[InlineData ("0_1234", 1, 1, 1, 1)]
[InlineData ("0_1234", 10, 1, 10, 1)]
[InlineData ("0_12你", 10, 3, 10, 3)]
[InlineData ("0_12你", 0, 0, 0, 0)]
[InlineData ("0_12你", 1, 0, 1, 0)]
[InlineData ("0_12你", 0, 1, 0, 1)]
[InlineData ("0_12你", 1, 1, 1, 1)]
[InlineData ("0_12你", 10, 1, 10, 1)]
public void Button_AbsoluteSize_Text (string text, int width, int height, int expectedWidth, int expectedHeight)
{
// Override CM
Button.DefaultShadow = ShadowStyle.None;
var btn1 = new Button
{
Text = text,
Width = width,
Height = height
};
Assert.Equal (new (expectedWidth, expectedHeight), btn1.Frame.Size);
Assert.Equal (new (expectedWidth, expectedHeight), btn1.Viewport.Size);
Assert.Equal (new (expectedWidth, expectedHeight), btn1.GetContentSize ());
Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.TextFormatter.ConstrainToSize);
btn1.Dispose ();
}
[Theory]
[InlineData (0, 0, 0, 0)]
[InlineData (1, 0, 1, 0)]
[InlineData (0, 1, 0, 1)]
[InlineData (1, 1, 1, 1)]
[InlineData (10, 1, 10, 1)]
[InlineData (10, 3, 10, 3)]
public void Button_AbsoluteSize_DefaultText (int width, int height, int expectedWidth, int expectedHeight)
{
// Override CM
Button.DefaultShadow = ShadowStyle.None;
var btn1 = new Button ();
btn1.Width = width;
btn1.Height = height;
Assert.Equal (new (expectedWidth, expectedHeight), btn1.Frame.Size);
Assert.Equal (new (expectedWidth, expectedHeight), btn1.Viewport.Size);
Assert.Equal (new Size (expectedWidth, expectedHeight), btn1.TextFormatter.ConstrainToSize);
btn1.Dispose ();
}
[Fact]
public void Button_HotKeyChanged_EventFires ()
{
var btn = new Button { Text = "_Yar" };
object sender = null;
KeyChangedEventArgs args = null;
btn.HotKeyChanged += (s, e) =>
{
sender = s;
args = e;
};
btn.HotKeyChanged += (s, e) =>
{
sender = s;
args = e;
};
btn.HotKey = KeyCode.R;
Assert.Same (btn, sender);
Assert.Equal (KeyCode.Y, args.OldKey);
Assert.Equal (KeyCode.R, args.NewKey);
btn.HotKey = KeyCode.R;
Assert.Same (btn, sender);
Assert.Equal (KeyCode.Y, args.OldKey);
Assert.Equal (KeyCode.R, args.NewKey);
btn.Dispose ();
}
[Fact]
public void Button_HotKeyChanged_EventFires_WithNone ()
{
var btn = new Button ();
object sender = null;
KeyChangedEventArgs args = null;
btn.HotKeyChanged += (s, e) =>
{
sender = s;
args = e;
};
btn.HotKey = KeyCode.R;
Assert.Same (btn, sender);
Assert.Equal (KeyCode.Null, args.OldKey);
Assert.Equal (KeyCode.R, args.NewKey);
btn.Dispose ();
}
[Fact]
[SetupFakeDriver]
public void Constructors_Defaults ()
{
// Override CM
Button.DefaultShadow = ShadowStyle.None;
var btn = new Button ();
Assert.Equal (string.Empty, btn.Text);
btn.BeginInit ();
btn.EndInit ();
btn.SetRelativeLayout (new (100, 100));
Assert.Equal ($"{Glyphs.LeftBracket} {Glyphs.RightBracket}", btn.TextFormatter.Text);
Assert.False (btn.IsDefault);
Assert.Equal (Alignment.Center, btn.TextAlignment);
Assert.Equal ('_', btn.HotKeySpecifier.Value);
Assert.True (btn.CanFocus);
Assert.Equal (new (0, 0, 4, 1), btn.Viewport);
Assert.Equal (new (0, 0, 4, 1), btn.Frame);
Assert.Equal ($"{Glyphs.LeftBracket} {Glyphs.RightBracket}", btn.TextFormatter.Text);
Assert.False (btn.IsDefault);
Assert.Equal (Alignment.Center, btn.TextAlignment);
Assert.Equal ('_', btn.HotKeySpecifier.Value);
Assert.True (btn.CanFocus);
Assert.Equal (new (0, 0, 4, 1), btn.Viewport);
Assert.Equal (new (0, 0, 4, 1), btn.Frame);
Assert.Equal (string.Empty, btn.Title);
Assert.Equal (KeyCode.Null, btn.HotKey);
btn.Draw ();
var expected = @$"
{Glyphs.LeftBracket} {Glyphs.RightBracket}
";
DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
btn.Dispose ();
btn = new () { Text = "_Test", IsDefault = true };
btn.Layout ();
Assert.Equal (new (10, 1), btn.TextFormatter.ConstrainToSize);
btn.BeginInit ();
btn.EndInit ();
Assert.Equal ('_', btn.HotKeySpecifier.Value);
Assert.Equal (Key.T, btn.HotKey);
Assert.Equal ("_Test", btn.Text);
Assert.Equal (
$"{Glyphs.LeftBracket}{Glyphs.LeftDefaultIndicator} Test {Glyphs.RightDefaultIndicator}{Glyphs.RightBracket}",
btn.TextFormatter.Format ()
);
Assert.True (btn.IsDefault);
Assert.Equal (Alignment.Center, btn.TextAlignment);
Assert.True (btn.CanFocus);
btn.SetRelativeLayout (new (100, 100));
// 0123456789012345678901234567890123456789
// [* Test *]
Assert.Equal ('_', btn.HotKeySpecifier.Value);
Assert.Equal (10, btn.TextFormatter.Format ().Length);
Assert.Equal (new (10, 1), btn.TextFormatter.ConstrainToSize);
Assert.Equal (new (10, 1), btn.GetContentSize ());
Assert.Equal (new (0, 0, 10, 1), btn.Viewport);
Assert.Equal (new (0, 0, 10, 1), btn.Frame);
Assert.Equal (KeyCode.T, btn.HotKey);
btn.Dispose ();
btn = new () { X = 1, Y = 2, Text = "_abc", IsDefault = true };
btn.BeginInit ();
btn.EndInit ();
Assert.Equal ("_abc", btn.Text);
Assert.Equal (Key.A, btn.HotKey);
Assert.Equal (
$"{Glyphs.LeftBracket}{Glyphs.LeftDefaultIndicator} abc {Glyphs.RightDefaultIndicator}{Glyphs.RightBracket}",
btn.TextFormatter.Format ()
);
Assert.True (btn.IsDefault);
Assert.Equal (Alignment.Center, btn.TextAlignment);
Assert.Equal ('_', btn.HotKeySpecifier.Value);
Assert.True (btn.CanFocus);
Application.Driver?.ClearContents ();
btn.Draw ();
expected = @$"
{Glyphs.LeftBracket}{Glyphs.LeftDefaultIndicator} abc {Glyphs.RightDefaultIndicator}{Glyphs.RightBracket}
";
DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 9, 1), btn.Viewport);
Assert.Equal (new (1, 2, 9, 1), btn.Frame);
btn.Dispose ();
}
[Fact]
public void HotKeyChange_Works ()
{
var clicked = false;
var btn = new Button { Text = "_Test" };
btn.Accepting += (s, e) => clicked = true;
Assert.Equal (KeyCode.T, btn.HotKey);
Assert.False (btn.NewKeyDownEvent (Key.T)); // Button processes, but does not handle
Assert.True (clicked);
clicked = false;
Assert.False (btn.NewKeyDownEvent (Key.T.WithAlt)); // Button processes, but does not handle
Assert.True (clicked);
clicked = false;
btn.HotKey = KeyCode.E;
Assert.False (btn.NewKeyDownEvent (Key.E.WithAlt)); // Button processes, but does not handle
Assert.True (clicked);
}
[Theory]
[InlineData (false, 0)]
[InlineData (true, 1)]
public void Space_Fires_Accept (bool focused, int expected)
{
var superView = new View
{
CanFocus = true
};
Button button = new ();
button.CanFocus = focused;
var acceptInvoked = 0;
button.Accepting += (s, e) => acceptInvoked++;
superView.Add (button);
button.SetFocus ();
Assert.Equal (focused, button.HasFocus);
superView.NewKeyDownEvent (Key.Space);
Assert.Equal (expected, acceptInvoked);
superView.Dispose ();
}
[Theory]
[InlineData (false, 0)]
[InlineData (true, 1)]
public void Enter_Fires_Accept (bool focused, int expected)
{
var superView = new View
{
CanFocus = true
};
Button button = new ();
button.CanFocus = focused;
var acceptInvoked = 0;
button.Accepting += (s, e) => acceptInvoked++;
superView.Add (button);
button.SetFocus ();
Assert.Equal (focused, button.HasFocus);
superView.NewKeyDownEvent (Key.Enter);
Assert.Equal (expected, acceptInvoked);
superView.Dispose ();
}
[Theory]
[InlineData (false, 1)]
[InlineData (true, 1)]
public void HotKey_Fires_Accept (bool focused, int expected)
{
var superView = new View
{
CanFocus = true
};
Button button = new ()
{
HotKey = Key.A
};
button.CanFocus = focused;
var acceptInvoked = 0;
button.Accepting += (s, e) => acceptInvoked++;
superView.Add (button);
button.SetFocus ();
Assert.Equal (focused, button.HasFocus);
superView.NewKeyDownEvent (Key.A);
Assert.Equal (expected, acceptInvoked);
superView.Dispose ();
}
/// <summary>
/// This test demonstrates how to change the activation key for Button as described in the README.md keyboard
/// handling section
/// </summary>
[Fact]
[AutoInitShutdown]
public void KeyBindingExample ()
{
var pressed = 0;
var btn = new Button { Text = "Press Me" };
btn.Accepting += (s, e) => pressed++;
// The Button class supports the Default and Accept command
Assert.Contains (Command.HotKey, btn.GetSupportedCommands ());
Assert.Contains (Command.Accept, btn.GetSupportedCommands ());
var top = new Toplevel ();
top.Add (btn);
Application.Begin (top);
Assert.True (btn.HasFocus);
// default keybinding is Space which results in Command.Accept (when focused)
Application.RaiseKeyDownEvent (new ((KeyCode)' '));
Assert.Equal (1, pressed);
// remove the default keybinding (Space)
btn.KeyBindings.Clear (Command.HotKey);
btn.KeyBindings.Clear (Command.Accept);
// After clearing the default keystroke the Space button no longer does anything for the Button
Application.RaiseKeyDownEvent (new ((KeyCode)' '));
Assert.Equal (1, pressed);
// Set a new binding of b for the click (Accept) event
btn.KeyBindings.Add (Key.B, Command.HotKey); // b will now trigger the Accept command (when focused or not)
// now pressing B should call the button click event
Application.RaiseKeyDownEvent (Key.B);
Assert.Equal (2, pressed);
// now pressing Shift-B should NOT call the button click event
Application.RaiseKeyDownEvent (Key.B.WithShift);
Assert.Equal (2, pressed);
// now pressing Alt-B should NOT call the button click event
Application.RaiseKeyDownEvent (Key.B.WithAlt);
Assert.Equal (2, pressed);
// now pressing Shift-Alt-B should NOT call the button click event
Application.RaiseKeyDownEvent (Key.B.WithAlt.WithShift);
Assert.Equal (2, pressed);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void KeyBindings_Command ()
{
var clicked = false;
var btn = new Button { Text = "_Test" };
btn.Accepting += (s, e) => clicked = true;
var top = new Toplevel ();
top.Add (btn);
Application.Begin (top);
// Hot key. Both alone and with alt
Assert.Equal (KeyCode.T, btn.HotKey);
Assert.False (btn.NewKeyDownEvent (Key.T)); // Button processes, but does not handle
Assert.True (clicked);
clicked = false;
Assert.False (btn.NewKeyDownEvent (Key.T.WithAlt));
Assert.True (clicked);
clicked = false;
Assert.False (btn.NewKeyDownEvent (btn.HotKey));
Assert.True (clicked);
clicked = false;
Assert.False (btn.NewKeyDownEvent (btn.HotKey));
Assert.True (clicked);
clicked = false;
// IsDefault = false
// Space and Enter should work
Assert.False (btn.IsDefault);
Assert.False (btn.NewKeyDownEvent (Key.Enter));
Assert.True (clicked);
clicked = false;
// IsDefault = true
// Space and Enter should work
btn.IsDefault = true;
Assert.False (btn.NewKeyDownEvent (Key.Enter));
Assert.True (clicked);
clicked = false;
// Toplevel does not handle Enter, so it should get passed on to button
Assert.False (Application.Top.NewKeyDownEvent (Key.Enter));
Assert.True (clicked);
clicked = false;
// Direct
Assert.False (btn.NewKeyDownEvent (Key.Enter));
Assert.True (clicked);
clicked = false;
Assert.False (btn.NewKeyDownEvent (Key.Space));
Assert.True (clicked);
clicked = false;
Assert.False (btn.NewKeyDownEvent (new ((KeyCode)'T')));
Assert.True (clicked);
clicked = false;
// Change hotkey:
btn.Text = "Te_st";
Assert.False (btn.NewKeyDownEvent (btn.HotKey));
Assert.True (clicked);
clicked = false;
top.Dispose ();
}
[Fact]
public void HotKey_Command_Accepts ()
{
var button = new Button ();
var accepted = false;
button.Accepting += ButtonOnAccept;
button.InvokeCommand (Command.HotKey);
Assert.True (accepted);
button.Dispose ();
return;
void ButtonOnAccept (object sender, CommandEventArgs e) { accepted = true; }
}
[Fact]
public void Accept_Cancel_Event_OnAccept_Returns_True ()
{
var button = new Button ();
var acceptInvoked = false;
button.Accepting += ButtonAccept;
bool? ret = button.InvokeCommand (Command.Accept);
Assert.True (ret);
Assert.True (acceptInvoked);
button.Dispose ();
return;
void ButtonAccept (object sender, CommandEventArgs e)
{
acceptInvoked = true;
e.Handled = true;
}
}
[Fact]
public void Setting_Empty_Text_Sets_HoKey_To_KeyNull ()
{
var super = new View ();
var btn = new Button { Text = "_Test" };
super.Add (btn);
super.BeginInit ();
super.EndInit ();
Assert.Equal ("_Test", btn.Text);
Assert.Equal (KeyCode.T, btn.HotKey);
btn.Text = string.Empty;
Assert.Equal ("", btn.Text);
Assert.Equal (KeyCode.Null, btn.HotKey);
btn.Text = string.Empty;
Assert.Equal ("", btn.Text);
Assert.Equal (KeyCode.Null, btn.HotKey);
btn.Text = "Te_st";
Assert.Equal ("Te_st", btn.Text);
Assert.Equal (KeyCode.S, btn.HotKey);
super.Dispose ();
}
[Fact]
public void TestAssignTextToButton ()
{
View b = new Button { Text = "heya" };
Assert.Equal ("heya", b.Text);
Assert.Contains ("heya", b.TextFormatter.Text);
b.Text = "heyb";
Assert.Equal ("heyb", b.Text);
Assert.Contains ("heyb", b.TextFormatter.Text);
// with cast
Assert.Equal ("heyb", ((Button)b).Text);
b.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Update_Parameterless_Only_On_Or_After_Initialize ()
{
Button.DefaultShadow = ShadowStyle.None;
var btn = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (btn);
var top = new Toplevel ();
top.Add (win);
Assert.False (btn.IsInitialized);
Application.Begin (top);
AutoInitShutdownAttribute.FakeResize(new Size(30, 5));
Assert.True (btn.IsInitialized);
Assert.Equal ("Say Hello 你", btn.Text);
Assert.Equal ($"{Glyphs.LeftBracket} {btn.Text} {Glyphs.RightBracket}", btn.TextFormatter.Text);
Assert.Equal (new (0, 0, 16, 1), btn.Viewport);
var btnTxt = $"{Glyphs.LeftBracket} {btn.Text} {Glyphs.RightBracket}";
var expected = @$"
┌────────────────────────────┐
│ │
│ {btnTxt} │
│ │
└────────────────────────────┘
";
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 5), pos);
top.Dispose ();
}
[Theory]
[InlineData (MouseFlags.Button1Pressed, MouseFlags.Button1Released, MouseFlags.Button1Clicked)]
[InlineData (MouseFlags.Button2Pressed, MouseFlags.Button2Released, MouseFlags.Button2Clicked)]
[InlineData (MouseFlags.Button3Pressed, MouseFlags.Button3Released, MouseFlags.Button3Clicked)]
[InlineData (MouseFlags.Button4Pressed, MouseFlags.Button4Released, MouseFlags.Button4Clicked)]
public void WantContinuousButtonPressed_True_ButtonClick_Accepts (MouseFlags pressed, MouseFlags released, MouseFlags clicked)
{
var me = new MouseEventArgs ();
var button = new Button
{
Width = 1,
Height = 1,
WantContinuousButtonPressed = true
};
var selectingCount = 0;
button.Selecting += (s, e) => selectingCount++;
var acceptedCount = 0;
button.Accepting += (s, e) =>
{
acceptedCount++;
e.Handled = true;
};
me = new ();
me.Flags = pressed;
button.NewMouseEvent (me);
Assert.Equal (0, selectingCount);
Assert.Equal (0, acceptedCount);
me = new ();
me.Flags = released;
button.NewMouseEvent (me);
Assert.Equal (0, selectingCount);
Assert.Equal (0, acceptedCount);
me = new ();
me.Flags = clicked;
button.NewMouseEvent (me);
Assert.Equal (1, selectingCount);
Assert.Equal (1, acceptedCount);
button.Dispose ();
}
[Theory]
[InlineData (MouseFlags.Button1Pressed, MouseFlags.Button1Released)]
[InlineData (MouseFlags.Button2Pressed, MouseFlags.Button2Released)]
[InlineData (MouseFlags.Button3Pressed, MouseFlags.Button3Released)]
[InlineData (MouseFlags.Button4Pressed, MouseFlags.Button4Released)]
public void WantContinuousButtonPressed_True_ButtonPressRelease_Does_Not_Raise_Selected_Or_Accepted (MouseFlags pressed, MouseFlags released)
{
var me = new MouseEventArgs ();
var button = new Button
{
Width = 1,
Height = 1,
WantContinuousButtonPressed = true
};
var acceptedCount = 0;
button.Accepting += (s, e) =>
{
acceptedCount++;
e.Handled = true;
};
var selectingCount = 0;
button.Selecting += (s, e) =>
{
selectingCount++;
e.Handled = true;
};
me.Flags = pressed;
button.NewMouseEvent (me);
Assert.Equal (0, acceptedCount);
Assert.Equal (0, selectingCount);
me.Flags = released;
button.NewMouseEvent (me);
Assert.Equal (0, acceptedCount);
Assert.Equal (0, selectingCount);
button.Dispose ();
}
}