Skip to content

Commit

Permalink
bugfix: added tests for contourLegend class
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikaLos committed Dec 5, 2024
1 parent 6ed39cc commit a08e315
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
29 changes: 6 additions & 23 deletions GsaGH/Helpers/GH/Legend/ContourLegend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -109,15 +101,6 @@ private void InitializeDimensions(int viewportEdge) {
_leftBitmapEdge = viewportEdge - CalculateScaledOffset(DefaultBitmapWidth);
}

/// <summary>
/// Ensures dimensions are initialized for individual drawing calls.
/// </summary>
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);
}
Expand Down
39 changes: 34 additions & 5 deletions GsaGHTests/Helpers/GH/Legend/ContourLegendTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,7 +15,7 @@ namespace GsaGHTests.Helpers {
[Collection("GrasshopperFixture collection")]
public class ContourLegendTests {
private readonly Mock<IContourLegendConfiguration> _mockConfiguration;
private readonly ContourLegend legend;
private ContourLegend legend;
private readonly Bitmap _mockBitmap;

public ContourLegendTests() {
Expand All @@ -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<ArgumentOutOfRangeException>(() => legend.DrawGradientLegend(5, 1, color));
}

[Fact]
public void DrawGradientWillThrowErrorWhenEndValueIsBiggerThanBitmapHeight() {
Color color = Color.Red;
Assert.Throws<ArgumentOutOfRangeException>(() => legend.DrawGradientLegend(5, 200, color));
}

[Fact]
public void DrawGradientWillThrowErrorWhenStartValueLessThan0() {
Color color = Color.Green;
Assert.Throws<ArgumentOutOfRangeException>(() => legend.DrawGradientLegend(-1, 1, color));
}

[Fact]
public void ShouldNotDrawILegendIsNotDisplayable() {
legend = new ContourLegend(new ContourLegendConfiguration());
var previewArgsSpy = new Mock<IGH_PreviewArgs>();

legend.DrawLegendRectangle(previewArgsSpy.Object, string.Empty, string.Empty,
new List<(int startY, int endY, Color gradientColor)>());
Assert.True(legend.IsInvalidConfiguration);
}
}
}

0 comments on commit a08e315

Please sign in to comment.