MockAssetLoader.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. using Avalonia.Utilities;
  9. namespace Avalonia.UnitTests
  10. {
  11. public class MockAssetLoader : IAssetLoader
  12. {
  13. private Dictionary<Uri, string> _assets;
  14. public MockAssetLoader(params (string, string)[] assets)
  15. {
  16. _assets = assets.ToDictionary(x => new Uri(x.Item1, UriKind.RelativeOrAbsolute), x => x.Item2);
  17. }
  18. public bool Exists(Uri uri, Uri baseUri = null)
  19. {
  20. return _assets.ContainsKey(uri);
  21. }
  22. public Stream Open(Uri uri, Uri baseUri = null)
  23. {
  24. return new MemoryStream(Encoding.UTF8.GetBytes(_assets[uri]));
  25. }
  26. public (Stream stream, Assembly assembly) OpenAndGetAssembly(Uri uri, Uri baseUri = null)
  27. {
  28. return (Open(uri, baseUri), (Assembly)null);
  29. }
  30. public Assembly GetAssembly(Uri uri, Uri baseUri = null)
  31. {
  32. return null;
  33. }
  34. public IEnumerable<Uri> GetAssets(Uri uri, Uri baseUri)
  35. {
  36. var absPath = uri.GetUnescapeAbsolutePath();
  37. return _assets.Keys.Where(
  38. x => x.GetUnescapeAbsolutePath().IndexOf(absPath, StringComparison.Ordinal) >= 0);
  39. }
  40. public void InvalidateAssemblyCache(string name)
  41. {
  42. throw new NotImplementedException();
  43. }
  44. public void InvalidateAssemblyCache()
  45. {
  46. throw new NotImplementedException();
  47. }
  48. public void SetDefaultAssembly(Assembly asm)
  49. {
  50. throw new NotImplementedException();
  51. }
  52. }
  53. }