public async Task ReactInstanceManager_Lifecycle_StartSuspended() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile, LifecycleState.BeforeCreate); var listener = new LifecycleEventsListener(new LifecycleEventsListener.Step[] { LifecycleEventsListener.Step.Resume, LifecycleEventsListener.Step.Suspend, }); await DispatcherHelpers.CallOnDispatcherAsync(async() => { var context = await manager.CreateReactContextAsync(CancellationToken.None); context.AddBackgroundEventListener(listener); context.AddLifecycleEventListener(listener); manager.OnSuspend(); context.RemoveLifecycleEventListener(listener); context.RemoveBackgroundEventListener(listener); }); listener.Dispose(); ReactNative.Bridge.DispatcherHelpers.Initialize(); }
public async Task ReactRootView_SecondaryWindowStress() { var jsBundleFile = "ms-appx:///Resources/mwtest.js"; ReactInstanceManager manager = null; await DispatcherHelpers.CallOnDispatcherAsync(() => manager = CreateReactInstanceManager(jsBundleFile)); var reactContext = await DispatcherHelpers.CallOnDispatcherAsync( () => manager.CreateReactContextAsync(CancellationToken.None)); int currentDelay = 2000; for (int i = 0; i < 30; i++) { currentDelay /= 2; // Create a window var dispatcher = await CreateView(() => { var rv = new ReactRootView(); rv.StartReactApplication( manager, "alt_window", null); return(rv); }); await Task.Delay(currentDelay); await CloseView(manager, dispatcher); } await DispatcherHelpers.CallOnDispatcherAsync(async() => await DisposeInstanceManager(manager)); }
public async Task ReactInstanceManager_CreateReactContextAsync_EnsuresOneCall() { var jsBundleFile = "ms-appx:///Resources/test.js"; ReactInstanceManager manager = null; await DispatcherHelpers.CallOnDispatcherAsync(() => manager = CreateReactInstanceManager(jsBundleFile)); var caught = false; await DispatcherHelpers.CallOnDispatcherAsync(async() => { var task = manager.CreateReactContextAsync(CancellationToken.None); try { await manager.CreateReactContextAsync(CancellationToken.None); } catch (InvalidOperationException) { caught = true; } }); Assert.IsTrue(caught); await DispatcherHelpers.CallOnDispatcherAsync(async() => await DisposeInstanceManager(manager)); }
public async Task ReactInstanceManager_CreateInBackground_EnsuresOneCall() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var waitHandle = new AutoResetEvent(false); manager.ReactContextInitialized += (sender, args) => waitHandle.Set(); var caught = false; await DispatcherHelpers.RunOnDispatcherAsync(async() => { manager.CreateReactContextInBackground(); try { await manager.CreateReactContextInBackgroundAsync(); } catch (InvalidOperationException) { caught = true; } }); Assert.IsTrue(caught); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_DisposeAsync_WhileBusy() { var jsBundleFile = "ms-appx:///Resources/test.js"; ReactInstanceManager manager = null; await DispatcherHelpers.CallOnDispatcherAsync(() => manager = CreateReactInstanceManager(jsBundleFile)); var reactContext = await DispatcherHelpers.CallOnDispatcherAsync( () => manager.CreateReactContextAsync(CancellationToken.None)); var e = new AutoResetEvent(false); reactContext.RunOnNativeModulesQueueThread(() => { e.WaitOne(); var x = DispatcherHelpers.CallOnDispatcherAsync(() => { return(42); }).Result; }); var task = DispatcherHelpers.CallOnDispatcherAsync(async() => { e.Set(); await DisposeInstanceManager(manager); ; }); var completedTask = await Task.WhenAny( Task.Delay(5000), task); Assert.IsTrue(task.IsCompleted); }
public async Task ReactInstanceManager_Lifecycle_Missing_Background() { var jsBundleFile = "ms-appx:///Resources/test.js"; ReactInstanceManager manager = null; await DispatcherHelpers.CallOnDispatcherAsync(() => manager = CreateReactInstanceManager(jsBundleFile, LifecycleState.Foreground)); var listener = new LifecycleEventsListener(new LifecycleEventsListener.Step[] { LifecycleEventsListener.Step.EnteredBackground, LifecycleEventsListener.Step.Suspend, }); await DispatcherHelpers.CallOnDispatcherAsync(async() => { var context = await manager.CreateReactContextAsync(CancellationToken.None); context.AddBackgroundEventListener(listener); context.AddLifecycleEventListener(listener); manager.OnSuspend(); context.RemoveLifecycleEventListener(listener); context.RemoveBackgroundEventListener(listener); }); listener.Dispose(); await DispatcherHelpers.CallOnDispatcherAsync(async() => await DisposeInstanceManager(manager)); }
private static async Task CloseView(ReactInstanceManager manager, CoreDispatcher dispatcher) { await DispatcherHelpers.CallOnDispatcherAsync(dispatcher, async() => { var window = Window.Current; if (window != null) { var frame = window.Content as Frame; if (frame != null) { var page = frame.Content as Page; if (page != null) { var rootView = page.Content as ReactRootView; if (rootView != null) { await rootView.StopReactApplicationAsync(); Window.Current.Close(); page.Content = null; } } } } }); }
private static async Task <CoreDispatcher> CreateView(Func <ReactRootView> rootViewCreateAction) { var dispatcherTask = DispatcherHelpers.CallOnDispatcherAsync <CoreDispatcher>(async() => { CoreApplicationView newView = CoreApplication.CreateNewView(); int newViewId = 0; var dispatcher = newView.Dispatcher; await DispatcherHelpers.CallOnDispatcherAsync(dispatcher, () => { Frame frame = new Frame(); Window.Current.Content = frame; frame.Content = new Page { Content = rootViewCreateAction(), }; // You have to activate the window in order to show it later. Window.Current.Activate(); newViewId = ApplicationView.GetForCurrentView().Id; return(true); }); bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId); return(dispatcher); }); return(await dispatcherTask); }
public async Task ReactInstanceManager_GetOrCreateReactContextAsync_Create() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var reactContext = await DispatcherHelpers.CallOnDispatcherAsync(() => manager.GetOrCreateReactContextAsync(CancellationToken.None)); Assert.IsNotNull(reactContext); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_CreateReactContextAsync() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var reactContext = await DispatcherHelpers.CallOnDispatcherAsync( () => manager.CreateReactContextAsync(CancellationToken.None)); Assert.AreEqual(jsBundleFile, manager.SourceUrl); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_TryGetReactContextAsync_Fail() { var jsBundleFile = "ms-appx:///Resources/test.js"; ReactInstanceManager manager = null; await DispatcherHelpers.CallOnDispatcherAsync(() => manager = CreateReactInstanceManager(jsBundleFile)); var context = await DispatcherHelpers.CallOnDispatcherAsync(async() => await manager.TryGetReactContextAsync(CancellationToken.None)); Assert.IsNull(context); await DispatcherHelpers.CallOnDispatcherAsync(async() => await DisposeInstanceManager(manager)); }
public async Task ReactInstanceManager_OnBackPressed_NoContext() { var waitHandle = new AutoResetEvent(false); var manager = CreateReactInstanceManager(); await DispatcherHelpers.RunOnDispatcherAsync(() => { manager.OnResume(() => waitHandle.Set()); manager.OnBackPressed(); }); Assert.IsTrue(waitHandle.WaitOne()); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_CreateReactContextAsync_Canceled() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.Cancel(); await AssertEx.ThrowsAsync <OperationCanceledException>( async() => await DispatcherHelpers.CallOnDispatcherAsync(() => manager.CreateReactContextAsync(cancellationTokenSource.Token)), ex => Assert.AreEqual(cancellationTokenSource.Token, ex.CancellationToken)); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_GetReactContextAsync_Finished() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var initialContext = await DispatcherHelpers.CallOnDispatcherAsync(async() => await manager.CreateReactContextAsync(CancellationToken.None)); var context = await DispatcherHelpers.CallOnDispatcherAsync(async() => await manager.GetReactContextAsync(CancellationToken.None)); Assert.AreSame(initialContext, context); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_CreateInBackground() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var waitHandle = new AutoResetEvent(false); manager.ReactContextInitialized += (sender, args) => waitHandle.Set(); await DispatcherHelpers.RunOnDispatcherAsync( () => manager.CreateReactContextInBackground()); Assert.IsTrue(waitHandle.WaitOne()); Assert.AreEqual(jsBundleFile, manager.SourceUrl); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_ArgumentChecks() { var manager = CreateReactInstanceManager(); AssertEx.Throws <ArgumentNullException>( () => manager.AttachMeasuredRootView(null), ex => Assert.AreEqual("rootView", ex.ParamName)); AssertEx.Throws <ArgumentNullException>( () => manager.CreateAllViewManagers(null), ex => Assert.AreEqual("reactContext", ex.ParamName)); AssertEx.Throws <ArgumentNullException>( () => manager.DetachRootView(null), ex => Assert.AreEqual("rootView", ex.ParamName)); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_GetOrCreateReactContextAsync_Get() { var jsBundleFile = "ms-appx:///Resources/test.js"; ReactInstanceManager manager = null; await DispatcherHelpers.CallOnDispatcherAsync(() => manager = CreateReactInstanceManager(jsBundleFile)); var initialContext = default(ReactContext); var context = default(ReactContext); await DispatcherHelpers.CallOnDispatcherAsync(async() => { var initialContextTask = manager.CreateReactContextAsync(CancellationToken.None); context = await manager.GetOrCreateReactContextAsync(CancellationToken.None); initialContext = await initialContextTask; }); Assert.AreSame(initialContext, context); await DispatcherHelpers.CallOnDispatcherAsync(async() => await DisposeInstanceManager(manager)); }
public async Task ReactInstanceManager_RecreateReactContextAsync() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var task = default(Task <ReactContext>); var reactContext = await DispatcherHelpers.CallOnDispatcherAsync(async() => { task = manager.CreateReactContextAsync(CancellationToken.None); return(await manager.RecreateReactContextAsync(CancellationToken.None)); }); var initialReactContext = await task; Assert.IsNotNull(reactContext); Assert.AreEqual(jsBundleFile, manager.SourceUrl); Assert.AreNotEqual(initialReactContext, reactContext); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_DisposeAsync_WhileBusy() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var initializedEvent = new AutoResetEvent(false); manager.ReactContextInitialized += (sender, args) => initializedEvent.Set(); await DispatcherHelpers.CallOnDispatcherAsync(async() => { await manager.CreateReactContextInBackgroundAsync(); }); var e = new AutoResetEvent(false); initializedEvent.WaitOne(); manager.CurrentReactContext.RunOnNativeModulesQueueThread(() => { e.WaitOne(); var x = DispatcherHelpers.CallOnDispatcherAsync(() => { return(42); }).Result; }); var tcs = new TaskCompletionSource <bool>(); await DispatcherHelpers.RunOnDispatcherAsync(async() => { e.Set(); await manager.DisposeAsync(); await Task.Run(() => tcs.SetResult(true)); }); var completedTask = await Task.WhenAny( Task.Delay(5000), tcs.Task); Assert.IsTrue(tcs.Task.IsCompleted); }
public async Task ReactInstanceManager_RecreateInBackground_EnsuresCalledOnce() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var caught = false; await DispatcherHelpers.RunOnDispatcherAsync(async() => { try { await manager.RecreateReactContextInBackgroundAsync(); } catch (InvalidOperationException) { caught = true; } }); Assert.IsTrue(caught); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }
public async Task ReactInstanceManager_GetReactContextAsync_Fail() { var jsBundleFile = "ms-appx:///Resources/test.js"; var manager = CreateReactInstanceManager(jsBundleFile); var caught = false; await DispatcherHelpers.CallOnDispatcherAsync(async() => { try { await manager.GetReactContextAsync(CancellationToken.None); } catch (InvalidOperationException) { caught = true; } }); Assert.IsTrue(caught); await DispatcherHelpers.CallOnDispatcherAsync(manager.DisposeAsync); }