diff --git a/GsaGH/Helpers/GH/Legend/ContourLegend.cs b/GsaGH/Helpers/GH/Legend/ContourLegend.cs
index 90587268e..b12122327 100644
--- a/GsaGH/Helpers/GH/Legend/ContourLegend.cs
+++ b/GsaGH/Helpers/GH/Legend/ContourLegend.cs
@@ -11,14 +11,12 @@
namespace GsaGH.Helpers.GH {
internal class ContourLegend : IContourLegend {
-
private const int DefaultTextHeight = 12;
private const int DefaultBitmapWidth = 110;
private readonly IContourLegendConfiguration _configuration;
- private bool _isDrawLegendCalled = false;
private int _leftBitmapEdge;
-
private int _textHeight = DefaultTextHeight;
+ internal bool IsInvalidConfiguration = false; //only for tests
public ContourLegend(IContourLegendConfiguration configuration) {
_configuration = configuration;
@@ -31,10 +29,10 @@ public void DrawLegendRectangle(
IGH_PreviewArgs args, string title, string bottomText,
List<(int startY, int endY, Color gradientColor)> gradients) {
if (!_configuration.IsLegendDisplayable()) {
+ IsInvalidConfiguration = true;
return;
}
- _isDrawLegendCalled = true;
InitializeDimensions(args.Viewport.Bounds.Right);
// Step 1: Apply all gradients to the bitmap
@@ -47,8 +45,6 @@ public void DrawLegendRectangle(
DrawTitle(args, title);
DrawValues(args);
DrawBottomText(args, bottomText);
-
- _isDrawLegendCalled = false;
}
public void DrawGradientLegend(int startY, int endY, Color gradientColor) {
@@ -63,24 +59,21 @@ public void DrawGradientLegend(int startY, int endY, Color gradientColor) {
}
}
- public void DrawBitmap(IGH_PreviewArgs args) {
- EnsureDimensionsInitialized(args);
+ private void DrawBitmap(IGH_PreviewArgs args) {
const int TopOffset = 20;
int topPosition = CalculateScaledOffset(TopOffset);
args.Display.DrawBitmap(new DisplayBitmap(_configuration.Bitmap), _leftBitmapEdge, topPosition);
}
- public void DrawTitle(IGH_PreviewArgs args, string title) {
- EnsureDimensionsInitialized(args);
+ private void DrawTitle(IGH_PreviewArgs args, string title) {
const int TopOffset = 7;
int topPosition = CalculateScaledOffset(TopOffset);
args.Display.Draw2dText(title, Color.Black, new Point2d(_leftBitmapEdge, topPosition), false, _textHeight);
}
- public void DrawValues(IGH_PreviewArgs args) {
- EnsureDimensionsInitialized(args);
+ private void DrawValues(IGH_PreviewArgs args) {
const int LeftOffset = 25;
int leftEdge = _leftBitmapEdge + CalculateScaledOffset(LeftOffset);
var zippedLists = _configuration.Values.Zip(_configuration.ValuePositionsY, (value, positionY) => new {
@@ -92,8 +85,7 @@ public void DrawValues(IGH_PreviewArgs args) {
}
}
- public void DrawBottomText(IGH_PreviewArgs args, string bottomText) {
- EnsureDimensionsInitialized(args);
+ private void DrawBottomText(IGH_PreviewArgs args, string bottomText) {
const int BottomOffset = 145;
const int ExtraOffset = 20;
int bitmapWidth = CalculateScaledOffset(DefaultBitmapWidth);
@@ -109,15 +101,6 @@ private void InitializeDimensions(int viewportEdge) {
_leftBitmapEdge = viewportEdge - CalculateScaledOffset(DefaultBitmapWidth);
}
- ///
- /// Ensures dimensions are initialized for individual drawing calls.
- ///
- private void EnsureDimensionsInitialized(IGH_PreviewArgs args) {
- if (!_isDrawLegendCalled) {
- InitializeDimensions(args.Viewport.Bounds.Right);
- }
- }
-
private string WrapText(string bottomText, int width) {
return TextWrapper.WrapText(bottomText, width, _textHeight);
}
diff --git a/GsaGHTests/Helpers/GH/Legend/ContourLegendTests.cs b/GsaGHTests/Helpers/GH/Legend/ContourLegendTests.cs
index 6de929341..e185aac48 100644
--- a/GsaGHTests/Helpers/GH/Legend/ContourLegendTests.cs
+++ b/GsaGHTests/Helpers/GH/Legend/ContourLegendTests.cs
@@ -1,4 +1,8 @@
-using System.Drawing;
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+
+using Grasshopper.Kernel;
using GsaGH.Helpers;
using GsaGH.Helpers.GH;
@@ -11,7 +15,7 @@ namespace GsaGHTests.Helpers {
[Collection("GrasshopperFixture collection")]
public class ContourLegendTests {
private readonly Mock _mockConfiguration;
- private readonly ContourLegend legend;
+ private ContourLegend legend;
private readonly Bitmap _mockBitmap;
public ContourLegendTests() {
@@ -38,8 +42,33 @@ public void DrawGradientLegend_UpdatesBitmapCorrectly() {
}
}
}
- //other methods need to be tested manually or by integration tests.
- //we are not able to mock or inherit DisplayPipeline(which is needed to draw stuff)
- //so we aren't able to check if drawings are correct
+
+ [Fact]
+ public void DrawGradientWillThrowErrorWhenStartValuesIsBiggerThanEndValue() {
+ Color color = Color.Red;
+ Assert.Throws(() => legend.DrawGradientLegend(5, 1, color));
+ }
+
+ [Fact]
+ public void DrawGradientWillThrowErrorWhenEndValueIsBiggerThanBitmapHeight() {
+ Color color = Color.Red;
+ Assert.Throws(() => legend.DrawGradientLegend(5, 200, color));
+ }
+
+ [Fact]
+ public void DrawGradientWillThrowErrorWhenStartValueLessThan0() {
+ Color color = Color.Green;
+ Assert.Throws(() => legend.DrawGradientLegend(-1, 1, color));
+ }
+
+ [Fact]
+ public void ShouldNotDrawILegendIsNotDisplayable() {
+ legend = new ContourLegend(new ContourLegendConfiguration());
+ var previewArgsSpy = new Mock();
+
+ legend.DrawLegendRectangle(previewArgsSpy.Object, string.Empty, string.Empty,
+ new List<(int startY, int endY, Color gradientColor)>());
+ Assert.True(legend.IsInvalidConfiguration);
+ }
}
}