public void BindWindowTitleExistingTest( [Values] bool isDialog, [Values] bool isUserControl) { const string title = "Window Title"; const string existingTitle = "Existing Window Title"; var viewModelMock = new Mock <IHaveTitle>(MockBehavior.Strict); viewModelMock.Setup(x => x.Title).Returns(title); var viewModel = viewModelMock.Object; string actualTitle = string.Empty; FrameworkElement view = null; if (isUserControl) { var userControlView = new MockUserControlView() { DataContext = viewModel }; userControlView.OnLoadedAction = userControl => actualTitle = (userControl.Parent as Window)?.Title; view = userControlView; } else { var windowView = new MockWindowView() { DataContext = viewModel, Title = existingTitle }; windowView.OnLoadedAction = window => actualTitle = window.Title; Assert.AreEqual(existingTitle, windowView.Title); view = windowView; } _viewManagerMock .Setup(x => x.GetViewFor(viewModel)) .Returns(view); if (isDialog) { _windowManager.ShowDialog(viewModel); } else { _windowManager.ShowWindow(viewModel); } if (isUserControl) { Assert.AreEqual(title, actualTitle); } else { Assert.AreEqual(existingTitle, actualTitle); } }
public void WindowManagerEnsureWindowForUserControlTest() { var windowManager = new MockWindowManager(_viewManager); UserControl userControl = new MockUserControlView(); var view = windowManager.EnsureWindow(userControl, false); Assert.AreSame(userControl, view.Content); }
public void ShowDialogForUserControlTest([Values] bool isAnotherActiveWindow) { var viewModel = new object(); var view = new MockUserControlView() { DataContext = viewModel }; Window ownerWindow = null; if (isAnotherActiveWindow) { ownerWindow = new Window() { Height = 1, Width = 1, WindowState = WindowState.Minimized }; ownerWindow.Show(); } _windowManager.GetActiveWindow = () => ownerWindow; bool windowShown = false; view.OnLoadedAction = userControl => { windowShown = true; Assert.AreSame(viewModel, userControl.DataContext); var parentWindow = userControl.Parent as Window; Assert.NotNull(parentWindow); Assert.AreSame(viewModel, parentWindow.DataContext); if (isAnotherActiveWindow) { Assert.AreSame(ownerWindow, parentWindow.Owner); } else { Assert.Null(parentWindow.Owner); } }; _viewManagerMock .Setup(x => x.GetViewFor(viewModel)) .Returns(view); _windowManager.ShowDialog(viewModel); _viewManagerMock.Verify(x => x.GetViewFor(viewModel)); Assert.True(windowShown); }