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 3.5 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace Discord.Net.Tests
  7. {
  8. [TestClass]
  9. public class Tests
  10. {
  11. private const int EventTimeout = 5000; //Max time in milliseconds to wait for an event response from our test actions
  12. private static Settings _settings;
  13. private static DiscordClient _bot1, _bot2;
  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. _settings = Settings.Load();
  21. _random = new Random();
  22. _bot1 = new DiscordClient();
  23. _bot2 = new DiscordClient();
  24. _bot1.Connect(_settings.User1.Email, _settings.User1.Password).Wait();
  25. _bot2.Connect(_settings.User2.Email, _settings.User2.Password).Wait();
  26. //Cleanup existing servers
  27. Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
  28. Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray());
  29. //Create new server and invite other bot to it
  30. _testServer = _bot1.CreateServer("Discord.Net Testing", Regions.US_East).Result;
  31. _testServerChannel = _testServer.DefaultChannel;
  32. Invite invite = _bot1.CreateInvite(_testServer, 60, 1, false, false).Result;
  33. _bot2.AcceptInvite(invite).Wait();
  34. }
  35. [TestMethod]
  36. public void TestSendMessage()
  37. {
  38. string text = $"test_{_random.Next()}";
  39. AssertEvent<DiscordClient.MessageEventArgs>(
  40. "MessageCreated event never received",
  41. () => _bot1.SendMessage(_testServerChannel, text),
  42. x => _bot2.MessageCreated += x,
  43. x => _bot2.MessageCreated -= x,
  44. (s, e) => e.Message.Text == text);
  45. }
  46. [TestMethod]
  47. public void TestCreateTextRoom()
  48. => TestCreateRoom(ChannelTypes.Text);
  49. [TestMethod]
  50. public void TestCreateVoiceRoom()
  51. => TestCreateRoom(ChannelTypes.Voice);
  52. private void TestCreateRoom(string type)
  53. {
  54. Channel channel = null;
  55. string name = $"#test_{_random.Next()}";
  56. AssertEvent<DiscordClient.ChannelEventArgs>(
  57. "ChannelCreated event never received",
  58. () => channel = _bot1.CreateChannel(_testServer, name.Substring(1), type).Result,
  59. x => _bot2.ChannelCreated += x,
  60. x => _bot2.ChannelCreated -= x,
  61. (s, e) => e.Channel.Name == name);
  62. AssertEvent<DiscordClient.ChannelEventArgs>(
  63. "ChannelDestroyed event never received",
  64. () => _bot1.DestroyChannel(channel),
  65. x => _bot2.ChannelDestroyed += x,
  66. x => _bot2.ChannelDestroyed -= x,
  67. (s, e) => e.Channel.Name == name);
  68. }
  69. [ClassCleanup]
  70. public static void Cleanup()
  71. {
  72. if (_bot1.IsConnected)
  73. Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
  74. if (_bot2.IsConnected)
  75. Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray());
  76. _bot1.Disconnect().Wait();
  77. _bot2.Disconnect().Wait();
  78. }
  79. private static void AssertEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
  80. {
  81. ManualResetEventSlim trigger = new ManualResetEventSlim(false);
  82. bool result = false;
  83. EventHandler<TArgs> handler = (s, e) =>
  84. {
  85. if (test != null)
  86. {
  87. result |= test(s, e);
  88. trigger.Set();
  89. }
  90. else
  91. result = true;
  92. };
  93. addEvent(handler);
  94. action();
  95. trigger.Wait(EventTimeout);
  96. removeEvent(handler);
  97. Assert.AreEqual(true, result, msg);
  98. }
  99. }
  100. }