123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System.Collections;
- using NUnit.Framework;
- using Xilium.CefGlue.Common.ObjectBinding;
- namespace CefGlue.Tests.Javascript
- {
- public class NativeObjectMethodExecutorTests
- {
- class NativeTestObject
- {
- public object MethodWithParams(string param1, int param2)
- {
- return new object[] { param1, param2 };
- }
- public string MethodWithReturn()
- {
- return "this is the result";
- }
- public object[] MethodWithOptionalParams(params string[] optionalParams)
- {
- return optionalParams;
- }
- public object[] MethodWithFixedAndOptionalParams(string fixedParam, params int[] optionalParams)
- {
- return new object[] { fixedParam, optionalParams };
- }
- }
- private NativeTestObject nativeTestObject = new NativeTestObject();
- private NativeObject nativeObjectInfo;
- [OneTimeSetUp]
- protected void Setup()
- {
- var objectMembers = NativeObjectAnalyser.AnalyseObjectMembers(nativeTestObject);
- nativeObjectInfo = new NativeObject("test", nativeTestObject, objectMembers);
- }
- private object ExecuteMethod(string name, object[] args)
- {
- var method = nativeObjectInfo.GetNativeMethod(name);
- return NativeObjectMethodExecutor.ExecuteMethod(nativeTestObject, method, args);
- }
- [Test]
- public void MethodWithReturnIsExecuted()
- {
- var result = ExecuteMethod("methodWithReturn", new object[0]);
- Assert.AreEqual(nativeTestObject.MethodWithReturn(), result);
- }
- [Test]
- public void MethodWithParamsIsExecuted()
- {
- const string Arg1 = "arg1";
- const int Arg2 = 2;
- var result = (object[]) ExecuteMethod("methodWithParams", new object[] { Arg1, Arg2 } );
- Assert.AreEqual(2, result.Length);
- Assert.AreEqual(Arg1, result[0]);
- Assert.AreEqual(Arg2, result[1]);
- }
- [Test]
- public void MethodWithOptionalParams()
- {
- const string Arg1 = "arg1";
- const string Arg2 = "arg2";
- var result = (object[])ExecuteMethod("methodWithOptionalParams", new object[] { Arg1, Arg2 });
- Assert.AreEqual(2, result.Length);
- Assert.AreEqual(Arg1, result[0]);
- Assert.AreEqual(Arg2, result[1]);
- result = (object[])ExecuteMethod("methodWithOptionalParams", new object[0]);
- Assert.AreEqual(0, result.Length);
- }
- [Test]
- public void MethodWithFixedAndOptionalParams()
- {
- const string Arg1 = "arg1";
- var arg2 = new int[] { 1, 2 , 3 };
- var result = (object[])ExecuteMethod("methodWithFixedAndOptionalParams", new object[] { Arg1, 1, 2, 3 });
- Assert.AreEqual(2, result.Length);
- Assert.AreEqual(Arg1, result[0]);
- CollectionAssert.AreEqual(arg2, (IEnumerable) result[1]);
- result = (object[])ExecuteMethod("methodWithFixedAndOptionalParams", new object[] { Arg1 });
- Assert.AreEqual(2, result.Length);
- Assert.AreEqual(Arg1, result[0]);
- Assert.AreEqual(0, ((int[]) result[1]).Length);
- }
- }
- }
|