FriendModuleTests.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 Nini.Config;
  30. using NUnit.Framework;
  31. using OpenMetaverse;
  32. using OpenSim.Data.Null;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.CoreModules.Avatar.Friends;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Tests.Common;
  37. using OpenSim.Tests.Common.Mock;
  38. namespace OpenSim.Region.CoreModules.Avatar.Friends.Tests
  39. {
  40. [TestFixture]
  41. public class FriendsModuleTests
  42. {
  43. private FriendsModule m_fm;
  44. private TestScene m_scene;
  45. [TestFixtureSetUp]
  46. public void FixtureInit()
  47. {
  48. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  49. Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
  50. }
  51. [TestFixtureTearDown]
  52. public void TearDown()
  53. {
  54. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  55. // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
  56. // tests really shouldn't).
  57. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  58. }
  59. [SetUp]
  60. public void Init()
  61. {
  62. // We must clear friends data between tests since Data.Null holds it in static properties. This is necessary
  63. // so that different services and simulator can share the data in standalone mode. This is pretty horrible
  64. // effectively the statics are global variables.
  65. NullFriendsData.Clear();
  66. IConfigSource config = new IniConfigSource();
  67. config.AddConfig("Modules");
  68. // Not strictly necessary since FriendsModule assumes it is the default (!)
  69. config.Configs["Modules"].Set("FriendsModule", "FriendsModule");
  70. config.AddConfig("Friends");
  71. config.Configs["Friends"].Set("Connector", "OpenSim.Services.FriendsService.dll");
  72. config.AddConfig("FriendsService");
  73. config.Configs["FriendsService"].Set("StorageProvider", "OpenSim.Data.Null.dll");
  74. m_scene = new SceneHelpers().SetupScene();
  75. m_fm = new FriendsModule();
  76. SceneHelpers.SetupSceneModules(m_scene, config, m_fm);
  77. }
  78. [Test]
  79. public void TestLoginWithNoFriends()
  80. {
  81. TestHelpers.InMethod();
  82. // log4net.Config.XmlConfigurator.Configure();
  83. UUID userId = TestHelpers.ParseTail(0x1);
  84. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  85. Assert.That(((TestClient)sp.ControllingClient).ReceivedOfflineNotifications.Count, Is.EqualTo(0));
  86. Assert.That(((TestClient)sp.ControllingClient).ReceivedOnlineNotifications.Count, Is.EqualTo(0));
  87. }
  88. [Test]
  89. public void TestLoginWithOfflineFriends()
  90. {
  91. TestHelpers.InMethod();
  92. // log4net.Config.XmlConfigurator.Configure();
  93. UUID user1Id = TestHelpers.ParseTail(0x1);
  94. UUID user2Id = TestHelpers.ParseTail(0x2);
  95. // UserAccountHelpers.CreateUserWithInventory(m_scene, user1Id);
  96. // UserAccountHelpers.CreateUserWithInventory(m_scene, user2Id);
  97. //
  98. // m_fm.AddFriendship(user1Id, user2Id);
  99. ScenePresence sp1 = SceneHelpers.AddScenePresence(m_scene, user1Id);
  100. ScenePresence sp2 = SceneHelpers.AddScenePresence(m_scene, user2Id);
  101. m_fm.AddFriendship(sp1.ControllingClient, user2Id);
  102. // Not necessary for this test. CanSeeOnline is automatically granted.
  103. // m_fm.GrantRights(sp1.ControllingClient, user2Id, (int)FriendRights.CanSeeOnline);
  104. // We must logout from the client end so that the presence service is correctly updated by the presence
  105. // detector. This is listening to the OnConnectionClosed event on the client.
  106. ((TestClient)sp1.ControllingClient).Logout();
  107. ((TestClient)sp2.ControllingClient).Logout();
  108. // m_scene.RemoveClient(sp1.UUID, true);
  109. // m_scene.RemoveClient(sp2.UUID, true);
  110. ScenePresence sp1Redux = SceneHelpers.AddScenePresence(m_scene, user1Id);
  111. // We don't expect to receive notifications of offline friends on login, just online.
  112. Assert.That(((TestClient)sp1Redux.ControllingClient).ReceivedOfflineNotifications.Count, Is.EqualTo(0));
  113. Assert.That(((TestClient)sp1Redux.ControllingClient).ReceivedOnlineNotifications.Count, Is.EqualTo(0));
  114. }
  115. [Test]
  116. public void TestLoginWithOnlineFriends()
  117. {
  118. TestHelpers.InMethod();
  119. // log4net.Config.XmlConfigurator.Configure();
  120. UUID user1Id = TestHelpers.ParseTail(0x1);
  121. UUID user2Id = TestHelpers.ParseTail(0x2);
  122. // UserAccountHelpers.CreateUserWithInventory(m_scene, user1Id);
  123. // UserAccountHelpers.CreateUserWithInventory(m_scene, user2Id);
  124. //
  125. // m_fm.AddFriendship(user1Id, user2Id);
  126. ScenePresence sp1 = SceneHelpers.AddScenePresence(m_scene, user1Id);
  127. ScenePresence sp2 = SceneHelpers.AddScenePresence(m_scene, user2Id);
  128. m_fm.AddFriendship(sp1.ControllingClient, user2Id);
  129. // Not necessary for this test. CanSeeOnline is automatically granted.
  130. // m_fm.GrantRights(sp1.ControllingClient, user2Id, (int)FriendRights.CanSeeOnline);
  131. // We must logout from the client end so that the presence service is correctly updated by the presence
  132. // detector. This is listening to the OnConnectionClosed event on the client.
  133. // ((TestClient)sp1.ControllingClient).Logout();
  134. ((TestClient)sp2.ControllingClient).Logout();
  135. // m_scene.RemoveClient(user2Id, true);
  136. ScenePresence sp2Redux = SceneHelpers.AddScenePresence(m_scene, user2Id);
  137. Assert.That(((TestClient)sp2Redux.ControllingClient).ReceivedOfflineNotifications.Count, Is.EqualTo(0));
  138. Assert.That(((TestClient)sp2Redux.ControllingClient).ReceivedOnlineNotifications.Count, Is.EqualTo(1));
  139. }
  140. [Test]
  141. public void TestAddFriendshipWhileOnline()
  142. {
  143. TestHelpers.InMethod();
  144. // log4net.Config.XmlConfigurator.Configure();
  145. UUID userId = TestHelpers.ParseTail(0x1);
  146. UUID user2Id = TestHelpers.ParseTail(0x2);
  147. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, userId);
  148. SceneHelpers.AddScenePresence(m_scene, user2Id);
  149. // This fiendship is two-way but without a connector, only the first user will receive the online
  150. // notification.
  151. m_fm.AddFriendship(sp.ControllingClient, user2Id);
  152. Assert.That(((TestClient)sp.ControllingClient).ReceivedOfflineNotifications.Count, Is.EqualTo(0));
  153. Assert.That(((TestClient)sp.ControllingClient).ReceivedOnlineNotifications.Count, Is.EqualTo(1));
  154. }
  155. [Test]
  156. public void TestRemoveFriendshipWhileOnline()
  157. {
  158. TestHelpers.InMethod();
  159. // log4net.Config.XmlConfigurator.Configure();
  160. UUID user1Id = TestHelpers.ParseTail(0x1);
  161. UUID user2Id = TestHelpers.ParseTail(0x2);
  162. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, user1Id);
  163. SceneHelpers.AddScenePresence(m_scene, user2Id);
  164. m_fm.AddFriendship(sp.ControllingClient, user2Id);
  165. m_fm.RemoveFriendship(sp.ControllingClient, user2Id);
  166. TestClient user1Client = sp.ControllingClient as TestClient;
  167. Assert.That(user1Client.ReceivedFriendshipTerminations.Count, Is.EqualTo(1));
  168. Assert.That(user1Client.ReceivedFriendshipTerminations[0], Is.EqualTo(user2Id));
  169. }
  170. }
  171. }