Exemplo n.º 1
0
        public void SerializesDotNetObjectWrappersInKnownFormat()
        {
            // Arrange
            var runtime = new TestJSInProcessRuntime {
                NextResultJson = null
            };
            var obj1 = new object();
            var obj2 = new object();
            var obj3 = new object();

            // Act
            // Showing we can pass the DotNetObject either as top-level args or nested
            var syncResult = runtime.Invoke <object>("test identifier",
                                                     new DotNetObjectRef(obj1),
                                                     new Dictionary <string, object>
            {
                { "obj2", new DotNetObjectRef(obj2) },
                { "obj3", new DotNetObjectRef(obj3) }
            });

            // Assert: Handles null result string
            Assert.Null(syncResult);

            // Assert: Serialized as expected
            var call = runtime.InvokeCalls.Single();

            Assert.Equal("test identifier", call.Identifier);
            Assert.Equal("[\"__dotNetObject:1\",{\"obj2\":\"__dotNetObject:2\",\"obj3\":\"__dotNetObject:3\"}]", call.ArgsJson);

            // Assert: Objects were tracked
            Assert.Same(obj1, runtime.ArgSerializerStrategy.FindDotNetObject(1));
            Assert.Same(obj2, runtime.ArgSerializerStrategy.FindDotNetObject(2));
            Assert.Same(obj3, runtime.ArgSerializerStrategy.FindDotNetObject(3));
        }
Exemplo n.º 2
0
        public void JSInProcessObjectReference_Dispose_DisallowsFurtherInteropCalls()
        {
            // Arrange
            var jsRuntime = new TestJSInProcessRuntime();
            var jsObject  = new JSInProcessObjectReference(jsRuntime, 0);

            // Act
            _ = jsObject.DisposeAsync();

            // Assert
            Assert.Throws <ObjectDisposedException>(() => jsObject.Invoke <object>("test", "arg1", "arg2"));
        }
Exemplo n.º 3
0
        public void JSInProcessObjectReference_Invoke_CallsUnderlyingJSRuntimeInvoke()
        {
            // Arrange
            var jsRuntime = new TestJSInProcessRuntime();
            var jsObject  = new JSInProcessObjectReference(jsRuntime, 0);

            // Act
            jsObject.Invoke <object>("test", "arg1", "arg2");

            // Assert
            Assert.Equal(1, jsRuntime.InvokeJSInvocationCount);
        }
Exemplo n.º 4
0
        public void SyncCallResultCanIncludeDotNetObjects()
        {
            // Arrange
            var runtime = new TestJSInProcessRuntime
            {
                NextResultJson = "[\"__dotNetObject:2\",\"__dotNetObject:1\"]"
            };
            var obj1 = new object();
            var obj2 = new object();

            // Act
            var syncResult = runtime.Invoke <object[]>("test identifier",
                                                       new DotNetObjectRef(obj1),
                                                       "some other arg",
                                                       new DotNetObjectRef(obj2));
            var call = runtime.InvokeCalls.Single();

            // Assert
            Assert.Equal(new[] { obj2, obj1 }, syncResult);
        }
Exemplo n.º 5
0
        public void DispatchesSyncCallsAndDeserializesResults()
        {
            // Arrange
            var runtime = new TestJSInProcessRuntime
            {
                NextResultJson = "{\"intValue\":123,\"stringValue\":\"Hello\"}"
            };

            JSRuntime.SetCurrentJSRuntime(runtime);

            // Act
            var syncResult = runtime.Invoke <TestDTO>("test identifier 1", "arg1", 123, true);
            var call       = runtime.InvokeCalls.Single();

            // Assert
            Assert.Equal(123, syncResult.IntValue);
            Assert.Equal("Hello", syncResult.StringValue);
            Assert.Equal("test identifier 1", call.Identifier);
            Assert.Equal("[\"arg1\",123,true]", call.ArgsJson);
        }
Exemplo n.º 6
0
        public void DispatchesSyncCallsAndDeserializesResults()
        {
            // Arrange
            var runtime = new TestJSInProcessRuntime
            {
                NextResultJson = Json.Serialize(
                    new TestDTO {
                    IntValue = 123, StringValue = "Hello"
                })
            };

            // Act
            var syncResult = runtime.Invoke <TestDTO>("test identifier 1", "arg1", 123, true);
            var call       = runtime.InvokeCalls.Single();

            // Assert
            Assert.Equal(123, syncResult.IntValue);
            Assert.Equal("Hello", syncResult.StringValue);
            Assert.Equal("test identifier 1", call.Identifier);
            Assert.Equal("[\"arg1\",123,true]", call.ArgsJson);
        }
Exemplo n.º 7
0
        public void SyncCallResultCanIncludeDotNetObjects()
        {
            // Arrange
            var runtime = new TestJSInProcessRuntime
            {
                NextResultJson = "[{\"__dotNetObject\":2},{\"__dotNetObject\":1}]"
            };

            JSRuntime.SetCurrentJSRuntime(runtime);
            var obj1 = new object();
            var obj2 = new object();

            // Act
            var syncResult = runtime.Invoke <DotNetObjectRef <object>[]>(
                "test identifier",
                DotNetObjectRef.Create(obj1),
                "some other arg",
                DotNetObjectRef.Create(obj2));
            var call = runtime.InvokeCalls.Single();

            // Assert
            Assert.Equal(new[] { obj2, obj1 }, syncResult.Select(r => r.Value));
        }