NativeObjectMethodExecutorTests.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections;
  2. using NUnit.Framework;
  3. using Xilium.CefGlue.Common.ObjectBinding;
  4. namespace CefGlue.Tests.Javascript
  5. {
  6. public class NativeObjectMethodExecutorTests
  7. {
  8. class NativeTestObject
  9. {
  10. public object MethodWithParams(string param1, int param2)
  11. {
  12. return new object[] { param1, param2 };
  13. }
  14. public string MethodWithReturn()
  15. {
  16. return "this is the result";
  17. }
  18. public object[] MethodWithOptionalParams(params string[] optionalParams)
  19. {
  20. return optionalParams;
  21. }
  22. public object[] MethodWithFixedAndOptionalParams(string fixedParam, params int[] optionalParams)
  23. {
  24. return new object[] { fixedParam, optionalParams };
  25. }
  26. }
  27. private NativeTestObject nativeTestObject = new NativeTestObject();
  28. private NativeObject nativeObjectInfo;
  29. [OneTimeSetUp]
  30. protected void Setup()
  31. {
  32. var objectMembers = NativeObjectAnalyser.AnalyseObjectMembers(nativeTestObject);
  33. nativeObjectInfo = new NativeObject("test", nativeTestObject, objectMembers);
  34. }
  35. private object ExecuteMethod(string name, object[] args)
  36. {
  37. var method = nativeObjectInfo.GetNativeMethod(name);
  38. return NativeObjectMethodExecutor.ExecuteMethod(nativeTestObject, method, args);
  39. }
  40. [Test]
  41. public void MethodWithReturnIsExecuted()
  42. {
  43. var result = ExecuteMethod("methodWithReturn", new object[0]);
  44. Assert.AreEqual(nativeTestObject.MethodWithReturn(), result);
  45. }
  46. [Test]
  47. public void MethodWithParamsIsExecuted()
  48. {
  49. const string Arg1 = "arg1";
  50. const int Arg2 = 2;
  51. var result = (object[]) ExecuteMethod("methodWithParams", new object[] { Arg1, Arg2 } );
  52. Assert.AreEqual(2, result.Length);
  53. Assert.AreEqual(Arg1, result[0]);
  54. Assert.AreEqual(Arg2, result[1]);
  55. }
  56. [Test]
  57. public void MethodWithOptionalParams()
  58. {
  59. const string Arg1 = "arg1";
  60. const string Arg2 = "arg2";
  61. var result = (object[])ExecuteMethod("methodWithOptionalParams", new object[] { Arg1, Arg2 });
  62. Assert.AreEqual(2, result.Length);
  63. Assert.AreEqual(Arg1, result[0]);
  64. Assert.AreEqual(Arg2, result[1]);
  65. result = (object[])ExecuteMethod("methodWithOptionalParams", new object[0]);
  66. Assert.AreEqual(0, result.Length);
  67. }
  68. [Test]
  69. public void MethodWithFixedAndOptionalParams()
  70. {
  71. const string Arg1 = "arg1";
  72. var arg2 = new int[] { 1, 2 , 3 };
  73. var result = (object[])ExecuteMethod("methodWithFixedAndOptionalParams", new object[] { Arg1, 1, 2, 3 });
  74. Assert.AreEqual(2, result.Length);
  75. Assert.AreEqual(Arg1, result[0]);
  76. CollectionAssert.AreEqual(arg2, (IEnumerable) result[1]);
  77. result = (object[])ExecuteMethod("methodWithFixedAndOptionalParams", new object[] { Arg1 });
  78. Assert.AreEqual(2, result.Length);
  79. Assert.AreEqual(Arg1, result[0]);
  80. Assert.AreEqual(0, ((int[]) result[1]).Length);
  81. }
  82. }
  83. }