ChatModuleTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 log4net.Config;
  30. using Nini.Config;
  31. using NUnit.Framework;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Servers;
  35. using OpenSim.Framework.Servers.HttpServer;
  36. using OpenSim.Region.CoreModules.Avatar.Chat;
  37. using OpenSim.Region.CoreModules.Framework;
  38. using OpenSim.Region.CoreModules.Framework.EntityTransfer;
  39. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Services.Interfaces;
  42. using OpenSim.Tests.Common;
  43. using System.Threading;
  44. namespace OpenSim.Region.CoreModules.Avatar.Chat.Tests
  45. {
  46. [TestFixture]
  47. public class ChatModuleTests : OpenSimTestCase
  48. {
  49. [TestFixtureSetUp]
  50. public void FixtureInit()
  51. {
  52. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  53. // We must do this here so that child agent positions are updated in a predictable manner.
  54. Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
  55. }
  56. [TestFixtureTearDown]
  57. public void TearDown()
  58. {
  59. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  60. // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
  61. // tests really shouldn't).
  62. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  63. }
  64. private void SetupNeighbourRegions(TestScene sceneA, TestScene sceneB)
  65. {
  66. // XXX: HTTP server is not (and should not be) necessary for this test, though it's absence makes the
  67. // CapabilitiesModule complain when it can't set up HTTP endpoints.
  68. BaseHttpServer httpServer = new BaseHttpServer(99999);
  69. MainServer.AddHttpServer(httpServer);
  70. MainServer.Instance = httpServer;
  71. // We need entity transfer modules so that when sp2 logs into the east region, the region calls
  72. // EntityTransferModuleto set up a child agent on the west region.
  73. // XXX: However, this is not an entity transfer so is misleading.
  74. EntityTransferModule etmA = new EntityTransferModule();
  75. EntityTransferModule etmB = new EntityTransferModule();
  76. LocalSimulationConnectorModule lscm = new LocalSimulationConnectorModule();
  77. IConfigSource config = new IniConfigSource();
  78. config.AddConfig("Chat");
  79. IConfig modulesConfig = config.AddConfig("Modules");
  80. modulesConfig.Set("EntityTransferModule", etmA.Name);
  81. modulesConfig.Set("SimulationServices", lscm.Name);
  82. SceneHelpers.SetupSceneModules(new Scene[] { sceneA, sceneB }, config, lscm);
  83. SceneHelpers.SetupSceneModules(sceneA, config, new CapabilitiesModule(), etmA, new ChatModule());
  84. SceneHelpers.SetupSceneModules(sceneB, config, new CapabilitiesModule(), etmB, new ChatModule());
  85. }
  86. /// <summary>
  87. /// Tests chat between neighbour regions on the east-west axis
  88. /// </summary>
  89. /// <remarks>
  90. /// Really, this is a combination of a child agent position update test and a chat range test. These need
  91. /// to be separated later on.
  92. /// </remarks>
  93. [Test]
  94. public void TestInterRegionChatDistanceEastWest()
  95. {
  96. TestHelpers.InMethod();
  97. // TestHelpers.EnableLogging();
  98. UUID sp1Uuid = TestHelpers.ParseTail(0x11);
  99. UUID sp2Uuid = TestHelpers.ParseTail(0x12);
  100. Vector3 sp1Position = new Vector3(6, 128, 20);
  101. Vector3 sp2Position = new Vector3(250, 128, 20);
  102. SceneHelpers sh = new SceneHelpers();
  103. TestScene sceneWest = sh.SetupScene("sceneWest", TestHelpers.ParseTail(0x1), 1000, 1000);
  104. TestScene sceneEast = sh.SetupScene("sceneEast", TestHelpers.ParseTail(0x2), 1001, 1000);
  105. SetupNeighbourRegions(sceneWest, sceneEast);
  106. ScenePresence sp1 = SceneHelpers.AddScenePresence(sceneEast, sp1Uuid);
  107. TestClient sp1Client = (TestClient)sp1.ControllingClient;
  108. // If we don't set agents to flying, test will go wrong as they instantly fall to z = 0.
  109. // TODO: May need to create special complete no-op test physics module rather than basic physics, since
  110. // physics is irrelevant to this test.
  111. sp1.Flying = true;
  112. // When sp1 logs in to sceneEast, it sets up a child agent in sceneWest and informs the sp2 client to
  113. // make the connection. For this test, will simplify this chain by making the connection directly.
  114. ScenePresence sp1Child = SceneHelpers.AddChildScenePresence(sceneWest, sp1Uuid);
  115. TestClient sp1ChildClient = (TestClient)sp1Child.ControllingClient;
  116. sp1.AbsolutePosition = sp1Position;
  117. ScenePresence sp2 = SceneHelpers.AddScenePresence(sceneWest, sp2Uuid);
  118. TestClient sp2Client = (TestClient)sp2.ControllingClient;
  119. sp2.Flying = true;
  120. ScenePresence sp2Child = SceneHelpers.AddChildScenePresence(sceneEast, sp2Uuid);
  121. TestClient sp2ChildClient = (TestClient)sp2Child.ControllingClient;
  122. sp2.AbsolutePosition = sp2Position;
  123. // We must update the scenes in order to make the root new root agents trigger position updates in their
  124. // children.
  125. sceneWest.Update(4);
  126. sceneEast.Update(4);
  127. sp1.DrawDistance += 64;
  128. sp2.DrawDistance += 64;
  129. sceneWest.Update(2);
  130. sceneEast.Update(2);
  131. // Check child positions are correct.
  132. Assert.AreEqual(
  133. new Vector3(sp1Position.X + sceneEast.RegionInfo.RegionSizeX, sp1Position.Y, sp1Position.Z),
  134. sp1ChildClient.SceneAgent.AbsolutePosition);
  135. Assert.AreEqual(
  136. new Vector3(sp2Position.X - sceneWest.RegionInfo.RegionSizeX, sp2Position.Y, sp2Position.Z),
  137. sp2ChildClient.SceneAgent.AbsolutePosition);
  138. string receivedSp1ChatMessage = "";
  139. string receivedSp2ChatMessage = "";
  140. sp1ChildClient.OnReceivedChatMessage
  141. += (message, type, fromPos, fromName, fromAgentID, ownerID, source, audible) => receivedSp1ChatMessage = message;
  142. sp2ChildClient.OnReceivedChatMessage
  143. += (message, type, fromPos, fromName, fromAgentID, ownerID, source, audible) => receivedSp2ChatMessage = message;
  144. TestUserInRange(sp1Client, "ello darling", ref receivedSp2ChatMessage);
  145. TestUserInRange(sp2Client, "fantastic cats", ref receivedSp1ChatMessage);
  146. sp1Position = new Vector3(30, 128, 20);
  147. sp1.AbsolutePosition = sp1Position;
  148. sceneWest.Update(1);
  149. sceneEast.Update(1);
  150. Thread.Sleep(12000); // child updates are now time limited
  151. sceneWest.Update(5);
  152. sceneEast.Update(5);
  153. // Check child position is correct.
  154. Assert.AreEqual(
  155. new Vector3(sp1Position.X + sceneEast.RegionInfo.RegionSizeX, sp1Position.Y, sp1Position.Z),
  156. sp1ChildClient.SceneAgent.AbsolutePosition);
  157. TestUserOutOfRange(sp1Client, "beef", ref receivedSp2ChatMessage);
  158. TestUserOutOfRange(sp2Client, "lentils", ref receivedSp1ChatMessage);
  159. }
  160. /// <summary>
  161. /// Tests chat between neighbour regions on the north-south axis
  162. /// </summary>
  163. /// <remarks>
  164. /// Really, this is a combination of a child agent position update test and a chat range test. These need
  165. /// to be separated later on.
  166. /// </remarks>
  167. [Test]
  168. public void TestInterRegionChatDistanceNorthSouth()
  169. {
  170. TestHelpers.InMethod();
  171. // TestHelpers.EnableLogging();
  172. UUID sp1Uuid = TestHelpers.ParseTail(0x11);
  173. UUID sp2Uuid = TestHelpers.ParseTail(0x12);
  174. Vector3 sp1Position = new Vector3(128, 250, 20);
  175. Vector3 sp2Position = new Vector3(128, 6, 20);
  176. SceneHelpers sh = new SceneHelpers();
  177. TestScene sceneNorth = sh.SetupScene("sceneNorth", TestHelpers.ParseTail(0x1), 1000, 1000);
  178. TestScene sceneSouth = sh.SetupScene("sceneSouth", TestHelpers.ParseTail(0x2), 1000, 1001);
  179. SetupNeighbourRegions(sceneNorth, sceneSouth);
  180. ScenePresence sp1 = SceneHelpers.AddScenePresence(sceneNorth, sp1Uuid);
  181. TestClient sp1Client = (TestClient)sp1.ControllingClient;
  182. // If we don't set agents to flying, test will go wrong as they instantly fall to z = 0.
  183. // TODO: May need to create special complete no-op test physics module rather than basic physics, since
  184. // physics is irrelevant to this test.
  185. sp1.Flying = true;
  186. // When sp1 logs in to sceneEast, it sets up a child agent in sceneNorth and informs the sp2 client to
  187. // make the connection. For this test, will simplify this chain by making the connection directly.
  188. ScenePresence sp1Child = SceneHelpers.AddChildScenePresence(sceneSouth, sp1Uuid);
  189. TestClient sp1ChildClient = (TestClient)sp1Child.ControllingClient;
  190. sp1.AbsolutePosition = sp1Position;
  191. ScenePresence sp2 = SceneHelpers.AddScenePresence(sceneSouth, sp2Uuid);
  192. TestClient sp2Client = (TestClient)sp2.ControllingClient;
  193. sp2.Flying = true;
  194. ScenePresence sp2Child = SceneHelpers.AddChildScenePresence(sceneNorth, sp2Uuid);
  195. TestClient sp2ChildClient = (TestClient)sp2Child.ControllingClient;
  196. sp2.AbsolutePosition = sp2Position;
  197. // We must update the scenes in order to make the root new root agents trigger position updates in their
  198. // children.
  199. sceneNorth.Update(4);
  200. sceneSouth.Update(4);
  201. sp1.DrawDistance += 64;
  202. sp2.DrawDistance += 64;
  203. sceneNorth.Update(4);
  204. sceneSouth.Update(4);
  205. // Check child positions are correct.
  206. Assert.AreEqual(
  207. new Vector3(sp1Position.X, sp1Position.Y - sceneNorth.RegionInfo.RegionSizeY, sp1Position.Z),
  208. sp1ChildClient.SceneAgent.AbsolutePosition);
  209. Assert.AreEqual(
  210. new Vector3(sp2Position.X, sp2Position.Y + sceneSouth.RegionInfo.RegionSizeY, sp2Position.Z),
  211. sp2ChildClient.SceneAgent.AbsolutePosition);
  212. string receivedSp1ChatMessage = "";
  213. string receivedSp2ChatMessage = "";
  214. sp1ChildClient.OnReceivedChatMessage
  215. += (message, type, fromPos, fromName, fromAgentID, ownerID, source, audible) => receivedSp1ChatMessage = message;
  216. sp2ChildClient.OnReceivedChatMessage
  217. += (message, type, fromPos, fromName, fromAgentID, ownerID, source, audible) => receivedSp2ChatMessage = message;
  218. TestUserInRange(sp1Client, "ello darling", ref receivedSp2ChatMessage);
  219. TestUserInRange(sp2Client, "fantastic cats", ref receivedSp1ChatMessage);
  220. sp1Position = new Vector3(30, 128, 20);
  221. sp1.AbsolutePosition = sp1Position;
  222. sceneNorth.Update(1);
  223. sceneSouth.Update(1);
  224. Thread.Sleep(12000); // child updates are now time limited
  225. sceneNorth.Update(5);
  226. sceneSouth.Update(5);
  227. // Check child position is correct.
  228. Assert.AreEqual(
  229. new Vector3(sp1Position.X, sp1Position.Y - sceneNorth.RegionInfo.RegionSizeY, sp1Position.Z),
  230. sp1ChildClient.SceneAgent.AbsolutePosition);
  231. TestUserOutOfRange(sp1Client, "beef", ref receivedSp2ChatMessage);
  232. TestUserOutOfRange(sp2Client, "lentils", ref receivedSp1ChatMessage);
  233. }
  234. private void TestUserInRange(TestClient speakClient, string testMessage, ref string receivedMessage)
  235. {
  236. receivedMessage = "";
  237. speakClient.Chat(0, ChatTypeEnum.Say, testMessage);
  238. Assert.AreEqual(testMessage, receivedMessage);
  239. }
  240. private void TestUserOutOfRange(TestClient speakClient, string testMessage, ref string receivedMessage)
  241. {
  242. receivedMessage = "";
  243. speakClient.Chat(0, ChatTypeEnum.Say, testMessage);
  244. Assert.AreNotEqual(testMessage, receivedMessage);
  245. }
  246. }
  247. }