Exemplo n.º 1
0
        public void GetPropertyTest()
        {
            using (var tc = new TestContext()) {
                var evalResult = tc.Context.Evaluate(
                    tc.Global,
                    @"var o = { 
                        'a': 1, 
                        'b': 'hello', 
                        'c': 3.5 
                      };
                      o"
                    );

                Assert.AreEqual(JSValueType.OBJECT, evalResult.Value.ValueType);
                var obj = new JSObjectReference(evalResult);

                var a = obj["a"];
                Assert.AreEqual(JSValueType.INT32, a.ValueType);
                Assert.AreEqual(1, (int)a);

                var b = obj["b"];
                Assert.AreEqual(JSValueType.STRING, b.ValueType);
                Assert.AreEqual("hello", b.ToManaged(tc));

                var c = obj["c"];
                Assert.AreEqual(JSValueType.DOUBLE, c.ValueType);
                Assert.AreEqual(3.5, (double)c);

                var d = obj["d"];
                Assert.IsTrue(d.IsNullOrUndefined);
            }
        }
Exemplo n.º 2
0
        public void GetPrototypeTest()
        {
            using (var tc = new TestContext()) {
                var evalResult = tc.Context.Evaluate(
                    tc.Global,
                    "[new String('a'), new Error('b'), []]"
                    );

                var arr = new JSArray(evalResult);
                var s   = new JSObjectReference(tc, arr[0]);
                var e   = new JSObjectReference(tc, arr[1]);
                var a   = new JSObjectReference(tc, arr[2]);

                var getClassProto = (Func <string, JSObjectReference>)((name) => {
                    JSObjectPtr p;
                    if (!tc.Global.TryGetNested(out p, name, "prototype"))
                    {
                        throw new Exception("Prototype not found");
                    }

                    return(new JSObjectReference(tc, p));
                });

                Assert.AreEqual(
                    getClassProto("String"), s.Prototype
                    );
                Assert.AreEqual(
                    getClassProto("Error"), e.Prototype
                    );
                Assert.AreEqual(
                    getClassProto("Array"), a.Prototype
                    );
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// コンストラクタ。
        /// </summary>
        public Offscreen(Size2D size, IJSInProcessRuntime jsRuntime)
        {
            _jsOffscreen = JSObjectReference.From(jsRuntime,
                                                  "Sample.BlazorWasm.TextEditor.Offscreen.create", new { w = size.Width, h = size.Height });

            _writer = new Utf8JsonWriter(_utf8jsonMemory);

            Init();
        }
Exemplo n.º 4
0
    public void Write_WritesValidJson()
    {
        // Arrange
        var jsObjectRef = new JSObjectReference(JSRuntime, 7);

        // Act
        var json = JsonSerializer.Serialize((IJSObjectReference)jsObjectRef, JsonSerializerOptions);

        // Assert
        Assert.Equal($"{{\"__jsObjectId\":{jsObjectRef.Id}}}", json);
    }
Exemplo n.º 5
0
    public void JSObjectReference_InvokeAsync_CallsUnderlyingJSRuntimeInvokeAsync()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();
        var jsObject  = new JSObjectReference(jsRuntime, 0);

        // Act
        _ = jsObject.InvokeAsync <object>("test", "arg1", "arg2");

        // Assert
        Assert.Equal(1, jsRuntime.BeginInvokeJSInvocationCount);
    }
Exemplo n.º 6
0
        public void NewInstanceTest()
        {
            using (var tc = new TestContext()) {
                tc.Context.Evaluate(tc.Global, "function cls (x) { this.x = x; };");

                var cls      = tc.Global["cls"];
                var arg1     = new JS.Value(1.5);
                var instance = new JSObjectReference(
                    tc, cls.InvokeConstructor(tc, arg1)
                    );

                Assert.AreEqual(arg1, instance["x"]);
            }
        }
Exemplo n.º 7
0
    public async Task JSObjectReference_Dispose_DisallowsFurtherInteropCalls()
    {
        // Arrange
        var jsRuntime = new TestJSRuntime();
        var jsObject  = new JSObjectReference(jsRuntime, 0);

        // Act
        _ = jsObject.DisposeAsync();

        // Assert
        await Assert.ThrowsAsync <ObjectDisposedException>(async() => await jsObject.InvokeAsync <object>("test", "arg1", "arg2"));

        await Assert.ThrowsAsync <ObjectDisposedException>(async() => await jsObject.InvokeAsync <object>("test", CancellationToken.None, "arg1", "arg2"));
    }
Exemplo n.º 8
0
 /// <summary>
 /// Gets the maximum zoom level on which the bounds fit the map view.
 /// </summary>
 /// <param name="bounds">The <see cref="LatLngBounds"/> to fit to the map.</param>
 /// <param name="inside">A flag indicating the fit direction. If true, method returns minimum zoom level
 /// on which the map fits into the bounds.</param>
 /// <param name="padding">The padding in pixels.</param>
 /// <returns></returns>
 public async Task <int> GetBoundsZoom(LatLngBounds bounds, bool inside = false, Point padding = null)
 {
     if (bounds.JSBinder is null)
     {
         await bounds.BindJsObjectReference(this.JSBinder);
     }
     bounds.GuardAgainstNullBinding("Cannot get bounds zoom. No JavaScript binding has been set up for the bounds parameter.");
     if (padding is not null)
     {
         if (padding.JSBinder is null)
         {
             await padding.BindJsObjectReference(this.JSBinder);
         }
         padding.GuardAgainstNullBinding("Cannot get bounds zoom. No JavaScript binding has been set up for the padding parameter.");
     }
     return(await JSObjectReference.InvokeAsync <int>("getBoundsZoom", bounds.JSObjectReference, inside, padding?.JSObjectReference));
 }
Exemplo n.º 9
0
        public void CustomClassTest()
        {
            using (var tc = new TestContext())
                using (var cc = new JSCustomClass(tc, "testClass", tc.Global)) {
                    cc.Initialize();

                    cc.Prototype["a"] = new JSString(tc, "hello");

                    var jsCtor   = tc.Global["testClass"];
                    var classObj = new JSObjectReference(tc, jsCtor);

                    Assert.AreEqual(cc.Prototype.Pointer, classObj["prototype"].AsObject);

                    Assert.AreEqual("hello", tc.Context.Evaluate(tc.Global, "testClass.prototype.a").Value.ToManagedString(tc));

                    var evalResult = tc.Context.Evaluate(tc.Global, "(new testClass())");
                    var resultObj  = new JSObjectReference(evalResult);

                    Assert.AreEqual(jsCtor.AsObject, resultObj["constructor"].AsObject);

                    Assert.AreEqual("hello", resultObj["a"].ToManagedString(tc));
                }
        }
Exemplo n.º 10
0
 public static JSObjectReference RoundTripJSObjectReference(JSObjectReference jsObjectReference)
 {
     return(jsObjectReference);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Gets the size of the map container in pixels.
 /// </summary>
 /// <returns>A <see cref="Point"/> representing the size of the map container in pixels.</returns>
 public async Task <Point> GetSize()
 {
     return(await JSObjectReference.InvokeAsync <Point>("getSize"));
 }
Exemplo n.º 12
0
 public JsObjectReferenceDynamic(JSObjectReference module)
 {
     Module = module;
 }
Exemplo n.º 13
0
 internal async ValueTask ImportModule()
 {
     module = await jsRuntime.InvokeAsync <JSObjectReference>("import", "./_content/Append.Blazor.Printing/scripts.js");
 }
Exemplo n.º 14
0
 /// <summary>
 /// コンストラクタ。
 /// </summary>
 public Font(string family, double height, IJSInProcessRuntime jsRuntime)
 {
     _jsFont = JSObjectReference.From(jsRuntime,
                                      "Sample.BlazorWasm.TextEditor.Font.create", family, height);
 }
Exemplo n.º 15
0
 /// <summary>
 /// Gets the bounds of the map view in projected pixel coordinates.
 /// </summary>
 /// <returns>A <see cref="Bounds"/> representing the size of the map container in pixels.</returns>
 public async Task <Bounds> GetPixelBounds()
 {
     return(await JSObjectReference.InvokeAsync <Bounds>("getPixelBounds"));
 }
Exemplo n.º 16
0
 /// <summary>
 /// Gets the projected pixel coordinates of the top left point of the map layer.
 /// </summary>
 /// <returns>A <see cref="Point"/> representing top left point of the map in pixels.</returns>
 public async Task <Point> GetPixelOrigin()
 {
     return(await JSObjectReference.InvokeAsync <Point>("getPixelOrigin"));
 }
Exemplo n.º 17
0
 /// <summary>
 /// Gets the world's bounds in pixel coordinates.
 /// </summary>
 /// <param name="zoom">The zoom level used to calculate the bounds. Current map zoom level is used if null or omitted.</param>
 /// <returns>A <see cref="Bounds"/> representing the size of the map container in pixels.</returns>
 public async Task <Bounds> GetPixelWorldBounds(int?zoom = null)
 {
     return(await JSObjectReference.InvokeAsync <Bounds>("getPixelWorldBounds", zoom));
 }
Exemplo n.º 18
0
 /// <summary>
 /// Gets the point at the centre of the map view.
 /// </summary>
 /// <returns>A <see cref="LatLng"/> representing the geographical centre of the map.</returns>
 public async Task <LatLng> GetCenter()
 {
     return(await JSObjectReference.InvokeAsync <LatLng>("getCenter"));
 }
Exemplo n.º 19
0
 /// <summary>
 /// Gets the maximum zoom level of the map view.
 /// </summary>
 /// <returns>The maximum zoom level.</returns>
 public async Task <int> GetMaxZoom()
 {
     return(await JSObjectReference.InvokeAsync <int>("getMaxZoom"));
 }
Exemplo n.º 20
0
 /// <summary>
 /// Gets the geographical bounds of the map view.
 /// </summary>
 /// <returns>A <see cref="LatLngBounds"/> object representing the geographical bounds of the map.</returns>
 public async Task <LatLngBounds> GetBounds()
 {
     return(await JSObjectReference.InvokeAsync <LatLngBounds>("getBounds"));
 }
Exemplo n.º 21
0
 public static int TestManagedObjArg(JSObjectReference o)
 {
     return((int)(o["x"]) * 2);
 }
Exemplo n.º 22
0
        public static async Task <JSObjectReference> RoundTripJSObjectReferenceAsync(JSObjectReference jSObjectReference)
        {
            await Task.Yield();

            return(jSObjectReference);
        }
Exemplo n.º 23
0
        public static async Task <string> InvokeDisposedJSObjectReferenceExceptionAsync(JSObjectReference jsObjectReference)
        {
            try
            {
                await jsObjectReference.InvokeVoidAsync("noop");

                return("No exception thrown");
            }
            catch (JSException e)
            {
                return(e.Message);
            }
        }
Exemplo n.º 24
0
 /// <summary>
 /// コンストラクタ。
 /// </summary>
 public InputMethod(JSObjectReference jsRefTextEditorComponent)
 {
     _jsInputMethod = JSObjectReference.From(jsRefTextEditorComponent, "bindInputMethod");
 }