Skip to content

Commit

Permalink
Add tests for default colors (#1532)
Browse files Browse the repository at this point in the history
* Add tests for default colors

* - add null check for editor

* - skip time picker

* - remove TP text color test
  • Loading branch information
PureWeen authored Jul 2, 2021
1 parent e7dbda8 commit da20ab0
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Core/src/Platform/Android/EditTextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public static void UpdateTextColor(this AppCompatEditText editText, ITextStyle e

if (textColor == null)
{
editText.SetTextColor(defaultColor);
if (defaultColor != null)
editText.SetTextColor(defaultColor);
}
else
{
Expand Down
5 changes: 4 additions & 1 deletion src/Core/src/Platform/Android/TextViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public static void UpdateText(this TextView textView, string newText)

public static void UpdateTextColor(this TextView textView, ITextStyle textStyle, Graphics.Color defaultColor)
{
textView.SetTextColor(textStyle.TextColor?.ToNative() ?? defaultColor.ToNative());
var textColor = textStyle.TextColor?.ToNative() ?? defaultColor?.ToNative();

if (textColor != null)
textView.SetTextColor(textColor.Value);
}

public static void UpdateTextColor(this TextView textView, ITextStyle textStyle) =>
Expand Down
12 changes: 12 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Button/ButtonHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public async Task TextColorInitializesCorrectly()
await ValidatePropertyInitValue(button, () => button.TextColor, GetNativeTextColor, button.TextColor);
}

[Fact(DisplayName = "Null Text Color Doesn't Crash")]
public async Task NullTextColorDoesntCrash()
{
var button = new ButtonStub()
{
Text = "Test",
TextColor = null
};

await CreateHandlerAsync(button);
}

[Fact(DisplayName = "Click event fires Correctly")]
public async Task ClickEventFires()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public async Task TextColorInitializesCorrectly()
await ValidatePropertyInitValue(datePicker, () => datePicker.TextColor, GetNativeTextColor, datePicker.TextColor);
}

[Fact(DisplayName = "Null Text Color Doesn't Crash")]
public async Task NullTextColorDoesntCrash()
{
var datePicker = new DatePickerStub()
{
Date = DateTime.Today,
TextColor = null
};

await CreateHandlerAsync(datePicker);
}

[Theory(DisplayName = "Font Size Initializes Correctly")]
[InlineData(1)]
[InlineData(10)]
Expand Down
24 changes: 24 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Editor/EditorHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public async Task TextColorInitializesCorrectly()
await ValidatePropertyInitValue(editor, () => editor.TextColor, GetNativeTextColor, editor.TextColor);
}

[Fact(DisplayName = "Null Text Color Doesn't Crash")]
public async Task NullTextColorDoesntCrash()
{
var editor = new EditorStub()
{
Text = "Test",
TextColor = null
};

await CreateHandlerAsync(editor);
}

[Fact(DisplayName = "PlaceholderColor Initializes Correctly")]
public async Task PlaceholderColorInitializesCorrectly()
{
Expand All @@ -67,6 +79,18 @@ public async Task PlaceholderColorInitializesCorrectly()
await ValidatePropertyInitValue(editor, () => editor.PlaceholderColor, GetNativePlaceholderColor, editor.PlaceholderColor);
}

[Fact(DisplayName = "Null Placeholder Color Doesn't Crash")]
public async Task NullPlaceholderColorDoesntCrash()
{
var editor = new EditorStub()
{
Placeholder = "Test",
PlaceholderColor = null
};

await CreateHandlerAsync(editor);
}

[Theory(DisplayName = "PlaceholderColor Updates Correctly")]
[InlineData(0xFF0000, 0x0000FF)]
[InlineData(0x0000FF, 0xFF0000)]
Expand Down
16 changes: 14 additions & 2 deletions src/Core/tests/DeviceTests/Handlers/Entry/EntryHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public async Task TextColorInitializesCorrectly()
await ValidatePropertyInitValue(entry, () => entry.TextColor, GetNativeTextColor, entry.TextColor);
}

[Fact(DisplayName = "Null Text Color Doesn't Crash")]
public async Task NullTextColorDoesntCrash()
{
var entry = new EntryStub()
{
Text = "Test",
TextColor = null
};

await CreateHandlerAsync(entry);
}

[Theory(DisplayName = "IsPassword Initializes Correctly")]
[InlineData(true)]
[InlineData(false)]
Expand All @@ -51,10 +63,10 @@ public async Task PlaceholderInitializesCorrectly()
{
var entry = new EntryStub()
{
Text = "Placeholder"
Placeholder = "Placeholder"
};

await ValidatePropertyInitValue(entry, () => entry.Placeholder, GetNativePlaceholder, entry.Placeholder);
await ValidatePropertyInitValue(entry, () => entry.Placeholder, GetNativePlaceholder, "Placeholder");
}

[Theory(DisplayName = "Is Text Prediction Enabled")]
Expand Down
12 changes: 12 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Label/LabelHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public async Task TextColorInitializesCorrectly()
await ValidatePropertyInitValue(label, () => label.TextColor, GetNativeTextColor, label.TextColor);
}

[Fact(DisplayName = "Null Text Color Doesn't Crash")]
public async Task NullTextColorDoesntCrash()
{
var label = new LabelStub()
{
Text = "Test",
TextColor = null
};

await CreateHandlerAsync(label);
}

[Theory(DisplayName = "Font Size Initializes Correctly")]
[InlineData(1)]
[InlineData(10)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ public async Task ProgressInitializesCorrectly(double progress)
{
var progressBar = new ProgressBarStub()
{
Progress = progress
Progress = progress,
};

var expected = progressBar.Progress;

await ValidatePropertyInitValue(progressBar, () => progressBar.Progress, GetNativeProgress, progressBar.Progress);
}

[Fact(DisplayName = "Null Progress Color Doesn't Crash")]
public async Task NullProgressColorDoesntCrash()
{
var progressBar = new ProgressBarStub()
{
ProgressColor = null
};

await CreateHandlerAsync(progressBar);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,19 @@ public async Task TextColorInitializesCorrectly()
TextColor = Colors.Red
};

await ValidatePropertyInitValue(searchBar, () => searchBar.TextColor, GetNativeTextColor, searchBar.TextColor);
await ValidatePropertyInitValue(searchBar, () => searchBar.TextColor, GetNativeTextColor, Colors.Red);
}

[Fact(DisplayName = "Null Text Color Doesn't Crash")]
public async Task NullTextColorDoesntCrash()
{
var searchBar = new SearchBarStub
{
Text = "TextColor",
TextColor = null,
};

await CreateHandlerAsync(searchBar);
}

[Fact(DisplayName = "Placeholder Initializes Correctly")]
Expand Down Expand Up @@ -130,5 +142,16 @@ public async Task CancelButtonColorInitializeCorrectly()

await ValidateHasColor(searchBar, Colors.MediumPurple, () => searchBar.CancelButtonColor = Colors.MediumPurple);
}

[Fact(DisplayName = "Null Cancel Button Color Doesn't Crash")]
public async Task NullCancelButtonColorDoesntCrash()
{
var searchBar = new SearchBarStub
{
CancelButtonColor = null,
};

await CreateHandlerAsync(searchBar);
}
}
}
11 changes: 11 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Slider/SliderHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public async Task ThumbColorInitializesCorrectly()
await ValidateNativeThumbColor(slider, Colors.Purple);
}

[Fact(DisplayName = "Null Thumb Color Doesn't Crash")]
public async Task NullThumbColorDoesntCrash()
{
var slider = new SliderStub()
{
ThumbColor = null,
};

await CreateHandlerAsync(slider);
}

#if !__ANDROID__ // Android native control behavior works differently; see SliderHandlerTests.Android.cs
[Fact(DisplayName = "Value Initializes Correctly")]
public async Task ValueInitializesCorrectly()
Expand Down
13 changes: 13 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Switch/SwitchHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ public async Task ThumbColorInitializesCorrectly()
await ValidateThumbColor(switchStub, Colors.Blue);
}


[Fact(DisplayName = "Null Thumb Color Doesn't Crash")]
public async Task NullThumbColorDoesntCrash()
{
var switchStub = new SwitchStub()
{
IsOn = true,
ThumbColor = null,
};

await CreateHandlerAsync(switchStub);
}

[Fact(DisplayName = "Track Color Updates Correctly")]
public async Task ThumbColorUpdatesCorrectly()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Xunit;
using AColor = Android.Graphics.Color;

namespace Microsoft.Maui.DeviceTests
{
Expand Down Expand Up @@ -91,6 +93,13 @@ double GetNativeCharacterSpacing(TimePickerHandler timePickerHandler)
return -1;
}

Color GetNativeTextColor(TimePickerHandler timePickerHandler)
{
int currentTextColorInt = GetNativeTimePicker(timePickerHandler).CurrentTextColor;
AColor currentTextColor = new AColor(currentTextColorInt);
return currentTextColor.ToColor();
}

double GetNativeUnscaledFontSize(TimePickerHandler timePickerHandler)
{
var mauiTimePicker = GetNativeTimePicker(timePickerHandler);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Xunit;

Expand Down Expand Up @@ -54,5 +55,17 @@ public async Task FontAttributesInitializeCorrectly(FontWeight weight, bool isBo
await ValidatePropertyInitValue(timePicker, () => timePicker.Font.Weight == FontWeight.Bold, GetNativeIsBold, isBold);
await ValidatePropertyInitValue(timePicker, () => timePicker.Font.FontSlant == FontSlant.Italic, GetNativeIsItalic, isItalic);
}

[Fact(DisplayName = "Null Text Color Doesn't Crash")]
public async Task NullTextColorDoesntCrash()
{
var timePicker = new TimePickerStub()
{
Time = DateTime.Now.TimeOfDay,
TextColor = null
};

await CreateHandlerAsync(timePicker);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using UIKit;
using Xunit;
Expand Down Expand Up @@ -62,6 +63,9 @@ public async Task FontFamilyInitializesCorrectly(string family)
MauiTimePicker GetNativeTimePicker(TimePickerHandler timePickerHandler) =>
(MauiTimePicker)timePickerHandler.NativeView;

Color GetNativeTextColor(TimePickerHandler timePickerHandler) =>
GetNativeTimePicker(timePickerHandler).TextColor.ToColor();

async Task ValidateTime(ITimePicker timePickerStub, Action action = null)
{
var actual = await GetValueAsync(timePickerStub, handler =>
Expand Down

0 comments on commit da20ab0

Please sign in to comment.