EventsTests.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using NUnit.Framework;
  2. using System.Threading.Tasks;
  3. using Xilium.CefGlue.Common.Events;
  4. namespace CefGlue.Tests.Events
  5. {
  6. public class EventsTests : TestBase
  7. {
  8. private const string Url = "data:";
  9. [Test]
  10. public async Task LoadStartIsFired()
  11. {
  12. var taskCompletionSource = new TaskCompletionSource<bool>();
  13. void OnBrowserLoadStart(object sender, LoadStartEventArgs e)
  14. {
  15. if (e.Frame.Url.StartsWith(Url))
  16. {
  17. taskCompletionSource.SetResult(true);
  18. }
  19. }
  20. Browser.LoadStart += OnBrowserLoadStart;
  21. await Browser.LoadContent("<html/>");
  22. await taskCompletionSource.Task;
  23. }
  24. [Test]
  25. public async Task LoadEndIsFired()
  26. {
  27. var taskCompletionSource = new TaskCompletionSource<bool>();
  28. void OnBrowserLoadEnd(object sender, LoadEndEventArgs e)
  29. {
  30. if (e.Frame.Url.StartsWith(Url))
  31. {
  32. taskCompletionSource.SetResult(true);
  33. }
  34. }
  35. Browser.LoadEnd += OnBrowserLoadEnd;
  36. await Browser.LoadContent("<html/>");
  37. await taskCompletionSource.Task;
  38. }
  39. [Test]
  40. public async Task LoadErrorIsFired()
  41. {
  42. var taskCompletionSource = new TaskCompletionSource<bool>();
  43. void OnBrowserLoadError(object sender, LoadErrorEventArgs e)
  44. {
  45. taskCompletionSource.SetResult(true);
  46. }
  47. Browser.LoadError += OnBrowserLoadError;
  48. Browser.Address = "http://0.0.0.0"; // navigate to an invalid url
  49. await taskCompletionSource.Task;
  50. }
  51. [Test]
  52. public async Task LoadingStateChangeIsFired()
  53. {
  54. var completed = false;
  55. var loadStateChangeCount = 0;
  56. var taskCompletionSource = new TaskCompletionSource<bool>();
  57. void OnLoadingStateChange(object sender, LoadingStateChangeEventArgs e)
  58. {
  59. loadStateChangeCount++;
  60. if (loadStateChangeCount > 1 && !completed)
  61. {
  62. completed = true;
  63. taskCompletionSource.SetResult(true);
  64. }
  65. }
  66. Browser.LoadingStateChange += OnLoadingStateChange;
  67. await Browser.LoadContent("<html/>");
  68. await taskCompletionSource.Task;
  69. }
  70. [Test]
  71. public async Task AddressChangedIsFired()
  72. {
  73. var taskCompletionSource = new TaskCompletionSource<bool>();
  74. void OnAddressChanged(object sender, string address)
  75. {
  76. if (address.StartsWith(Url))
  77. {
  78. taskCompletionSource.SetResult(true);
  79. }
  80. }
  81. Browser.AddressChanged += OnAddressChanged;
  82. await Browser.LoadContent("<html/>");
  83. await taskCompletionSource.Task;
  84. }
  85. [Test]
  86. public async Task ConsoleMessageIsFired()
  87. {
  88. const string ConsoleMessage = "this is a test";
  89. var taskCompletionSource = new TaskCompletionSource<string>();
  90. void OnConsoleMessage(object sender, ConsoleMessageEventArgs e)
  91. {
  92. taskCompletionSource.SetResult(e.Message);
  93. }
  94. Browser.ConsoleMessage += OnConsoleMessage;
  95. await Browser.LoadContent($"<html><script>console.log('{ConsoleMessage}')</script></html>");
  96. var message = await taskCompletionSource.Task;
  97. Assert.AreEqual(ConsoleMessage, message);
  98. }
  99. [Test]
  100. public async Task JavascriptContextCreatedAndReleasedAreFired()
  101. {
  102. var contextCreatedCompletionSource = new TaskCompletionSource<bool>();
  103. var contextReleasedCompletionSource = new TaskCompletionSource<bool>();
  104. void OnJavascriptContextCreated(object sender, JavascriptContextLifetimeEventArgs e)
  105. {
  106. contextCreatedCompletionSource.SetResult(true);
  107. }
  108. void OnJavascriptContextReleased(object sender, JavascriptContextLifetimeEventArgs e)
  109. {
  110. contextReleasedCompletionSource.SetResult(true);
  111. }
  112. Browser.JavascriptContextCreated += OnJavascriptContextCreated;
  113. Browser.JavascriptContextReleased += OnJavascriptContextReleased;
  114. await Browser.LoadContent($"<script>1+1</script>");
  115. await contextCreatedCompletionSource.Task;
  116. await Browser.LoadContent($"<html/>");
  117. await contextReleasedCompletionSource.Task;
  118. }
  119. [Test]
  120. public async Task JavascriptUncaughExceptionIsFired()
  121. {
  122. const string ExceptionMessage = "ups";
  123. var taskCompletionSource = new TaskCompletionSource<string>();
  124. void OnJavascriptUncaughException(object sender, JavascriptUncaughtExceptionEventArgs e)
  125. {
  126. taskCompletionSource.SetResult(e.Message);
  127. }
  128. Browser.JavascriptUncaughException += OnJavascriptUncaughException;
  129. await Browser.LoadContent($"<script>throw new Error('{ExceptionMessage}')</script>");
  130. var message = await taskCompletionSource.Task;
  131. StringAssert.Contains(ExceptionMessage, message);
  132. }
  133. }
  134. }