FriendModuleTests.cs 9.3 KB

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