From f5df76a6310105a99542983d8efe1f0b5556d52d Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Mon, 7 Feb 2022 16:11:40 +0800 Subject: [PATCH 01/15] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20circle=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/CircleTest.cs | 72 ++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/UnitTest/Components/CircleTest.cs diff --git a/test/UnitTest/Components/CircleTest.cs b/test/UnitTest/Components/CircleTest.cs new file mode 100644 index 000000000..72dc12c91 --- /dev/null +++ b/test/UnitTest/Components/CircleTest.cs @@ -0,0 +1,72 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using BootstrapBlazor.Components; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UnitTest.Components; + +public class CircleTest : TestBase +{ + [Fact] + public void Value_Ok() + { + var cut = Context.RenderComponent(builder => builder.Add(a => a.Value, 100)); + + Assert.Contains("100%", cut.Markup); + } + + [Fact] + public void Width_Ok() + { + var cut = Context.RenderComponent(builder => builder.Add(a => a.Width, 100)); + + Assert.Contains("width: 100px", cut.Markup); + } + + [Fact] + public void StrokeWidth_Ok() + { + var cut = Context.RenderComponent(builder => builder.Add(a => a.StrokeWidth, 5)); + + Assert.Contains("stroke-width=\"5\"", cut.Markup); + } + + [Fact] + public void Color_Ok() + { + var cut = Context.RenderComponent(builder => builder.Add(a => a.Color, Color.Success)); + + var element = cut.Find(".circle-success"); + + Assert.NotNull(element); + } + + [Fact] + public void ShowProgress_Ok() + { + var cut = Context.RenderComponent(builder => builder.Add(a => a.ShowProgress, false)); + + var element = cut.Find(".d-none"); + + Assert.NotNull(element); + } + + [Fact] + public void Value_ChildContent() + { + var cut = Context.RenderComponent(builder => builder.Add(a => a.ChildContent, s => + { + s.OpenElement(1, "div"); + s.AddContent(2, "I am cricle"); + s.CloseElement(); + })); + + Assert.Contains("I am cricle", cut.Markup); + } +} -- Gitee From fdfabbec0a5781c47dcbfd3bb3914f73b82c51ff Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 01:03:41 +0800 Subject: [PATCH 02/15] =?UTF-8?q?refactor:=20=E5=90=88=E5=B9=B6=20CircleBa?= =?UTF-8?q?se=20=E5=9F=BA=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Circle/Circle.razor | 4 +- .../Components/Circle/Circle.razor.cs | 106 +++++++++++++++++- .../Components/Circle/CircleBase.cs | 105 ----------------- 3 files changed, 102 insertions(+), 113 deletions(-) delete mode 100644 src/BootstrapBlazor/Components/Circle/CircleBase.cs diff --git a/src/BootstrapBlazor/Components/Circle/Circle.razor b/src/BootstrapBlazor/Components/Circle/Circle.razor index 75310805f..c7ce41f5d 100644 --- a/src/BootstrapBlazor/Components/Circle/Circle.razor +++ b/src/BootstrapBlazor/Components/Circle/Circle.razor @@ -1,5 +1,5 @@ @namespace BootstrapBlazor.Components -@inherits CircleBase +@inherits BootstrapComponentBase
@@ -8,4 +8,4 @@
@ValueTitleString
@ChildContent -
\ No newline at end of file + diff --git a/src/BootstrapBlazor/Components/Circle/Circle.razor.cs b/src/BootstrapBlazor/Components/Circle/Circle.razor.cs index a63e4ff04..83825f396 100644 --- a/src/BootstrapBlazor/Components/Circle/Circle.razor.cs +++ b/src/BootstrapBlazor/Components/Circle/Circle.razor.cs @@ -7,10 +7,69 @@ using Microsoft.AspNetCore.Components; namespace BootstrapBlazor.Components; /// -/// +/// CirCle 组件 /// -public sealed partial class Circle +public partial class Circle { + /// + /// 获得/设置 当前进度值 + /// + private string? ValueString => $"{Math.Round(((1 - Value * 1.0 / 100) * CircleLength), 2)}"; + + /// + /// 获得/设置 Title 字符串 + /// + private string ValueTitleString => $"{Value}%"; + /// + /// 获得 组件样式字符串 + /// + protected virtual string? ClassString => CssBuilder.Default("circle") + .AddClassFromAttributes(AdditionalAttributes) + .Build(); + + /// + /// 获得 预览框 Style 属性 + /// + protected string? PrevStyleString => CssBuilder.Default() + .AddClass($"width: {Width}px;", Width > 0) + .AddClass($"height: {Width}px;", Width > 0) + .AddClass("transform: rotate(-90deg);") + .Build(); + + /// + /// 获得 进度条样式 + /// + protected string? ProgressClassString => CssBuilder.Default("circle-progress") + .AddClass($"circle-{Color.ToDescriptionString()}") + .Build(); + + /// + /// 获得 进度条百分比样式 + /// + protected string? TitleClassString => CssBuilder.Default("circle-title") + .AddClass("d-none", !ShowProgress) + .Build(); + + /// + /// 获得/设置 Dash 字符串 + /// + protected string DashString => $"{CircleLength}, {CircleLength}"; + + /// + /// 获得/设置 圆形进度半径 + /// + protected string CircleDiameter => $"{Width / 2}"; + + /// + /// 获得/设置 半径 + /// + protected string CircleR => $"{Width / 2 - StrokeWidth}"; + + /// + /// 获得 圆形周长 + /// + protected double CircleLength => Math.Round((Width / 2 - StrokeWidth) * 2 * Math.PI, 2); + /// /// 获得/设置 当前值 /// @@ -18,12 +77,47 @@ public sealed partial class Circle public int Value { get; set; } /// - /// 获得/设置 当前进度值 + /// 获得/设置 文件预览框宽度 /// - private string? ValueString => $"{Math.Round(((1 - Value * 1.0 / 100) * CircleLength), 2)}"; + [Parameter] + public virtual int Width { get; set; } = 120; /// - /// 获得/设置 Title 字符串 + /// 获得/设置 进度条宽度 默认为 2 /// - private string ValueTitleString => $"{Value}%"; + [Parameter] + public virtual int StrokeWidth { get; set; } = 2; + + /// + /// 获得/设置 组件进度条颜色 + /// + [Parameter] + public Color Color { get; set; } = Color.Primary; + + /// + /// 获得/设置 是否显示进度百分比 默认显示 + /// + [Parameter] + public bool ShowProgress { get; set; } = true; + + /// + /// 获得/设置 子组件 + /// + [Parameter] + public RenderFragment? ChildContent { get; set; } + + /// + /// OnParametersSet 方法 + /// + protected override void OnParametersSet() + { + base.OnParametersSet(); + + // 检查 StrokeWidth 参数 + if (Width / 2 < StrokeWidth) + { + StrokeWidth = 2; + } + Width = Math.Max(6, Width); + } } diff --git a/src/BootstrapBlazor/Components/Circle/CircleBase.cs b/src/BootstrapBlazor/Components/Circle/CircleBase.cs deleted file mode 100644 index 31dbf9a61..000000000 --- a/src/BootstrapBlazor/Components/Circle/CircleBase.cs +++ /dev/null @@ -1,105 +0,0 @@ -// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -// Website: https://www.blazor.zone or https://argozhang.github.io/ - -using Microsoft.AspNetCore.Components; - -namespace BootstrapBlazor.Components; - -/// -/// Circle 组件基类 -/// -public abstract class CircleBase : BootstrapComponentBase -{ - /// - /// 获得 组件样式字符串 - /// - protected virtual string? ClassString => CssBuilder.Default("circle") - .AddClassFromAttributes(AdditionalAttributes) - .Build(); - - /// - /// 获得 预览框 Style 属性 - /// - protected string? PrevStyleString => CssBuilder.Default() - .AddClass($"width: {Width}px;", Width > 0) - .AddClass($"height: {Width}px;", Width > 0) - .AddClass("transform: rotate(-90deg);") - .Build(); - - /// - /// 获得 进度条样式 - /// - protected string? ProgressClassString => CssBuilder.Default("circle-progress") - .AddClass($"circle-{Color.ToDescriptionString()}") - .Build(); - - /// - /// 获得 进度条百分比样式 - /// - protected string? TitleClassString => CssBuilder.Default("circle-title") - .AddClass("d-none", !ShowProgress) - .Build(); - - /// - /// 获得/设置 Dash 字符串 - /// - protected string DashString => $"{CircleLength}, {CircleLength}"; - - /// - /// 获得/设置 圆形进度半径 - /// - protected string CircleDiameter => $"{Width / 2}"; - - /// - /// 获得/设置 半径 - /// - protected string CircleR => $"{Width / 2 - StrokeWidth}"; - - /// - /// 获得 圆形周长 - /// - protected double CircleLength => Math.Round((Width / 2 - StrokeWidth) * 2 * Math.PI, 2); - - /// - /// 获得/设置 文件预览框宽度 - /// - [Parameter] - public virtual int Width { get; set; } = 120; - - /// - /// 获得/设置 进度条宽度 默认为 2 - /// - [Parameter] - public virtual int StrokeWidth { get; set; } = 2; - - /// - /// 获得/设置 组件进度条颜色 - /// - [Parameter] - public Color Color { get; set; } = Color.Primary; - - /// - /// 获得/设置 是否显示进度百分比 默认显示 - /// - [Parameter] - public bool ShowProgress { get; set; } = true; - - /// - /// 获得/设置 子组件 - /// - [Parameter] - public RenderFragment? ChildContent { get; set; } - - /// - /// OnInitialized 方法 - /// - protected override void OnInitialized() - { - base.OnInitialized(); - - // 检查 StrokeWidth 参数 - if (Width / 2 < StrokeWidth) StrokeWidth = 2; - Width = Math.Max(6, Width); - } -} -- Gitee From ad45fea8b1fcab93f88eff8005de0736ffd7ebad Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 01:05:23 +0800 Subject: [PATCH 03/15] =?UTF-8?q?Revert=20"refactor:=20=E5=90=88=E5=B9=B6?= =?UTF-8?q?=20CircleBase=20=E5=9F=BA=E7=B1=BB"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit fdfabbec0a5781c47dcbfd3bb3914f73b82c51ff. --- .../Components/Circle/Circle.razor | 4 +- .../Components/Circle/Circle.razor.cs | 106 +----------------- .../Components/Circle/CircleBase.cs | 105 +++++++++++++++++ 3 files changed, 113 insertions(+), 102 deletions(-) create mode 100644 src/BootstrapBlazor/Components/Circle/CircleBase.cs diff --git a/src/BootstrapBlazor/Components/Circle/Circle.razor b/src/BootstrapBlazor/Components/Circle/Circle.razor index c7ce41f5d..75310805f 100644 --- a/src/BootstrapBlazor/Components/Circle/Circle.razor +++ b/src/BootstrapBlazor/Components/Circle/Circle.razor @@ -1,5 +1,5 @@ @namespace BootstrapBlazor.Components -@inherits BootstrapComponentBase +@inherits CircleBase
@@ -8,4 +8,4 @@
@ValueTitleString
@ChildContent -
+ \ No newline at end of file diff --git a/src/BootstrapBlazor/Components/Circle/Circle.razor.cs b/src/BootstrapBlazor/Components/Circle/Circle.razor.cs index 83825f396..a63e4ff04 100644 --- a/src/BootstrapBlazor/Components/Circle/Circle.razor.cs +++ b/src/BootstrapBlazor/Components/Circle/Circle.razor.cs @@ -7,69 +7,10 @@ using Microsoft.AspNetCore.Components; namespace BootstrapBlazor.Components; /// -/// CirCle 组件 +/// /// -public partial class Circle +public sealed partial class Circle { - /// - /// 获得/设置 当前进度值 - /// - private string? ValueString => $"{Math.Round(((1 - Value * 1.0 / 100) * CircleLength), 2)}"; - - /// - /// 获得/设置 Title 字符串 - /// - private string ValueTitleString => $"{Value}%"; - /// - /// 获得 组件样式字符串 - /// - protected virtual string? ClassString => CssBuilder.Default("circle") - .AddClassFromAttributes(AdditionalAttributes) - .Build(); - - /// - /// 获得 预览框 Style 属性 - /// - protected string? PrevStyleString => CssBuilder.Default() - .AddClass($"width: {Width}px;", Width > 0) - .AddClass($"height: {Width}px;", Width > 0) - .AddClass("transform: rotate(-90deg);") - .Build(); - - /// - /// 获得 进度条样式 - /// - protected string? ProgressClassString => CssBuilder.Default("circle-progress") - .AddClass($"circle-{Color.ToDescriptionString()}") - .Build(); - - /// - /// 获得 进度条百分比样式 - /// - protected string? TitleClassString => CssBuilder.Default("circle-title") - .AddClass("d-none", !ShowProgress) - .Build(); - - /// - /// 获得/设置 Dash 字符串 - /// - protected string DashString => $"{CircleLength}, {CircleLength}"; - - /// - /// 获得/设置 圆形进度半径 - /// - protected string CircleDiameter => $"{Width / 2}"; - - /// - /// 获得/设置 半径 - /// - protected string CircleR => $"{Width / 2 - StrokeWidth}"; - - /// - /// 获得 圆形周长 - /// - protected double CircleLength => Math.Round((Width / 2 - StrokeWidth) * 2 * Math.PI, 2); - /// /// 获得/设置 当前值 /// @@ -77,47 +18,12 @@ public partial class Circle public int Value { get; set; } /// - /// 获得/设置 文件预览框宽度 - /// - [Parameter] - public virtual int Width { get; set; } = 120; - - /// - /// 获得/设置 进度条宽度 默认为 2 - /// - [Parameter] - public virtual int StrokeWidth { get; set; } = 2; - - /// - /// 获得/设置 组件进度条颜色 - /// - [Parameter] - public Color Color { get; set; } = Color.Primary; - - /// - /// 获得/设置 是否显示进度百分比 默认显示 - /// - [Parameter] - public bool ShowProgress { get; set; } = true; - - /// - /// 获得/设置 子组件 + /// 获得/设置 当前进度值 /// - [Parameter] - public RenderFragment? ChildContent { get; set; } + private string? ValueString => $"{Math.Round(((1 - Value * 1.0 / 100) * CircleLength), 2)}"; /// - /// OnParametersSet 方法 + /// 获得/设置 Title 字符串 /// - protected override void OnParametersSet() - { - base.OnParametersSet(); - - // 检查 StrokeWidth 参数 - if (Width / 2 < StrokeWidth) - { - StrokeWidth = 2; - } - Width = Math.Max(6, Width); - } + private string ValueTitleString => $"{Value}%"; } diff --git a/src/BootstrapBlazor/Components/Circle/CircleBase.cs b/src/BootstrapBlazor/Components/Circle/CircleBase.cs new file mode 100644 index 000000000..31dbf9a61 --- /dev/null +++ b/src/BootstrapBlazor/Components/Circle/CircleBase.cs @@ -0,0 +1,105 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Microsoft.AspNetCore.Components; + +namespace BootstrapBlazor.Components; + +/// +/// Circle 组件基类 +/// +public abstract class CircleBase : BootstrapComponentBase +{ + /// + /// 获得 组件样式字符串 + /// + protected virtual string? ClassString => CssBuilder.Default("circle") + .AddClassFromAttributes(AdditionalAttributes) + .Build(); + + /// + /// 获得 预览框 Style 属性 + /// + protected string? PrevStyleString => CssBuilder.Default() + .AddClass($"width: {Width}px;", Width > 0) + .AddClass($"height: {Width}px;", Width > 0) + .AddClass("transform: rotate(-90deg);") + .Build(); + + /// + /// 获得 进度条样式 + /// + protected string? ProgressClassString => CssBuilder.Default("circle-progress") + .AddClass($"circle-{Color.ToDescriptionString()}") + .Build(); + + /// + /// 获得 进度条百分比样式 + /// + protected string? TitleClassString => CssBuilder.Default("circle-title") + .AddClass("d-none", !ShowProgress) + .Build(); + + /// + /// 获得/设置 Dash 字符串 + /// + protected string DashString => $"{CircleLength}, {CircleLength}"; + + /// + /// 获得/设置 圆形进度半径 + /// + protected string CircleDiameter => $"{Width / 2}"; + + /// + /// 获得/设置 半径 + /// + protected string CircleR => $"{Width / 2 - StrokeWidth}"; + + /// + /// 获得 圆形周长 + /// + protected double CircleLength => Math.Round((Width / 2 - StrokeWidth) * 2 * Math.PI, 2); + + /// + /// 获得/设置 文件预览框宽度 + /// + [Parameter] + public virtual int Width { get; set; } = 120; + + /// + /// 获得/设置 进度条宽度 默认为 2 + /// + [Parameter] + public virtual int StrokeWidth { get; set; } = 2; + + /// + /// 获得/设置 组件进度条颜色 + /// + [Parameter] + public Color Color { get; set; } = Color.Primary; + + /// + /// 获得/设置 是否显示进度百分比 默认显示 + /// + [Parameter] + public bool ShowProgress { get; set; } = true; + + /// + /// 获得/设置 子组件 + /// + [Parameter] + public RenderFragment? ChildContent { get; set; } + + /// + /// OnInitialized 方法 + /// + protected override void OnInitialized() + { + base.OnInitialized(); + + // 检查 StrokeWidth 参数 + if (Width / 2 < StrokeWidth) StrokeWidth = 2; + Width = Math.Max(6, Width); + } +} -- Gitee From 56640adb6bbdb13ebaaa83679093deac6b8d806c Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 01:07:41 +0800 Subject: [PATCH 04/15] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=AA=8C=E8=AF=81=E9=80=BB=E8=BE=91=E5=88=B0=20OnPara?= =?UTF-8?q?meterSet=20=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Circle/CircleBase.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/Components/Circle/CircleBase.cs b/src/BootstrapBlazor/Components/Circle/CircleBase.cs index 31dbf9a61..2ff1c9e88 100644 --- a/src/BootstrapBlazor/Components/Circle/CircleBase.cs +++ b/src/BootstrapBlazor/Components/Circle/CircleBase.cs @@ -94,9 +94,9 @@ public abstract class CircleBase : BootstrapComponentBase /// /// OnInitialized 方法 /// - protected override void OnInitialized() + protected override void OnParametersSet() { - base.OnInitialized(); + base.OnParametersSet(); // 检查 StrokeWidth 参数 if (Width / 2 < StrokeWidth) StrokeWidth = 2; -- Gitee From 4ca9386097befa84a5a26184490cf142c8d0a8bc Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 01:07:53 +0800 Subject: [PATCH 05/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=A6=86=E7=9B=96=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/CircleTest.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/UnitTest/Components/CircleTest.cs b/test/UnitTest/Components/CircleTest.cs index 72dc12c91..274ac2415 100644 --- a/test/UnitTest/Components/CircleTest.cs +++ b/test/UnitTest/Components/CircleTest.cs @@ -35,6 +35,15 @@ public class CircleTest : TestBase var cut = Context.RenderComponent(builder => builder.Add(a => a.StrokeWidth, 5)); Assert.Contains("stroke-width=\"5\"", cut.Markup); + + // 增加代码覆盖率 + //Width / 2 < StrokeWidth + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.Width, 6); + pb.Add(a => a.StrokeWidth, 6); + }); + Assert.Equal(2, cut.Instance.StrokeWidth); } [Fact] -- Gitee From d160804ace188c2b92b3701dbb4f67f0f72e03a6 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:05:19 +0800 Subject: [PATCH 06/15] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=20Timer=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Timer/Timer.razor | 4 +- .../Components/Timer/Timer.razor.cs | 68 +++++++++++-------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/BootstrapBlazor/Components/Timer/Timer.razor b/src/BootstrapBlazor/Components/Timer/Timer.razor index ffc51e674..f36ac98ff 100644 --- a/src/BootstrapBlazor/Components/Timer/Timer.razor +++ b/src/BootstrapBlazor/Components/Timer/Timer.razor @@ -19,6 +19,6 @@ } else { - + } - \ No newline at end of file + diff --git a/src/BootstrapBlazor/Components/Timer/Timer.razor.cs b/src/BootstrapBlazor/Components/Timer/Timer.razor.cs index 0fc64c911..c9d1ad3d1 100644 --- a/src/BootstrapBlazor/Components/Timer/Timer.razor.cs +++ b/src/BootstrapBlazor/Components/Timer/Timer.razor.cs @@ -42,7 +42,7 @@ public partial class Timer : IDisposable private CancellationTokenSource? CancelTokenSource { get; set; } - private ManualResetEvent ResetEvent { get; set; } = new(true); + private ManualResetEvent ResetEvent { get; } = new(true); private bool Vibrate { get; set; } @@ -153,44 +153,49 @@ public partial class Timer : IDisposable Task.Run(async () => { + // 点击 Cancel 后重新设置再点击 Star if (CancelTokenSource != null) { + CancelTokenSource.Cancel(); CancelTokenSource.Dispose(); } CancelTokenSource = new CancellationTokenSource(); - try + do { - do + try { - await Task.Delay(1000, CancelTokenSource?.Token ?? new CancellationToken(true)); + await Task.Delay(1000, CancelTokenSource.Token); + } + catch (TaskCanceledException) { } - if (!(CancelTokenSource?.IsCancellationRequested ?? true)) - { - ResetEvent.WaitOne(); - CurrentTimespan = CurrentTimespan.Subtract(TimeSpan.FromSeconds(1)); - await InvokeAsync(StateHasChanged); - } + if (!CancelTokenSource.IsCancellationRequested) + { + ResetEvent.WaitOne(); + CurrentTimespan = CurrentTimespan.Subtract(TimeSpan.FromSeconds(1)); + await InvokeAsync(StateHasChanged); } - while (!(CancelTokenSource?.IsCancellationRequested ?? true) && CurrentTimespan > TimeSpan.Zero); + } + while (!CancelTokenSource.IsCancellationRequested && CurrentTimespan > TimeSpan.Zero); - if (CurrentTimespan == TimeSpan.Zero) + if (CurrentTimespan == TimeSpan.Zero) + { + await Task.Delay(500, CancelTokenSource.Token); + if (!CancelTokenSource.IsCancellationRequested) { - await Task.Delay(500, CancelTokenSource?.Token ?? new CancellationToken(true)); - if (!(CancelTokenSource?.IsCancellationRequested ?? true)) + Value = TimeSpan.Zero; + await InvokeAsync(() => { - Value = TimeSpan.Zero; - await InvokeAsync(() => + Vibrate = IsVibrate; + StateHasChanged(); + if (OnTimeout != null) { - Vibrate = IsVibrate; - StateHasChanged(); - OnTimeout?.Invoke(); - }); - } + OnTimeout(); + } + }); } } - catch (TaskCanceledException) { } }); } @@ -214,8 +219,14 @@ public partial class Timer : IDisposable private async Task OnClickCancel() { Value = TimeSpan.Zero; - CancelTokenSource?.Cancel(); - if (OnCancel != null) await OnCancel.Invoke(); + if (CancelTokenSource != null) + { + CancelTokenSource.Cancel(); + } + if (OnCancel != null) + { + await OnCancel(); + } } /// @@ -226,9 +237,12 @@ public partial class Timer : IDisposable { if (disposing) { - CancelTokenSource?.Dispose(); - CancelTokenSource = null; - + if (CancelTokenSource != null) + { + CancelTokenSource.Cancel(); + CancelTokenSource.Dispose(); + CancelTokenSource = null; + } ResetEvent.Dispose(); } } -- Gitee From fa7651bdd0cb6cd6c208d95520a085ae658cb7af Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:05:47 +0800 Subject: [PATCH 07/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Timer=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TimerTest.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/UnitTest/Components/TimerTest.cs diff --git a/test/UnitTest/Components/TimerTest.cs b/test/UnitTest/Components/TimerTest.cs new file mode 100644 index 000000000..ea09f4d1a --- /dev/null +++ b/test/UnitTest/Components/TimerTest.cs @@ -0,0 +1,12 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using System.Threading; +using Timer = BootstrapBlazor.Components.Timer; + +namespace UnitTest.Components; + +public class TimerTest : BootstrapBlazorTestBase +{ +} -- Gitee From 67a0ffefa1976811349366f10a257b5c9144a055 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:06:06 +0800 Subject: [PATCH 08/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Value=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TimerTest.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/UnitTest/Components/TimerTest.cs b/test/UnitTest/Components/TimerTest.cs index ea09f4d1a..7f92903c9 100644 --- a/test/UnitTest/Components/TimerTest.cs +++ b/test/UnitTest/Components/TimerTest.cs @@ -9,4 +9,17 @@ namespace UnitTest.Components; public class TimerTest : BootstrapBlazorTestBase { + [Fact] + public void Value_Ok() + { + var cut = Context.RenderComponent(builder => builder.Add(a => a.Value, TimeSpan.FromSeconds(10))); + + Assert.Contains("circle-body", cut.Markup); + + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.Value, TimeSpan.Zero); + }); + Assert.DoesNotContain("circle-body", cut.Markup); + } } -- Gitee From 1ef9438eece2a2f2715ca660c5008cad4fb53ecd Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:06:21 +0800 Subject: [PATCH 09/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Width=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TimerTest.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/UnitTest/Components/TimerTest.cs b/test/UnitTest/Components/TimerTest.cs index 7f92903c9..5b8aededb 100644 --- a/test/UnitTest/Components/TimerTest.cs +++ b/test/UnitTest/Components/TimerTest.cs @@ -22,4 +22,16 @@ public class TimerTest : BootstrapBlazorTestBase }); Assert.DoesNotContain("circle-body", cut.Markup); } + + [Fact] + public void Width_Ok() + { + var cut = Context.RenderComponent(builder => + { + builder.Add(a => a.Value, TimeSpan.FromSeconds(5)); + builder.Add(a => a.Width, 100); + }); + + Assert.Contains("width: 100px", cut.Markup); + } } -- Gitee From bbef01672bc83e9da956cc0a6a288b3abec7d76f Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:06:53 +0800 Subject: [PATCH 10/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Stroke=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TimerTest.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/UnitTest/Components/TimerTest.cs b/test/UnitTest/Components/TimerTest.cs index 5b8aededb..509272c07 100644 --- a/test/UnitTest/Components/TimerTest.cs +++ b/test/UnitTest/Components/TimerTest.cs @@ -34,4 +34,24 @@ public class TimerTest : BootstrapBlazorTestBase Assert.Contains("width: 100px", cut.Markup); } + + [Fact] + public void StrokeWidth_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.Value, TimeSpan.FromSeconds(5)); + pb.Add(a => a.StrokeWidth, 5); + }); + Assert.Contains("stroke-width=\"5\"", cut.Markup); + + // 增加代码覆盖率 + //Width / 2 < StrokeWidth + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.Width, 6); + pb.Add(a => a.StrokeWidth, 6); + }); + Assert.Equal(2, cut.Instance.StrokeWidth); + } } -- Gitee From d05797e49b7a670ba550978553e6b2daa73feffd Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:07:06 +0800 Subject: [PATCH 11/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Color=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TimerTest.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/UnitTest/Components/TimerTest.cs b/test/UnitTest/Components/TimerTest.cs index 509272c07..3dfda5597 100644 --- a/test/UnitTest/Components/TimerTest.cs +++ b/test/UnitTest/Components/TimerTest.cs @@ -54,4 +54,15 @@ public class TimerTest : BootstrapBlazorTestBase }); Assert.Equal(2, cut.Instance.StrokeWidth); } + + [Fact] + public void Color_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.Value, TimeSpan.FromSeconds(5)); + pb.Add(a => a.Color, Color.Success); + }); + Assert.Contains("circle-success", cut.Markup); + } } -- Gitee From 724813f73dc99ee21c1778b59f0026c42e5ac7aa Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:07:25 +0800 Subject: [PATCH 12/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Progress=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TimerTest.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/UnitTest/Components/TimerTest.cs b/test/UnitTest/Components/TimerTest.cs index 3dfda5597..55272ccd1 100644 --- a/test/UnitTest/Components/TimerTest.cs +++ b/test/UnitTest/Components/TimerTest.cs @@ -65,4 +65,15 @@ public class TimerTest : BootstrapBlazorTestBase }); Assert.Contains("circle-success", cut.Markup); } + + [Fact] + public void ShowProgress_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.Value, TimeSpan.FromSeconds(5)); + pb.Add(a => a.ShowProgress, false); + }); + Assert.Contains("circle-title d-none", cut.Markup); + } } -- Gitee From f06006dfeeadc5e0c263cd4767dda5aa2db67e18 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:07:40 +0800 Subject: [PATCH 13/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20IsVibrate=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TimerTest.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/UnitTest/Components/TimerTest.cs b/test/UnitTest/Components/TimerTest.cs index 55272ccd1..7d14ed4b4 100644 --- a/test/UnitTest/Components/TimerTest.cs +++ b/test/UnitTest/Components/TimerTest.cs @@ -76,4 +76,14 @@ public class TimerTest : BootstrapBlazorTestBase }); Assert.Contains("circle-title d-none", cut.Markup); } + + [Fact] + public void IsVibrate_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.Value, TimeSpan.FromSeconds(5)); + pb.Add(a => a.IsVibrate, false); + }); + } } -- Gitee From 682d15d7c63086cce6921966ace26db8907e0b3e Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:07:52 +0800 Subject: [PATCH 14/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20OnStar=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TimerTest.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/UnitTest/Components/TimerTest.cs b/test/UnitTest/Components/TimerTest.cs index 7d14ed4b4..b9cf45596 100644 --- a/test/UnitTest/Components/TimerTest.cs +++ b/test/UnitTest/Components/TimerTest.cs @@ -86,4 +86,24 @@ public class TimerTest : BootstrapBlazorTestBase pb.Add(a => a.IsVibrate, false); }); } + + [Fact] + public async Task OnStart_Ok() + { + var timeout = false; + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.OnTimeout, () => + { + timeout = true; + return Task.CompletedTask; + }); + }); + var downs = cut.FindAll(".time-spinner-arrow.fa-angle-down"); + downs[2].Click(); + cut.Find(".time-panel-btn.confirm").Click(); + + await Task.Delay(2000); + Assert.True(timeout); + } } -- Gitee From cea02c7b4eece969d3e07098d2d5c8d2521d3795 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 9 Feb 2022 02:08:08 +0800 Subject: [PATCH 15/15] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20OnCancel=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TimerTest.cs | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/UnitTest/Components/TimerTest.cs b/test/UnitTest/Components/TimerTest.cs index b9cf45596..26ed13a9e 100644 --- a/test/UnitTest/Components/TimerTest.cs +++ b/test/UnitTest/Components/TimerTest.cs @@ -106,4 +106,42 @@ public class TimerTest : BootstrapBlazorTestBase await Task.Delay(2000); Assert.True(timeout); } + + [Fact] + public async Task OnCancel_Ok() + { + var cancelled = false; + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.OnCancel, () => + { + cancelled = true; + return Task.CompletedTask; + }); + }); + var downs = cut.FindAll(".time-spinner-arrow.fa-angle-down"); + downs[0].Click(); + cut.Find(".time-panel-btn.confirm").Click(); + await Task.Delay(1000); + var buttons = cut.FindAll(".timer-buttons button"); + // pause + Assert.True(buttons[1].ClassList.Contains("btn-warning")); + buttons[1].Click(); + + // resume + buttons = cut.FindAll(".timer-buttons button"); + Assert.True(buttons[1].ClassList.Contains("btn-success")); + buttons[1].Click(); + + // cancel + buttons = cut.FindAll(".timer-buttons button"); + buttons[0].Click(); + Assert.True(cancelled); + + // 代码覆盖率 Cancel 后再 Star + downs = cut.FindAll(".time-spinner-arrow.fa-angle-down"); + downs[0].Click(); + cut.Find(".time-panel-btn.confirm").Click(); + await Task.Delay(1000); + } } -- Gitee