NativeObjectInteropTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Threading.Tasks;
  3. using NUnit.Framework;
  4. namespace CefGlue.Tests.Javascript
  5. {
  6. public class NativeObjectInteropTests : TestBase
  7. {
  8. const string ObjName = "nativeObj";
  9. private NativeObject nativeObject;
  10. class Person
  11. {
  12. public string Name = null;
  13. public int Age = 0;
  14. }
  15. class NativeObject
  16. {
  17. public event Action TestCalled;
  18. public void Test()
  19. {
  20. TestCalled?.Invoke();
  21. }
  22. public event Action<object[]> MethodWithParamsCalled;
  23. public void MethodWithParams(string param1, int param2)
  24. {
  25. MethodWithParamsCalled?.Invoke(new object[] { param1, param2 });
  26. }
  27. public event Action<object[]> MethodWithObjectParamCalled;
  28. public void MethodWithObjectParam(Person param)
  29. {
  30. MethodWithObjectParamCalled?.Invoke(new object[] { param });
  31. }
  32. public string MethodWithReturn()
  33. {
  34. return "this is the result";
  35. }
  36. }
  37. private Task Load()
  38. {
  39. return Browser.LoadContent($"<script></script>");
  40. }
  41. private void Execute(string script)
  42. {
  43. Browser.ExecuteJavaScript("(function() { " + script + " })()");
  44. }
  45. protected async override Task ExtraSetup()
  46. {
  47. nativeObject = new NativeObject();
  48. Browser.RegisterJavascriptObject(nativeObject, ObjName);
  49. await Load();
  50. await base.ExtraSetup();
  51. }
  52. [Test]
  53. public async Task ObjectIsRegistered()
  54. {
  55. var nativeObjectDefined = await EvaluateJavascript<bool>($"return window['{ObjName}'] !== null");
  56. Assert.IsTrue(nativeObjectDefined);
  57. var unregisteredObjectDefined = await EvaluateJavascript<bool>($"return window['foo'] === null");
  58. Assert.IsFalse(unregisteredObjectDefined);
  59. }
  60. [Test]
  61. public async Task NativeObjectMethodIsCalled()
  62. {
  63. var taskCompletionSource = new TaskCompletionSource<bool>();
  64. nativeObject.TestCalled += () => taskCompletionSource.SetResult(true);
  65. Execute($"{ObjName}.test()");
  66. await taskCompletionSource.Task;
  67. }
  68. [Test]
  69. public async Task NativeObjectMethodParamsArePassed()
  70. {
  71. const string Arg1 = "test";
  72. const int Arg2 = 5;
  73. var taskCompletionSource = new TaskCompletionSource<object[]>();
  74. nativeObject.MethodWithParamsCalled += (args) => taskCompletionSource.SetResult(args);
  75. Execute($"{ObjName}.methodWithParams('{Arg1}', {Arg2})");
  76. var result = await taskCompletionSource.Task;
  77. Assert.AreEqual(2, result.Length);
  78. Assert.AreEqual(Arg1, result[0]);
  79. Assert.AreEqual(Arg2, result[1]);
  80. }
  81. [Test]
  82. public async Task NativeObjectMethodWithObjectParamIsPassed()
  83. {
  84. var taskCompletionSource = new TaskCompletionSource<object[]>();
  85. nativeObject.MethodWithObjectParamCalled += (args) => taskCompletionSource.SetResult(args);
  86. Execute($"{ObjName}.methodWithObjectParam({{'Name': 'cef', 'Age': 10 }})");
  87. var result = await taskCompletionSource.Task;
  88. Assert.AreEqual(1, result.Length);
  89. Assert.AreEqual(typeof(Person), result[0].GetType());
  90. var arg = (Person) result[0];
  91. Assert.AreEqual("cef", arg.Name);
  92. Assert.AreEqual(10, arg.Age);
  93. }
  94. [Test]
  95. public async Task NativeObjectMethodResultIsReturned()
  96. {
  97. var taskCompletionSource = new TaskCompletionSource<object[]>();
  98. nativeObject.MethodWithParamsCalled += (args) => taskCompletionSource.SetResult(args);
  99. Execute($"{ObjName}.methodWithReturn().then(r => {ObjName}.methodWithParams(r, 0));");
  100. var result = await taskCompletionSource.Task;
  101. Assert.AreEqual(nativeObject.MethodWithReturn(), result[0]);
  102. }
  103. }
  104. }