Skip to content

Commit

Permalink
[tests] Rework StringAttributesTests.SimpleAttributesTest. Fixes #18656
Browse files Browse the repository at this point in the history
…. (#21336)

* CTLine crashes with certain NSAttributedStrings, so don't do that. This fixes #18656.
* Implement this test for macOS.
* Misc other fixes.

Fixes #18656.
  • Loading branch information
rolfbjarne committed Oct 2, 2024
1 parent 47926eb commit 85550d7
Showing 1 changed file with 47 additions and 13 deletions.
60 changes: 47 additions & 13 deletions tests/monotouch-test/CoreText/StringAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ namespace MonoTouchFixtures.CoreText {
[TestFixture]
[Preserve (AllMembers = true)]
public class StringAttributesTests {
#if !MONOMAC // No UIGraphics on mac
[Test]
public void SimpleValuesSet ()
public void NoCTLine ()
{
if (TestRuntime.CheckXcodeVersion (15, 0))
Assert.Ignore ("Test timeouts on Xcode 15 beta 4: https://github.com/xamarin/xamarin-macios/issues/18656");

var sa = new CTStringAttributes ();
sa.ForegroundColor = UIColor.Blue.CGColor;
sa.Font = new CTFont ("Georgia-BoldItalic", 24);
Expand All @@ -50,14 +46,51 @@ public void SimpleValuesSet ()
sa.TrackingAdjustment = 1.0f;

AdaptiveImageProvider? provider = null;
#if NET
if (TestRuntime.CheckXcodeVersion (16, 0)) {
sa.AdaptiveImageProvider = provider = new AdaptiveImageProvider ();
Assert.AreSame (provider, sa.AdaptiveImageProvider, "AdaptiveImageProvider");
}
}

[Test]
public void SimpleValuesSet ()
{
var sa = new CTStringAttributes ();
sa.ForegroundColor = UIColor.Blue.CGColor;
sa.Font = new CTFont ("Georgia-BoldItalic", 24);
sa.UnderlineStyle = CTUnderlineStyle.Double; // It does not seem to do anything
sa.UnderlineColor = UIColor.Blue.CGColor;
sa.UnderlineStyleModifiers = CTUnderlineStyleModifiers.PatternDashDotDot;

Assert.IsNull (sa.BaselineClass, "#0");
sa.BaselineClass = CTBaselineClass.IdeographicHigh;
Assert.AreEqual (CTBaselineClass.IdeographicHigh, sa.BaselineClass, "#1");

// Calling sa.SetBaselineInfo makes the CTLine ctor crash (https://github.com/xamarin/maccore/issues/2947)
// so don't do that here.
// sa.SetBaselineInfo (CTBaselineClass.Roman, 13);
// sa.SetBaselineInfo (CTBaselineClass.IdeographicHigh, 3);
sa.SetWritingDirection (CTWritingDirection.LeftToRight);

if (TestRuntime.CheckXcodeVersion (11, 0))
sa.TrackingAdjustment = 1.0f;

AdaptiveImageProvider? provider = null;
if (TestRuntime.CheckXcodeVersion (16, 0))
sa.AdaptiveImageProvider = provider = new AdaptiveImageProvider ();
#endif

var size = new CGSize (300, 300);
#if MONOMAC
using var imageRep = new NSBitmapImageRep (IntPtr.Zero, (nint) size.Width, (nint) size.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (int) size.Width, 32);
using var graphicsContext = NSGraphicsContext.FromBitmap (imageRep);
using var img = new NSImage (size);
img.AddRepresentation (imageRep);
img.LockFocus ();
using var gctx = graphicsContext.CGContext;
#else
UIGraphics.BeginImageContext (size);
var gctx = UIGraphics.GetCurrentContext ();
using var gctx = UIGraphics.GetCurrentContext ();
#endif

gctx.SetFillColor (UIColor.Green.CGColor);

Expand All @@ -67,22 +100,23 @@ public void SimpleValuesSet ()
textLine.Draw (gctx);
}

#if NET
if (TestRuntime.CheckXcodeVersion (16, 0))
Assert.AreEqual (0, provider!.Count, "AdaptiveImageProvider #0");
Assert.AreEqual (1, provider!.Count, "AdaptiveImageProvider #0");

attributedString = new NSAttributedString ("🙈`", sa);
using (var textLine = new CTLine (attributedString)) {
textLine.Draw (gctx);
}

if (TestRuntime.CheckXcodeVersion (16, 0))
Assert.AreEqual (1, provider!.Count, "AdaptiveImageProvider #1");
#endif
Assert.AreEqual (2, provider!.Count, "AdaptiveImageProvider #1");

#if MONOMAC
img.UnlockFocus ();
#else
UIGraphics.EndImageContext ();
}
#endif
}

[Test]
public void BackgroundColor ()
Expand Down

0 comments on commit 85550d7

Please sign in to comment.