NodeTests.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Diagnostics;
  2. using System.Threading.Tasks;
  3. using NUnit.Framework;
  4. using Urho.Tests.Bootstrap;
  5. namespace Urho.Tests
  6. {
  7. [TestFixture]
  8. public class NodeTests
  9. {
  10. [Test]
  11. public async Task GetComponent()
  12. {
  13. TestApp.RunSimpleApp(async app =>
  14. {
  15. CreateHeirarchy(app.RootNode, 0);
  16. Assert.IsNotNull(app.Scene.GetComponent<Octree>(false));
  17. Assert.IsNotNull(app.Scene.GetComponent<Octree>(true));
  18. Assert.IsNull(app.Scene.GetComponent<ManagedComponent>(false));
  19. Assert.IsNotNull(app.Scene.GetComponent<ManagedComponent>(true));
  20. Stopwatch sw = Stopwatch.StartNew();
  21. for (int i = 0; i < 10; i++)
  22. Assert.IsNotNull(app.Scene.GetComponent<ManagedComponent>(true));
  23. sw.Stop();
  24. System.Console.WriteLine($"GetComponent<C#>: {sw.ElapsedMilliseconds}ms");
  25. sw.Restart();
  26. for (int i = 0; i < 5; i++)
  27. Assert.IsNotNull(app.Scene.GetComponent<AnimationController>(true));
  28. sw.Stop();
  29. System.Console.WriteLine($"GetComponent<C++>: {sw.ElapsedMilliseconds}ms");
  30. var node1 = app.Scene.CreateChild();
  31. var node2 = node1.CreateChild("node2");
  32. node2.AddTag("foo");
  33. var node3 = node1.CreateChild("node3");
  34. node3.AddTag("boo");
  35. node3.AddTag("foo");
  36. var node4 = node1.CreateChild("node3");
  37. node4.AddTag("boo");
  38. var fooNodes = node1.GetChildrenWithTag("foo", true);
  39. Assert.AreEqual(2, fooNodes.Length);
  40. Assert.AreEqual(fooNodes[0].Name, "node2");
  41. Assert.AreEqual(fooNodes[1].Name, "node3");
  42. fooNodes = node1.GetChildrenWithTag("foo", false);
  43. Assert.AreEqual(0, fooNodes.Length);
  44. await app.Exit();
  45. });
  46. }
  47. const int MaxLevel = 5;
  48. static void CreateHeirarchy(Node node, int level)
  49. {
  50. if (level > MaxLevel)
  51. return;
  52. node.CreateComponent<Camera>();
  53. node.CreateComponent<StaticModel>();
  54. node.CreateComponent<AnimatedModel>();
  55. node.CreateComponent<ManagedCamera>();
  56. if (level == MaxLevel)
  57. {
  58. node.CreateComponent<AnimationController>();
  59. node.CreateComponent<Component>();
  60. node.CreateComponent<ManagedComponent>();
  61. }
  62. for (int i = 0; i < MaxLevel; i++)
  63. {
  64. CreateHeirarchy(node.CreateChild($"Node_{level}_{i}"), level + 1);
  65. }
  66. }
  67. }
  68. public class ManagedCamera : Camera { }
  69. public class ManagedComponent : Component { }
  70. }