SceneObjectTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using NUnit.Framework;
  29. using NUnit.Framework.SyntaxHelpers;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Communications;
  33. using OpenSim.Framework.Communications.Cache;
  34. using OpenSim.Region.Communications.Local;
  35. using OpenSim.Region.Environment.Scenes;
  36. using OpenSim.Tests.Common.Mock;
  37. namespace OpenSim.Region.Environment.Scenes.Tests
  38. {
  39. /// <summary>
  40. /// Scene object tests
  41. /// </summary>
  42. [TestFixture]
  43. public class SceneObjectTests
  44. {
  45. /// <summary>
  46. /// Test adding an object to a scene.
  47. /// </summary>
  48. [Test]
  49. public void TestAddSceneObject()
  50. {
  51. Scene scene = SceneTestUtils.SetupScene();
  52. SceneObjectPart part = SceneTestUtils.AddSceneObject(scene);
  53. SceneObjectPart retrievedPart = scene.GetSceneObjectPart(part.LocalId);
  54. //System.Console.WriteLine("retrievedPart : {0}", retrievedPart);
  55. // If the parts have the same UUID then we will consider them as one and the same
  56. Assert.That(retrievedPart.UUID, Is.EqualTo(part.UUID));
  57. }
  58. /// <summary>
  59. /// Test deleting an object from a scene.
  60. /// </summary>
  61. [Test]
  62. public void TestDeleteSceneObject()
  63. {
  64. TestScene scene = SceneTestUtils.SetupScene();
  65. SceneObjectPart part = SceneTestUtils.AddSceneObject(scene);
  66. scene.DeleteSceneObject(part.ParentGroup, false);
  67. SceneObjectPart retrievedPart = scene.GetSceneObjectPart(part.LocalId);
  68. Assert.That(retrievedPart, Is.Null);
  69. }
  70. /// <summary>
  71. /// Test deleting an object asynchronously
  72. /// </summary>
  73. [Test]
  74. public void TestDeleteSceneObjectAsync()
  75. {
  76. UUID agentId = UUID.Parse("00000000-0000-0000-0000-000000000001");
  77. TestScene scene = SceneTestUtils.SetupScene();
  78. // Turn off the timer on the async sog deleter - we'll crank it by hand for this test.
  79. AsyncSceneObjectGroupDeleter sogd = scene.SceneObjectGroupDeleter;
  80. sogd.Enabled = false;
  81. SceneObjectPart part = SceneTestUtils.AddSceneObject(scene);
  82. IClientAPI client = SceneTestUtils.AddRootAgent(scene, agentId);
  83. scene.DeRezObject(client, part.LocalId, UUID.Zero, DeRezAction.Delete, UUID.Zero);
  84. SceneObjectPart retrievedPart = scene.GetSceneObjectPart(part.LocalId);
  85. Assert.That(retrievedPart, Is.Not.Null);
  86. sogd.InventoryDeQueueAndDelete();
  87. SceneObjectPart retrievedPart2 = scene.GetSceneObjectPart(part.LocalId);
  88. Assert.That(retrievedPart2, Is.Null);
  89. }
  90. [Test]
  91. public void TestLinkDelink2SceneObjects()
  92. {
  93. bool debugtest = false;
  94. Scene scene = SceneTestUtils.SetupScene();
  95. SceneObjectPart part1 = SceneTestUtils.AddSceneObject(scene);
  96. SceneObjectGroup grp1 = part1.ParentGroup;
  97. SceneObjectPart part2 = SceneTestUtils.AddSceneObject(scene);
  98. SceneObjectGroup grp2 = part2.ParentGroup;
  99. grp1.AbsolutePosition = new Vector3(10, 10, 10);
  100. grp2.AbsolutePosition = Vector3.Zero;
  101. // <90,0,0>
  102. grp1.Rotation = (Quaternion.CreateFromEulers(90 * Utils.DEG_TO_RAD, 0, 0));
  103. // <180,0,0>
  104. grp2.UpdateGroupRotation(Quaternion.CreateFromEulers(180 * Utils.DEG_TO_RAD, 0, 0));
  105. // Required for linking
  106. grp1.RootPart.UpdateFlag = 0;
  107. grp2.RootPart.UpdateFlag = 0;
  108. // Link grp2 to grp1. part2 becomes child prim to grp1. grp2 is eliminated.
  109. grp1.LinkToGroup(grp2);
  110. Assert.That(grp1.Children.Count == 2);
  111. if (debugtest)
  112. {
  113. System.Console.WriteLine("parts: {0}", grp1.Children.Count);
  114. System.Console.WriteLine("Group1: Pos:{0}, Rot:{1}", grp1.AbsolutePosition, grp1.Rotation);
  115. System.Console.WriteLine("Group1: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part1.OffsetPosition, part1.RotationOffset);
  116. System.Console.WriteLine("Group1: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part2.OffsetPosition, part2.RotationOffset);
  117. }
  118. // root part should have no offset position or rotation
  119. Assert.That(part1.OffsetPosition == Vector3.Zero && part1.RotationOffset == Quaternion.Identity);
  120. // offset position should be root part position - part2.absolute position.
  121. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10));
  122. float roll = 0;
  123. float pitch = 0;
  124. float yaw = 0;
  125. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  126. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  127. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  128. if (debugtest)
  129. System.Console.WriteLine(rotEuler1);
  130. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  131. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  132. if (debugtest)
  133. System.Console.WriteLine(rotEuler2);
  134. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f));
  135. // Delink part 2
  136. grp1.DelinkFromGroup(part2.LocalId);
  137. if (debugtest)
  138. System.Console.WriteLine("Group2: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part2.AbsolutePosition, part2.RotationOffset);
  139. Assert.That(part2.AbsolutePosition == Vector3.Zero);
  140. }
  141. [Test]
  142. public void TestLinkDelink2groups4SceneObjects()
  143. {
  144. bool debugtest = false;
  145. Scene scene = SceneTestUtils.SetupScene();
  146. SceneObjectPart part1 = SceneTestUtils.AddSceneObject(scene);
  147. SceneObjectGroup grp1 = part1.ParentGroup;
  148. SceneObjectPart part2 = SceneTestUtils.AddSceneObject(scene);
  149. SceneObjectGroup grp2 = part2.ParentGroup;
  150. SceneObjectPart part3 = SceneTestUtils.AddSceneObject(scene);
  151. SceneObjectGroup grp3 = part3.ParentGroup;
  152. SceneObjectPart part4 = SceneTestUtils.AddSceneObject(scene);
  153. SceneObjectGroup grp4 = part4.ParentGroup;
  154. grp1.AbsolutePosition = new Vector3(10, 10, 10);
  155. grp2.AbsolutePosition = Vector3.Zero;
  156. grp3.AbsolutePosition = new Vector3(20, 20, 20);
  157. grp4.AbsolutePosition = new Vector3(40, 40, 40);
  158. // <90,0,0>
  159. grp1.Rotation = (Quaternion.CreateFromEulers(90 * Utils.DEG_TO_RAD, 0, 0));
  160. // <180,0,0>
  161. grp2.UpdateGroupRotation(Quaternion.CreateFromEulers(180 * Utils.DEG_TO_RAD, 0, 0));
  162. // <270,0,0>
  163. grp3.Rotation = (Quaternion.CreateFromEulers(270 * Utils.DEG_TO_RAD, 0, 0));
  164. // <0,90,0>
  165. grp4.UpdateGroupRotation(Quaternion.CreateFromEulers(0, 90 * Utils.DEG_TO_RAD, 0));
  166. // Required for linking
  167. grp1.RootPart.UpdateFlag = 0;
  168. grp2.RootPart.UpdateFlag = 0;
  169. grp3.RootPart.UpdateFlag = 0;
  170. grp4.RootPart.UpdateFlag = 0;
  171. // Link grp2 to grp1. part2 becomes child prim to grp1. grp2 is eliminated.
  172. grp1.LinkToGroup(grp2);
  173. // Link grp4 to grp3.
  174. grp3.LinkToGroup(grp4);
  175. // Required for linking
  176. grp1.RootPart.UpdateFlag = 0;
  177. grp3.RootPart.UpdateFlag = 0;
  178. // At this point we should have 4 parts total in two groups.
  179. Assert.That(grp1.Children.Count == 2);
  180. Assert.That(grp3.Children.Count == 2);
  181. if (debugtest)
  182. {
  183. System.Console.WriteLine("--------After Link-------");
  184. System.Console.WriteLine("Group1: parts: {0}", grp1.Children.Count);
  185. System.Console.WriteLine("Group1: Pos:{0}, Rot:{1}", grp1.AbsolutePosition, grp1.Rotation);
  186. System.Console.WriteLine("Group1: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part1.OffsetPosition, part1.RotationOffset);
  187. System.Console.WriteLine("Group1: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part2.OffsetPosition, part2.RotationOffset);
  188. System.Console.WriteLine("Group3: parts: {0}", grp3.Children.Count);
  189. System.Console.WriteLine("Group3: Pos:{0}, Rot:{1}", grp3.AbsolutePosition, grp3.Rotation);
  190. System.Console.WriteLine("Group3: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part3.OffsetPosition, part3.RotationOffset);
  191. System.Console.WriteLine("Group3: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part4.OffsetPosition, part4.RotationOffset);
  192. }
  193. // root part should have no offset position or rotation
  194. Assert.That(part1.OffsetPosition == Vector3.Zero && part1.RotationOffset == Quaternion.Identity);
  195. // offset position should be root part position - part2.absolute position.
  196. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10));
  197. float roll = 0;
  198. float pitch = 0;
  199. float yaw = 0;
  200. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  201. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  202. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  203. if (debugtest)
  204. System.Console.WriteLine(rotEuler1);
  205. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  206. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  207. if (debugtest)
  208. System.Console.WriteLine(rotEuler2);
  209. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f));
  210. // Now we're linking the first group to the second group. This will make the first group child parts of the second one.
  211. grp3.LinkToGroup(grp1);
  212. // Delink part 2
  213. grp1.DelinkFromGroup(part2.LocalId);
  214. grp1.DelinkFromGroup(part3.LocalId);
  215. if (debugtest)
  216. {
  217. System.Console.WriteLine("--------After De-Link-------");
  218. System.Console.WriteLine("Group1: parts: {0}", grp1.Children.Count);
  219. System.Console.WriteLine("Group1: Pos:{0}, Rot:{1}", grp1.AbsolutePosition, grp1.Rotation);
  220. System.Console.WriteLine("Group1: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part1.OffsetPosition, part1.RotationOffset);
  221. System.Console.WriteLine("NoGroup: Prim2: AbsolutePosition:{0}, OffsetRotation:{1}", part2.AbsolutePosition, part2.RotationOffset);
  222. System.Console.WriteLine("Group3: parts: {0}", grp3.Children.Count);
  223. System.Console.WriteLine("Group3: Pos:{0}, Rot:{1}", grp3.AbsolutePosition, grp3.Rotation);
  224. System.Console.WriteLine("Group3: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part3.OffsetPosition, part3.RotationOffset);
  225. System.Console.WriteLine("Group3: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part4.OffsetPosition, part4.RotationOffset);
  226. }
  227. Assert.That(part2.AbsolutePosition == Vector3.Zero);
  228. Assert.That(part4.OffsetPosition == new Vector3(20, 20, 20));
  229. Quaternion compareQuaternion = new Quaternion(0, 0.7071068f, 0, 0.7071068f);
  230. Assert.That((part4.RotationOffset.X - compareQuaternion.X < 0.00003)
  231. && (part4.RotationOffset.Y - compareQuaternion.Y < 0.00003)
  232. && (part4.RotationOffset.Z - compareQuaternion.Z < 0.00003)
  233. && (part4.RotationOffset.W - compareQuaternion.W < 0.00003));
  234. }
  235. /// <summary>
  236. /// Test deleting an object asynchronously to user inventory.
  237. /// </summary>
  238. [Test]
  239. public void TestDeleteSceneObjectAsyncToUserInventory()
  240. {
  241. //log4net.Config.XmlConfigurator.Configure();
  242. UUID agentId = UUID.Parse("00000000-0000-0000-0000-000000000001");
  243. string myObjectName = "Fred";
  244. TestScene scene = SceneTestUtils.SetupScene();
  245. SceneObjectPart part = SceneTestUtils.AddSceneObject(scene, myObjectName);
  246. Assert.That(
  247. scene.CommsManager.UserAdminService.AddUser(
  248. "Bob", "Hoskins", "test", "[email protected]", 1000, 1000, agentId),
  249. Is.EqualTo(agentId));
  250. IClientAPI client = SceneTestUtils.AddRootAgent(scene, agentId);
  251. CachedUserInfo userInfo = scene.CommsManager.UserProfileCacheService.GetUserDetails(agentId);
  252. Assert.That(userInfo, Is.Not.Null);
  253. Assert.That(userInfo.RootFolder, Is.Not.Null);
  254. SceneTestUtils.DeleteSceneObjectAsync(scene, part, DeRezAction.Take, userInfo.RootFolder.ID, client);
  255. // Check that we now have the taken part in our inventory
  256. Assert.That(myObjectName, Is.EqualTo(userInfo.RootFolder.FindItemByPath(myObjectName).Name));
  257. // Check that the taken part has actually disappeared
  258. SceneObjectPart retrievedPart = scene.GetSceneObjectPart(part.LocalId);
  259. Assert.That(retrievedPart, Is.Null);
  260. }
  261. }
  262. }