Skip to content

Commit

Permalink
Merge pull request #309 from enisn/4.1-unit-tests
Browse files Browse the repository at this point in the history
Update Unit Tests
  • Loading branch information
enisn authored Sep 17, 2022
2 parents 72d397b + 6f8d6eb commit b5c2f9a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 21 deletions.
7 changes: 5 additions & 2 deletions src/InputKit.Maui/Shared/Controls/CheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public CheckBox()
UpdateShape();
GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(() => { if (IsDisabled) return; IsChecked = !IsChecked; ExecuteCommand(); CheckChanged?.Invoke(this, new EventArgs()); ValidationChanged?.Invoke(this, new EventArgs()); }),
Command = new Command(() => { if (IsDisabled) return; IsChecked = !IsChecked; }),
});

iconValidation = new Lazy<Path>(() => new Path
Expand Down Expand Up @@ -125,7 +125,6 @@ public CheckBox(string optionName, int key) : this()
/// Invoked when check changed
/// </summary>
public event EventHandler CheckChanged;
public event EventHandler ValidationChanged;
#endregion

#region Properties
Expand Down Expand Up @@ -464,6 +463,10 @@ public static void ApplyIsChecked(CheckBox checkBox, bool isChecked)

checkBox.UpdateColors();

checkBox.ExecuteCommand();

checkBox.CheckChanged?.Invoke(checkBox, new EventArgs());

if (checkBox.iconValidation.IsValueCreated && isChecked)
{
checkBox.DisplayValidation();
Expand Down
30 changes: 13 additions & 17 deletions src/InputKit.Maui/Shared/Controls/RadioButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@ public RadioButton()
ApplyLabelPosition(LabelPosition);
UpdateShape();

GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(Tapped) });
GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() =>
{
if (IsDisabled)
{
return;
}

IsChecked = true;
})});
}
#endregion

Expand Down Expand Up @@ -205,7 +213,7 @@ public LabelPosition LabelPosition

#region BindableProperties
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(RadioButton), false, propertyChanged: (bo, ov, nv) => (bo as RadioButton).ApplyIsCheckedAction((bool)nv));
public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(RadioButton), false, BindingMode.TwoWay, propertyChanged: (bo, ov, nv) => (bo as RadioButton).ApplyIsCheckedAction((bool)nv));
public static readonly BindableProperty IsDisabledProperty = BindableProperty.Create(nameof(IsDisabled), typeof(bool), typeof(RadioButton), false, propertyChanged: (bo, ov, nv) => (bo as RadioButton).IsDisabled = (bool)nv);
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(RadioButton), "", propertyChanged: (bo, ov, nv) => (bo as RadioButton).Text = (string)nv);
public static readonly BindableProperty TextFontSizeProperty = BindableProperty.Create(nameof(TextFontSize), typeof(double), typeof(RadioButton), 20.0, propertyChanged: (bo, ov, nv) => (bo as RadioButton).TextFontSize = (double)nv);
Expand Down Expand Up @@ -248,21 +256,6 @@ private protected virtual void UpdateShape()
iconChecked.Data = SelectedIconGeomerty;
}

/// <summary>
/// That handles tapps and triggers event, commands etc.
/// </summary>
void Tapped()
{
if (IsDisabled)
{
return;
}

IsChecked = true;
Clicked?.Invoke(this, new EventArgs());
ClickCommand?.Execute(CommandParameter ?? Value);
}

void UpdateColors()
{
iconChecked.Fill = Color;
Expand All @@ -272,6 +265,9 @@ void UpdateColors()

public virtual void ApplyIsChecked(bool isChecked)
{
Clicked?.Invoke(this, new EventArgs());
ClickCommand?.Execute(CommandParameter ?? Value);

var isCheckedInLastState = iconChecked.Scale == 1;

var changed = isCheckedInLastState != isChecked;
Expand Down
54 changes: 54 additions & 0 deletions test/InputKit.Maui.Test/AdvancedEntry_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using InputKit.Maui.Test.TestClasses;
using InputKit.Shared.Controls;
using Shouldly;
using System.Windows.Input;

namespace InputKit.Maui.Test;
public class AdvancedEntry_Tests
{
public AdvancedEntry_Tests()
{
ApplicationExtensions.CreateAndSetMockApplication();
}

[Fact]
public void Text_BindingForInitializtion_FromSource()
{
var control = new AdvancedEntry();
var viewModel = new TestViewModel { Text = "Text Initial Value" };
control.BindingContext = viewModel;
control.SetBinding(AdvancedEntry.TextProperty, new Binding(nameof(TestViewModel.Text)));

// Assert
control.Text.ShouldBe(viewModel.Text);
}

[Fact]
public void Text_Binding_FromSource()
{
var control = new AdvancedEntry();
var viewModel = new TestViewModel { Text = "Text Initial Value" };
control.BindingContext = viewModel;
control.SetBinding(AdvancedEntry.TextProperty, new Binding(nameof(TestViewModel.Text)));

// Act
viewModel.Text = "Changed Value";

// Assert
control.Text.ShouldBe(viewModel.Text);
}

public class TestViewModel : InputKitBindableObject
{
private bool isChecked;
private string text;

public bool IsChecked { get => isChecked; set => SetProperty(ref isChecked, value); }

public string Text { get => text; set => SetProperty(ref text, value); }

public ICommand Command { get; set; }

public object CommandParameter { get; set; } = "My Command Parameter 1";
}
}
4 changes: 3 additions & 1 deletion test/InputKit.Maui.Test/CheckBox_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void CheckChangedCommand_ShouldBeExecuted_InSource()
});

var control = AnimationReadyHandler.Prepare(new CheckBox());
control.BindingContext = viewModel;
control.SetBinding(CheckBox.CheckChangedCommandProperty, new Binding(nameof(TestViewModel.Command)));

// Act
Expand All @@ -103,8 +104,9 @@ public void CommandParameter_ShouldBePassed_ToSource()
});

var control = AnimationReadyHandler.Prepare(new CheckBox());
control.BindingContext = viewModel;
control.SetBinding(CheckBox.CheckChangedCommandProperty, new Binding(nameof(TestViewModel.Command)));
control.SetBinding(CheckBox.CommandParameterProperty, new Binding(nameof(TestViewModel.Command)));
control.SetBinding(CheckBox.CommandParameterProperty, new Binding(nameof(TestViewModel.CommandParameter)));

// Act
control.IsChecked = true;
Expand Down
4 changes: 3 additions & 1 deletion test/InputKit.Maui.Test/RadioButton_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void ClickCommandProperty_ShouldBeExecuted_InSource()
});

var control = AnimationReadyHandler.Prepare(new RadioButton());
control.BindingContext = viewModel;
control.SetBinding(RadioButton.ClickCommandProperty, new Binding(nameof(TestViewModel.Command)));

// Act
Expand All @@ -101,8 +102,9 @@ public void CommandParameter_ShouldBePassed_ToSource()
});

var control = AnimationReadyHandler.Prepare(new RadioButton());
control.BindingContext = viewModel;
control.SetBinding(RadioButton.ClickCommandProperty, new Binding(nameof(TestViewModel.Command)));
control.SetBinding(RadioButton.CommandParameterProperty, new Binding(nameof(TestViewModel.Command)));
control.SetBinding(RadioButton.CommandParameterProperty, new Binding(nameof(TestViewModel.CommandParameter)));

// Act
control.IsChecked = true;
Expand Down

0 comments on commit b5c2f9a

Please sign in to comment.