TaskInventoryHelpers.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 OpenMetaverse;
  29. using OpenMetaverse.Assets;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework.Scenes;
  32. using OpenSim.Services.Interfaces;
  33. namespace OpenSim.Tests.Common
  34. {
  35. /// <summary>
  36. /// Utility functions for carrying out task inventory tests.
  37. /// </summary>
  38. ///
  39. public static class TaskInventoryHelpers
  40. {
  41. /// <summary>
  42. /// Add a notecard item to the given part.
  43. /// </summary>
  44. /// <param name="scene"></param>
  45. /// <param name="part"></param>
  46. /// <param name="itemName"></param>
  47. /// <param name="itemIDFrag">UUID or UUID stem</param>
  48. /// <param name="assetIDFrag">UUID or UUID stem</param>
  49. /// <param name="text">The tex to put in the notecard.</param>
  50. /// <returns>The item that was added</returns>
  51. public static TaskInventoryItem AddNotecard(
  52. Scene scene, SceneObjectPart part, string itemName, string itemIDStem, string assetIDStem, string text)
  53. {
  54. return AddNotecard(
  55. scene, part, itemName, TestHelpers.ParseStem(itemIDStem), TestHelpers.ParseStem(assetIDStem), text);
  56. }
  57. /// <summary>
  58. /// Add a notecard item to the given part.
  59. /// </summary>
  60. /// <param name="scene"></param>
  61. /// <param name="part"></param>
  62. /// <param name="itemName"></param>
  63. /// <param name="itemID"></param>
  64. /// <param name="assetID"></param>
  65. /// <param name="text">The tex to put in the notecard.</param>
  66. /// <returns>The item that was added</returns>
  67. public static TaskInventoryItem AddNotecard(
  68. Scene scene, SceneObjectPart part, string itemName, UUID itemID, UUID assetID, string text)
  69. {
  70. AssetNotecard nc = new AssetNotecard();
  71. nc.BodyText = text;
  72. nc.Encode();
  73. AssetBase ncAsset
  74. = AssetHelpers.CreateAsset(assetID, AssetType.Notecard, nc.AssetData, UUID.Zero);
  75. scene.AssetService.Store(ncAsset);
  76. TaskInventoryItem ncItem
  77. = new TaskInventoryItem
  78. { Name = itemName, AssetID = assetID, ItemID = itemID,
  79. Type = (int)AssetType.Notecard, InvType = (int)InventoryType.Notecard };
  80. part.Inventory.AddInventoryItem(ncItem, true);
  81. return ncItem;
  82. }
  83. /// <summary>
  84. /// Add a simple script to the given part.
  85. /// </summary>
  86. /// <remarks>
  87. /// TODO: Accept input for item and asset IDs to avoid mysterious script failures that try to use any of these
  88. /// functions more than once in a test.
  89. /// </remarks>
  90. /// <param name="scene"></param>
  91. /// <param name="part"></param>
  92. /// <returns>The item that was added</returns>
  93. public static TaskInventoryItem AddScript(Scene scene, SceneObjectPart part)
  94. {
  95. return AddScript(scene, part, "scriptItem", "default { state_entry() { llSay(0, \"Hello World\"); } }");
  96. }
  97. /// <summary>
  98. /// Add a simple script to the given part.
  99. /// </summary>
  100. /// <remarks>
  101. /// TODO: Accept input for item and asset IDs so that we have completely replicatable regression tests rather
  102. /// than a random component.
  103. /// </remarks>
  104. /// <param name="scene"></param>
  105. /// <param name="part"></param>
  106. /// <param name="scriptName">Name of the script to add</param>
  107. /// <param name="scriptSource">LSL script source</param>
  108. /// <returns>The item that was added</returns>
  109. public static TaskInventoryItem AddScript(
  110. Scene scene, SceneObjectPart part, string scriptName, string scriptSource)
  111. {
  112. return AddScript(scene, part, UUID.Random(), UUID.Random(), scriptName, scriptSource);
  113. }
  114. /// <summary>
  115. /// Add a simple script to the given part.
  116. /// </summary>
  117. /// <remarks>
  118. /// TODO: Accept input for item and asset IDs so that we have completely replicatable regression tests rather
  119. /// than a random component.
  120. /// </remarks>
  121. /// <param name="scene"></param>
  122. /// <param name="part"></param>
  123. /// <param name="itemId">Item UUID for the script</param>
  124. /// <param name="assetId">Asset UUID for the script</param>
  125. /// <param name="scriptName">Name of the script to add</param>
  126. /// <param name="scriptSource">LSL script source</param>
  127. /// <returns>The item that was added</returns>
  128. public static TaskInventoryItem AddScript(
  129. Scene scene, SceneObjectPart part, UUID itemId, UUID assetId, string scriptName, string scriptSource)
  130. {
  131. AssetScriptText ast = new AssetScriptText();
  132. ast.Source = scriptSource;
  133. ast.Encode();
  134. AssetBase asset
  135. = AssetHelpers.CreateAsset(assetId, AssetType.LSLText, ast.AssetData, UUID.Zero);
  136. scene.AssetService.Store(asset);
  137. TaskInventoryItem item
  138. = new TaskInventoryItem
  139. { Name = scriptName, AssetID = assetId, ItemID = itemId,
  140. Type = (int)AssetType.LSLText, InvType = (int)InventoryType.LSL };
  141. part.Inventory.AddInventoryItem(item, true);
  142. return item;
  143. }
  144. /// <summary>
  145. /// Add a scene object item to the given part.
  146. /// </summary>
  147. /// <remarks>
  148. /// TODO: Accept input for item and asset IDs to avoid mysterious script failures that try to use any of these
  149. /// functions more than once in a test.
  150. /// </remarks>
  151. ///
  152. /// <param name="scene"></param>
  153. /// <param name="sop"></param>
  154. /// <param name="itemName"></param>
  155. /// <param name="id"></param>
  156. /// <param name="userId"></param>
  157. public static TaskInventoryItem AddSceneObject(
  158. Scene scene, SceneObjectPart sop, string itemName, UUID id, UUID userId)
  159. {
  160. SceneObjectGroup taskSceneObject = SceneHelpers.CreateSceneObject(1, UUID.Zero);
  161. AssetBase taskSceneObjectAsset = AssetHelpers.CreateAsset(0x10, taskSceneObject);
  162. scene.AssetService.Store(taskSceneObjectAsset);
  163. TaskInventoryItem taskSceneObjectItem
  164. = new TaskInventoryItem
  165. { Name = itemName,
  166. AssetID = taskSceneObjectAsset.FullID,
  167. ItemID = id,
  168. OwnerID = userId,
  169. Type = (int)AssetType.Object,
  170. InvType = (int)InventoryType.Object };
  171. sop.Inventory.AddInventoryItem(taskSceneObjectItem, true);
  172. return taskSceneObjectItem;
  173. }
  174. }
  175. }