forked from syncfusion/maui-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChartFeatureAxisUnitTest.cs
More file actions
2509 lines (1980 loc) · 84.7 KB
/
ChartFeatureAxisUnitTest.cs
File metadata and controls
2509 lines (1980 loc) · 84.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.ObjectModel;
using Syncfusion.Maui.Toolkit.Charts;
namespace Syncfusion.Maui.Toolkit.UnitTest.Charts
{
public class ChartFeatureAxisUnitTest : BaseUnitTest
{
#region ChartAxis partial methods
[Fact]
public void Test_RenderRectContains_Test()
{
var axis = new CategoryAxis()
{
IsVertical = true,
ArrangeRect = new Rect(0, 0, 100, 100),
InsidePadding = 10,
};
axis.AxisLabelsRenderer = new CartesianAxisLabelsRenderer(axis)
{
LabelLayout = new AxisLabelLayout(axis)
{
LabelsRect =
[
new Rect(0, 0, 50, 20),
new Rect(0, 20, 50, 20)
]
}
};
bool result = axis.RenderRectContains(50, 50);
Assert.True(result);
}
[Fact]
public void Test_UpdateLayout()
{
var axis = new NumericalAxis
{
Area = new CartesianChartArea(new SfCartesianChart())
};
axis.UpdateLayout();
Assert.True(axis.Area.NeedsRelayout);
axis.PolarArea = new PolarChartArea(new SfPolarChart());
axis.UpdateLayout();
Assert.True(axis.PolarArea.NeedsRelayout);
}
[Theory]
[InlineData(true, 10.0, true)]
[InlineData(true, double.NaN, false)]
[InlineData(true, double.MinValue, false)]
[InlineData(true, double.MaxValue, false)]
[InlineData(false, 10.0, false)]
public void Test_CanRenderNextToCrossingValue(bool renderNextToCrossingValue, double actualCrossingValue, bool expectedResult)
{
var renderClass = new NumericalAxis()
{
RenderNextToCrossingValue = renderNextToCrossingValue,
ActualCrossingValue = actualCrossingValue
};
var result = renderClass.CanRenderNextToCrossingValue();
Assert.Equal(expectedResult, result);
}
[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
public void Test_CanDrawMajorGridLines(bool showMajorGridLines, bool expectedResult)
{
var axis = new CategoryAxis()
{
ShowMajorGridLines = showMajorGridLines,
MajorGridLineStyle = new ChartLineStyle() { Stroke = Colors.Black, StrokeWidth = 1 },
};
var result = axis.CanDrawMajorGridLines();
Assert.Equal(expectedResult, result);
}
[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
public void Test_CanDrawMinorGridLines(bool showMinorGridLines, bool expectedResult)
{
var axis = new NumericalAxis()
{
ShowMinorGridLines = showMinorGridLines,
MinorGridLineStyle = new ChartLineStyle() { Stroke = Colors.Black, StrokeWidth = 1 },
};
var result = axis.CanDrawMinorGridLines();
Assert.Equal(expectedResult, result);
}
[Fact]
public void Test_AddRegisteredSeries()
{
var axis = new CategoryAxis();
var series = new LineSeries();
axis.AddRegisteredSeries(series);
Assert.Contains(series, axis.RegisteredSeries);
}
[Fact]
public void Test_ClearRegisteredSeries()
{
var axis = new CategoryAxis
{
Area = new CartesianChartArea(new SfCartesianChart())
};
axis.RegisteredSeries.Add(new LineSeries());
axis.AssociatedAxes.Add(new NumericalAxis());
axis.ClearRegisteredSeries();
Assert.Empty(axis.RegisteredSeries);
Assert.Empty(axis.AssociatedAxes);
}
[Fact]
public void Test_RemoveRegisteredSeries()
{
var axis = new CategoryAxis() { Name = "X-Axis" };
var yaxis = new NumericalAxis();
var series = new LineSeries();
axis.RegisteredSeries.Add(series);
series.ActualXAxis = axis;
series.ActualYAxis = yaxis;
axis.Area = new CartesianChartArea(new SfCartesianChart()) { PrimaryAxis = new CategoryAxis(), SecondaryAxis = new NumericalAxis() };
axis.RemoveRegisteredSeries(series);
Assert.DoesNotContain(series, axis.RegisteredSeries);
Assert.DoesNotContain(axis, series.ActualXAxis.AssociatedAxes);
Assert.DoesNotContain(yaxis, series.ActualYAxis.AssociatedAxes);
}
[Fact]
public void GetCrossingAxis_Test()
{
var axis = new CategoryAxis
{
Name = "CrossXAxis",
IsVertical = false
};
var expectedAxis = new NumericalAxis() { Name = "CrossAxis" };
var area = new CartesianChartArea(new SfCartesianChart());
area._axisLayout.HorizontalAxes.Add(new CategoryAxis());
area._axisLayout.VerticalAxes.Add(new NumericalAxis());
area._axisLayout.VerticalAxes.Add(expectedAxis);
axis.CrossAxisName = "CrossAxis";
var result = axis.GetCrossingAxis(area);
Assert.NotNull(result);
Assert.Equal(expectedAxis, result);
}
[Fact]
public void Dispose_Test()
{
var _chartAxis = new CategoryAxis()
{
Title = new ChartAxisTitle(),
AxisLineStyle = new ChartLineStyle(),
LabelStyle = new ChartAxisLabelStyle(),
MajorGridLineStyle = new ChartLineStyle(),
MajorTickStyle = new ChartAxisTickStyle(),
RegisteredSeries = [new LineSeries()],
};
bool isAxisLineStyleChanged = false;
bool isLabelStyleChanged = false;
_chartAxis.AxisLineStyle.PropertyChanged += (s, e) => isAxisLineStyleChanged = true;
_chartAxis.LabelStyle.PropertyChanged += (s, e) => isLabelStyleChanged = true;
_chartAxis.Dispose();
Assert.Null(_chartAxis.Title.Axis);
Assert.Empty(_chartAxis.RegisteredSeries);
Assert.Empty(_chartAxis.AssociatedAxes);
Assert.Null(_chartAxis.AxisLabelsRenderer);
Assert.Null(_chartAxis.AxisElementRenderer);
Assert.Null(_chartAxis.Area);
Assert.False(isAxisLineStyleChanged);
Assert.False(isLabelStyleChanged);
}
[Fact]
public void InitializeConstructor_Test()
{
var _chartAxis = new CategoryAxis();
InvokePrivateMethod(_chartAxis, "InitializeConstructor");
Assert.NotNull(_chartAxis.AxisLineStyle);
Assert.NotNull(_chartAxis.LabelStyle);
Assert.NotNull(_chartAxis.MajorGridLineStyle);
Assert.NotNull(_chartAxis.MajorTickStyle);
Assert.Equal(EdgeLabelsDrawingMode.Shift, _chartAxis.EdgeLabelsDrawingMode);
}
[Fact]
public void UpdateRenderers_Test()
{
var _chartAxis = new CategoryAxis();
InvokePrivateMethod(_chartAxis, "UpdateRenderers");
var initialRenderer = _chartAxis.AxisLabelsRenderer;
InvokePrivateMethod(_chartAxis, "UpdateRenderers");
Assert.NotNull(_chartAxis.AxisLabelsRenderer);
Assert.NotNull(_chartAxis.AxisElementRenderer);
Assert.Equal(initialRenderer, _chartAxis.AxisLabelsRenderer);
}
[Fact]
public void GetPlotSize_ShouldReturnCorrectSize_WhenNotVertical()
{
var _chartAxis = new CategoryAxis
{
IsVertical = false
};
var availableSize = new Size(200, 100);
var plotSize = InvokePrivateMethod(_chartAxis, "GetPlotSize", availableSize);
Assert.NotNull(plotSize);
Assert.Equal(new Size(200 - _chartAxis.GetActualPlotOffset(), 100), (Size)plotSize);
}
[Fact]
public void GetPlotSize_ShouldReturnCorrectSize_WhenVertical()
{
var _chartAxis = new CategoryAxis
{
IsVertical = true
};
var availableSize = new Size(200, 100);
var plotSize = InvokePrivateMethod(_chartAxis, "GetPlotSize", availableSize);
Assert.NotNull(plotSize);
Assert.Equal(new Size(200, 100 - _chartAxis.GetActualPlotOffset()), (Size)plotSize);
}
[Fact]
public void InitTitle_ShouldSetAxisPropertyOfTitle()
{
var _chartAxis = new CategoryAxis();
var title = new ChartAxisTitle();
InvokePrivateMethod(_chartAxis, "InitTitle", title);
Assert.Equal(_chartAxis, title.Axis);
}
[Fact]
public void ResetPlotBand_ShouldClearActualPlotBands()
{
var _chartAxis = new NumericalAxis();
var parameter = new object[] { 0, new NumericalPlotBand() };
InvokePrivateMethod(_chartAxis, "AddPlotBand", parameter);
Assert.NotEmpty(_chartAxis.ActualPlotBands);
InvokePrivateMethod(_chartAxis, "ResetPlotBand");
Assert.Empty(_chartAxis.ActualPlotBands);
}
[Fact]
public void RemovePlotBand_ShouldRemovePlotBandFromActualPlotBands()
{
var _chartAxis = new NumericalAxis();
var plotBand = new NumericalPlotBand();
var parameter = new object[] { 0, plotBand };
InvokePrivateMethod(_chartAxis, "AddPlotBand", parameter);
Assert.Single(_chartAxis.ActualPlotBands);
InvokePrivateMethod(_chartAxis, "RemovePlotBand", parameter);
Assert.Empty(_chartAxis.ActualPlotBands);
}
[Fact]
public void AddPlotBand_ShouldAddPlotBandToActualPlotBands()
{
var _chartAxis = new NumericalAxis();
var plotBand = new NumericalPlotBand();
var parameter = new object[] { 0, plotBand };
InvokePrivateMethod(_chartAxis, "AddPlotBand", parameter);
Assert.Single(_chartAxis.ActualPlotBands);
Assert.Equal(plotBand, _chartAxis.ActualPlotBands[0]);
}
#endregion
#region AxisLabelLayout methods
[Theory]
[InlineData(100, 20, ChartAxisLabelAlignment.Start, 90)]
[InlineData(100, 20, ChartAxisLabelAlignment.End, 110)]
[InlineData(100, 20, ChartAxisLabelAlignment.Center, 100)]
public void OnAxisLabelAlignment_Test(float position, float size, ChartAxisLabelAlignment alignment, float expected)
{
var labelStyle = new ChartAxisLabelStyle { LabelAlignment = alignment };
var result = AxisLabelLayout.OnAxisLabelAlignment(position, size, labelStyle);
Assert.Equal(expected, result);
}
[Theory]
[InlineData(ChartAxisLabelAlignment.Start, HorizontalAlignment.Left)]
[InlineData(ChartAxisLabelAlignment.Center, HorizontalAlignment.Center)]
[InlineData(ChartAxisLabelAlignment.End, HorizontalAlignment.Right)]
public void OnWrapAxisLabelAlignment_Test(ChartAxisLabelAlignment alignment, HorizontalAlignment expected)
{
var labelStyle = new ChartAxisLabelStyle { WrappedLabelAlignment = alignment };
var result = AxisLabelLayout.OnWrapAxisLabelAlignment(labelStyle);
Assert.Equal(expected, result);
}
[Fact]
public void CreateAxisLayout_Test_WhenIsVerticalIsFalse()
{
var chartAxis = new CategoryAxis { IsVertical = false };
var layout = AxisLabelLayout.CreateAxisLayout(chartAxis);
Assert.IsType<HorizontalLabelLayout>(layout);
}
[Fact]
public void CreateAxisLayout_Test_WhenIsVerticalIsTrue()
{
var chartAxis = new CategoryAxis { IsVertical = true };
var layout = AxisLabelLayout.CreateAxisLayout(chartAxis);
Assert.IsType<VerticalLabelLayout>(layout);
}
[Fact]
public void IsOpposed_Test_WhenAxisIsNull()
{
var layout = new AxisLabelLayout(new CategoryAxis()) { Axis = null! };
var result = layout.IsOpposed();
Assert.False(result);
}
[Theory]
[InlineData(AxisElementPosition.Outside, false)]
[InlineData(AxisElementPosition.Inside, true)]
public void IsOpposed_Test_WhenAxisPosition(AxisElementPosition position, bool expected)
{
var layout = new AxisLabelLayout(new CategoryAxis());
layout.Axis.LabelsPosition = position;
var result = layout.IsOpposed();
Assert.Equal(expected, result);
}
[Theory]
[InlineData(0, 0, 100, 100, 50, 50, 150, 150, true)]
[InlineData(0, 0, 50, 50, 51, 51, 100, 100, false)]
[InlineData(0, 0, 100, 100, 100, 0, 200, 100, true)]
[InlineData(0, 0, 200, 200, 50, 50, 150, 150, true)]
public void IntersectsWith_ShouldReturnExpectedResult(float left1, float top1, float right1, float bottom1,float left2, float top2, float right2, float bottom2,bool expectedResult)
{
var r1 = new RectF(left1, top1, right1, bottom1);
var r2 = new RectF(left2, top2, right2, bottom2);
var layout = new AxisLabelLayout(new CategoryAxis());
var result = AxisLabelLayout.IntersectsWith(r1, r2, 0, 1);
Assert.Equal(expectedResult, result);
}
[Theory]
[InlineData(100, 100, double.NaN, double.NaN, 0f, 0f)]
[InlineData(100, 100, 10.0, double.NaN, 10f, 0f)]
[InlineData(100, 100, double.NaN, 15.0, 0f, 15f)]
[InlineData(100, 100, 10.0, 15.0, 10f, 15f)]
public void CalculateActualPlotOffset_ShouldSetOffsetsCorrectly(float availableWidth, float availableHeight, double plotOffsetStart, double plotOffsetEnd, float expectedOffsetStart, float expectedOffsetEnd)
{
var layout = new AxisLabelLayout(new CategoryAxis())
{
Axis = new CategoryAxis()
{
PlotOffsetStart = plotOffsetStart,
PlotOffsetEnd = plotOffsetEnd
}
};
var availableSize = new SizeF(availableWidth, availableHeight);
layout.CalculateActualPlotOffset(availableSize);
Assert.Equal(expectedOffsetStart, layout.Axis.ActualPlotOffsetStart);
Assert.Equal(expectedOffsetEnd, layout.Axis.ActualPlotOffsetEnd);
}
[Theory]
[InlineData(0, 0, 5, 5, 1)]
public void InsertToRowOrColumn_ShouldInsertCorrectly(int rowOrColIndex, int itemIndex, float rectX, float rectY, int expectedRowCount)
{
var layout = new AxisLabelLayout(new CategoryAxis())
{
RectByRowsAndCols = []
};
var rect = new RectF(rectX, rectY, 10, 10);
var parameter = new object[] { rowOrColIndex, itemIndex, rect };
InvokePrivateMethod(layout, "InsertToRowOrColumn", parameter);
Assert.Equal(expectedRowCount, layout.RectByRowsAndCols.Count);
}
[Fact]
public void InsertToRowOrColumn_ShouldHandleIntersection()
{
var layout = new AxisLabelLayout(new CategoryAxis())
{
RectByRowsAndCols =
[
new Dictionary<int, RectF>
{
{ 0, new RectF(0, 0, 10, 10) }
}
]
};
var intersectingRect = new RectF(5, 5, 10, 10);
var parameter = new object[] { 0, 1, intersectingRect };
InvokePrivateMethod(layout, "InsertToRowOrColumn", parameter);
Assert.Equal(2, layout.RectByRowsAndCols.Count);
}
[Theory]
[InlineData(0, 100, 50, 100, 50)]
//[InlineData(90, 100, 50, 50, 100)] Expected width 50.000000000000007 Accuracy problem thus failed
//[InlineData(180, 100, 50, 100, 50)] Expected height 50.000000000000014 Accuracy problem thus failed
public void GetRotatedSize_ShouldReturnExpectedSize(double degrees, double originalWidth, double originalHeight, double expectedWidth, double expectedHeight)
{
var layout = new AxisLabelLayout(new CategoryAxis());
var originalSize = new Size(originalWidth, originalHeight);
var rotatedSize = InvokePrivateMethod(layout, "GetRotatedSize", [originalSize, degrees]);
Assert.NotNull(rotatedSize);
Assert.Equal(expectedWidth, ((Size)rotatedSize).Width);
Assert.Equal(expectedHeight, ((Size)rotatedSize).Height);
}
#region horizontal label layout
[Theory]
[InlineData(EdgeLabelsDrawingMode.Shift)]
[InlineData(EdgeLabelsDrawingMode.Center)]
[InlineData(EdgeLabelsDrawingMode.Hide)]
public void CalcBounds_ShouldAdjustBoundsForLabels(EdgeLabelsDrawingMode edgeLabelsDrawingMode)
{
var layout = new HorizontalLabelLayout(new CategoryAxis())
{
ComputedSizes = [new SizeF(50, 20), new SizeF(60, 20)],
Axis = new CategoryAxis()
{
VisibleLabels =
[
new ChartAxisLabel ( 0, new ChartAxisLabelStyle { Margin = new Thickness(5, 0, 5, 0) }),
new ChartAxisLabel ( 1, new ChartAxisLabelStyle { Margin = new Thickness(5, 0, 5, 0) })
],
EdgeLabelsDrawingMode = edgeLabelsDrawingMode
}
};
InvokePrivateMethod(layout, "CalcBounds", 100);
Assert.NotNull(layout.RectByRowsAndCols);
Assert.Equal(layout.RectByRowsAndCols[0].Values, layout.LabelsRect);
}
#endregion
#region VerticleLabel layout
[Theory]
[InlineData(0.5f, 100, 50, 200, 180, -150)]
[InlineData(0.5f, 100, 50, 200, 90, 275)]
[InlineData(0.5f, 100, 50, 200, 0, 50)]
public void CalculatePolarPosition_ShouldReturnExpectedResult(float coeff, float width, float height, float size, int angle, float expected)
{
var layout = new VerticalLabelLayout(new NumericalAxis());
var chart = new SfPolarChart() { PolarStartAngle = angle };
layout.Axis = new NumericalAxis
{
Parent = chart
};
var parameter = new object[] { coeff, width, height, size };
var result = InvokePrivateMethod(layout, "CalculatePolarPosition", parameter);
Assert.NotNull(result);
Assert.Equal(expected, (float)result);
}
#endregion
#endregion
#region CartesianAxisLayout methods
[Fact]
public void ClearActualAxis_Test()
{
var chartArea = new CartesianChartArea(new SfCartesianChart()) { RequiredAxisReset = true };
var series1 = new LineSeries() { ActualXAxis = new CategoryAxis(), ActualYAxis = new NumericalAxis() };
var series2 = new ColumnSeries() { ActualXAxis = new CategoryAxis(), ActualYAxis = new NumericalAxis() };
var visibleSeries = new ReadOnlyObservableCollection<ChartSeries>(
[series1, series2]
);
var layout = new CartesianAxisLayout(chartArea);
InvokePrivateMethod(layout, "ClearActualAxis", visibleSeries);
Assert.Null(series1.ActualXAxis);
Assert.Null(series1.ActualYAxis);
Assert.Null(series2.ActualXAxis);
Assert.Null(series2.ActualYAxis);
Assert.False(chartArea.RequiredAxisReset);
}
[Fact]
public void GetAxisByName_Test()
{
var area = new CartesianChartArea(new SfCartesianChart());
var layout = new CartesianAxisLayout(area);
var axes = new ObservableCollection<ChartAxis>
{
new CategoryAxis() { Name = "XAxis" },
new NumericalAxis() { Name = "YAxis" }
};
var parameter = new object[] { "XAxis", axes };
var result = InvokePrivateMethod(layout, "GetAxisByName", parameter);
Assert.NotNull(result);
Assert.Equal("XAxis", ((ChartAxis)result).Name);
}
[Fact]
public void UpdateActualAxis_Test()
{
var visibleSeries = new ReadOnlyObservableCollection<ChartSeries>(
[
new LineSeries { XAxisName = "XAxis1", YAxisName = "YAxis1" },
new LineSeries { XAxisName = "XAxis2", YAxisName = "YAxis2" }
]
);
var chart = new SfCartesianChart();
chart.XAxes.Add(new CategoryAxis() { Name = "XAxis1" });
chart.XAxes.Add(new CategoryAxis() { Name = "XAxis2" });
chart.YAxes.Add(new NumericalAxis() { Name = "YAxis1" });
chart.YAxes.Add(new NumericalAxis() { Name = "YAxis2" });
var area = new CartesianChartArea(chart)
{
PrimaryAxis = new CategoryAxis() { Name = "PrimaryAxis" },
SecondaryAxis = new NumericalAxis() { Name = "SecondaryAxis" },
};
var layout = new CartesianAxisLayout(area)
{
HorizontalAxes = chart.XAxes,
VerticalAxes =
[
new NumericalAxis() { Name = "YAxis1" },
new NumericalAxis() { Name = "YAxis2" },
]
};
InvokePrivateMethod(layout, "UpdateActualAxis", visibleSeries);
Assert.Equal("XAxis1", ((CartesianSeries)visibleSeries[0]).ActualXAxis?.Name);
Assert.Equal("YAxis1", ((CartesianSeries)visibleSeries[0]).ActualYAxis?.Name);
Assert.Equal("XAxis2", ((CartesianSeries)visibleSeries[1]).ActualXAxis?.Name);
Assert.Equal("YAxis2", ((CartesianSeries)visibleSeries[1]).ActualYAxis?.Name);
}
[Fact]
public void ValidateMinMaxWithAxisCrossingValue_Test()
{
var area = new CartesianChartArea(new SfCartesianChart());
var layout = new CartesianAxisLayout(area);
var currentAxis = new CategoryAxis() { ActualCrossingValue = 50 };
var associatedAxis = new NumericalAxis() { ActualRange = new DoubleRange(0, 100) };
var result = InvokePrivateMethod(layout, "ValidateMinMaxWithAxisCrossingValue", currentAxis);
Assert.NotNull(result);
Assert.Equal(associatedAxis.ValueToPoint(50), (double)result);
}
[Theory]
[InlineData(true, 2, 2)]
[InlineData(false, 2, 2)]
public void UpdateAxisTransposed_ShouldAssignAxesCorrectly(bool isTransposed, int expectedVerticalCount, int expectedHorizontalCount)
{
var chart = new SfCartesianChart();
var area = new CartesianChartArea(chart)
{
IsTransposed = isTransposed,
};
chart.XAxes.Add(new CategoryAxis() { Name = "XAxis1" });
chart.XAxes.Add(new CategoryAxis() { Name = "XAxis2" });
chart.YAxes.Add(new NumericalAxis() { Name = "YAxis1" });
chart.YAxes.Add(new NumericalAxis() { Name = "YAxis2" });
var layout = new CartesianAxisLayout(area);
InvokePrivateMethod(layout, "UpdateAxisTransposed");
Assert.Equal(expectedVerticalCount, layout.VerticalAxes.Count);
Assert.Equal(expectedHorizontalCount, layout.HorizontalAxes.Count);
Assert.All(layout.VerticalAxes, axis => Assert.True(axis.IsVertical));
Assert.All(layout.HorizontalAxes, axis => Assert.False(axis.IsVertical));
}
[Fact]
public void Test_InitMethod()
{
var area = new CartesianChartArea(new SfCartesianChart());
var layout = new CartesianAxisLayout(area);
SetPrivateField(layout, "_leftSizes", null);
SetPrivateField(layout, "_topSizes", null);
SetPrivateField(layout, "_rightSizes", null);
SetPrivateField(layout, "_bottomSizes", null);
Assert.Null(GetPrivateField(layout, "_leftSizes"));
Assert.Null(GetPrivateField(layout, "_topSizes"));
Assert.Null(GetPrivateField(layout, "_rightSizes"));
Assert.Null(GetPrivateField(layout, "_bottomSizes"));
InvokePrivateMethod(layout, "Init");
Assert.NotNull(GetPrivateField(layout, "_leftSizes"));
Assert.NotNull(GetPrivateField(layout, "_topSizes"));
Assert.NotNull(GetPrivateField(layout, "_rightSizes"));
Assert.NotNull(GetPrivateField(layout, "_bottomSizes"));
}
[Fact]
public void UpdateCrossingAxes_Test()
{
var area = new CartesianChartArea(new SfCartesianChart());
var layout = new CartesianAxisLayout(area);
var verticalAxis = new NumericalAxis
{
TickPosition = AxisElementPosition.Inside,
LabelsPosition = AxisElementPosition.Inside,
ActualRange = new DoubleRange(0, 100),
VisibleRange = new DoubleRange(0, 150),
ComputedDesiredSize = new Size(0, 100),
InsidePadding = 5,
RenderedRect = new RectF(0, 0, 20, 100),
ActualCrossingValue = 30
};
layout.VerticalAxes.Add(verticalAxis);
area.SecondaryAxis = verticalAxis;
var horizontalAxis = new CategoryAxis
{
TickPosition = AxisElementPosition.Outside,
LabelsPosition = AxisElementPosition.Outside,
ActualRange = new DoubleRange(20, 150),
VisibleRange = new DoubleRange(20, 150),
ComputedDesiredSize = new Size(100, 20),
RenderedRect = new RectF(0, 0, 100, 20),
InsidePadding = 10,
ActualCrossingValue = 30
};
layout.HorizontalAxes.Add(horizontalAxis);
area.PrimaryAxis = horizontalAxis;
var left = 10;
var top = 20;
SetPrivateField(layout, "_left", 10);
SetPrivateField(layout, "_top", 20);
InvokePrivateMethod(layout, "UpdateCrossingAxes");
Assert.Equal(new Rect(9, top, 0, 100), verticalAxis.ArrangeRect);
Assert.Equal(new Rect(left, top, 100, 20), horizontalAxis.ArrangeRect);
}
#endregion
#region PolarAxisLayout methods
[Fact]
public void UpdateArrangeRect_Test()
{
var chart = new SfPolarChart();
var area = new PolarChartArea(chart);
var layout = new PolarAxisLayoutView(area);
chart.PrimaryAxis = new CategoryAxis();
chart.SecondaryAxis = new NumericalAxis() { ComputedDesiredSize = new Size(50, 50) };
var availableSize = new Size(200, 100);
InvokePrivateMethod(layout, "UpdateArrangeRect", availableSize);
Assert.Equal(new Rect(0, 0, 200, 100), chart.PrimaryAxis.ArrangeRect);
Assert.Equal(new Rect(100, 0, 150, 50), chart.SecondaryAxis.ArrangeRect);
}
[Fact]
public void PolarClearActualAxis_Test()
{
var chartArea = new PolarChartArea(new SfPolarChart());
var series1 = new PolarLineSeries() { ActualXAxis = new CategoryAxis(), ActualYAxis = new NumericalAxis() };
var series2 = new PolarLineSeries() { ActualXAxis = new CategoryAxis(), ActualYAxis = new NumericalAxis() };
var visibleSeries = new ReadOnlyObservableCollection<ChartSeries>(
[series1, series2]
);
var layout = new PolarAxisLayoutView(chartArea);
InvokePrivateMethod(layout, "ClearActualAxis", visibleSeries);
Assert.Null(series1.ActualXAxis);
Assert.Null(series1.ActualYAxis);
Assert.Null(series2.ActualXAxis);
Assert.Null(series2.ActualYAxis);
}
[Fact]
public void PolarUpdateActualAxis_Test()
{
var visibleSeries = new ReadOnlyObservableCollection<ChartSeries>(
[
new PolarLineSeries (),
new PolarLineSeries ()
]
);
var chart = new SfPolarChart
{
PrimaryAxis = (new CategoryAxis() { Name = "XAxis1" }),
SecondaryAxis = (new NumericalAxis() { Name = "YAxis1" })
};
var area = new PolarChartArea(chart);
var layout = new PolarAxisLayoutView(area);
InvokePrivateMethod(layout, "UpdateActualAxis", visibleSeries);
Assert.Equal("XAxis1", ((PolarLineSeries)visibleSeries[0]).ActualXAxis?.Name);
Assert.Equal("YAxis1", ((PolarLineSeries)visibleSeries[0]).ActualYAxis?.Name);
}
#endregion
#region PolarGridlinestyle method
[Fact]
public void Measure_ShouldReturnAvailableSize()
{
var area = new PolarChartArea(new SfPolarChart());
var layout = new PolarGridLineLayout(area);
var availableSize = new Size(150, 100);
var result = layout.Measure(availableSize);
var desiredSize = GetPrivateField(layout, "_desiredSize");
Assert.NotNull(desiredSize);
Assert.Equal(availableSize, (Size)desiredSize);
}
[Fact]
public void GetDefaultGridLineStyle_ShouldReturnDefaultStyle()
{
var area = new PolarChartArea(new SfPolarChart());
var layout = new PolarGridLineLayout(area);
var result = InvokePrivateMethod(layout, "GetDefaultGridLineStyle") as ChartLineStyle;
Assert.NotNull(result);
Assert.Equal(1, result.StrokeWidth);
}
#endregion
#region Renderer methods
[Fact]
public void Test_GetLeftAndSetLeft()
{
var element = new CartesianAxisElementRenderer(new CategoryAxis());
var result = element.GetLeft();
Assert.Equal(0, result);
var _left = 10;
element.SetLeft(_left);
Assert.Equal(_left, element.GetLeft());
}
[Fact]
public void Test_GetTopAndSetTop()
{
var element = new CartesianAxisElementRenderer(new CategoryAxis());
var result = element.GetTop();
Assert.Equal(0, result);
var _left = 10;
element.SetTop(_left);
Assert.Equal(_left, element.GetTop());
}
[Fact]
public void Test_GetDesiredSize()
{
var element = new CartesianAxisElementRenderer(new NumericalAxis());
var size = new SizeF(50, 50);
SetPrivateField(element, "_desiredSize", size);
var result = element.GetDesiredSize();
Assert.Equal(size, (SizeF)result);
}
[Theory]
[InlineData(true, 100, 50, 5, 2, 7, 100)]
[InlineData(false, 100, 50, 5, 2, 50, 7)]
public void Measure_ShouldReturnCorrectSize(
bool isVertical, double availableWidth, double availableHeight,
double majorTickSize, double strokeWidth, double expectedWidth, double expectedHeight)
{
var axis = new NumericalAxis()
{
IsVertical = isVertical,
};
var element = new CartesianAxisElementRenderer(axis);
var availableSize = new Size(availableWidth, availableHeight);
axis.MajorTickStyle = new ChartAxisTickStyle { TickSize = majorTickSize };
axis.AxisLineStyle = new ChartLineStyle { StrokeWidth = strokeWidth };
var size = element.Measure(availableSize);
Assert.Equal(expectedWidth, size.Width);
Assert.Equal(expectedHeight, size.Height);
}
#region CartesianAxis label renderer
[Fact]
public void TestAxisLabel_GetLeftAndSetLeft()
{
var element = new CartesianAxisLabelsRenderer(new CategoryAxis());
var result = element.GetLeft();
Assert.Equal(0, result);
var _left = 10;
element.SetLeft(_left);
Assert.Equal(_left, element.GetLeft());
}
[Fact]
public void TestAxisLabel_GetTopAndSetTop()
{
var element = new CartesianAxisLabelsRenderer(new CategoryAxis());
var result = element.GetTop();
Assert.Equal(0, result);
var _left = 10;
element.SetTop(_left);
Assert.Equal(_left, element.GetTop());
}
[Fact]
public void TestAxisLabel_GetDesiredSize()
{
var element = new CartesianAxisLabelsRenderer(new NumericalAxis());
var size = new SizeF(50, 50);
SetPrivateField(element, "_desiredSize", size);
var result = element.GetDesiredSize();
Assert.Equal(size, (SizeF)result);
}
#endregion
[Theory]
[InlineData(true)]
[InlineData(false)]
public void UpdateRendererVisible_Test(bool visible)
{
var axis = new CategoryAxis();
ILayoutCalculator layout1 = new CartesianAxisElementRenderer(axis);
ILayoutCalculator layout2 = new CartesianAxisElementRenderer(axis);
layout1.IsVisible = !visible;
layout2.IsVisible = !visible;
var layoutCalculators = new List<ILayoutCalculator> { layout1, layout2 };
var element = new CartesianAxisRenderer(axis) { LayoutCalculators = layoutCalculators };
element.UpdateRendererVisible(visible);
Assert.Equal(visible, (layout1).IsVisible);
Assert.Equal(visible, (layout1).IsVisible);
}
[Theory]
[InlineData(true, 0f, 10f, 0f, 0f)]
[InlineData(false, 0f, 0f, 25f, 0f)]
public void Layout_ShouldPositionElementsCorrectlyForVerticalAxis(bool isVertical, float layoutOneLeft, float layoutTwoLeft, float layoutOneTop, float layoutTwoTop)
{
var axis = new CategoryAxis()
{
IsVertical = isVertical,
LabelsPosition = AxisElementPosition.Outside
};
var layout1 = new CartesianAxisElementRenderer(axis);
((ILayoutCalculator)layout1).IsVisible = true;
layout1.SetLeft(0);
layout1.SetTop(0);
SetPrivateField(layout1, "_desiredSize", new SizeF(10, 20));
var layout2 = new CartesianAxisElementRenderer(axis);
((ILayoutCalculator)layout2).IsVisible = true;
layout2.SetLeft(0);
layout2.SetTop(0);
SetPrivateField(layout2, "_desiredSize", new SizeF(15, 25));
var element = new CartesianAxisRenderer(axis)
{
LayoutCalculators = [layout1, layout2],
};
element.Layout(new SizeF(100, 200));
Assert.Equal(layoutOneLeft, layout1.GetLeft());
Assert.Equal(layoutTwoLeft, layout2.GetLeft());
Assert.Equal(layoutOneTop, layout1.GetTop());
Assert.Equal(layoutTwoTop, layout2.GetTop());
}
#endregion
#region CategoryAxis methods
[Fact]
public void CategoryAxis_CalculateActualInterval_Test()
{