Skip to content

Commit

Permalink
bugfix: added unit tests for text wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikaLos committed Dec 4, 2024
1 parent 7166b44 commit 01f16f6
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 4 deletions.
8 changes: 4 additions & 4 deletions GsaGH/Helpers/GH/TextWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static string WrapText(string text, int maxWidth, int fontSize) {

if (testLineWidth > maxWidth) {
lines.Add(currentLine);
textWidthCache[(text, font.Size)] = testLineWidth;
currentLine = word;
} else {
currentLine = testLine;
Expand Down Expand Up @@ -63,16 +64,15 @@ private static float GetCachedTextWidth(string text, Font font, int maxWidth) {

var graphics = System.Drawing.Graphics.FromImage(new Bitmap(maxWidth, 1)); //we care only about width
float width = graphics.MeasureString(text, font).Width;
textWidthCache[(text, font.Size)] = width;

return width;
}

public static string GetFontName() {
public static string GetFontName(RhinoDoc testPurpose = null) {
string fontName = "Arial";
try {
RhinoDoc activeRhino = RhinoDoc.ActiveDoc;
fontName = activeRhino.DimStyles.Current.Font.LogfontName;
RhinoDoc activeRhino = testPurpose ?? RhinoDoc.ActiveDoc;
fontName = activeRhino.DimStyles.Current.Font.FamilyName;
} catch { //will be catched only by tests
return fontName;
}
Expand Down
108 changes: 108 additions & 0 deletions GsaGHTests/Helpers/GH/Legend/ContourLegendMenagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using Grasshopper.Kernel;

using GsaGH.Components;
using GsaGH.Helpers;
using GsaGH.Helpers.GH;

using Moq;

using Xunit;

namespace GsaGHTests.Helpers {
[Collection("GrasshopperFixture collection")]
public class ContourLegendMenagerTests {
private static Mock<IContourLegend> _mockLegend;
private static Mock<ContourLegendConfiguration> _mockConfiguration;
private static Mock<LegendMenuBuilder> _mockMenuBuilder;
private static ContourLegendManager _manager;
private static Mock<IGH_PreviewArgs> _mockArgs;

public ContourLegendMenagerTests() {
_mockLegend = new Mock<IContourLegend>();
_mockConfiguration = new Mock<ContourLegendConfiguration>();
_mockMenuBuilder = new Mock<LegendMenuBuilder>();
_manager = new ContourLegendManager(_mockConfiguration.Object, _mockLegend.Object, _mockMenuBuilder.Object);

_mockArgs = new Mock<IGH_PreviewArgs>();
}

[Fact]
public void DrawLegend_CallsDrawLegendRectangle() {
string title = "Test Title";
string bottomText = "Test Bottom Text";
var gradients = new List<(int, int, Color)> {
(0, 10, Color.Red),
(10, 20, Color.Green),
};

_manager.DrawLegend(_mockArgs.Object, title, bottomText, gradients);

_mockLegend.Verify(l => l.DrawLegendRectangle(_mockArgs.Object, title, bottomText, gradients), Times.Once);
}

[Fact]
public void CreateMenu_CallsCreateLegendToolStripMenuItem() {
var mockUpdateUI = new Mock<Action>();

ToolStripMenuItem menuItem
= _manager.CreateMenu(new Contour1dResults(), mockUpdateUI.Object); // don't care of the component type

Assert.NotNull(menuItem);
}

[Fact]
public void UpdateScale_CallsSetLegendScale() {
double scale = 2.5;
_manager.UpdateScale(scale);
Assert.Equal(scale, _mockConfiguration.Object.Scale);
}

[Fact]
public void ToggleVisibility_CallsToggleLegendVisibility() {
bool result = _manager.ToggleVisibility();
Assert.False(_mockConfiguration.Object.IsVisible);
}

[Fact]
public void GetBitmapSize_ReturnsCorrectSize() {
double scale = 2;
_manager.UpdateScale(scale);
int defaultWidth = _mockConfiguration.Object.DefaultWidth;
int defaultHeight = _mockConfiguration.Object.DefaultHeight;
int expectedWidth = (int)(defaultWidth * scale);
int expectedHeight = (int)(defaultHeight * scale);
// Set up the mock configuration to return specific values for the Bitmap size
(int width, int height) = _manager.GetBitmapSize();

Assert.Equal(expectedWidth, width);
Assert.Equal(expectedHeight, height);
}

[Fact]
public void SetTextValues_SetsTextValuesInConfiguration() {
var textValues = new List<string> {
"Value 1",
"Value 2",
"Value 3",
};
_manager.SetTextValues(textValues);
Assert.Equal(textValues, _mockConfiguration.Object.Values);
}

[Fact]
public void SetPositionYValues_SetsPositionYValuesInConfiguration() {
var yValues = new List<int> {
10,
20,
30,
};
_manager.SetPositionYValues(yValues);
Assert.Equal(yValues, _mockConfiguration.Object.ValuePositionsY);
}
}
}
72 changes: 72 additions & 0 deletions GsaGHTests/Helpers/GH/TextWrapperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections.Generic;
using System.Reflection;

using GsaGH.Helpers.GH;

using Rhino;
using Rhino.DocObjects;

using Xunit;

namespace GsaGHTests.Helpers {
[Collection("GrasshopperFixture collection")]
public class TextWrapperTests {

[Fact]
public void WrapText_ShouldWrapTextCorrectly() {
string inputText = "This is a test sentence that should be wrapped correctly.";
int maxWidth = 100;
int fontSize = 10;

string result = TextWrapper.WrapText(inputText, maxWidth, fontSize);

Assert.NotNull(result);
Assert.Contains("\n", result);
Assert.Equal("This is a test\nsentence that\nshould be\nwrapped\ncorrectly.", result);
}

[Fact]
public void GetCachedTextWidth_ShouldCacheTextWidthCorrectly() {
string inputText = "This is a test sentence that should be wrapped correctly.";
int fontSize = 10;
int maxWidth = 100;

string text1 = TextWrapper.WrapText(inputText, maxWidth, fontSize);
string text2 = TextWrapper.WrapText(inputText, maxWidth, fontSize);

Assert.Single(GetPrivateFieldValue());
}

private Dictionary<(string, float), float> GetPrivateFieldValue() {
FieldInfo fieldInfo
= typeof(TextWrapper).GetField("textWidthCache", BindingFlags.NonPublic | BindingFlags.Static);
return (Dictionary<(string, float), float>)fieldInfo.GetValue(null);
}

// Testowanie metody GetFontName
[Fact]
public void GetFontName_ShouldReturnFontNameFromRhino() {
RhinoDoc rhinoDoc = CreateRhinoDocument("test");
string fontName = TextWrapper.GetFontName(rhinoDoc);
CloseRhinoDoc(rhinoDoc);
Assert.Equal("Arial", fontName);
}

[Fact]
public void GetFontName_ShouldReturnDefaultWhenRhinoDocIsNull() {
string fontName = TextWrapper.GetFontName();
Assert.Equal("Arial", fontName);
}

private static RhinoDoc CreateRhinoDocument(string modelTemplateFileName) {
var rhinooDoc = RhinoDoc.Create(modelTemplateFileName);
rhinooDoc.DimStyles.Current.Font = new Font("Test");

return rhinooDoc;
}

private static void CloseRhinoDoc(RhinoDoc rhinoDoc) {
rhinoDoc.Dispose();
}
}
}

0 comments on commit 01f16f6

Please sign in to comment.