SceneObjectTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. using OpenSim.Tests.Common.Setup;
  38. namespace OpenSim.Region.Environment.Scenes.Tests
  39. {
  40. /// <summary>
  41. /// Scene object tests
  42. /// </summary>
  43. [TestFixture]
  44. public class SceneObjectTests
  45. {
  46. /// <summary>
  47. /// Test adding an object to a scene.
  48. /// </summary>
  49. [Test]
  50. public void TestAddSceneObject()
  51. {
  52. Scene scene = SceneSetupHelpers.SetupScene();
  53. SceneObjectPart part = SceneSetupHelpers.AddSceneObject(scene);
  54. SceneObjectPart retrievedPart = scene.GetSceneObjectPart(part.LocalId);
  55. //System.Console.WriteLine("retrievedPart : {0}", retrievedPart);
  56. // If the parts have the same UUID then we will consider them as one and the same
  57. Assert.That(retrievedPart.UUID, Is.EqualTo(part.UUID));
  58. }
  59. /// <summary>
  60. /// Test deleting an object from a scene.
  61. /// </summary>
  62. [Test]
  63. public void TestDeleteSceneObject()
  64. {
  65. TestScene scene = SceneSetupHelpers.SetupScene();
  66. SceneObjectPart part = SceneSetupHelpers.AddSceneObject(scene);
  67. scene.DeleteSceneObject(part.ParentGroup, false);
  68. SceneObjectPart retrievedPart = scene.GetSceneObjectPart(part.LocalId);
  69. Assert.That(retrievedPart, Is.Null);
  70. }
  71. /// <summary>
  72. /// Test deleting an object asynchronously
  73. /// </summary>
  74. [Test]
  75. public void TestDeleteSceneObjectAsync()
  76. {
  77. UUID agentId = UUID.Parse("00000000-0000-0000-0000-000000000001");
  78. TestScene scene = SceneSetupHelpers.SetupScene();
  79. // Turn off the timer on the async sog deleter - we'll crank it by hand for this test.
  80. AsyncSceneObjectGroupDeleter sogd = scene.SceneObjectGroupDeleter;
  81. sogd.Enabled = false;
  82. SceneObjectPart part = SceneSetupHelpers.AddSceneObject(scene);
  83. IClientAPI client = SceneSetupHelpers.AddRootAgent(scene, agentId);
  84. scene.DeRezObject(client, part.LocalId, UUID.Zero, DeRezAction.Delete, UUID.Zero);
  85. SceneObjectPart retrievedPart = scene.GetSceneObjectPart(part.LocalId);
  86. Assert.That(retrievedPart, Is.Not.Null);
  87. sogd.InventoryDeQueueAndDelete();
  88. SceneObjectPart retrievedPart2 = scene.GetSceneObjectPart(part.LocalId);
  89. Assert.That(retrievedPart2, Is.Null);
  90. }
  91. [Test]
  92. public void TestLinkDelink2SceneObjects()
  93. {
  94. bool debugtest = false;
  95. Scene scene = SceneSetupHelpers.SetupScene();
  96. SceneObjectPart part1 = SceneSetupHelpers.AddSceneObject(scene);
  97. SceneObjectGroup grp1 = part1.ParentGroup;
  98. SceneObjectPart part2 = SceneSetupHelpers.AddSceneObject(scene);
  99. SceneObjectGroup grp2 = part2.ParentGroup;
  100. grp1.AbsolutePosition = new Vector3(10, 10, 10);
  101. grp2.AbsolutePosition = Vector3.Zero;
  102. // <90,0,0>
  103. grp1.Rotation = (Quaternion.CreateFromEulers(90 * Utils.DEG_TO_RAD, 0, 0));
  104. // <180,0,0>
  105. grp2.UpdateGroupRotation(Quaternion.CreateFromEulers(180 * Utils.DEG_TO_RAD, 0, 0));
  106. // Required for linking
  107. grp1.RootPart.UpdateFlag = 0;
  108. grp2.RootPart.UpdateFlag = 0;
  109. // Link grp2 to grp1. part2 becomes child prim to grp1. grp2 is eliminated.
  110. grp1.LinkToGroup(grp2);
  111. Assert.That(grp1.Children.Count == 2);
  112. if (debugtest)
  113. {
  114. System.Console.WriteLine("parts: {0}", grp1.Children.Count);
  115. System.Console.WriteLine("Group1: Pos:{0}, Rot:{1}", grp1.AbsolutePosition, grp1.Rotation);
  116. System.Console.WriteLine("Group1: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part1.OffsetPosition, part1.RotationOffset);
  117. System.Console.WriteLine("Group1: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part2.OffsetPosition, part2.RotationOffset);
  118. }
  119. // root part should have no offset position or rotation
  120. Assert.That(part1.OffsetPosition == Vector3.Zero && part1.RotationOffset == Quaternion.Identity);
  121. // offset position should be root part position - part2.absolute position.
  122. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10));
  123. float roll = 0;
  124. float pitch = 0;
  125. float yaw = 0;
  126. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  127. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  128. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  129. if (debugtest)
  130. System.Console.WriteLine(rotEuler1);
  131. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  132. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  133. if (debugtest)
  134. System.Console.WriteLine(rotEuler2);
  135. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f));
  136. // Delink part 2
  137. grp1.DelinkFromGroup(part2.LocalId);
  138. if (debugtest)
  139. System.Console.WriteLine("Group2: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part2.AbsolutePosition, part2.RotationOffset);
  140. Assert.That(part2.AbsolutePosition == Vector3.Zero);
  141. }
  142. [Test]
  143. public void TestLinkDelink2groups4SceneObjects()
  144. {
  145. bool debugtest = false;
  146. Scene scene = SceneSetupHelpers.SetupScene();
  147. SceneObjectPart part1 = SceneSetupHelpers.AddSceneObject(scene);
  148. SceneObjectGroup grp1 = part1.ParentGroup;
  149. SceneObjectPart part2 = SceneSetupHelpers.AddSceneObject(scene);
  150. SceneObjectGroup grp2 = part2.ParentGroup;
  151. SceneObjectPart part3 = SceneSetupHelpers.AddSceneObject(scene);
  152. SceneObjectGroup grp3 = part3.ParentGroup;
  153. SceneObjectPart part4 = SceneSetupHelpers.AddSceneObject(scene);
  154. SceneObjectGroup grp4 = part4.ParentGroup;
  155. grp1.AbsolutePosition = new Vector3(10, 10, 10);
  156. grp2.AbsolutePosition = Vector3.Zero;
  157. grp3.AbsolutePosition = new Vector3(20, 20, 20);
  158. grp4.AbsolutePosition = new Vector3(40, 40, 40);
  159. // <90,0,0>
  160. grp1.Rotation = (Quaternion.CreateFromEulers(90 * Utils.DEG_TO_RAD, 0, 0));
  161. // <180,0,0>
  162. grp2.UpdateGroupRotation(Quaternion.CreateFromEulers(180 * Utils.DEG_TO_RAD, 0, 0));
  163. // <270,0,0>
  164. grp3.Rotation = (Quaternion.CreateFromEulers(270 * Utils.DEG_TO_RAD, 0, 0));
  165. // <0,90,0>
  166. grp4.UpdateGroupRotation(Quaternion.CreateFromEulers(0, 90 * Utils.DEG_TO_RAD, 0));
  167. // Required for linking
  168. grp1.RootPart.UpdateFlag = 0;
  169. grp2.RootPart.UpdateFlag = 0;
  170. grp3.RootPart.UpdateFlag = 0;
  171. grp4.RootPart.UpdateFlag = 0;
  172. // Link grp2 to grp1. part2 becomes child prim to grp1. grp2 is eliminated.
  173. grp1.LinkToGroup(grp2);
  174. // Link grp4 to grp3.
  175. grp3.LinkToGroup(grp4);
  176. // Required for linking
  177. grp1.RootPart.UpdateFlag = 0;
  178. grp3.RootPart.UpdateFlag = 0;
  179. // At this point we should have 4 parts total in two groups.
  180. Assert.That(grp1.Children.Count == 2);
  181. Assert.That(grp3.Children.Count == 2);
  182. if (debugtest)
  183. {
  184. System.Console.WriteLine("--------After Link-------");
  185. System.Console.WriteLine("Group1: parts: {0}", grp1.Children.Count);
  186. System.Console.WriteLine("Group1: Pos:{0}, Rot:{1}", grp1.AbsolutePosition, grp1.Rotation);
  187. System.Console.WriteLine("Group1: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part1.OffsetPosition, part1.RotationOffset);
  188. System.Console.WriteLine("Group1: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part2.OffsetPosition, part2.RotationOffset);
  189. System.Console.WriteLine("Group3: parts: {0}", grp3.Children.Count);
  190. System.Console.WriteLine("Group3: Pos:{0}, Rot:{1}", grp3.AbsolutePosition, grp3.Rotation);
  191. System.Console.WriteLine("Group3: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part3.OffsetPosition, part3.RotationOffset);
  192. System.Console.WriteLine("Group3: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part4.OffsetPosition, part4.RotationOffset);
  193. }
  194. // root part should have no offset position or rotation
  195. Assert.That(part1.OffsetPosition == Vector3.Zero && part1.RotationOffset == Quaternion.Identity);
  196. // offset position should be root part position - part2.absolute position.
  197. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10));
  198. float roll = 0;
  199. float pitch = 0;
  200. float yaw = 0;
  201. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  202. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  203. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  204. if (debugtest)
  205. System.Console.WriteLine(rotEuler1);
  206. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  207. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  208. if (debugtest)
  209. System.Console.WriteLine(rotEuler2);
  210. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f));
  211. // Now we're linking the first group to the second group. This will make the first group child parts of the second one.
  212. grp3.LinkToGroup(grp1);
  213. // Delink part 2
  214. grp1.DelinkFromGroup(part2.LocalId);
  215. grp1.DelinkFromGroup(part3.LocalId);
  216. if (debugtest)
  217. {
  218. System.Console.WriteLine("--------After De-Link-------");
  219. System.Console.WriteLine("Group1: parts: {0}", grp1.Children.Count);
  220. System.Console.WriteLine("Group1: Pos:{0}, Rot:{1}", grp1.AbsolutePosition, grp1.Rotation);
  221. System.Console.WriteLine("Group1: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part1.OffsetPosition, part1.RotationOffset);
  222. System.Console.WriteLine("NoGroup: Prim2: AbsolutePosition:{0}, OffsetRotation:{1}", part2.AbsolutePosition, part2.RotationOffset);
  223. System.Console.WriteLine("Group3: parts: {0}", grp3.Children.Count);
  224. System.Console.WriteLine("Group3: Pos:{0}, Rot:{1}", grp3.AbsolutePosition, grp3.Rotation);
  225. System.Console.WriteLine("Group3: Prim1: OffsetPosition:{0}, OffsetRotation:{1}", part3.OffsetPosition, part3.RotationOffset);
  226. System.Console.WriteLine("Group3: Prim2: OffsetPosition:{0}, OffsetRotation:{1}", part4.OffsetPosition, part4.RotationOffset);
  227. }
  228. Assert.That(part2.AbsolutePosition == Vector3.Zero);
  229. Assert.That(part4.OffsetPosition == new Vector3(20, 20, 20));
  230. Quaternion compareQuaternion = new Quaternion(0, 0.7071068f, 0, 0.7071068f);
  231. Assert.That((part4.RotationOffset.X - compareQuaternion.X < 0.00003)
  232. && (part4.RotationOffset.Y - compareQuaternion.Y < 0.00003)
  233. && (part4.RotationOffset.Z - compareQuaternion.Z < 0.00003)
  234. && (part4.RotationOffset.W - compareQuaternion.W < 0.00003));
  235. }
  236. /// <summary>
  237. /// Test deleting an object asynchronously to user inventory.
  238. /// </summary>
  239. [Test]
  240. public void TestDeleteSceneObjectAsyncToUserInventory()
  241. {
  242. //log4net.Config.XmlConfigurator.Configure();
  243. UUID agentId = UUID.Parse("00000000-0000-0000-0000-000000000001");
  244. string myObjectName = "Fred";
  245. TestScene scene = SceneSetupHelpers.SetupScene();
  246. SceneObjectPart part = SceneSetupHelpers.AddSceneObject(scene, myObjectName);
  247. Assert.That(
  248. scene.CommsManager.UserAdminService.AddUser(
  249. "Bob", "Hoskins", "test", "[email protected]", 1000, 1000, agentId),
  250. Is.EqualTo(agentId));
  251. IClientAPI client = SceneSetupHelpers.AddRootAgent(scene, agentId);
  252. CachedUserInfo userInfo = scene.CommsManager.UserProfileCacheService.GetUserDetails(agentId);
  253. Assert.That(userInfo, Is.Not.Null);
  254. Assert.That(userInfo.RootFolder, Is.Not.Null);
  255. SceneSetupHelpers.DeleteSceneObjectAsync(scene, part, DeRezAction.Take, userInfo.RootFolder.ID, client);
  256. // Check that we now have the taken part in our inventory
  257. Assert.That(myObjectName, Is.EqualTo(userInfo.RootFolder.FindItemByPath(myObjectName).Name));
  258. // Check that the taken part has actually disappeared
  259. SceneObjectPart retrievedPart = scene.GetSceneObjectPart(part.LocalId);
  260. Assert.That(retrievedPart, Is.Null);
  261. }
  262. }
  263. }