FbxExporterTest.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // ***********************************************************************
  2. // Copyright (c) 2017 Unity Technologies. All rights reserved.
  3. //
  4. // Licensed under the ##LICENSENAME##.
  5. // See LICENSE.md file in the project root for full license information.
  6. // ***********************************************************************
  7. using NUnit.Framework;
  8. using Autodesk.Fbx;
  9. using System.IO;
  10. namespace Autodesk.Fbx.UnitTests
  11. {
  12. public class FbxExporterTest : FbxIOBaseTest<FbxExporter>
  13. {
  14. FbxExporter m_exporter;
  15. string m_testFolderPrefix = "to_delete_";
  16. string m_testFolder;
  17. private string GetRandomDirectory()
  18. {
  19. string randomDir = Path.Combine(Path.GetTempPath(), m_testFolderPrefix);
  20. string temp;
  21. do {
  22. // check that the directory does not already exist
  23. temp = randomDir + Path.GetRandomFileName ();
  24. } while(Directory.Exists (temp));
  25. return temp;
  26. }
  27. private string GetRandomFilename(string path, bool fbxExtension = true)
  28. {
  29. string temp;
  30. do {
  31. // check that the directory does not already exist
  32. temp = Path.Combine (path, Path.GetRandomFileName ());
  33. if(fbxExtension){
  34. temp = Path.ChangeExtension(temp, ".fbx");
  35. }
  36. } while(File.Exists (temp));
  37. return temp;
  38. }
  39. public override void Init()
  40. {
  41. base.Init ();
  42. m_exporter = FbxExporter.Create (Manager, "exporter");
  43. Assert.IsNotNull (m_exporter);
  44. var testDirectories = Directory.GetDirectories(Path.GetTempPath(), m_testFolderPrefix + "*");
  45. foreach (var directory in testDirectories)
  46. {
  47. Directory.Delete(directory, true);
  48. }
  49. m_testFolder = GetRandomDirectory ();
  50. Directory.CreateDirectory (m_testFolder);
  51. }
  52. public override void Term()
  53. {
  54. try{
  55. m_exporter.Destroy();
  56. }
  57. catch(System.ArgumentNullException){
  58. // already destroyed in test
  59. }
  60. base.Term ();
  61. // delete all files that were created
  62. Directory.Delete(m_testFolder, true);
  63. }
  64. [Test]
  65. public override void TestBasics()
  66. {
  67. base.TestBasics();
  68. // Call each function that doesn't write a file, just to see whether it crashes.
  69. m_exporter.Initialize("foo.fbx");
  70. m_exporter.SetFileExportVersion("FBX201400");
  71. m_exporter.GetCurrentWritableVersions();
  72. m_exporter.SetProgressCallback(null);
  73. m_exporter.SetProgressCallback((float a, string b) => true);
  74. m_exporter.SetProgressCallback(null);
  75. // test GetFileHeaderInfo()
  76. TestGetter(m_exporter.GetFileHeaderInfo());
  77. Assert.IsNotNull (m_exporter.GetFileHeaderInfo ());
  78. }
  79. [Test]
  80. public void TestExportEmptyFbxDocument ()
  81. {
  82. FbxDocument emptyDoc = FbxDocument.Create (Manager, "empty");
  83. string filename = GetRandomFilename (m_testFolder);
  84. // Initialize the exporter.
  85. bool exportStatus = m_exporter.Initialize (filename, -1, Manager.GetIOSettings());
  86. Assert.IsTrue (exportStatus);
  87. m_exporter.SetProgressCallback((float a, string b) => true);
  88. bool status = m_exporter.Export (emptyDoc);
  89. Assert.IsTrue (status);
  90. Assert.IsTrue (File.Exists (filename));
  91. }
  92. [Test]
  93. public void TestExportNull ()
  94. {
  95. string filename = GetRandomFilename (m_testFolder);
  96. // Initialize the exporter.
  97. bool exportStatus = m_exporter.Initialize (filename, -1, Manager.GetIOSettings());
  98. Assert.IsTrue (exportStatus);
  99. // Export a null document. This is documented to fail.
  100. bool status = m_exporter.Export (null);
  101. Assert.IsFalse (status);
  102. // FbxSdk creates an empty file even though the export status was false
  103. Assert.IsTrue (File.Exists (filename));
  104. }
  105. [Test]
  106. public void TestInitializeInvalidFilenameOnly()
  107. {
  108. FbxDocument emptyDoc = FbxDocument.Create (Manager, "empty");
  109. // Build the filename without the extension.
  110. string filename = GetRandomFilename (m_testFolder, false);
  111. // Initialize the exporter. Use default file type and IO settings.
  112. bool exportStatus = m_exporter.Initialize (filename);
  113. Assert.IsTrue (exportStatus);
  114. bool status = m_exporter.Export (emptyDoc);
  115. Assert.IsTrue (status);
  116. // FbxSdk doesn't create a file in this situation
  117. Assert.IsFalse (File.Exists (filename));
  118. }
  119. [Test]
  120. public void TestInitializeValidFilenameOnly()
  121. {
  122. FbxDocument emptyDoc = FbxDocument.Create (Manager, "empty");
  123. string filename = GetRandomFilename (m_testFolder);
  124. // Initialize the exporter. Use default file type and IO settings.
  125. bool exportStatus = m_exporter.Initialize (filename);
  126. Assert.IsTrue (exportStatus);
  127. bool status = m_exporter.Export (emptyDoc);
  128. Assert.IsTrue (status);
  129. Assert.IsTrue (File.Exists (filename));
  130. }
  131. [Test]
  132. public void TestInitializeFileFormatNegative()
  133. {
  134. FbxDocument emptyDoc = FbxDocument.Create (Manager, "empty");
  135. string filename = GetRandomFilename (m_testFolder);
  136. // Initialize the exporter. Pass it a negative file format different than -1.
  137. bool exportStatus = m_exporter.Initialize (filename, int.MinValue);
  138. Assert.IsTrue (exportStatus);
  139. bool status = m_exporter.Export (emptyDoc);
  140. Assert.IsTrue (status);
  141. Assert.IsTrue (File.Exists (filename));
  142. }
  143. [Test]
  144. public void TestInitializeFileFormatInvalid()
  145. {
  146. FbxDocument emptyDoc = FbxDocument.Create (Manager, "empty");
  147. string filename = GetRandomFilename (m_testFolder);
  148. // Initialize the exporter. Pass it a file format that's not valid.
  149. bool exportStatus = m_exporter.Initialize (filename, int.MaxValue);
  150. Assert.IsTrue (exportStatus);
  151. bool status = m_exporter.Export (emptyDoc);
  152. Assert.IsFalse (status);
  153. Assert.IsFalse (File.Exists (filename));
  154. }
  155. [Test]
  156. public void TestInitializeValidFileFormat()
  157. {
  158. FbxDocument emptyDoc = FbxDocument.Create (Manager, "empty");
  159. string filename = GetRandomFilename (m_testFolder);
  160. // Initialize the exporter. Use a valid non-default file format.
  161. bool exportStatus = m_exporter.Initialize (filename, 1);
  162. Assert.IsTrue (exportStatus);
  163. bool status = m_exporter.Export (emptyDoc);
  164. Assert.IsTrue (status);
  165. Assert.IsTrue (File.Exists (filename));
  166. }
  167. [Test]
  168. public void TestInitializeNullIOSettings()
  169. {
  170. FbxDocument emptyDoc = FbxDocument.Create (Manager, "empty");
  171. string filename = GetRandomFilename (m_testFolder);
  172. // Initialize the exporter with explicit null IO settings (which is
  173. // also the default).
  174. bool exportStatus = m_exporter.Initialize (filename, -1, null);
  175. Assert.IsTrue (exportStatus);
  176. bool status = m_exporter.Export (emptyDoc);
  177. Assert.IsTrue (status);
  178. Assert.IsTrue (File.Exists (filename));
  179. }
  180. [Test]
  181. public void TestInitializeInvalidIOSettings()
  182. {
  183. string filename = GetRandomFilename (m_testFolder);
  184. // Initialize the exporter. Pass it zombie IO settings.
  185. var ioSettings = FbxIOSettings.Create(Manager, "");
  186. ioSettings.Destroy();
  187. Assert.That (() => { m_exporter.Initialize (filename, -1, ioSettings); }, Throws.Exception.TypeOf<System.ArgumentNullException>());
  188. }
  189. }
  190. }