OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
ChatModuleTests.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using log4net.Config;
31 using Nini.Config;
32 using NUnit.Framework;
33 using OpenMetaverse;
34 using OpenSim.Framework;
35 using OpenSim.Framework.Servers;
36 using OpenSim.Framework.Servers.HttpServer;
37 using OpenSim.Region.CoreModules.Avatar.Chat;
38 using OpenSim.Region.CoreModules.Framework;
39 using OpenSim.Region.CoreModules.Framework.EntityTransfer;
40 using OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation;
41 using OpenSim.Region.Framework.Scenes;
42 using OpenSim.Services.Interfaces;
43 using OpenSim.Tests.Common;
44 using System.Threading;
45 
46 namespace OpenSim.Region.CoreModules.Avatar.Chat.Tests
47 {
48  [TestFixture]
50  {
51  [TestFixtureSetUp]
52  public void FixtureInit()
53  {
54  // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
55  // We must do this here so that child agent positions are updated in a predictable manner.
56  Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
57  }
58 
59  [TestFixtureTearDown]
60  public void TearDown()
61  {
62  // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
63  // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
64  // tests really shouldn't).
65  Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
66  }
67 
68  private void SetupNeighbourRegions(TestScene sceneA, TestScene sceneB)
69  {
70  // XXX: HTTP server is not (and should not be) necessary for this test, though it's absence makes the
71  // CapabilitiesModule complain when it can't set up HTTP endpoints.
73  MainServer.AddHttpServer(httpServer);
74  MainServer.Instance = httpServer;
75 
76  // We need entity transfer modules so that when sp2 logs into the east region, the region calls
77  // EntityTransferModuleto set up a child agent on the west region.
78  // XXX: However, this is not an entity transfer so is misleading.
82 
83  IConfigSource config = new IniConfigSource();
84  config.AddConfig("Chat");
85  IConfig modulesConfig = config.AddConfig("Modules");
86  modulesConfig.Set("EntityTransferModule", etmA.Name);
87  modulesConfig.Set("SimulationServices", lscm.Name);
88 
89  SceneHelpers.SetupSceneModules(new Scene[] { sceneA, sceneB }, config, lscm);
90  SceneHelpers.SetupSceneModules(sceneA, config, new CapabilitiesModule(), etmA, new ChatModule());
91  SceneHelpers.SetupSceneModules(sceneB, config, new CapabilitiesModule(), etmB, new ChatModule());
92  }
93 
101  [Test]
103  {
104  TestHelpers.InMethod();
105 // TestHelpers.EnableLogging();
106 
107  UUID sp1Uuid = TestHelpers.ParseTail(0x11);
108  UUID sp2Uuid = TestHelpers.ParseTail(0x12);
109 
110  Vector3 sp1Position = new Vector3(6, 128, 20);
111  Vector3 sp2Position = new Vector3(250, 128, 20);
112 
113  SceneHelpers sh = new SceneHelpers();
114  TestScene sceneWest = sh.SetupScene("sceneWest", TestHelpers.ParseTail(0x1), 1000, 1000);
115  TestScene sceneEast = sh.SetupScene("sceneEast", TestHelpers.ParseTail(0x2), 1001, 1000);
116 
117  SetupNeighbourRegions(sceneWest, sceneEast);
118 
119  ScenePresence sp1 = SceneHelpers.AddScenePresence(sceneEast, sp1Uuid);
120  TestClient sp1Client = (TestClient)sp1.ControllingClient;
121 
122  // If we don't set agents to flying, test will go wrong as they instantly fall to z = 0.
123  // TODO: May need to create special complete no-op test physics module rather than basic physics, since
124  // physics is irrelevant to this test.
125  sp1.Flying = true;
126 
127  // When sp1 logs in to sceneEast, it sets up a child agent in sceneWest and informs the sp2 client to
128  // make the connection. For this test, will simplify this chain by making the connection directly.
129  ScenePresence sp1Child = SceneHelpers.AddChildScenePresence(sceneWest, sp1Uuid);
130  TestClient sp1ChildClient = (TestClient)sp1Child.ControllingClient;
131 
132  sp1.AbsolutePosition = sp1Position;
133 
134  ScenePresence sp2 = SceneHelpers.AddScenePresence(sceneWest, sp2Uuid);
135  TestClient sp2Client = (TestClient)sp2.ControllingClient;
136  sp2.Flying = true;
137 
138  ScenePresence sp2Child = SceneHelpers.AddChildScenePresence(sceneEast, sp2Uuid);
139  TestClient sp2ChildClient = (TestClient)sp2Child.ControllingClient;
140 
141  sp2.AbsolutePosition = sp2Position;
142 
143  // We must update the scenes in order to make the root new root agents trigger position updates in their
144  // children.
145  sceneWest.Update(1);
146  sceneEast.Update(1);
147 
148  // Check child positions are correct.
149  Assert.AreEqual(
150  new Vector3(sp1Position.X + sceneEast.RegionInfo.RegionSizeX, sp1Position.Y, sp1Position.Z),
151  sp1ChildClient.SceneAgent.AbsolutePosition);
152 
153  Assert.AreEqual(
154  new Vector3(sp2Position.X - sceneWest.RegionInfo.RegionSizeX, sp2Position.Y, sp2Position.Z),
155  sp2ChildClient.SceneAgent.AbsolutePosition);
156 
157  string receivedSp1ChatMessage = "";
158  string receivedSp2ChatMessage = "";
159 
160  sp1ChildClient.OnReceivedChatMessage
161  += (message, type, fromPos, fromName, fromAgentID, ownerID, source, audible) => receivedSp1ChatMessage = message;
162  sp2ChildClient.OnReceivedChatMessage
163  += (message, type, fromPos, fromName, fromAgentID, ownerID, source, audible) => receivedSp2ChatMessage = message;
164 
165  TestUserInRange(sp1Client, "ello darling", ref receivedSp2ChatMessage);
166  TestUserInRange(sp2Client, "fantastic cats", ref receivedSp1ChatMessage);
167 
168  sp1Position = new Vector3(30, 128, 20);
169  sp1.AbsolutePosition = sp1Position;
170  sceneWest.Update(1);
171  sceneEast.Update(1);
172  Thread.Sleep(12000); // child updates are now time limited
173  sceneWest.Update(5);
174  sceneEast.Update(5);
175 
176  // Check child position is correct.
177  Assert.AreEqual(
178  new Vector3(sp1Position.X + sceneEast.RegionInfo.RegionSizeX, sp1Position.Y, sp1Position.Z),
179  sp1ChildClient.SceneAgent.AbsolutePosition);
180 
181  TestUserOutOfRange(sp1Client, "beef", ref receivedSp2ChatMessage);
182  TestUserOutOfRange(sp2Client, "lentils", ref receivedSp1ChatMessage);
183  }
184 
192  [Test]
194  {
195  TestHelpers.InMethod();
196  // TestHelpers.EnableLogging();
197 
198  UUID sp1Uuid = TestHelpers.ParseTail(0x11);
199  UUID sp2Uuid = TestHelpers.ParseTail(0x12);
200 
201  Vector3 sp1Position = new Vector3(128, 250, 20);
202  Vector3 sp2Position = new Vector3(128, 6, 20);
203 
204  SceneHelpers sh = new SceneHelpers();
205  TestScene sceneNorth = sh.SetupScene("sceneNorth", TestHelpers.ParseTail(0x1), 1000, 1000);
206  TestScene sceneSouth = sh.SetupScene("sceneSouth", TestHelpers.ParseTail(0x2), 1000, 1001);
207 
208  SetupNeighbourRegions(sceneNorth, sceneSouth);
209 
210  ScenePresence sp1 = SceneHelpers.AddScenePresence(sceneNorth, sp1Uuid);
211  TestClient sp1Client = (TestClient)sp1.ControllingClient;
212 
213  // If we don't set agents to flying, test will go wrong as they instantly fall to z = 0.
214  // TODO: May need to create special complete no-op test physics module rather than basic physics, since
215  // physics is irrelevant to this test.
216  sp1.Flying = true;
217 
218  // When sp1 logs in to sceneEast, it sets up a child agent in sceneNorth and informs the sp2 client to
219  // make the connection. For this test, will simplify this chain by making the connection directly.
220  ScenePresence sp1Child = SceneHelpers.AddChildScenePresence(sceneSouth, sp1Uuid);
221  TestClient sp1ChildClient = (TestClient)sp1Child.ControllingClient;
222 
223  sp1.AbsolutePosition = sp1Position;
224 
225  ScenePresence sp2 = SceneHelpers.AddScenePresence(sceneSouth, sp2Uuid);
226  TestClient sp2Client = (TestClient)sp2.ControllingClient;
227  sp2.Flying = true;
228 
229  ScenePresence sp2Child = SceneHelpers.AddChildScenePresence(sceneNorth, sp2Uuid);
230  TestClient sp2ChildClient = (TestClient)sp2Child.ControllingClient;
231 
232  sp2.AbsolutePosition = sp2Position;
233 
234  // We must update the scenes in order to make the root new root agents trigger position updates in their
235  // children.
236  sceneNorth.Update(1);
237  sceneSouth.Update(1);
238 
239  // Check child positions are correct.
240  Assert.AreEqual(
241  new Vector3(sp1Position.X, sp1Position.Y - sceneNorth.RegionInfo.RegionSizeY, sp1Position.Z),
242  sp1ChildClient.SceneAgent.AbsolutePosition);
243 
244  Assert.AreEqual(
245  new Vector3(sp2Position.X, sp2Position.Y + sceneSouth.RegionInfo.RegionSizeY, sp2Position.Z),
246  sp2ChildClient.SceneAgent.AbsolutePosition);
247 
248  string receivedSp1ChatMessage = "";
249  string receivedSp2ChatMessage = "";
250 
251  sp1ChildClient.OnReceivedChatMessage
252  += (message, type, fromPos, fromName, fromAgentID, ownerID, source, audible) => receivedSp1ChatMessage = message;
253  sp2ChildClient.OnReceivedChatMessage
254  += (message, type, fromPos, fromName, fromAgentID, ownerID, source, audible) => receivedSp2ChatMessage = message;
255 
256  TestUserInRange(sp1Client, "ello darling", ref receivedSp2ChatMessage);
257  TestUserInRange(sp2Client, "fantastic cats", ref receivedSp1ChatMessage);
258 
259  sp1Position = new Vector3(30, 128, 20);
260  sp1.AbsolutePosition = sp1Position;
261  sceneNorth.Update(1);
262  sceneSouth.Update(1);
263  Thread.Sleep(12000); // child updates are now time limited
264  sceneNorth.Update(5);
265  sceneSouth.Update(5);
266 
267  // Check child position is correct.
268  Assert.AreEqual(
269  new Vector3(sp1Position.X, sp1Position.Y - sceneNorth.RegionInfo.RegionSizeY, sp1Position.Z),
270  sp1ChildClient.SceneAgent.AbsolutePosition);
271 
272  TestUserOutOfRange(sp1Client, "beef", ref receivedSp2ChatMessage);
273  TestUserOutOfRange(sp2Client, "lentils", ref receivedSp1ChatMessage);
274  }
275 
276  private void TestUserInRange(TestClient speakClient, string testMessage, ref string receivedMessage)
277  {
278  receivedMessage = "";
279 
280  speakClient.Chat(0, ChatTypeEnum.Say, testMessage);
281 
282  Assert.AreEqual(testMessage, receivedMessage);
283  }
284 
285  private void TestUserOutOfRange(TestClient speakClient, string testMessage, ref string receivedMessage)
286  {
287  receivedMessage = "";
288 
289  speakClient.Chat(0, ChatTypeEnum.Say, testMessage);
290 
291  Assert.AreNotEqual(testMessage, receivedMessage);
292  }
293  }
294 }
override void Update(int frames)
Called to update the scene loop by a number of frames and until shutdown.
Definition: Scene.cs:1721
uint RegionSizeX
X dimension of the region.
Definition: RegionInfo.cs:161
uint RegionSizeY
X dimension of the region.
Definition: RegionInfo.cs:169
override Vector3 AbsolutePosition
Position of this avatar relative to the region the avatar is in
void TestInterRegionChatDistanceNorthSouth()
Tests chat between neighbour regions on the north-south axis
Helpers for setting up scenes.
Definition: SceneHelpers.cs:60
Interactive OpenSim region server
Definition: OpenSim.cs:55
static ScenePresence AddScenePresence(Scene scene, UUID agentId)
Add a root agent where the details of the agent connection (apart from the id) are unimportant for th...
void TestInterRegionChatDistanceEastWest()
Tests chat between neighbour regions on the east-west axis
static ScenePresence AddChildScenePresence(Scene scene, UUID agentId)