public void WebViewDisposeDoesNotBlockWhenHasPendingJSCalls()
        {
            const string DotNetObject = "DotNetObject";

            var functionCalled = false;
            var disposeCalled  = false;

            Func <int> functionToCall = () => {
                functionCalled = true;
                TargetView.Dispose();
                Assert.IsFalse(disposeCalled); // dispose should have been scheduled
                return(1);
            };

            TargetView.RegisterJavascriptObject(DotNetObject, functionToCall, executeCallsInUI: true);
            LoadAndWaitReady($"<html><script>function test() {{ {DotNetObject}.invoke(); return 1; }}</script><body></body></html>");
            TargetView.Disposed += () => disposeCalled = true;

            var result = TargetView.EvaluateScriptFunction <int>("test");

            WaitFor(() => functionCalled, DefaultTimeout);

            WaitFor(() => disposeCalled, DefaultTimeout);

            Assert.IsTrue(disposeCalled);
        }
Exemple #2
0
        public async Task WebViewDisposeDoesNotBlockWhenHasPendingJSCalls()
        {
            await Run(async() => {
                const string DotNetObject = "DotNetObject";

                var taskCompletionSourceFunction = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
                var taskCompletionSourceDispose  = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

                Func <int> functionToCall = () => {
                    TargetView.Dispose();
                    var disposeRan = taskCompletionSourceDispose.Task.IsCompleted;
                    taskCompletionSourceFunction.SetResult(disposeRan);
                    return(1);
                };

                TargetView.RegisterJavascriptObject(DotNetObject, functionToCall);
                await Load($"<html><script>function test() {{ {DotNetObject}.invoke(); while(true); return 1; }}</script><body></body></html>");

                TargetView.Disposed += () => taskCompletionSourceDispose.SetResult(true);

                var result = await TargetView.EvaluateScriptFunction <int>("test");
                Assert.AreEqual(0, result, "Script evaluation should be cancelled and default value returned");

                var disposed = await taskCompletionSourceFunction.Task;
                Assert.IsFalse(disposed, "Dispose should have been scheduled");

                disposed = await taskCompletionSourceDispose.Task;
                Assert.IsTrue(disposed);
            });
        }
Exemple #3
0
        public async Task WebViewDisposeDoesNotBlockWhenHasPendingJSCalls()
        {
            await Run(async() => {
                const string DotNetObject = "DotNetObject";

                var taskCompletionSourceFunction = new TaskCompletionSource <bool>();
                var taskCompletionSourceDispose  = new TaskCompletionSource <bool>();

                Func <int> functionToCall = () => {
                    TargetView.Dispose();
                    var disposeRan = taskCompletionSourceDispose.Task.IsCompleted;
                    taskCompletionSourceFunction.SetResult(disposeRan);
                    return(1);
                };

                TargetView.RegisterJavascriptObject(DotNetObject, functionToCall, executeCallsInUI: true);
                await Load($"<html><script>function test() {{ {DotNetObject}.invoke(); return 1; }}</script><body></body></html>");

                TargetView.Disposed += () => taskCompletionSourceDispose.SetResult(true);

                TargetView.EvaluateScriptFunction <int>("test");

                var disposed = await taskCompletionSourceFunction.Task;
                Assert.IsFalse(disposed, "Dispose should have been scheduled");

                disposed = await taskCompletionSourceDispose.Task;
                Assert.IsTrue(disposed);
            });
        }
        public void JsEvaluationReturnsDefaultValuesAfterWebViewDispose()
        {
            var disposeCalled = false;

            LoadAndWaitReady("<html><script>function test() { return 1; }</script><body></body></html>");
            TargetView.Disposed += () => disposeCalled = true;
            TargetView.Dispose();

            WaitFor(() => disposeCalled, DefaultTimeout);

            var result = TargetView.EvaluateScriptFunction <int>("test");

            Assert.IsTrue(disposeCalled);
            Assert.AreEqual(result, 0);
        }
Exemple #5
0
        public void DisposeDoesNotHang()
        {
            var disposed = false;

            TargetView.Event += (args) => {
                TargetView.Dispatcher.Invoke((() => {
                    TargetView.Dispose();
                    disposed = true;
                }));
            };

            TargetView.ExecuteMethodOnRoot("callEvent");

            WaitFor(() => disposed, TimeSpan.FromSeconds(10), "view disposed");
        }
Exemple #6
0
        public async Task DisposeDoesNotHang()
        {
            await Run(async() => {
                var taskCompletionSource = new TaskCompletionSource <bool>();
                TargetView.Event        += delegate {
                    Dispatcher.UIThread.InvokeAsync(() => {
                        TargetView.Dispose();
                        taskCompletionSource.SetResult(true);
                    }).Wait();
                };

                TargetView.ExecuteMethod("callEvent");
                var eventCalled = await taskCompletionSource.Task;

                Assert.IsTrue(eventCalled, "View was not disposed!");
            });
        }
Exemple #7
0
        public async Task JsEvaluationReturnsDefaultValuesAfterWebViewDispose()
        {
            await Run(async() => {
                var taskCompletionSource = new TaskCompletionSource <bool>();
                await Load("<html><script>function test() { return 1; }</script><body></body></html>");

                TargetView.Disposed += () => {
                    taskCompletionSource.SetResult(true);
                };
                TargetView.Dispose();

                var disposeCalled = await taskCompletionSource.Task;

                var result = await TargetView.EvaluateScriptFunction <int>("test");

                Assert.IsTrue(disposeCalled);
                Assert.AreEqual(result, 0);
            });
        }