TaskInventoryHelpers.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. AssetScriptText ast = new AssetScriptText();
  113. ast.Source = scriptSource;
  114. ast.Encode();
  115. UUID assetUuid = UUID.Random();
  116. UUID itemUuid = UUID.Random();
  117. AssetBase asset
  118. = AssetHelpers.CreateAsset(assetUuid, AssetType.LSLText, ast.AssetData, UUID.Zero);
  119. scene.AssetService.Store(asset);
  120. TaskInventoryItem item
  121. = new TaskInventoryItem
  122. { Name = scriptName, AssetID = assetUuid, ItemID = itemUuid,
  123. Type = (int)AssetType.LSLText, InvType = (int)InventoryType.LSL };
  124. part.Inventory.AddInventoryItem(item, true);
  125. return item;
  126. }
  127. /// <summary>
  128. /// Add a scene object item to the given part.
  129. /// </summary>
  130. /// <remarks>
  131. /// TODO: Accept input for item and asset IDs to avoid mysterious script failures that try to use any of these
  132. /// functions more than once in a test.
  133. /// </remarks>
  134. ///
  135. /// <param name="scene"></param>
  136. /// <param name="sop"></param>
  137. /// <param name="itemName"></param>
  138. /// <param name="id"></param>
  139. /// <param name="userId"></param>
  140. public static TaskInventoryItem AddSceneObject(
  141. Scene scene, SceneObjectPart sop, string itemName, UUID id, UUID userId)
  142. {
  143. SceneObjectGroup taskSceneObject = SceneHelpers.CreateSceneObject(1, UUID.Zero);
  144. AssetBase taskSceneObjectAsset = AssetHelpers.CreateAsset(0x10, taskSceneObject);
  145. scene.AssetService.Store(taskSceneObjectAsset);
  146. TaskInventoryItem taskSceneObjectItem
  147. = new TaskInventoryItem
  148. { Name = itemName,
  149. AssetID = taskSceneObjectAsset.FullID,
  150. ItemID = id,
  151. OwnerID = userId,
  152. Type = (int)AssetType.Object,
  153. InvType = (int)InventoryType.Object };
  154. sop.Inventory.AddInventoryItem(taskSceneObjectItem, true);
  155. return taskSceneObjectItem;
  156. }
  157. }
  158. }