Ejemplo n.º 1
0
        public static void MainThreadEntrypoint(Func <Task> mainThreadEntrypoint)
        {
            UIDispatcherMock       syncContext    = new UIDispatcherMock();
            SynchronizationContext oldSyncContext = Current;

            SetSynchronizationContext(syncContext);
            Exception unhandledException = null;

            syncContext.Post(
                async state =>
            {
                try
                {
                    await mainThreadEntrypoint?.Invoke();
                }
                catch (Exception ex)
                {
                    unhandledException = ex;
                }
                finally
                {
                    syncContext.Continue = false;
                }
            },
                null);
            syncContext.Loop();
            SetSynchronizationContext(oldSyncContext);
            if (unhandledException != null)
            {
                ExceptionDispatchInfo.Capture(unhandledException).Throw();
            }
        }
Ejemplo n.º 2
0
        public void OpenFileForRead_NoHangForSync()
        {
            // Simulate the UI thread of an app.
            UIDispatcherMock.MainThreadEntrypoint(delegate
            {
                //  Arrange
                IFolder folder  = TestFileSystem.LocalStorage;
                string fileName = "fileToOpenForRead.txt";
                IFile file      = folder.CreateFileAsync(fileName, CreationCollisionOption.FailIfExists).Result;
                //  Act
                using (Stream stream = file.OpenAsync(FileAccess.Read).Result)
                {
                    //  Assert
                    Assert.IsFalse(stream.CanWrite);
                    Assert.IsTrue(stream.CanRead);
                    Assert.IsTrue(stream.CanSeek);
                }

                //  Cleanup
                file.DeleteAsync().Wait();
            });
        }