RegressionTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using NUnit.Framework;
  7. using SharpGLTF.Validation;
  8. namespace SharpGLTF.Schema2.LoadAndSave
  9. {
  10. [AttachmentPathFormat("*/TestResults/LoadAndSave/?", true)]
  11. internal class RegressionTests
  12. {
  13. [Test]
  14. public void LoadWithRelativeAndAbsolutePath()
  15. {
  16. // store current directory
  17. var cdir = Environment.CurrentDirectory;
  18. var modelPath = ResourceInfo.From("SpecialCases/RelativePaths.gltf");
  19. // absolute path
  20. var model1 = ModelRoot.Load(modelPath, Validation.ValidationMode.TryFix);
  21. Assert.That(model1, Is.Not.Null);
  22. Assert.That(model1.LogicalImages, Has.Count.EqualTo(4));
  23. TestContext.WriteLine(string.Join(" ", ModelRoot.GetSatellitePaths(modelPath)));
  24. // local path
  25. Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(modelPath);
  26. var modelFile = System.IO.Path.GetFileName(modelPath);
  27. var model2 = ModelRoot.Load(modelFile, Validation.ValidationMode.TryFix);
  28. Assert.That(model2, Is.Not.Null);
  29. Assert.That(model2.LogicalImages, Has.Count.EqualTo(4));
  30. TestContext.WriteLine(string.Join(" ", ModelRoot.GetSatellitePaths(modelPath)));
  31. // relative path:
  32. modelFile = System.IO.Path.Combine(System.IO.Path.GetFileName(Environment.CurrentDirectory), modelFile);
  33. Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(Environment.CurrentDirectory);
  34. var model3 = ModelRoot.Load(modelFile, Validation.ValidationMode.TryFix);
  35. Assert.That(model3, Is.Not.Null);
  36. Assert.That(model3.LogicalImages, Has.Count.EqualTo(4));
  37. TestContext.WriteLine(string.Join(" ", ModelRoot.GetSatellitePaths(modelPath)));
  38. // restore current directory
  39. Environment.CurrentDirectory = cdir;
  40. }
  41. [Test]
  42. public void LoadSuzanneTest()
  43. {
  44. var path1 = TestFiles.GetSampleModelsPaths().First(item => item.EndsWith("Suzanne.gltf"));
  45. var suzanne1 = ModelRoot.Load(path1, ValidationMode.TryFix);
  46. var suzanne1Mem = suzanne1.LogicalBuffers.Sum(item => item.Content.Length);
  47. // direct save-load
  48. var path2 = suzanne1
  49. .AttachToCurrentTest("suzanne2.glb");
  50. var suzanne2 = ModelRoot.Load(path2);
  51. var suzanne2Mem = suzanne1.LogicalBuffers.Sum(item => item.Content.Length);
  52. Assert.That(suzanne2Mem, Is.EqualTo(suzanne1Mem));
  53. Assert.That(suzanne2.LogicalMeshes, Has.Count.EqualTo(suzanne1.LogicalMeshes.Count));
  54. // scenebuilder roundtrip
  55. var path3 = Scenes.SceneBuilder
  56. .CreateFrom(suzanne1.DefaultScene)
  57. .ToGltf2()
  58. .AttachToCurrentTest("suzanne.glb");
  59. var suzanne3 = ModelRoot.Load(path3);
  60. var suzanne3Mem = suzanne1.LogicalBuffers.Sum(item => item.Content.Length);
  61. Assert.That(suzanne3Mem, Is.EqualTo(suzanne1Mem));
  62. Assert.That(suzanne3.LogicalMeshes, Has.Count.EqualTo(suzanne1.LogicalMeshes.Count));
  63. }
  64. [Test]
  65. public void LoadBinaryWithLimitedStream()
  66. {
  67. var path1 = TestFiles.GetSampleModelsPaths().First(item => item.EndsWith("BrainStem.glb"));
  68. var bytes = System.IO.File.ReadAllBytes(path1);
  69. using(var ls = new ReadOnlyTestStream(bytes))
  70. {
  71. var model = ModelRoot.ReadGLB(ls);
  72. Assert.That(model, Is.Not.Null);
  73. }
  74. }
  75. }
  76. }