Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danipen committed Apr 25, 2023
1 parent b7084f4 commit ace157d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 73 deletions.
40 changes: 25 additions & 15 deletions test/AvaloniaEdit.Tests/AvaloniaMocks/MockFontManagerImpl.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using Avalonia.Media;
using Avalonia.Platform;

using Moq;

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;

#nullable enable
using Avalonia.Media;
using Avalonia.Platform;

namespace AvaloniaEdit.AvaloniaMocks
{
Expand All @@ -20,34 +16,48 @@ public MockFontManagerImpl(string defaultFamilyName = "Default")
_defaultFamilyName = defaultFamilyName;
}

public int TryCreateGlyphTypefaceCount { get; private set; }

public string GetDefaultFontFamilyName()
{
return _defaultFamilyName;
}

public string[] GetInstalledFontFamilyNames(bool checkForUpdates = false)
string[] IFontManagerImpl.GetInstalledFontFamilyNames(bool checkForUpdates)
{
return new[] { _defaultFamilyName };
}

public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch,
FontFamily? fontFamily, CultureInfo? culture, out Typeface typeface)
public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight,
FontStretch fontStretch,
CultureInfo culture, out Typeface fontKey)
{
typeface = new Typeface(_defaultFamilyName);
fontKey = new Typeface(_defaultFamilyName);

return true;
return false;
}

public bool TryCreateGlyphTypeface(string familyName, FontStyle style, FontWeight weight,
FontStretch stretch, out IGlyphTypeface glyphTypeface)
public virtual bool TryCreateGlyphTypeface(string familyName, FontStyle style, FontWeight weight,
FontStretch stretch, [NotNullWhen(true)] out IGlyphTypeface glyphTypeface)
{
glyphTypeface = null;

TryCreateGlyphTypefaceCount++;

if (familyName == "Unknown")
{
return false;
}

glyphTypeface = new MockGlyphTypeface();

return true;
}

public bool TryCreateGlyphTypeface(Stream stream, out IGlyphTypeface glyphTypeface)
public virtual bool TryCreateGlyphTypeface(Stream stream, out IGlyphTypeface glyphTypeface)
{
glyphTypeface = new MockGlyphTypeface();

return true;
}
}
Expand Down
145 changes: 87 additions & 58 deletions test/AvaloniaEdit.Tests/AvaloniaMocks/MockPlatformRenderInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,153 +10,182 @@

namespace AvaloniaEdit.AvaloniaMocks
{
public class MockPlatformRenderInterface : IPlatformRenderInterface
public class MockPlatformRenderInterface : IPlatformRenderInterface, IPlatformRenderInterfaceContext
{
public IEnumerable<string> InstalledFontNames => throw new NotImplementedException();

public bool SupportsIndividualRoundRects => throw new NotImplementedException();

public AlphaFormat DefaultAlphaFormat => throw new NotImplementedException();

public PixelFormat DefaultPixelFormat => throw new NotImplementedException();

public IGeometryImpl CreateEllipseGeometry(Rect rect)
{
throw new NotImplementedException();
return Moq.Mock.Of<IGeometryImpl>();
}

public IGeometryImpl CreateLineGeometry(Point p1, Point p2)
{
throw new NotImplementedException();
return Moq.Mock.Of<IGeometryImpl>();
}

public IGeometryImpl CreateRectangleGeometry(Rect rect)
{
throw new NotImplementedException();
return Moq.Mock.Of<IGeometryImpl>(x => x.Bounds == rect);
}

public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
class MockRenderTarget : IRenderTarget
{
return Mock.Of<IRenderTarget>();
public void Dispose()
{

}

public IDrawingContextImpl CreateDrawingContext()
{
var m = new Mock<IDrawingContextImpl>();
m.Setup(c => c.CreateLayer(It.IsAny<Size>()))
.Returns(() =>
{
var r = new Mock<IDrawingContextLayerImpl>();
r.Setup(r => r.CreateDrawingContext())
.Returns(CreateDrawingContext());
return r.Object;
}
);
return m.Object;

}

public bool IsCorrupted => false;
}

public IRenderTargetBitmapImpl CreateRenderTargetBitmap(
int width,
int height,
double dpiX,
double dpiY)
public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
{
return Mock.Of<IRenderTargetBitmapImpl>();
return new MockRenderTarget();
}

public bool IsLost => false;

public object TryGetFeature(Type featureType) => null;

public IRenderTargetBitmapImpl CreateRenderTargetBitmap(PixelSize size, Vector dpi)
{
throw new NotImplementedException();
return Moq.Mock.Of<IRenderTargetBitmapImpl>();
}

public IStreamGeometryImpl CreateStreamGeometry()
{
return new MockStreamGeometryImpl();
}

public IWriteableBitmapImpl CreateWriteableBitmap(PixelSize size, Vector dpi, PixelFormat? format = null)
public IGeometryImpl CreateGeometryGroup(FillRule fillRule, IReadOnlyList<IGeometryImpl> children)
{
throw new NotImplementedException();
return Moq.Mock.Of<IGeometryImpl>();
}

public IBitmapImpl LoadBitmap(Stream stream)
public IGeometryImpl CreateCombinedGeometry(GeometryCombineMode combineMode, IGeometryImpl g1, IGeometryImpl g2)
{
return Mock.Of<IBitmapImpl>();
}

public IBitmapImpl LoadBitmap(string fileName)
{
return Mock.Of<IBitmapImpl>();
}

public IBitmapImpl LoadBitmap(PixelFormat format, IntPtr data, PixelSize size, Vector dpi, int stride)
{
throw new NotImplementedException();
return Moq.Mock.Of<IGeometryImpl>();
}

public IGeometryImpl CreateGeometryGroup(FillRule fillRule, IReadOnlyList<Geometry> children)
public IWriteableBitmapImpl CreateWriteableBitmap(
PixelSize size,
Vector dpi,
PixelFormat format,
AlphaFormat alphaFormat)
{
throw new NotImplementedException();
}

public IGeometryImpl CreateCombinedGeometry(GeometryCombineMode combineMode, Geometry g1, Geometry g2)
public IBitmapImpl LoadBitmap(Stream stream)
{
throw new NotImplementedException();
return Moq.Mock.Of<IBitmapImpl>();
}

public IWriteableBitmapImpl CreateWriteableBitmap(PixelSize size, Vector dpi, PixelFormat format, AlphaFormat alphaFormat)
public IWriteableBitmapImpl LoadWriteableBitmapToWidth(Stream stream, int width,
BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
{
throw new NotImplementedException();
}

public IWriteableBitmapImpl LoadWriteableBitmapToWidth(Stream stream, int width, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
public IWriteableBitmapImpl LoadWriteableBitmapToHeight(Stream stream, int height,
BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
{
throw new NotImplementedException();
}

public IWriteableBitmapImpl LoadWriteableBitmapToHeight(Stream stream, int height, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
public IWriteableBitmapImpl LoadWriteableBitmap(string fileName)
{
throw new NotImplementedException();
}

public IWriteableBitmapImpl LoadWriteableBitmap(string fileName)
public IWriteableBitmapImpl LoadWriteableBitmap(Stream stream)
{
throw new NotImplementedException();
}

public IWriteableBitmapImpl LoadWriteableBitmap(Stream stream)
public IBitmapImpl LoadBitmap(string fileName)
{
throw new NotImplementedException();
return Moq.Mock.Of<IBitmapImpl>();
}

public IBitmapImpl LoadBitmapToWidth(Stream stream, int width, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
{
throw new NotImplementedException();
return Moq.Mock.Of<IBitmapImpl>();
}

public IBitmapImpl LoadBitmapToHeight(Stream stream, int height, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
{
throw new NotImplementedException();
return Moq.Mock.Of<IBitmapImpl>();
}

public IBitmapImpl ResizeBitmap(IBitmapImpl bitmapImpl, PixelSize destinationSize, BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.HighQuality)
{
throw new NotImplementedException();
return Moq.Mock.Of<IBitmapImpl>();
}

public IBitmapImpl LoadBitmap(PixelFormat format, AlphaFormat alphaFormat, IntPtr data, PixelSize size, Vector dpi, int stride)
public IBitmapImpl LoadBitmap(
PixelFormat format,
AlphaFormat alphaFormat,
IntPtr data,
PixelSize size,
Vector dpi,
int stride)
{
throw new NotImplementedException();
return Moq.Mock.Of<IBitmapImpl>();
}

public IGlyphRunImpl CreateGlyphRun(GlyphRun glyphRun)
public IGlyphRunImpl CreateGlyphRun(IGlyphTypeface glyphTypeface, double fontRenderingEmSize,
IReadOnlyList<GlyphInfo> glyphInfos, Point baselineOrigin)
{
throw new NotImplementedException();
return new MockGlyphRun(glyphInfos);
}

public IPlatformRenderInterfaceContext CreateBackendContext(IPlatformGraphicsContext graphicsContext) => this;

public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun)
{
throw new NotImplementedException();
return Moq.Mock.Of<IGeometryImpl>();
}

public IGlyphRunImpl CreateGlyphRun(IGlyphTypeface glyphTypeface, double fontRenderingEmSize, IReadOnlyList<GlyphInfo> glyphInfos, Point baselineOrigin)
public IGlyphRunBuffer AllocateGlyphRun(IGlyphTypeface glyphTypeface, float fontRenderingEmSize, int length)
{
return new MockGlyphRun(glyphInfos);
return Moq.Mock.Of<IGlyphRunBuffer>();
}

public IPlatformRenderInterfaceContext CreateBackendContext(IPlatformGraphicsContext graphicsApiContext)
public IHorizontalGlyphRunBuffer AllocateHorizontalGlyphRun(IGlyphTypeface glyphTypeface, float fontRenderingEmSize, int length)
{
throw new NotImplementedException();
return Moq.Mock.Of<IHorizontalGlyphRunBuffer>();
}

public bool IsSupportedBitmapPixelFormat(PixelFormat format)
public IPositionedGlyphRunBuffer AllocatePositionedGlyphRun(IGlyphTypeface glyphTypeface, float fontRenderingEmSize, int length)
{
return Moq.Mock.Of<IPositionedGlyphRunBuffer>();
}

public bool SupportsIndividualRoundRects { get; set; }

public AlphaFormat DefaultAlphaFormat => AlphaFormat.Premul;

public PixelFormat DefaultPixelFormat => PixelFormat.Rgba8888;
public bool IsSupportedBitmapPixelFormat(PixelFormat format) => true;

public void Dispose()
{
throw new NotImplementedException();
}
}
}

0 comments on commit ace157d

Please sign in to comment.