Пример #1
0
    public void Dispose_StopsTrackingObject()
    {
        // Arrange
        var objRef    = DotNetObjectReference.Create("Hello world");
        var jsRuntime = new TestJSRuntime();

        jsRuntime.TrackObjectReference(objRef);
        var objectId = objRef.ObjectId;
        var expected = $"There is no tracked object with id '{objectId}'. Perhaps the DotNetObjectReference instance was already disposed.";

        // Act
        Assert.Same(objRef, jsRuntime.GetObjectReference(objectId));
        objRef.Dispose();

        // Assert
        Assert.True(objRef.Disposed);
        Assert.Throws <ArgumentException>(() => jsRuntime.GetObjectReference(objectId));
    }
Пример #2
0
    public void SerializesDotNetObjectWrappersInKnownFormat()
    {
        // Arrange
        var runtime = new TestJSRuntime();
        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 obj1Ref          = DotNetObjectReference.Create(obj1);
        var obj1DifferentRef = DotNetObjectReference.Create(obj1);

        runtime.InvokeAsync <object>("test identifier",
                                     obj1Ref,
                                     new Dictionary <string, object>
        {
            { "obj2", DotNetObjectReference.Create(obj2) },
            { "obj3", DotNetObjectReference.Create(obj3) },
            { "obj1SameRef", obj1Ref },
            { "obj1DifferentRef", obj1DifferentRef },
        });

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

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

        // Assert: Objects were tracked
        Assert.Same(obj1Ref, runtime.GetObjectReference(1));
        Assert.Same(obj1, obj1Ref.Value);
        Assert.NotSame(obj1Ref, runtime.GetObjectReference(2));
        Assert.Same(obj2, runtime.GetObjectReference(2).Value);
        Assert.Same(obj3, runtime.GetObjectReference(3).Value);
        Assert.Same(obj1, runtime.GetObjectReference(4).Value);
    }
Пример #3
0
    public void DoubleDispose_Works()
    {
        // Arrange
        var objRef    = DotNetObjectReference.Create("Hello world");
        var jsRuntime = new TestJSRuntime();

        jsRuntime.TrackObjectReference(objRef);
        var objectId = objRef.ObjectId;

        // Act
        Assert.Same(objRef, jsRuntime.GetObjectReference(objectId));
        objRef.Dispose();

        // Assert
        objRef.Dispose();
        // If we got this far, this did not throw.
    }