MockAssetLoader.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using Avalonia.Platform;
  8. namespace Avalonia.UnitTests
  9. {
  10. public class MockAssetLoader : IAssetLoader
  11. {
  12. private Dictionary<Uri, string> _assets;
  13. public MockAssetLoader(params (string, string)[] assets)
  14. {
  15. _assets = assets.ToDictionary(x => new Uri(x.Item1, UriKind.RelativeOrAbsolute), x => x.Item2);
  16. }
  17. public bool Exists(Uri uri, Uri baseUri = null)
  18. {
  19. return _assets.ContainsKey(uri);
  20. }
  21. public Stream Open(Uri uri, Uri baseUri = null)
  22. {
  23. return new MemoryStream(Encoding.UTF8.GetBytes(_assets[uri]));
  24. }
  25. public (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri baseUri = null)
  26. {
  27. return (Open(uri, baseUri), (Assembly)null);
  28. }
  29. public IEnumerable<(string absolutePath, Assembly assembly)> GetAssets(Uri uri)
  30. {
  31. return _assets.Keys.Where(x => x.AbsolutePath.Contains(uri.AbsolutePath))
  32. .Select(x => (x.AbsolutePath, Assembly.GetEntryAssembly()));
  33. }
  34. public void SetDefaultAssembly(Assembly asm)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. }
  39. }