/// <summary>Add user documentation, based on the example.</summary> /// <param name="tags">The tags to add to.</param> /// <param name="modelName">Name of model to document.</param> private void AddUserDocumentation(List <AutoDocumentation.ITag> tags, string modelName) { // Look for some instructions on which models in the example file we should write. // Instructions will be in a memo in the validation .apsimx file IModel userDocumentation = Apsim.Get(ExplorerPresenter.ApsimXFile, ".Simulations.UserDocumentation") as IModel; string exampleFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", "Examples", modelName + ".apsimx"); if (userDocumentation != null && userDocumentation.Children.Count > 0 && File.Exists(exampleFileName)) { // Write heading. tags.Add(new AutoDocumentation.Heading("User documentation", 1)); // Open the related example .apsimx file and get its presenter. ExplorerPresenter examplePresenter = ExplorerPresenter.MainPresenter.OpenApsimXFileInTab(exampleFileName, onLeftTabControl: true); Memo instructionsMemo = userDocumentation.Children[0] as Memo; string[] instructions = instructionsMemo.MemoText.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (string instruction in instructions) { IModel model = Apsim.Find(examplePresenter.ApsimXFile, instruction); if (model != null) { examplePresenter.SelectNode(Apsim.FullPath(model)); while (Gtk.Application.EventsPending()) { Gtk.Application.RunIteration(); } if (model is Memo) { model.Document(tags, 1, 0); } else { System.Drawing.Image image = null; if (model is Manager) { image = (examplePresenter.CurrentPresenter as ManagerPresenter).GetScreenshot(); } else { image = examplePresenter.GetScreenhotOfRightHandPanel(); } if (image != null) { string name = "Example" + instruction; tags.Add(new AutoDocumentation.Image() { name = name, image = image }); } } } } // Close the tab examplePresenter.MainPresenter.CloseTabContaining(examplePresenter.GetView().MainWidget); while (Gtk.Application.EventsPending()) { Gtk.Application.RunIteration(); } } }
public void CreateGraphs() { Simulations sims = CreateTemplate(); sims.FileName = Path.ChangeExtension(Path.GetTempFileName(), ".apsimx"); DataStore storage = Apsim.Find(sims, typeof(DataStore)) as DataStore; storage.FileName = Path.ChangeExtension(sims.FileName, ".db"); // Run the file to populate the datastore. Runner runner = new Runner(sims); List <Exception> errors = runner.Run(); if (errors != null && errors.Count > 0) { throw errors[0]; } // Open the .apsimx file in the GUI. sims.Write(sims.FileName); ExplorerPresenter explorer = UITestsMain.MasterPresenter.OpenApsimXFileInTab(sims.FileName, true); GtkUtilities.WaitForGtkEvents(); // Create a graphs folder under the zone. IModel paddock = Apsim.Find(sims, typeof(Zone)); Folder graphs = new Folder(); graphs.Name = "Graphs"; explorer.Add(graphs, Apsim.FullPath(paddock)); // Add an empty graph to the folder. Models.Graph.Graph graph = new Models.Graph.Graph(); graph.Name = "Graph"; explorer.Add(graph, Apsim.FullPath(graphs)); // Add an empty series to the graph. Models.Graph.Series series = new Models.Graph.Series(); series.Name = "Series"; explorer.Add(series, Apsim.FullPath(graph)); // click on the series node. explorer.SelectNode(Apsim.FullPath(series)); GtkUtilities.WaitForGtkEvents(); // Get a reference to the OxyPlot PlotView via reflection. SeriesView seriesView = explorer.CurrentRightHandView as SeriesView; GraphView view = seriesView?.GraphView as GraphView; Assert.NotNull(view); PlotView plot = ReflectionUtilities.GetValueOfFieldOrProperty("plot1", view) as PlotView; Assert.NotNull(plot); // Series has no table name or x/y series names yet, so there should // be nothing shown on the graph. Assert.AreEqual(0, plot.Model.Series.Count); // Now draw some data on the graph. seriesView.DataSource.SelectedValue = "Report"; seriesView.X.SelectedValue = "n"; seriesView.Y.SelectedValue = "n2"; seriesView.SeriesType.SelectedValue = "Scatter"; GtkUtilities.WaitForGtkEvents(); // There should now be one series showing. Assert.AreEqual(1, plot.Model.Series.Count); // It should be a line series. Assert.True(plot.Model.Series[0] is LineSeries, "Graph series type is set to scatter, but the series object is not a LineSeries."); // Series colour should not be white, and should not be the same as the background colour. LineSeries line = plot.Model.Series[0] as LineSeries; OxyPlot.OxyColor empty = OxyPlot.OxyColor.FromArgb(0, 0, 0, 0); OxyPlot.OxyColor white = OxyPlot.OxyColor.FromArgb(0, 255, 255, 255); Assert.AreNotEqual(empty, line.Color, "Graph line series default colour is white on white."); Assert.AreNotEqual(white, line.Color, "Graph line series default colour is white on white."); // Legend should be visible but empty by default. Assert.True(plot.Model.IsLegendVisible); // todo - ensure legend is empty // Next, we want to change the legend position and ensure that the legend actually moves. // Click on the 'show in legend' checkbox. seriesView.ShowInLegend.IsChecked = true; GtkUtilities.WaitForGtkEvents(); // Double click on the middle of the legend. Cairo.Rectangle legendRect = plot.Model.LegendArea.ToRect(true); double x = (legendRect.X + (legendRect.X + legendRect.Width)) / 2; double y = (legendRect.Y + (legendRect.Y + legendRect.Height)) / 2; GtkUtilities.DoubleClick(plot, x, y, wait: true); // Default legend position should be top-left. Assert.AreEqual(plot.Model.LegendPosition, OxyPlot.LegendPosition.TopLeft); // Now we want to change the legend position. First, get a reference to the legend view // via the legend presenter, via the graph presenter, via the series presenter, via the explorer presenter. Assert.True(explorer.CurrentPresenter is SeriesPresenter); SeriesPresenter seriesPresenter = explorer.CurrentPresenter as SeriesPresenter; LegendPresenter legendPresenter = seriesPresenter.GraphPresenter.CurrentPresenter as LegendPresenter; // todo: should we add something like a GetView() method to the IPresenter interface? // It might be a bit of work to set up but would save us having to use reflection LegendView legendView = ReflectionUtilities.GetValueOfFieldOrProperty("view", legendPresenter) as LegendView; // The legend options are inside a Gtk expander. Assert.IsTrue(legendView.MainWidget.Parent is Expander); Expander expander = legendView.MainWidget.Parent as Expander; // The expander should be expanded and the options visible. Assert.IsTrue(expander.Expanded); Assert.IsTrue(legendView.MainWidget.Visible); // The legend view contains a combo box with the legend position options (top-right, bottom-left, etc). // This should really be refactored to use a public IDropDownView, which is much more convenient to use. // First, get a reference to the combo box via reflection. ComboBox combo = ReflectionUtilities.GetValueOfFieldOrProperty("combobox1", legendView) as ComboBox; // fixme - we should support all valid OxyPlot legend position types. foreach (Models.Graph.Graph.LegendPositionType legendPosition in Enum.GetValues(typeof(Models.Graph.Graph.LegendPositionType))) { string name = legendPosition.ToString(); GtkUtilities.SelectComboBoxItem(combo, name, wait: true); OxyPlot.LegendPosition oxyPlotEquivalent = (OxyPlot.LegendPosition)Enum.Parse(typeof(OxyPlot.LegendPosition), name); Assert.AreEqual(plot.Model.LegendPosition, oxyPlotEquivalent); } }