using System; using System.Linq; using System.Windows; using Core.Common.Base.Storage; using Core.Common.Controls.Views; using Core.Common.Gui; using Core.Common.Gui.Forms.MainWindow; using Core.Common.Gui.Forms.ViewManager; using Core.Components.Charting.Data; using Core.Components.OxyPlot.Forms; using Core.Plugins.OxyPlot.Forms; using Core.Plugins.OxyPlot.Legend; using NUnit.Framework; using Rhino.Mocks; namespace Core.Plugins.OxyPlot.Test { [TestFixture] public class OxyPlotGuiPluginTest { [Test] public void DefaultConstructor_Always_NoRibbonCommandHandlerSet() { // Call using (var plugin = new OxyPlotGuiPlugin()) { // Assert Assert.IsInstanceOf(plugin); Assert.IsInstanceOf(plugin); Assert.IsNull(plugin.RibbonCommandHandler); } } [Test] [RequiresSTA] public void Activate_WithoutGui_ThrowsNullReferenceException() { // Setup using (var plugin = new OxyPlotGuiPlugin()) { // Call TestDelegate test = () => plugin.Activate(); // Assert Assert.Throws(test); } } [Test] [RequiresSTA] public void Activate_WithGui_InitializesComponentsAndBindsActiveViewChanged() { // Setup var mocks = new MockRepository(); using (var plugin = new OxyPlotGuiPlugin()) { var gui = mocks.StrictMock(); var dockingManger = mocks.Stub(); var toolWindows = new ViewList(dockingManger, null); var view = mocks.StrictMock(); gui.Expect(g => g.ActiveViewChanged += null).IgnoreArguments(); gui.Expect(g => g.ActiveViewChanged -= null).IgnoreArguments(); gui.Expect(g => g.ToolWindowViews).Return(toolWindows).Repeat.Twice(); gui.Expect(g => g.OpenToolView(Arg.Matches(c => true))); gui.Expect(g => g.ActiveView).Return(view); mocks.ReplayAll(); plugin.Gui = gui; // Call plugin.Activate(); // Assert Assert.IsInstanceOf(plugin); Assert.NotNull(plugin.RibbonCommandHandler); } mocks.VerifyAll(); } [Test] public void GetViewInfoObjects_Always_ReturnsChartDataViewInfo() { // Setup using (var plugin = new OxyPlotGuiPlugin()) { var view = new ChartDataView(); // Call var views = plugin.GetViewInfoObjects().ToArray(); // Assert Assert.AreEqual(1, views.Length); Assert.AreEqual(typeof(ChartDataCollection), views[0].DataType); Assert.AreEqual(typeof(ChartDataView), views[0].ViewType); Assert.AreEqual("Diagram", views[0].GetViewName(view, null)); } } [Test] public void CloseToolView_Always_CloseToolView() { // Setup using (var plugin = new OxyPlotGuiPlugin()) { var mocks = new MockRepository(); var gui = mocks.StrictMock(); var view = mocks.StrictMock(); gui.Expect(g => g.CloseToolView(view)); mocks.ReplayAll(); plugin.Gui = gui; // Call plugin.CloseToolView(view); // Assert mocks.VerifyAll(); } } [Test] [RequiresSTA] [TestCase(true)] [TestCase(false)] public void GivenConfiguredGui_WhenOpenToolView_UpdateComponentsWithDataFromActiveView(bool isChartViewActive) { // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); mocks.ReplayAll(); using (var gui = new RingtoetsGui(new MainWindow(null), projectStore)) { var plugin = new OxyPlotGuiPlugin(); IView viewMock = isChartViewActive ? (IView)new TestChartView() : new TestView(); var baseChart = new BaseChart { Data = new LineData(Enumerable.Empty>()) }; viewMock.Data = baseChart; gui.Plugins.Add(plugin); gui.Run(); gui.DocumentViews.Add(viewMock); gui.DocumentViews.ActiveView = viewMock; var legendView = gui.ToolWindowViews.First(t => t is LegendView); legendView.Data = null; // When plugin.OpenToolView(legendView); // Then Assert.AreSame(isChartViewActive ? baseChart.Data : null, legendView.Data); mocks.VerifyAll(); } } [Test] [TestCase(true)] [TestCase(false)] [RequiresSTA] public void GivenConfiguredGui_WhenActiveViewChangesToViewWithChart_ThenRibbonSetVisibility(bool visible) { // Given var mocks = new MockRepository(); var projectStore = mocks.Stub(); mocks.ReplayAll(); using (var gui = new RingtoetsGui(new MainWindow(null), projectStore)) { var plugin = new OxyPlotGuiPlugin(); var testChartView = new TestChartView(); var chart = new BaseChart(); IView viewMock = visible ? (IView) testChartView : new TestView(); testChartView.Data = chart; gui.Plugins.Add(plugin); plugin.Gui = gui; gui.Run(); // When gui.DocumentViews.Add(viewMock); gui.DocumentViews.ActiveView = viewMock; // Then Assert.AreEqual(visible ? Visibility.Visible : Visibility.Collapsed, plugin.RibbonCommandHandler.GetRibbonControl().ContextualGroups[0].Visibility); mocks.VerifyAll(); } } } }