-
Notifications
You must be signed in to change notification settings - Fork 976
Expand file tree
/
Copy pathPlotModelTests.cs
More file actions
220 lines (190 loc) · 8.41 KB
/
Copy pathPlotModelTests.cs
File metadata and controls
220 lines (190 loc) · 8.41 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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PlotModelTests.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Tests the <see cref="PlotModel.Render" /> method.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.Tests
{
using System;
using System.Diagnostics.CodeAnalysis;
using ExampleLibrary;
using NSubstitute;
using NUnit.Framework;
using OxyPlot.Axes;
using OxyPlot.Series;
// ReSharper disable InconsistentNaming
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
[TestFixture]
public class PlotModelTests
{
[Test]
[TestCaseSource(typeof(Examples), nameof(Examples.GetListForAutomatedTest))]
public void Update_AllExamples_ThrowsNoExceptions(ExampleInfo example)
{
((IPlotModel)example.PlotModel)?.Update(true);
var first = 1; // skip the 'none', since we do that above for clarity
var all = (int)(ExampleFlags.Transpose | ExampleFlags.Reverse);
for (int flags = first; flags < all; flags++)
{
((IPlotModel)example.GetModel((ExampleFlags)flags))?.Update(true);
}
}
[Test]
public void B11_Backgrounds()
{
var plot = new PlotModel { Title = "Backgrounds" };
plot.Axes.Add(new LinearAxis { Position = AxisPosition.Bottom, Title = "X-axis" });
var yaxis1 = new LinearAxis { Position = AxisPosition.Left, Title = "Y1", Key = "Y1", StartPosition = 0, EndPosition = 0.5 };
var yaxis2 = new LinearAxis { Position = AxisPosition.Left, Title = "Y2", Key = "Y2", StartPosition = 0.5, EndPosition = 1 };
plot.Axes.Add(yaxis1);
plot.Axes.Add(yaxis2);
Action<LineSeries> addExamplePoints = ls =>
{
ls.Points.Add(new DataPoint(3, 13));
ls.Points.Add(new DataPoint(10, 47));
ls.Points.Add(new DataPoint(30, 23));
ls.Points.Add(new DataPoint(40, 65));
ls.Points.Add(new DataPoint(80, 10));
};
var ls1 = new LineSeries { Background = OxyColors.LightSeaGreen, YAxisKey = "Y1" };
addExamplePoints(ls1);
plot.Series.Add(ls1);
var ls2 = new LineSeries { Background = OxyColors.LightSkyBlue, YAxisKey = "Y2" };
addExamplePoints(ls2);
plot.Series.Add(ls2);
// OxyAssert.AreEqual(plot, "B11");
}
[Test]
public void PlotControl_CollectedPlotControl_ReferenceShouldNotBeAlive()
{
var pm = new PlotModel();
var plot = Substitute.For<IPlotView>();
((IPlotModel)pm).AttachPlotView(plot);
Assert.IsNotNull(pm.PlotView);
// ReSharper disable once RedundantAssignment
plot = null;
GC.Collect();
// Verify that the reference is lost
// In debug builds a reference may be kept around for the debugger.
#if !DEBUG
Assert.IsNull(pm.PlotView);
#endif
}
/// <summary>
/// Tests rendering on a collapsed output surface.
/// </summary>
[Test]
public void Collapsed()
{
var model = new PlotModel();
var rc = Substitute.For<IRenderContext>();
((IPlotModel)model).Render(rc, new OxyRect(0, 0, 0, 0));
}
/// <summary>
/// Tests rendering on a small output surface.
/// </summary>
[Test]
public void NoPadding()
{
var model = new PlotModel { Padding = new OxyThickness(0) };
var rc = Substitute.For<IRenderContext>();
((IPlotModel)model).Render(rc, new OxyRect(0, 0, double.Epsilon, double.Epsilon));
}
/// <summary>
/// The same axis cannot be added more than once.
/// </summary>
[Test]
public void AddAxisTwice()
{
var model = new PlotModel();
var axis = new LinearAxis();
model.Axes.Add(axis);
Assert.Throws<InvalidOperationException>(() => model.Axes.Add(axis));
}
/// <summary>
/// The same axis cannot be added to different PlotModels.
/// </summary>
[Test]
public void AddAxisToDifferentModels()
{
var model1 = new PlotModel();
var model2 = new PlotModel();
var axis = new LinearAxis();
model1.Axes.Add(axis);
Assert.Throws<InvalidOperationException>(() => model2.Axes.Add(axis));
}
/// <summary>
/// An exception should be thrown when the axis key is invalid.
/// </summary>
[Test]
public void InvalidAxisKey()
{
var model = new PlotModel();
model.Axes.Add(new LinearAxis());
model.Series.Add(new LineSeries { XAxisKey = "invalidKey" });
((IPlotModel)model).Update(true);
Assert.IsNotNull(model.GetLastPlotException() as InvalidOperationException);
}
/// <summary>
/// When PlotMargins is not set, the ActualPlotMargins should use the desired size of the axes.
/// </summary>
[Test]
public void AutoPlotMargins()
{
var plot = new PlotModel { Title = "Auto PlotMargins" };
var verticalAxis = new LinearAxis { Position = AxisPosition.Left };
var horizontalAxis = new LinearAxis { Position = AxisPosition.Bottom };
plot.Axes.Add(verticalAxis);
plot.Axes.Add(horizontalAxis);
plot.UpdateAndRenderToNull(800, 600);
Assert.That(plot.ActualPlotMargins.Left, Is.EqualTo(26).Within(1), "left");
Assert.That(plot.ActualPlotMargins.Top, Is.EqualTo(5).Within(1), "top");
Assert.That(plot.ActualPlotMargins.Right, Is.EqualTo(7.5).Within(1), "right");
Assert.That(plot.ActualPlotMargins.Bottom, Is.EqualTo(21).Within(1), "bottom");
}
/// <summary>
/// When PlotMargins is not set, the ActualPlotMargins should have the same value.
/// </summary>
[Test]
public void FixedPlotMargins()
{
var plot = new PlotModel { PlotMargins = new OxyThickness(23, 19, 17, 31) };
plot.UpdateAndRenderToNull(800, 600);
Assert.That(plot.ActualPlotMargins.Left, Is.EqualTo(plot.PlotMargins.Left), "left");
Assert.That(plot.ActualPlotMargins.Top, Is.EqualTo(plot.PlotMargins.Top), "top");
Assert.That(plot.ActualPlotMargins.Right, Is.EqualTo(plot.PlotMargins.Right), "right");
Assert.That(plot.ActualPlotMargins.Bottom, Is.EqualTo(plot.PlotMargins.Bottom), "bottom");
}
/// <summary>
/// When GetAxis is called with an unknown key, an InvalidOperationException should be thrown.
/// </summary>
[Test]
public void GetAxis()
{
var plot = new PlotModel { Title = "Get Axis Or Default" };
var verticalAxis = new LinearAxis { Key = "YAxis", Position = AxisPosition.Left };
var horizontalAxis = new LinearAxis { Key = "XAxis", Position = AxisPosition.Bottom };
plot.Axes.Add(verticalAxis);
plot.Axes.Add(horizontalAxis);
Assert.That(() => plot.GetAxis("ThisIsAnInvalidKey"), Throws.InvalidOperationException);
}
/// <summary>
/// When GetAxisOrDefault is called with an unknown key, the provided default value should
/// be returned.
/// </summary>
[Test]
public void GetAxisOrDefault()
{
var plot = new PlotModel { Title = "Get Axis Or Default" };
var verticalAxis = new LinearAxis { Key = "YAxis", Position = AxisPosition.Left };
var horizontalAxis = new LinearAxis { Key = "XAxis", Position = AxisPosition.Bottom };
plot.Axes.Add(verticalAxis);
plot.Axes.Add(horizontalAxis);
var defaultValue = new LinearAxis { Key = "DefaultAxis" };
Assert.That(plot.GetAxisOrDefault("ThisIsAnInvalidKey", defaultValue), Is.EqualTo(defaultValue));
}
}
}