You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tests.cs 5.6 kB

10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace Discord.Net.Tests
  8. {
  9. [TestClass]
  10. public class Tests
  11. {
  12. private const int EventTimeout = 5000; //Max time in milliseconds to wait for an event response from our test actions
  13. private static DiscordClient _hostClient, _targetBot, _observerBot;
  14. private static Server _testServer;
  15. private static Channel _testServerChannel;
  16. private static Random _random;
  17. [ClassInitialize]
  18. public static void Initialize(TestContext testContext)
  19. {
  20. var settings = Settings.Instance;
  21. _random = new Random();
  22. _hostClient = new DiscordClient();
  23. _targetBot = new DiscordClient();
  24. _observerBot = new DiscordClient();
  25. _hostClient.Connect(settings.User1.Email, settings.User1.Password).Wait();
  26. _targetBot.Connect(settings.User2.Email, settings.User2.Password).Wait();
  27. _observerBot.Connect(settings.User3.Email, settings.User3.Password).Wait();
  28. //Cleanup existing servers
  29. WaitMany(
  30. _hostClient.Servers.Select(x => _hostClient.LeaveServer(x)),
  31. _targetBot.Servers.Select(x => _targetBot.LeaveServer(x)),
  32. _observerBot.Servers.Select(x => _observerBot.LeaveServer(x)));
  33. //Create new server and invite the other bots to it
  34. _testServer = _hostClient.CreateServer("Discord.Net Testing", Regions.US_East).Result;
  35. _testServerChannel = _testServer.DefaultChannel;
  36. Invite invite = _hostClient.CreateInvite(_testServer, 60, 1, false, false).Result;
  37. WaitAll(
  38. _targetBot.AcceptInvite(invite),
  39. _observerBot.AcceptInvite(invite));
  40. }
  41. //Channels
  42. [TestMethod]
  43. public void TestCreateTextChannel()
  44. => TestCreateChannel(ChannelTypes.Text);
  45. [TestMethod]
  46. public void TestCreateVoiceChannel()
  47. => TestCreateChannel(ChannelTypes.Voice);
  48. private void TestCreateChannel(string type)
  49. {
  50. Channel channel = null;
  51. string name = $"#test_{_random.Next()}";
  52. AssertEvent<ChannelEventArgs>(
  53. "ChannelCreated event never received",
  54. () => channel = _hostClient.CreateChannel(_testServer, name.Substring(1), type).Result,
  55. x => _targetBot.ChannelCreated += x,
  56. x => _targetBot.ChannelCreated -= x,
  57. (s, e) => e.Channel.Name == name);
  58. AssertEvent<ChannelEventArgs>(
  59. "ChannelDestroyed event never received",
  60. () => _hostClient.DestroyChannel(channel),
  61. x => _targetBot.ChannelDestroyed += x,
  62. x => _targetBot.ChannelDestroyed -= x,
  63. (s, e) => e.Channel.Name == name);
  64. }
  65. [TestMethod]
  66. [ExpectedException(typeof(InvalidOperationException))]
  67. public async Task TestCreateChannel_NoName()
  68. {
  69. await _hostClient.CreateChannel(_testServer, $"", ChannelTypes.Text);
  70. }
  71. [TestMethod]
  72. [ExpectedException(typeof(InvalidOperationException))]
  73. public async Task TestCreateChannel_NoType()
  74. {
  75. string name = $"#test_{_random.Next()}";
  76. await _hostClient.CreateChannel(_testServer, $"", "");
  77. }
  78. [TestMethod]
  79. [ExpectedException(typeof(InvalidOperationException))]
  80. public async Task TestCreateChannel_BadType()
  81. {
  82. string name = $"#test_{_random.Next()}";
  83. await _hostClient.CreateChannel(_testServer, $"", "badtype");
  84. }
  85. //Messages
  86. [TestMethod]
  87. public void TestSendMessage()
  88. {
  89. string text = $"test_{_random.Next()}";
  90. AssertEvent<MessageEventArgs>(
  91. "MessageCreated event never received",
  92. () => _hostClient.SendMessage(_testServerChannel, text),
  93. x => _targetBot.MessageCreated += x,
  94. x => _targetBot.MessageCreated -= x,
  95. (s, e) => e.Message.Text == text);
  96. }
  97. [ClassCleanup]
  98. public static void Cleanup()
  99. {
  100. WaitMany(
  101. _hostClient.State == DiscordClientState.Connected ? _hostClient.Servers.Select(x => _hostClient.LeaveServer(x)) : null,
  102. _targetBot.State == DiscordClientState.Connected ? _targetBot.Servers.Select(x => _targetBot.LeaveServer(x)) : null,
  103. _observerBot.State == DiscordClientState.Connected ? _observerBot.Servers.Select(x => _observerBot.LeaveServer(x)) : null);
  104. WaitAll(
  105. _hostClient.Disconnect(),
  106. _targetBot.Disconnect(),
  107. _observerBot.Disconnect());
  108. }
  109. private static void AssertEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
  110. {
  111. AssertEvent(msg, action, addEvent, removeEvent, test, true);
  112. }
  113. private static void AssertNoEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
  114. {
  115. AssertEvent(msg, action, addEvent, removeEvent, test, false);
  116. }
  117. private static void AssertEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test, bool assertTrue)
  118. {
  119. ManualResetEventSlim trigger = new ManualResetEventSlim(false);
  120. bool result = false;
  121. EventHandler<TArgs> handler = (s, e) =>
  122. {
  123. if (test != null)
  124. {
  125. result |= test(s, e);
  126. trigger.Set();
  127. }
  128. else
  129. result = true;
  130. };
  131. addEvent(handler);
  132. action();
  133. trigger.Wait(EventTimeout);
  134. removeEvent(handler);
  135. Assert.AreEqual(assertTrue, result, msg);
  136. }
  137. private static void WaitAll(params Task[] tasks)
  138. {
  139. Task.WaitAll(tasks);
  140. }
  141. private static void WaitAll(IEnumerable<Task> tasks)
  142. {
  143. Task.WaitAll(tasks.ToArray());
  144. }
  145. private static void WaitMany(params IEnumerable<Task>[] tasks)
  146. {
  147. Task.WaitAll(tasks.Where(x => x != null).SelectMany(x => x).ToArray());
  148. }
  149. }
  150. }