AttachmentsModuleTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 OpenSimulator 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 System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Threading;
  32. using System.Timers;
  33. using Timer=System.Timers.Timer;
  34. using Nini.Config;
  35. using NUnit.Framework;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Communications;
  39. using OpenSim.Region.CoreModules.Avatar.Attachments;
  40. using OpenSim.Region.CoreModules.Framework.InventoryAccess;
  41. using OpenSim.Region.CoreModules.World.Serialiser;
  42. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation;
  43. using OpenSim.Region.Framework.Scenes;
  44. using OpenSim.Region.Framework.Interfaces;
  45. using OpenSim.Tests.Common;
  46. using OpenSim.Tests.Common.Mock;
  47. namespace OpenSim.Region.CoreModules.Avatar.Attachments.Tests
  48. {
  49. /// <summary>
  50. /// Attachment tests
  51. /// </summary>
  52. [TestFixture]
  53. public class AttachmentsModuleTests
  54. {
  55. private Scene scene;
  56. private AttachmentsModule m_attMod;
  57. private ScenePresence m_presence;
  58. [TestFixtureSetUp]
  59. public void FixtureInit()
  60. {
  61. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  62. Util.FireAndForgetMethod = FireAndForgetMethod.None;
  63. }
  64. [SetUp]
  65. public void Init()
  66. {
  67. IConfigSource config = new IniConfigSource();
  68. config.AddConfig("Modules");
  69. config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule");
  70. scene = SceneHelpers.SetupScene();
  71. m_attMod = new AttachmentsModule();
  72. SceneHelpers.SetupSceneModules(scene, config, m_attMod, new BasicInventoryAccessModule());
  73. }
  74. [TestFixtureTearDown]
  75. public void TearDown()
  76. {
  77. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  78. // threads. Possibly, later tests should be rewritten not to worry about such things.
  79. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  80. }
  81. /// <summary>
  82. /// Add the standard presence for a test.
  83. /// </summary>
  84. private void AddPresence()
  85. {
  86. UUID userId = TestHelpers.ParseTail(0x1);
  87. UserAccountHelpers.CreateUserWithInventory(scene, userId);
  88. m_presence = SceneHelpers.AddScenePresence(scene, userId);
  89. }
  90. [Test]
  91. public void TestAddAttachmentFromGround()
  92. {
  93. TestHelpers.InMethod();
  94. // log4net.Config.XmlConfigurator.Configure();
  95. AddPresence();
  96. string attName = "att";
  97. SceneObjectGroup so = SceneHelpers.AddSceneObject(scene, attName).ParentGroup;
  98. m_attMod.AttachObject(m_presence.ControllingClient, so, (uint)AttachmentPoint.Chest, false);
  99. // Check status on scene presence
  100. Assert.That(m_presence.HasAttachments(), Is.True);
  101. List<SceneObjectGroup> attachments = m_presence.GetAttachments();
  102. Assert.That(attachments.Count, Is.EqualTo(1));
  103. SceneObjectGroup attSo = attachments[0];
  104. Assert.That(attSo.Name, Is.EqualTo(attName));
  105. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  106. Assert.That(attSo.IsAttachment);
  107. Assert.That(attSo.UsesPhysics, Is.False);
  108. Assert.That(attSo.IsTemporary, Is.False);
  109. // Check item status
  110. Assert.That(m_presence.Appearance.GetAttachpoint(
  111. attSo.GetFromItemID()), Is.EqualTo((int)AttachmentPoint.Chest));
  112. }
  113. [Test]
  114. public void TestAddAttachmentFromInventory()
  115. {
  116. TestHelpers.InMethod();
  117. // log4net.Config.XmlConfigurator.Configure();
  118. AddPresence();
  119. UUID attItemId = TestHelpers.ParseTail(0x2);
  120. UUID attAssetId = TestHelpers.ParseTail(0x3);
  121. string attName = "att";
  122. UserInventoryHelpers.CreateInventoryItem(
  123. scene, attName, attItemId, attAssetId, m_presence.UUID, InventoryType.Object);
  124. m_attMod.RezSingleAttachmentFromInventory(
  125. m_presence.ControllingClient, attItemId, (uint)AttachmentPoint.Chest);
  126. // Check scene presence status
  127. Assert.That(m_presence.HasAttachments(), Is.True);
  128. List<SceneObjectGroup> attachments = m_presence.GetAttachments();
  129. Assert.That(attachments.Count, Is.EqualTo(1));
  130. SceneObjectGroup attSo = attachments[0];
  131. Assert.That(attSo.Name, Is.EqualTo(attName));
  132. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  133. Assert.That(attSo.IsAttachment);
  134. Assert.That(attSo.UsesPhysics, Is.False);
  135. Assert.That(attSo.IsTemporary, Is.False);
  136. // Check appearance status
  137. Assert.That(m_presence.Appearance.GetAttachments().Count, Is.EqualTo(1));
  138. Assert.That(m_presence.Appearance.GetAttachpoint(attItemId), Is.EqualTo((int)AttachmentPoint.Chest));
  139. }
  140. [Test]
  141. public void TestDetachAttachmentToGround()
  142. {
  143. TestHelpers.InMethod();
  144. // log4net.Config.XmlConfigurator.Configure();
  145. AddPresence();
  146. UUID attItemId = TestHelpers.ParseTail(0x2);
  147. UUID attAssetId = TestHelpers.ParseTail(0x3);
  148. string attName = "att";
  149. UserInventoryHelpers.CreateInventoryItem(
  150. scene, attName, attItemId, attAssetId, m_presence.UUID, InventoryType.Object);
  151. ISceneEntity so = m_attMod.RezSingleAttachmentFromInventory(
  152. m_presence.ControllingClient, attItemId, (uint)AttachmentPoint.Chest);
  153. m_attMod.DetachSingleAttachmentToGround(so.LocalId, m_presence.ControllingClient);
  154. // Check scene presence status
  155. Assert.That(m_presence.HasAttachments(), Is.False);
  156. List<SceneObjectGroup> attachments = m_presence.GetAttachments();
  157. Assert.That(attachments.Count, Is.EqualTo(0));
  158. // Check appearance status
  159. Assert.That(m_presence.Appearance.GetAttachments().Count, Is.EqualTo(0));
  160. // Check item status
  161. Assert.That(scene.InventoryService.GetItem(new InventoryItemBase(attItemId)), Is.Null);
  162. // Check object in scene
  163. Assert.That(scene.GetSceneObjectGroup("att"), Is.Not.Null);
  164. }
  165. [Test]
  166. public void TestDetachAttachmentToInventory()
  167. {
  168. TestHelpers.InMethod();
  169. // log4net.Config.XmlConfigurator.Configure();
  170. AddPresence();
  171. UUID attItemId = TestHelpers.ParseTail(0x2);
  172. UUID attAssetId = TestHelpers.ParseTail(0x3);
  173. string attName = "att";
  174. UserInventoryHelpers.CreateInventoryItem(
  175. scene, attName, attItemId, attAssetId, m_presence.UUID, InventoryType.Object);
  176. m_attMod.RezSingleAttachmentFromInventory(
  177. m_presence.ControllingClient, attItemId, (uint)AttachmentPoint.Chest);
  178. m_attMod.DetachSingleAttachmentToInv(attItemId, m_presence.ControllingClient);
  179. // Check status on scene presence
  180. Assert.That(m_presence.HasAttachments(), Is.False);
  181. List<SceneObjectGroup> attachments = m_presence.GetAttachments();
  182. Assert.That(attachments.Count, Is.EqualTo(0));
  183. // Check item status
  184. Assert.That(m_presence.Appearance.GetAttachpoint(attItemId), Is.EqualTo(0));
  185. }
  186. /// <summary>
  187. /// Test that attachments don't hang about in the scene when the agent is closed
  188. /// </summary>
  189. [Test]
  190. public void TestRemoveAttachmentsOnAvatarExit()
  191. {
  192. TestHelpers.InMethod();
  193. // log4net.Config.XmlConfigurator.Configure();
  194. UUID userId = TestHelpers.ParseTail(0x1);
  195. UUID attItemId = TestHelpers.ParseTail(0x2);
  196. UUID attAssetId = TestHelpers.ParseTail(0x3);
  197. string attName = "att";
  198. UserAccountHelpers.CreateUserWithInventory(scene, userId);
  199. InventoryItemBase attItem
  200. = UserInventoryHelpers.CreateInventoryItem(
  201. scene, attName, attItemId, attAssetId, userId, InventoryType.Object);
  202. AgentCircuitData acd = SceneHelpers.GenerateAgentData(userId);
  203. acd.Appearance = new AvatarAppearance();
  204. acd.Appearance.SetAttachment((int)AttachmentPoint.Chest, attItem.ID, attItem.AssetID);
  205. ScenePresence presence = SceneHelpers.AddScenePresence(scene, acd);
  206. SceneObjectGroup rezzedAtt = presence.GetAttachments()[0];
  207. scene.IncomingCloseAgent(presence.UUID);
  208. // Check that we can't retrieve this attachment from the scene.
  209. Assert.That(scene.GetSceneObjectGroup(rezzedAtt.UUID), Is.Null);
  210. }
  211. [Test]
  212. public void TestRezAttachmentsOnAvatarEntrance()
  213. {
  214. TestHelpers.InMethod();
  215. // log4net.Config.XmlConfigurator.Configure();
  216. UUID userId = TestHelpers.ParseTail(0x1);
  217. UUID attItemId = TestHelpers.ParseTail(0x2);
  218. UUID attAssetId = TestHelpers.ParseTail(0x3);
  219. string attName = "att";
  220. UserAccountHelpers.CreateUserWithInventory(scene, userId);
  221. InventoryItemBase attItem
  222. = UserInventoryHelpers.CreateInventoryItem(
  223. scene, attName, attItemId, attAssetId, userId, InventoryType.Object);
  224. AgentCircuitData acd = SceneHelpers.GenerateAgentData(userId);
  225. acd.Appearance = new AvatarAppearance();
  226. acd.Appearance.SetAttachment((int)AttachmentPoint.Chest, attItem.ID, attItem.AssetID);
  227. ScenePresence presence = SceneHelpers.AddScenePresence(scene, acd);
  228. Assert.That(presence.HasAttachments(), Is.True);
  229. List<SceneObjectGroup> attachments = presence.GetAttachments();
  230. Assert.That(attachments.Count, Is.EqualTo(1));
  231. SceneObjectGroup attSo = attachments[0];
  232. Assert.That(attSo.Name, Is.EqualTo(attName));
  233. Assert.That(attSo.AttachmentPoint, Is.EqualTo((byte)AttachmentPoint.Chest));
  234. Assert.That(attSo.IsAttachment);
  235. Assert.That(attSo.UsesPhysics, Is.False);
  236. Assert.That(attSo.IsTemporary, Is.False);
  237. // Check appearance status
  238. List<AvatarAttachment> retreivedAttachments = presence.Appearance.GetAttachments();
  239. Assert.That(retreivedAttachments.Count, Is.EqualTo(1));
  240. Assert.That(retreivedAttachments[0].AttachPoint, Is.EqualTo((int)AttachmentPoint.Chest));
  241. Assert.That(retreivedAttachments[0].ItemID, Is.EqualTo(attItemId));
  242. Assert.That(retreivedAttachments[0].AssetID, Is.EqualTo(attAssetId));
  243. Assert.That(presence.Appearance.GetAttachpoint(attItemId), Is.EqualTo((int)AttachmentPoint.Chest));
  244. }
  245. // I'm commenting this test because scene setup NEEDS InventoryService to
  246. // be non-null
  247. //[Test]
  248. // public void T032_CrossAttachments()
  249. // {
  250. // TestHelpers.InMethod();
  251. //
  252. // ScenePresence presence = scene.GetScenePresence(agent1);
  253. // ScenePresence presence2 = scene2.GetScenePresence(agent1);
  254. // presence2.AddAttachment(sog1);
  255. // presence2.AddAttachment(sog2);
  256. //
  257. // ISharedRegionModule serialiser = new SerialiserModule();
  258. // SceneHelpers.SetupSceneModules(scene, new IniConfigSource(), serialiser);
  259. // SceneHelpers.SetupSceneModules(scene2, new IniConfigSource(), serialiser);
  260. //
  261. // Assert.That(presence.HasAttachments(), Is.False, "Presence has attachments before cross");
  262. //
  263. // //Assert.That(presence2.CrossAttachmentsIntoNewRegion(region1, true), Is.True, "Cross was not successful");
  264. // Assert.That(presence2.HasAttachments(), Is.False, "Presence2 objects were not deleted");
  265. // Assert.That(presence.HasAttachments(), Is.True, "Presence has not received new objects");
  266. // }
  267. }
  268. }