// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // namespace ImageSharp.Tests { using System; using System.Collections.Concurrent; using Xunit.Abstractions; public abstract partial class TestImageProvider where TColor : struct, IPixel { private class FileProvider : TestImageProvider, IXunitSerializable { // Need PixelTypes in the dictionary key, because result images of TestImageProvider.FileProvider // are shared between PixelTypes.Color & PixelTypes.StandardImageClass private class Key : Tuple { public Key(PixelTypes item1, string item2) : base(item1, item2) { } } private static ConcurrentDictionary> cache = new ConcurrentDictionary>(); private string filePath; public FileProvider(string filePath) { this.filePath = filePath; } public FileProvider() { } public override string SourceFileOrDescription => this.filePath; public override Image GetImage() { Key key = new Key(this.PixelType, this.filePath); Image cachedImage = cache.GetOrAdd( key, fn => { TestFile testFile = TestFile.Create(this.filePath); return this.Factory.CreateImage(testFile.Bytes); }); return this.Factory.CreateImage(cachedImage); } public override void Deserialize(IXunitSerializationInfo info) { this.filePath = info.GetValue("path"); base.Deserialize(info); // must be called last } public override void Serialize(IXunitSerializationInfo info) { base.Serialize(info); info.AddValue("path", this.filePath); } } } }