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.7 KiB

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