mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-27 00:07:58 +01:00
* Initial plan * Change MessageBox to return nullable int instead of -1 Co-authored-by: tig <585482+tig@users.noreply.github.com> * Initial plan * Add fencing to prevent mixing Application models Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fix fence logic to work with parallel tests Co-authored-by: tig <585482+tig@users.noreply.github.com> * WIP: Fixing Application issues. * Refactor error messages into constants Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor ConfigurationProperty properties to use static backing fields and raise events Co-authored-by: tig <585482+tig@users.noreply.github.com> * Reset static Application properties in ResetStateStatic Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor tests to decouple from global Application state Commented out `driver ??= Application.Driver` assignments in `DriverAssert` to prevent automatic global driver assignment. Removed `Application.ResetState(true)` calls and commented out state validation assertions in `GlobalTestSetup` to reduce dependency on global state. Reintroduced `ApplicationForceDriverTests` and `ApplicationModelFencingTests` to validate `ForceDriver` behavior and ensure proper handling of legacy and modern Application models. Skipped certain `ToAnsiTests` that rely on `Application`. Removed direct `Application.Driver` assignments in `ViewDrawingClippingTests` and `ViewDrawingFlowTests`. Performed general cleanup of redundant code and unused imports to simplify the codebase. * WIP: Fixed Parallel tests; non-Parallel still broken Refactor application model usage tracking Refactored `ApplicationModelUsage` into a public enum in the new `Terminal.Gui.App` namespace, making it accessible across the codebase. Replaced the private `_modelUsage` field in `ApplicationImpl` with a public static `ModelUsage` property to improve clarity and accessibility. Renamed error message constants for consistency and updated methods like `SetInstance` and `MarkInstanceBasedModelUsed` to use the new `ModelUsage` property. Removed the private `ApplicationModelUsage` enum from `ApplicationImpl`. Updated test cases to use `ApplicationImpl.Instance` instead of `Application.Create` to enforce the legacy static model. Skipped obsolete tests in `ApplicationForceDriverTests` and added null checks in `DriverAssert` and `SelectorBase` to handle edge cases. Commented out an unused line in `WindowsOutput` and made general improvements to code readability, maintainability, and consistency. * WIP: Almost there! Refactored tests and code to align with the modern instance-based application model. Key changes include: - Disabled Sixel rendering in `OutputBase.cs` due to dependency on legacy static `Application` object. - Hardcoded `force16Colors` to `false` in `WindowsOutput.cs` with a `BUGBUG` note. - Updated `ApplicationImplTests` to use `ApplicationImpl.SetInstance` and return `ApplicationImpl.Instance`. - Refactored `ApplicationModelFencingTests` to use `Application.Create()` and added `ResetModelUsageTracking()` for model switching. - Removed legacy `DriverTests` and reintroduced updated versions with cross-platform driver tests. - Reverted `ArrangementTests` and `ShortcutTests` to use legacy static `ApplicationImpl.Instance`. - Reintroduced driver tests in `DriverTests.cs` with modern `Application.Create()` and added `TestTop` for driver content verification. - General cleanup, including removal of outdated code and addition of `BUGBUG` notes for temporary workarounds. * Fixed all modelusage bugs? Replaced static `Application` references with instance-based `App` context across the codebase. Updated calls to `Application.RequestStop()` and `Application.Screen` to use `App?.RequestStop()` and `App?.Screen` for better encapsulation and flexibility. Refactored test infrastructure to align with the new context, including reintroducing `FakeApplicationFactory` and `FakeApplicationLifecycle` for testing purposes. Improved logging, error handling, and test clarity by adding `logWriter` support and simplifying test setup. Removed redundant or obsolete code, such as `NetSequences` and the old `FakeApplicationFactory` implementation. Updated documentation to reflect the new `IApplication.RequestStop()` usage. * merged * Refactor KeyboardImpl and modernize MessageBoxTests Refactored the `KeyboardImpl` class to remove hardcoded default key values, replacing them with uninitialized fields for dynamic configuration. Updated key binding logic to use `ReplaceCommands` instead of `Add` for better handling of dynamic changes. Removed unnecessary `KeyBindings.Clear()` calls to avoid side effects. Rewrote `MessageBoxTests.cs` to improve readability, maintainability, and adherence to modern C# standards. Enabled nullable reference checks, updated the namespace, and restructured test methods for clarity. Marked non-functional tests with `[Theory(Skip)]` and improved test organization with parameterized inputs. Enhanced test assertions, lifecycle handling, and error handling across the test suite. Updated `UICatalog_AboutBox` to use multiline string literals for expected outputs. These changes improve the overall maintainability and flexibility of the codebase. * Atempt to fix windows only CI/CD Unit tests failure Refactor Application lifecycle and test cleanup Refactored the `Application` class to phase out legacy static properties `SessionStack` and `TopRunnable` from `Application.Current.cs`. These were reintroduced in a new file `Application.TopRunnable.cs` for better modularity, while retaining their `[Obsolete]` status. Updated `ApplicationPopoverTests.cs` to replace `Application.ResetState(true)` with `Application.Shutdown()` for consistent application state cleanup. Added explicit cleanup for `Application.TopRunnable` in relevant test cases to ensure proper resource management. Adjusted namespaces and `using` directives to support the new structure. These changes improve code organization and align with updated application lifecycle management practices. * Fixes #<Issue> - Dispose TopRunnable in cleanup logic Updated the `finally` block in `ApplicationPopoverTests` to dispose of the `Application.TopRunnable` object if it is not null, ensuring proper resource cleanup. Previously, the property was being set to `null` without disposal. The `Application.Shutdown()` call remains unchanged. * Improve thread safety, reduce static dependencies, and align the codebase with the updated `IApplication` interface. Refactored the `MainThreadId` property to improve encapsulation: - Updated `Application.MainThreadId` to use `ApplicationImpl.Instance` directly. - Added `MainThreadId` to `ApplicationImpl` and `IApplication`. - Removed redundant `MainThreadId` from `ApplicationImpl.Run.cs`. Updated `EnqueueMouseEvent` to include an `IApplication?` parameter: - Modified `FakeInputProcessor`, `InputProcessorImpl`, and `WindowsInputProcessor` to support the new parameter. - Updated `IInputProcessor` interface to reflect the new method signature. - Adjusted `GuiTestContext` and `EnqueueMouseEventTests` to pass `IApplication` where required. Improved test coverage and code maintainability: - Added test cases for negative positions and empty mouse events. - Commented out legacy code in `GraphView` and `FakeDriverBase`. - Enhanced readability in `EnqueueMouseEventTests`. These changes improve thread safety, reduce static dependencies, and align the codebase with the updated `IApplication` interface. * Fixed more bugs. Enabled nullable reference types across multiple files to improve code safety. Refactored and modularized test classes, improving readability and maintainability. Removed outdated test cases and added new tests for edge cases, including culture-specific and non-Gregorian calendar handling. Addressed timeout issues in `ScenarioTests` with a watchdog timer and improved error handling. Updated `ApplicationImplTests` to use instance fields instead of static references for better test isolation. Refactored `ScenarioTests` to dynamically load and test all UI Catalog scenarios, with macOS-specific skips for known issues. Aligned `MessageBox.Query` calls with updated API signatures. Performed general code cleanup, including removing unused directives, improving formatting, and consolidating repetitive logic into helper methods. * Made the `InputBindings<TEvent, TBinding>` class thread-safe by replacing the internal `Dictionary<TEvent, TBinding>` with `ConcurrentDictionary<TEvent, TBinding>`. This fixes parallel test failures where "Collection was modified; enumeration operation may not execute" exceptions were thrown. ## Changes Made ### 1. InputBindings.cs - **File**: `Terminal.Gui/Input/InputBindings.cs` - **Change**: Replaced `Dictionary` with `ConcurrentDictionary` - **Key modifications**: - Changed `_bindings` from `Dictionary<TEvent, TBinding>` to `ConcurrentDictionary<TEvent, TBinding>` - Updated `Add()` methods to use `TryAdd()` instead of checking with `TryGet()` then `Add()` - Updated `Remove()` to use `TryRemove()` (no need to check existence first) - Updated `ReplaceCommands()` to use `ContainsKey()` instead of `TryGet()` - Added `.ToList()` to `GetAllFromCommands()` to create a snapshot for safe enumeration - Added comment explaining that `ConcurrentDictionary` provides snapshot enumeration in `GetBindings()` - Added `.ToArray()` to `Clear(Command[])` to create snapshot before iteration ### 2. Thread Safety Test Suite - **File**: `Tests/UnitTestsParallelizable/Input/InputBindingsThreadSafetyTests.cs` - **New file** with comprehensive thread safety tests: - `Add_ConcurrentAccess_NoExceptions` - Tests concurrent additions - `GetBindings_DuringConcurrentModification_NoExceptions` - Tests enumeration during modifications - `TryGet_ConcurrentAccess_ReturnsConsistentResults` - Tests concurrent reads - `Clear_ConcurrentAccess_NoExceptions` - Tests concurrent clearing - `Remove_ConcurrentAccess_NoExceptions` - Tests concurrent removals - `Replace_ConcurrentAccess_NoExceptions` - Tests concurrent replacements - `GetAllFromCommands_DuringModification_NoExceptions` - Tests LINQ queries during modifications - `MixedOperations_ConcurrentAccess_NoExceptions` - Tests mixed operations (add/read/remove) - `KeyBindings_ConcurrentAccess_NoExceptions` - Tests actual `KeyBindings` class - `MouseBindings_ConcurrentAccess_NoExceptions` - Tests actual `MouseBindings` class ## Benefits of ConcurrentDictionary Approach 1. **Lock-Free Reads**: Most read operations don't require locks, improving performance 2. **Snapshot Enumeration**: Built-in support for safe enumeration during concurrent modifications 3. **Simplified Code**: No need for explicit `lock` statements or lock objects 4. **Better Scalability**: Multiple threads can read/write simultaneously 5. **No "Collection was modified" Exceptions**: Enumeration creates a snapshot ## Performance Characteristics - **Read Operations**: Lock-free, very fast - **Write Operations**: Uses fine-grained locking internally, minimal contention - **Memory Overhead**: Slightly higher than `Dictionary` but negligible in practice - **Enumeration**: Creates a snapshot, safe for concurrent modifications ## Test Results - **Original failing test now passes**: `ApplicationImplTests.Init_CreatesKeybindings` - **10 new thread safety tests**: All passing - **All 11,741 parallelizable tests**: All passing (11,731 passed, 10 skipped) - **All 1,779 non-parallelizable tests**: All passing (1,762 passed, 17 skipped) - **No compilation errors**: Clean build with no xUnit1031 warnings (suppressed with pragmas) ## Verification The original failure was: ``` System.InvalidOperationException: Collection was modified; enumeration operation may not execute. ``` This occurred in parallelizable tests when multiple threads accessed `KeyBindings.GetBindings()` simultaneously. The `ConcurrentDictionary` implementation resolves this by providing thread-safe operations and snapshot enumeration. ## Notes - The xUnit1031 warnings about using `Task.WaitAll` instead of `async/await` have been suppressed with `#pragma warning disable xUnit1031` directives, as these are intentional blocking operations in stress tests that test concurrent scenarios - All existing functionality is preserved; this is a drop-in replacement - No changes to public API surface - Existing tests continue to pass * Make InputBindings and KeyboardImpl thread-safe for concurrent access Replace Dictionary with ConcurrentDictionary in InputBindings<TEvent, TBinding> and KeyboardImpl to enable safe parallel test execution and multi-threaded usage. Changes: - InputBindings: Replace Dictionary with ConcurrentDictionary for _bindings - InputBindings: Make Replace() atomic using AddOrUpdate instead of Remove+Add - InputBindings: Make ReplaceCommands() atomic using AddOrUpdate - InputBindings: Add IsValid() check to both Add() overloads - InputBindings: Add defensive .ToList()/.ToArray() for safe LINQ enumeration - KeyboardImpl: Replace Dictionary with ConcurrentDictionary for _commandImplementations - KeyboardImpl: Change AddKeyBindings() to use ReplaceCommands for idempotent initialization - Add 10 comprehensive thread safety tests for InputBindings - Add 9 comprehensive thread safety tests for KeyboardImpl The ConcurrentDictionary implementation provides: - Lock-free reads for better performance under concurrent access - Atomic operations for Replace/ReplaceCommands preventing race conditions - Snapshot enumeration preventing "Collection was modified" exceptions - No breaking API changes - maintains backward compatibility All 11,750 parallelizable tests pass (11,740 passed, 10 skipped). Fixes race conditions that caused ApplicationImplTests.Init_CreatesKeybindings to fail intermittently during parallel test execution. * Decouple ApplicationImpl from Application static props Removed initialization of `Force16Colors` and `ForceDriver` from `Application` static properties in the `ApplicationImpl` constructor. The class still subscribes to the `Force16ColorsChanged` and `ForceDriverChanged` events, but no longer sets initial values for these properties. This change simplifies the constructor and reduces coupling between `ApplicationImpl` and `Application`. * Refactored keyboard initialization in `ApplicationImpl` to use `Application` static properties for default key assignments, ensuring synchronization with pre-`Init()` changes. Improved `KeyboardImpl` initialization to avoid premature `ApplicationImpl.Instance` access, enhancing testability. Standardized constant naming conventions and improved code readability in thread safety tests for `KeyboardImpl` and `InputBindings`. Updated `TestInputBindings` implementation for clarity and conciseness. Applied consistent code style improvements across files, including spacing, formatting, and variable naming, to enhance maintainability and readability. * Fix race conditions in parallel tests - thread-safe ApplicationImpl and KeyboardImpl Fixes intermittent failures in parallel tests caused by three separate race conditions: 1. **KeyboardImpl constructor race condition** - Constructor was accessing Application.QuitKey/ArrangeKey/etc which triggered ApplicationImpl.Instance getter, setting ModelUsage=LegacyStatic before Application.Create() was called - Changed constructor to initialize keys with hard-coded defaults instead - Added synchronization from Application static properties during Init() 2. **InputBindings.Replace() race condition** - Between GetOrAdd(oldEventArgs) and AddOrUpdate(newEventArgs), another thread could modify bindings, causing stale data to overwrite valid bindings - Added early return for same-key case (oldEventArgs == newEventArgs) - Kept atomic operations with proper updateValueFactory handling - Added detailed thread-safety documentation 3. **ApplicationImpl model usage fence checks race condition** - Two threads calling Init() simultaneously could both pass fence checks before either set ModelUsage, allowing improper model mixing - Added _modelUsageLock for thread-safe synchronization - Made all ModelUsage operations atomic (Instance getter, SetInstance, MarkInstanceBasedModelUsed, ResetModelUsageTracking, Init fence checks) **Files Changed:** - Terminal.Gui/App/ApplicationImpl.cs - Added _modelUsageLock, made all ModelUsage access thread-safe - Terminal.Gui/App/ApplicationImpl.Lifecycle.cs - Thread-safe fence checks in Init(), sync keyboard keys from Application properties - Terminal.Gui/App/Keyboard/KeyboardImpl.cs - Fixed constructor to not trigger ApplicationImpl.Instance - Terminal.Gui/Input/InputBindings.cs - Fixed Replace() race condition with proper atomic operations **Testing:** - All 11 ApplicationImplTests pass - All 9 KeyboardImplThreadSafetyTests pass - All 10 InputBindingsThreadSafetyTests pass - No more intermittent "Cannot use modern instance-based model after using legacy static Application model" errors in parallel test execution The root cause was KeyboardImpl constructor accessing Application static properties during object creation, which would lazily initialize ApplicationImpl.Instance and set the wrong ModelUsage before Application.Create() could mark it as InstanceBased. * Warning cleanup * docs: Add comprehensive MessageBox and Clipboard API documentation - Updated MessageBox class docs with nullable return value explanation - Created docfx/docs/messagebox-clipboard-changes-v2.md migration guide - Updated migratingfromv1.md with quick links to major changes - Created PR-SUMMARY.md documenting all changes - Added examples for both instance-based and legacy patterns - Documented application model fencing and thread safety improvements The documentation covers: • MessageBox nullable int? returns (null = cancelled) • Clipboard refactoring from static to instance-based • Application model usage fencing to prevent pattern mixing • Thread safety improvements in KeyboardImpl and InputBindings • Complete migration guide with code examples • Benefits and rationale for all changes * Refactor static properties to use backing fields Refactored static properties in multiple classes (`Button`, `CheckBox`, `Dialog`, `FrameView`, `MessageBox`, `StatusBar`, and `Window`) to use private backing fields for better encapsulation and configurability. Default values are now stored in private static fields, allowing overrides via configuration files (e.g., `Resources/config.json`). Updated property definitions to use `get`/`set` accessors interacting with the backing fields. Retained the `[ConfigurationProperty]` attribute to ensure runtime configurability. Removed redundant code, improved XML documentation, adjusted namespace declarations for consistency, and performed general code cleanup to enhance readability and maintainability. * Fix Windows-only parallel test failure by preventing ConfigurationManager from triggering ApplicationImpl.Instance Problem: `MessageBoxTests.Location_And_Size_Correct` was failing only on Windows in parallel tests with: System.InvalidOperationException: Cannot use modern instance-based model (Application.Create) after using legacy static Application model (Application.Init/ApplicationImpl.Instance). Root Cause (maybe): View classes (MessageBox, Dialog, Window, Button, CheckBox, FrameView, StatusBar) had `[ConfigurationProperty]` decorated auto-properties with inline initializers. When ConfigurationManager's module initializer scanned assemblies using reflection, accessing these auto-properties could trigger lazy initialization of other static members, which in some cases indirectly referenced `ApplicationImpl.Instance`, marking the model as "legacy" before parallel tests called `Application.Create()`. Solution: Converted all `[ConfigurationProperty]` auto-properties in View classes to use private backing fields with explicit getters/setters, matching the pattern used by `Application.QuitKey`. This prevents any code execution during reflection-based property discovery. Files Changed: - Terminal.Gui/Views/MessageBox.cs - 4 properties converted - Terminal.Gui/Views/Dialog.cs - 6 properties converted - Terminal.Gui/Views/Window.cs - 2 properties converted - Terminal.Gui/Views/Button.cs - 2 properties converted - Terminal.Gui/Views/CheckBox.cs - 1 property converted - Terminal.Gui/Views/FrameView.cs - 1 property converted - Terminal.Gui/Views/StatusBar.cs - 1 property converted Test Reorganization: - Moved `ConfigurationManagerTests.GetConfigPropertiesByScope_Gets` from UnitTestsParallelizable to UnitTests (defines custom ConfigurationProperty which affects global state) - Moved `SourcesManagerTests.Sources_StaysConsistentWhenUpdateFails` from UnitTestsParallelizable to UnitTests (modifies static ConfigurationManager.ThrowOnJsonErrors property) Best Practice: All `[ConfigurationProperty]` decorated static properties should use private backing fields to avoid triggering lazy initialization during ConfigurationManager's module initialization. Fixes: Windows-only parallel test failure in MessageBoxTests * Add thread-safety to CollectionNavigator classes - Add lock-based synchronization to CollectionNavigatorBase for _searchString and _lastKeystroke fields - Add lock-based synchronization to CollectionNavigator for Collection property access - Protect ElementAt and GetCollectionLength methods with locks - Add 6 comprehensive thread-safety tests covering: - Concurrent SearchString access - Concurrent Collection property access - Concurrent navigation operations (50 parallel tasks) - Concurrent collection modification with readers/writers - Concurrent search string changes - Stress test with 100 tasks × 1000 operations each All tests pass (31/31) including new thread-safety tests. The implementation uses lock-based synchronization rather than concurrent collections because: - IList interface is not thread-safe by design - CollectionNavigator is internal and used by UI components (ListView/TreeView) - Matches existing Terminal.Gui patterns (Scope<T>, ConfigProperty) - Provides simpler and more predictable behavior Fixes thread-safety issues when CollectionNavigator is accessed from multiple threads. * cleanup * Run parallel unit tests 10 times with varying parallelization to expose concurrency issues Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fix parallel unit tests workflow - use proper xUnit parallelization parameters Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fix environment variable reference in workflow - use proper bash syntax Co-authored-by: tig <585482+tig@users.noreply.github.com> * Run parallel tests 10 times sequentially instead of matrix expansion Co-authored-by: tig <585482+tig@users.noreply.github.com> * Make ConfigurationManager thread-safe - use ConcurrentDictionary and add locks Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add Debug.Fail to detect legacy Application usage in parallelizable tests Co-authored-by: tig <585482+tig@users.noreply.github.com> * Move ScrollSliderTests to UnitTests project - they access legacy Application model Co-authored-by: tig <585482+tig@users.noreply.github.com> * Revert ScrollSliderTests move and document root cause analysis Co-authored-by: tig <585482+tig@users.noreply.github.com> * Remove Debug.Fail and move ScrollSliderTests to UnitTests project Co-authored-by: tig <585482+tig@users.noreply.github.com> * Re-add Debug.Fail to detect legacy Application usage in parallelizable tests Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor tests and improve parallelization support Commented out `Debug.Fail` statements in `Application.Lifecycle.cs` and `ApplicationImpl.cs` to prevent interruptions during parallel tests. Refactored `ToString` in `ApplicationImpl.cs` to use an expression-bodied member and removed unused imports. Rewrote tests in `ClipRegionTests.cs` and `ScrollSliderTests.cs` to remove global state dependencies and migrated them to the `UnitTests_Parallelizable` namespace. Enabled nullable annotations and updated assertions for clarity and modern patterns. Improved test coverage by adding scenarios for clamping, layout, and size calculations. Updated `README.md` to include `[SetupFakeApplication]` in the list of patterns that block parallelization and clarified migration guidelines. Replaced `[SetupFakeDriver]` with `[SetupFakeApplication]` in examples. Added `<Folder Include="Drivers\" />` to `UnitTests.csproj` for better organization. Adjusted test project references to reflect test migration. Enhanced test output validation in `ScrollSliderTests.cs`. Removed redundant test cases and improved documentation to align with modern C# practices and ensure maintainability. * marked as a "TODO" for potential future configurability. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tig <585482+tig@users.noreply.github.com> Co-authored-by: Tig <tig@users.noreply.github.com>
1648 lines
52 KiB
C#
1648 lines
52 KiB
C#
using System.Text;
|
|
using UnitTests;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace UnitTests.ViewsTests;
|
|
|
|
#region Helper Classes
|
|
|
|
internal class FakeHAxis : HorizontalAxis
|
|
{
|
|
public List<Point> DrawAxisLinePoints = new ();
|
|
public List<int> LabelPoints = new ();
|
|
|
|
public override void DrawAxisLabel (GraphView graph, int screenPosition, string text)
|
|
{
|
|
base.DrawAxisLabel (graph, screenPosition, text);
|
|
LabelPoints.Add (screenPosition);
|
|
}
|
|
|
|
protected override void DrawAxisLine (GraphView graph, int x, int y)
|
|
{
|
|
base.DrawAxisLine (graph, x, y);
|
|
DrawAxisLinePoints.Add (new Point (x, y));
|
|
}
|
|
}
|
|
|
|
internal class FakeVAxis : VerticalAxis
|
|
{
|
|
public List<Point> DrawAxisLinePoints = new ();
|
|
public List<int> LabelPoints = new ();
|
|
|
|
public override void DrawAxisLabel (GraphView graph, int screenPosition, string text)
|
|
{
|
|
base.DrawAxisLabel (graph, screenPosition, text);
|
|
LabelPoints.Add (screenPosition);
|
|
}
|
|
|
|
protected override void DrawAxisLine (GraphView graph, int x, int y)
|
|
{
|
|
base.DrawAxisLine (graph, x, y);
|
|
DrawAxisLinePoints.Add (new Point (x, y));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
public class GraphViewTests : FakeDriverBase
|
|
{
|
|
/// <summary>
|
|
/// A cell size of 0 would result in mapping all graph space into the same cell of the console. Since
|
|
/// <see cref="GraphView.CellSize"/> is mutable a sensible place to check this is in redraw.
|
|
/// </summary>
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void CellSizeZero ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
//gv.Scheme = new Scheme ();
|
|
gv.Viewport = new Rectangle (0, 0, 50, 30);
|
|
gv.Series.Add (new ScatterSeries { Points = new List<PointF> { new (1, 1) } });
|
|
gv.CellSize = new PointF (0, 5);
|
|
var ex = Assert.Throws<Exception> (() => gv.Draw ());
|
|
|
|
Assert.Equal ("CellSize cannot be 0", ex.Message);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
/// <summary>Returns a basic very small graph (10 x 5)</summary>
|
|
/// <returns></returns>
|
|
public static GraphView GetGraph ()
|
|
{
|
|
var gv = new GraphView ()
|
|
{
|
|
Driver = Application.Driver ?? CreateFakeDriver ()
|
|
};
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
//gv.Scheme = new Scheme ();
|
|
gv.MarginBottom = 1;
|
|
gv.MarginLeft = 1;
|
|
gv.Viewport = new Rectangle (0, 0, 10, 5);
|
|
|
|
return gv;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Tests that each point in the screen space maps to a rectangle of (float) graph space and that each corner of
|
|
/// that rectangle of graph space maps back to the same row/col of the graph that was fed in
|
|
/// </summary>
|
|
[Fact]
|
|
public void TestReversing_ScreenToGraphSpace ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
gv.Viewport = new Rectangle (0, 0, 50, 30);
|
|
|
|
// How much graph space each cell of the console depicts
|
|
gv.CellSize = new PointF (0.1f, 0.25f);
|
|
gv.AxisX.Increment = 1;
|
|
gv.AxisX.ShowLabelsEvery = 1;
|
|
|
|
gv.AxisY.Increment = 1;
|
|
gv.AxisY.ShowLabelsEvery = 1;
|
|
|
|
// Start the graph at 80
|
|
gv.ScrollOffset = new PointF (0, 80);
|
|
|
|
for (var x = 0; x < gv.Viewport.Width; x++)
|
|
{
|
|
for (var y = 0; y < gv.Viewport.Height; y++)
|
|
{
|
|
RectangleF graphSpace = gv.ScreenToGraphSpace (x, y);
|
|
|
|
// See
|
|
// https://en.wikipedia.org/wiki/Machine_epsilon
|
|
var epsilon = 0.0001f;
|
|
|
|
Point p = gv.GraphSpaceToScreen (
|
|
new PointF (
|
|
graphSpace.Left + epsilon,
|
|
graphSpace.Top + epsilon
|
|
)
|
|
);
|
|
Assert.Equal (x, p.X);
|
|
Assert.Equal (y, p.Y);
|
|
|
|
p = gv.GraphSpaceToScreen (
|
|
new PointF (
|
|
graphSpace.Right - epsilon,
|
|
graphSpace.Top + epsilon
|
|
)
|
|
);
|
|
Assert.Equal (x, p.X);
|
|
Assert.Equal (y, p.Y);
|
|
|
|
p = gv.GraphSpaceToScreen (
|
|
new PointF (
|
|
graphSpace.Left + epsilon,
|
|
graphSpace.Bottom - epsilon
|
|
)
|
|
);
|
|
Assert.Equal (x, p.X);
|
|
Assert.Equal (y, p.Y);
|
|
|
|
p = gv.GraphSpaceToScreen (
|
|
new PointF (
|
|
graphSpace.Right - epsilon,
|
|
graphSpace.Bottom - epsilon
|
|
)
|
|
);
|
|
Assert.Equal (x, p.X);
|
|
Assert.Equal (y, p.Y);
|
|
}
|
|
}
|
|
}
|
|
|
|
#region Screen to Graph Tests
|
|
|
|
[Fact]
|
|
public void ScreenToGraphSpace_DefaultCellSize ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
|
|
// origin should be bottom left
|
|
RectangleF botLeft = gv.ScreenToGraphSpace (0, 9);
|
|
Assert.Equal (0, botLeft.X);
|
|
Assert.Equal (0, botLeft.Y);
|
|
Assert.Equal (1, botLeft.Width);
|
|
Assert.Equal (1, botLeft.Height);
|
|
|
|
// up 2 rows of the console and along 1 col
|
|
RectangleF up2along1 = gv.ScreenToGraphSpace (1, 7);
|
|
Assert.Equal (1, up2along1.X);
|
|
Assert.Equal (2, up2along1.Y);
|
|
}
|
|
|
|
[Fact]
|
|
public void ScreenToGraphSpace_DefaultCellSize_WithMargin ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
|
|
// origin should be bottom left
|
|
RectangleF botLeft = gv.ScreenToGraphSpace (0, 9);
|
|
Assert.Equal (0, botLeft.X);
|
|
Assert.Equal (0, botLeft.Y);
|
|
Assert.Equal (1, botLeft.Width);
|
|
Assert.Equal (1, botLeft.Height);
|
|
|
|
gv.MarginLeft = 1;
|
|
|
|
botLeft = gv.ScreenToGraphSpace (0, 9);
|
|
|
|
// Origin should be at 1,9 now to leave a margin of 1
|
|
// so screen position 0,9 would be data space -1,0
|
|
Assert.Equal (-1, botLeft.X);
|
|
Assert.Equal (0, botLeft.Y);
|
|
Assert.Equal (1, botLeft.Width);
|
|
Assert.Equal (1, botLeft.Height);
|
|
|
|
gv.MarginLeft = 1;
|
|
gv.MarginBottom = 1;
|
|
|
|
botLeft = gv.ScreenToGraphSpace (0, 9);
|
|
|
|
// Origin should be at 1,0 (to leave a margin of 1 in both sides)
|
|
// so screen position 0,9 would be data space -1,-1
|
|
Assert.Equal (-1, botLeft.X);
|
|
Assert.Equal (-1, botLeft.Y);
|
|
Assert.Equal (1, botLeft.Width);
|
|
Assert.Equal (1, botLeft.Height);
|
|
}
|
|
|
|
[Fact]
|
|
public void ScreenToGraphSpace_CustomCellSize ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
|
|
// Each cell of screen measures 5 units in graph data model vertically and 1/4 horizontally
|
|
gv.CellSize = new PointF (0.25f, 5);
|
|
|
|
// origin should be bottom left
|
|
// (note that y=10 is actually overspilling the control, the last row is 9)
|
|
RectangleF botLeft = gv.ScreenToGraphSpace (0, 9);
|
|
Assert.Equal (0, botLeft.X);
|
|
Assert.Equal (0, botLeft.Y);
|
|
Assert.Equal (0.25f, botLeft.Width);
|
|
Assert.Equal (5, botLeft.Height);
|
|
|
|
// up 2 rows of the console and along 1 col
|
|
RectangleF up2along1 = gv.ScreenToGraphSpace (1, 7);
|
|
Assert.Equal (0.25f, up2along1.X);
|
|
Assert.Equal (10, up2along1.Y);
|
|
Assert.Equal (0.25f, botLeft.Width);
|
|
Assert.Equal (5, botLeft.Height);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Graph to Screen Tests
|
|
|
|
[Fact]
|
|
public void GraphSpaceToScreen_DefaultCellSize ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
|
|
// origin should be bottom left
|
|
Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
|
|
Assert.Equal (0, botLeft.X);
|
|
Assert.Equal (9, botLeft.Y); // row 9 of the view is the bottom left
|
|
|
|
// along 2 and up 1 in graph space
|
|
Point along2up1 = gv.GraphSpaceToScreen (new PointF (2, 1));
|
|
Assert.Equal (2, along2up1.X);
|
|
Assert.Equal (8, along2up1.Y);
|
|
}
|
|
|
|
[Fact]
|
|
public void GraphSpaceToScreen_DefaultCellSize_WithMargin ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
|
|
// origin should be bottom left
|
|
Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
|
|
Assert.Equal (0, botLeft.X);
|
|
Assert.Equal (9, botLeft.Y); // row 9 of the view is the bottom left
|
|
|
|
gv.MarginLeft = 1;
|
|
|
|
// With a margin of 1 the origin should be at x=1 y= 9
|
|
botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
|
|
Assert.Equal (1, botLeft.X);
|
|
Assert.Equal (9, botLeft.Y); // row 9 of the view is the bottom left
|
|
|
|
gv.MarginLeft = 1;
|
|
gv.MarginBottom = 1;
|
|
|
|
// With a margin of 1 in both directions the origin should be at x=1 y= 9
|
|
botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
|
|
Assert.Equal (1, botLeft.X);
|
|
Assert.Equal (8, botLeft.Y); // row 8 of the view is the bottom left up 1 cell
|
|
}
|
|
|
|
[Fact]
|
|
public void GraphSpaceToScreen_ScrollOffset ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
|
|
//graph is scrolled to present chart space -5 to 5 in both axes
|
|
gv.ScrollOffset = new PointF (-5, -5);
|
|
|
|
// origin should be right in the middle of the control
|
|
Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
|
|
Assert.Equal (5, botLeft.X);
|
|
Assert.Equal (4, botLeft.Y);
|
|
|
|
// along 2 and up 1 in graph space
|
|
Point along2up1 = gv.GraphSpaceToScreen (new PointF (2, 1));
|
|
Assert.Equal (7, along2up1.X);
|
|
Assert.Equal (3, along2up1.Y);
|
|
}
|
|
|
|
[Fact]
|
|
public void GraphSpaceToScreen_CustomCellSize ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
|
|
// Each cell of screen is responsible for rendering 5 units in graph data model
|
|
// vertically and 1/4 horizontally
|
|
gv.CellSize = new PointF (0.25f, 5);
|
|
|
|
// origin should be bottom left
|
|
Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
|
|
Assert.Equal (0, botLeft.X);
|
|
|
|
// row 9 of the view is the bottom left (height is 10 so 0,1,2,3..9)
|
|
Assert.Equal (9, botLeft.Y);
|
|
|
|
// along 2 and up 1 in graph space
|
|
Point along2up1 = gv.GraphSpaceToScreen (new PointF (2, 1));
|
|
Assert.Equal (8, along2up1.X);
|
|
Assert.Equal (9, along2up1.Y);
|
|
|
|
// Y value 4 should be rendered in bottom most row
|
|
Assert.Equal (9, gv.GraphSpaceToScreen (new PointF (2, 4)).Y);
|
|
|
|
// Cell height is 5 so this is the first point of graph space that should
|
|
// be rendered in the graph in next row up (row 9)
|
|
Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 5)).Y);
|
|
|
|
// More boundary testing for this cell size
|
|
Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 6)).Y);
|
|
Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 7)).Y);
|
|
Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 8)).Y);
|
|
Assert.Equal (8, gv.GraphSpaceToScreen (new PointF (2, 9)).Y);
|
|
Assert.Equal (7, gv.GraphSpaceToScreen (new PointF (2, 10)).Y);
|
|
Assert.Equal (7, gv.GraphSpaceToScreen (new PointF (2, 11)).Y);
|
|
}
|
|
|
|
[Fact]
|
|
public void GraphSpaceToScreen_CustomCellSize_WithScrollOffset ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
|
|
// Each cell of screen is responsible for rendering 5 units in graph data model
|
|
// vertically and 1/4 horizontally
|
|
gv.CellSize = new PointF (0.25f, 5);
|
|
|
|
//graph is scrolled to present some negative chart (4 negative cols and 2 negative rows)
|
|
gv.ScrollOffset = new PointF (-1, -10);
|
|
|
|
// origin should be in the lower left (but not right at the bottom)
|
|
Point botLeft = gv.GraphSpaceToScreen (new PointF (0, 0));
|
|
Assert.Equal (4, botLeft.X);
|
|
Assert.Equal (7, botLeft.Y);
|
|
|
|
// along 2 and up 1 in graph space
|
|
Point along2up1 = gv.GraphSpaceToScreen (new PointF (2, 1));
|
|
Assert.Equal (12, along2up1.X);
|
|
Assert.Equal (7, along2up1.Y);
|
|
|
|
// More boundary testing for this cell size/offset
|
|
Assert.Equal (6, gv.GraphSpaceToScreen (new PointF (2, 6)).Y);
|
|
Assert.Equal (6, gv.GraphSpaceToScreen (new PointF (2, 7)).Y);
|
|
Assert.Equal (6, gv.GraphSpaceToScreen (new PointF (2, 8)).Y);
|
|
Assert.Equal (6, gv.GraphSpaceToScreen (new PointF (2, 9)).Y);
|
|
Assert.Equal (5, gv.GraphSpaceToScreen (new PointF (2, 10)).Y);
|
|
Assert.Equal (5, gv.GraphSpaceToScreen (new PointF (2, 11)).Y);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public class SeriesTests
|
|
{
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Series_GetsPassedCorrectViewport_AllAtOnce ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
gv.Viewport = new Rectangle (0, 0, 50, 30);
|
|
//gv.Scheme = new Scheme ();
|
|
|
|
var fullGraphBounds = RectangleF.Empty;
|
|
var graphScreenBounds = Rectangle.Empty;
|
|
|
|
var series = new FakeSeries (
|
|
(v, s, g) =>
|
|
{
|
|
graphScreenBounds = s;
|
|
fullGraphBounds = g;
|
|
}
|
|
);
|
|
gv.Series.Add (series);
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
Assert.Equal (new RectangleF (0, 0, 50, 30), fullGraphBounds);
|
|
Assert.Equal (new Rectangle (0, 0, 50, 30), graphScreenBounds);
|
|
|
|
// Now we put a margin in
|
|
// Graph should not spill into the margins
|
|
|
|
gv.MarginBottom = 2;
|
|
gv.MarginLeft = 5;
|
|
|
|
// Even with a margin the graph should be drawn from
|
|
// the origin, we just get less visible width/height
|
|
gv.LayoutSubViews ();
|
|
gv.SetNeedsDraw ();
|
|
gv.Draw ();
|
|
Assert.Equal (new RectangleF (0, 0, 45, 28), fullGraphBounds);
|
|
|
|
// The screen space the graph will be rendered into should
|
|
// not overspill the margins
|
|
Assert.Equal (new Rectangle (5, 0, 45, 28), graphScreenBounds);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tests that the bounds passed to the ISeries for drawing into are correct even when the
|
|
/// <see cref="GraphView.CellSize"/> results in multiple units of graph space being condensed into each cell of console
|
|
/// </summary>
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Series_GetsPassedCorrectViewport_AllAtOnce_LargeCellSize ()
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
//gv.Scheme = new Scheme ();
|
|
gv.Viewport = new Rectangle (0, 0, 50, 30);
|
|
|
|
// the larger the cell size the more condensed (smaller) the graph space is
|
|
gv.CellSize = new PointF (2, 5);
|
|
|
|
var fullGraphBounds = RectangleF.Empty;
|
|
var graphScreenBounds = Rectangle.Empty;
|
|
|
|
var series = new FakeSeries (
|
|
(v, s, g) =>
|
|
{
|
|
graphScreenBounds = s;
|
|
fullGraphBounds = g;
|
|
}
|
|
);
|
|
|
|
gv.Series.Add (series);
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
// Since each cell of the console is 2x5 of graph space the graph
|
|
// bounds to be rendered are larger
|
|
Assert.Equal (new RectangleF (0, 0, 100, 150), fullGraphBounds);
|
|
Assert.Equal (new Rectangle (0, 0, 50, 30), graphScreenBounds);
|
|
|
|
// Graph should not spill into the margins
|
|
|
|
gv.MarginBottom = 2;
|
|
gv.MarginLeft = 5;
|
|
|
|
// Even with a margin the graph should be drawn from
|
|
// the origin, we just get less visible width/height
|
|
gv.LayoutSubViews ();
|
|
gv.SetNeedsDraw ();
|
|
gv.Draw ();
|
|
Assert.Equal (new RectangleF (0, 0, 90, 140), fullGraphBounds);
|
|
|
|
// The screen space the graph will be rendered into should
|
|
// not overspill the margins
|
|
Assert.Equal (new Rectangle (5, 0, 45, 28), graphScreenBounds);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
private class FakeSeries : ISeries
|
|
{
|
|
private readonly Action<GraphView, Rectangle, RectangleF> _drawSeries;
|
|
|
|
public FakeSeries (
|
|
Action<GraphView, Rectangle, RectangleF> drawSeries
|
|
)
|
|
{
|
|
_drawSeries = drawSeries;
|
|
}
|
|
|
|
public void DrawSeries (GraphView graph, Rectangle bounds, RectangleF graphBounds) { _drawSeries (graph, bounds, graphBounds); }
|
|
}
|
|
}
|
|
|
|
public class MultiBarSeriesTests
|
|
{
|
|
private readonly ITestOutputHelper _output;
|
|
public MultiBarSeriesTests (ITestOutputHelper output) { _output = output; }
|
|
|
|
[Fact]
|
|
public void MultiBarSeries_BarSpacing ()
|
|
{
|
|
// Creates clusters of 5 adjacent bars with 2 spaces between clusters
|
|
var series = new MultiBarSeries (5, 7, 1);
|
|
|
|
Assert.Equal (5, series.SubSeries.Count);
|
|
|
|
Assert.Equal (0, series.SubSeries.ElementAt (0).Offset);
|
|
Assert.Equal (1, series.SubSeries.ElementAt (1).Offset);
|
|
Assert.Equal (2, series.SubSeries.ElementAt (2).Offset);
|
|
Assert.Equal (3, series.SubSeries.ElementAt (3).Offset);
|
|
Assert.Equal (4, series.SubSeries.ElementAt (4).Offset);
|
|
}
|
|
|
|
[Fact]
|
|
public void MultiBarSeriesAddValues_WrongNumber ()
|
|
{
|
|
// user asks for 3 bars per category
|
|
var series = new MultiBarSeries (3, 7, 1);
|
|
|
|
var ex = Assert.Throws<ArgumentException> (() => series.AddBars ("Cars", (Rune)'#', 1));
|
|
|
|
Assert.Equal (
|
|
"Number of values must match the number of bars per category (Parameter 'values')",
|
|
ex.Message
|
|
);
|
|
}
|
|
|
|
[Fact]
|
|
public void MultiBarSeriesColors_RightNumber ()
|
|
{
|
|
Attribute [] colors =
|
|
{
|
|
new (Color.Green, Color.Black), new (Color.Green, Color.White), new (Color.BrightYellow, Color.White)
|
|
};
|
|
|
|
// user passes 3 colors and asks for 3 bars
|
|
var series = new MultiBarSeries (3, 7, 1, colors);
|
|
|
|
Assert.Equal (series.SubSeries.ElementAt (0).OverrideBarColor, colors [0]);
|
|
Assert.Equal (series.SubSeries.ElementAt (1).OverrideBarColor, colors [1]);
|
|
Assert.Equal (series.SubSeries.ElementAt (2).OverrideBarColor, colors [2]);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
public void MultiBarSeriesColors_WrongNumber ()
|
|
{
|
|
Attribute [] colors = { new (Color.Green, Color.Black) };
|
|
|
|
// user passes 1 color only but asks for 5 bars
|
|
var ex = Assert.Throws<ArgumentException> (() => new MultiBarSeries (5, 7, 1, colors));
|
|
|
|
Assert.Equal (
|
|
"Number of colors must match the number of bars (Parameter 'numberOfBarsPerCategory')",
|
|
ex.Message
|
|
);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestRendering_MultibarSeries ()
|
|
{
|
|
var gv = new GraphView ()
|
|
{
|
|
App = ApplicationImpl.Instance,
|
|
};
|
|
//gv.Scheme = new Scheme ();
|
|
|
|
// y axis goes from 0.1 to 1 across 10 console rows
|
|
// x axis goes from 0 to 20 across 20 console columns
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
gv.CellSize = new PointF (1f, 0.1f);
|
|
gv.MarginBottom = 1;
|
|
gv.MarginLeft = 1;
|
|
|
|
var multibarSeries = new MultiBarSeries (2, 4, 1);
|
|
|
|
//nudge them left to avoid float rounding errors at the boundaries of cells
|
|
foreach (BarSeries sub in multibarSeries.SubSeries)
|
|
{
|
|
sub.Offset -= 0.001f;
|
|
}
|
|
|
|
gv.Series.Add (multibarSeries);
|
|
|
|
FakeHAxis fakeXAxis;
|
|
|
|
// don't show axis labels that means any labels
|
|
// that appear are explicitly from the bars
|
|
gv.AxisX = fakeXAxis = new FakeHAxis { Increment = 0 };
|
|
gv.AxisY = new FakeVAxis { Increment = 0 };
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
// Since bar series has no bars yet no labels should be displayed
|
|
Assert.Empty (fakeXAxis.LabelPoints);
|
|
|
|
multibarSeries.AddBars ("hey", (Rune)'M', 0.5001f, 0.5001f);
|
|
fakeXAxis.LabelPoints.Clear ();
|
|
gv.LayoutSubViews ();
|
|
gv.SetNeedsDraw ();
|
|
gv.Draw ();
|
|
|
|
Assert.Equal (4, fakeXAxis.LabelPoints.Single ());
|
|
|
|
multibarSeries.AddBars ("there", (Rune)'M', 0.24999f, 0.74999f);
|
|
multibarSeries.AddBars ("bob", (Rune)'M', 1, 2);
|
|
fakeXAxis.LabelPoints.Clear ();
|
|
gv.LayoutSubViews ();
|
|
gv.SetNeedsDraw ();
|
|
gv.SetClipToScreen ();
|
|
gv.Draw ();
|
|
|
|
Assert.Equal (3, fakeXAxis.LabelPoints.Count);
|
|
Assert.Equal (4, fakeXAxis.LabelPoints [0]);
|
|
Assert.Equal (8, fakeXAxis.LabelPoints [1]);
|
|
Assert.Equal (12, fakeXAxis.LabelPoints [2]);
|
|
|
|
var looksLike =
|
|
@"
|
|
│ MM
|
|
│ M MM
|
|
│ M MM
|
|
│ MM M MM
|
|
│ MM M MM
|
|
│ MM M MM
|
|
│ MM MM MM
|
|
│ MM MM MM
|
|
┼──┬M──┬M──┬M──────
|
|
heytherebob ";
|
|
DriverAssert.AssertDriverContentsAre (looksLike, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
}
|
|
|
|
public class BarSeriesTests : FakeDriverBase
|
|
{
|
|
[Fact]
|
|
public void TestOneLongOneShortHorizontalBars_WithOffset ()
|
|
{
|
|
GraphView graph = GetGraph (out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
|
|
graph.Driver = CreateFakeDriver ();
|
|
graph.Draw ();
|
|
|
|
// no bars
|
|
Assert.Empty (barSeries.BarScreenStarts);
|
|
Assert.Empty (axisX.LabelPoints);
|
|
Assert.Empty (axisY.LabelPoints);
|
|
|
|
// 0.1 units of graph y fit every screen row
|
|
// so 1 unit of graph y space is 10 screen rows
|
|
graph.CellSize = new PointF (0.5f, 0.1f);
|
|
|
|
// Start bar 3 screen units up (y = height-3)
|
|
barSeries.Offset = 0.25f;
|
|
|
|
// 1 bar every 3 rows of screen
|
|
barSeries.BarEvery = 0.3f;
|
|
barSeries.Orientation = Orientation.Horizontal;
|
|
|
|
// 1 bar that is very wide (100 graph units horizontally = screen pos 50 but bounded by screen)
|
|
barSeries.Bars.Add (
|
|
new BarSeriesBar ("hi1", new GraphCellToRender ((Rune)'.'), 100)
|
|
);
|
|
|
|
// 1 bar that is shorter
|
|
barSeries.Bars.Add (
|
|
new BarSeriesBar ("hi2", new GraphCellToRender ((Rune)'.'), 5)
|
|
);
|
|
|
|
// redraw graph
|
|
graph.SetNeedsDraw ();
|
|
graph.Draw ();
|
|
|
|
// since bars are horizontal all have the same X start cordinates
|
|
Assert.Equal (0, barSeries.BarScreenStarts [0].X);
|
|
Assert.Equal (0, barSeries.BarScreenStarts [1].X);
|
|
|
|
// bar goes all the way to the end so bumps up against right screen boundary
|
|
// width of graph is 20
|
|
Assert.Equal (19, barSeries.BarScreenEnds [0].X);
|
|
|
|
// shorter bar is 5 graph units wide which is 10 screen units
|
|
Assert.Equal (10, barSeries.BarScreenEnds [1].X);
|
|
|
|
// first bar should be offset 6 screen units (0.25f + 0.3f graph units)
|
|
// since height of control is 10 then first bar should be at screen row 4 (10-6)
|
|
Assert.Equal (4, barSeries.BarScreenStarts [0].Y);
|
|
|
|
// second bar should be offset 9 screen units (0.25f + 0.6f graph units)
|
|
// since height of control is 10 then second bar should be at screen row 1 (10-9)
|
|
Assert.Equal (1, barSeries.BarScreenStarts [1].Y);
|
|
|
|
// both bars should have labels but on the y axis
|
|
Assert.Equal (2, axisY.LabelPoints.Count);
|
|
Assert.Empty (axisX.LabelPoints);
|
|
|
|
// labels should align with the bars (same screen y axis point)
|
|
Assert.Contains (4, axisY.LabelPoints);
|
|
Assert.Contains (1, axisY.LabelPoints);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestTwoTallBars_WithOffset ()
|
|
{
|
|
GraphView graph = GetGraph (out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
|
|
graph.Draw ();
|
|
|
|
// no bars
|
|
Assert.Empty (barSeries.BarScreenStarts);
|
|
Assert.Empty (axisX.LabelPoints);
|
|
Assert.Empty (axisY.LabelPoints);
|
|
|
|
// 0.5 units of graph fit every screen cell
|
|
// so 1 unit of graph space is 2 screen columns
|
|
graph.CellSize = new PointF (0.5f, 0.1f);
|
|
|
|
// Start bar 1 screen unit along
|
|
barSeries.Offset = 0.5f;
|
|
barSeries.BarEvery = 1f;
|
|
|
|
barSeries.Bars.Add (
|
|
new BarSeriesBar ("hi1", new GraphCellToRender ((Rune)'.'), 100)
|
|
);
|
|
|
|
barSeries.Bars.Add (
|
|
new BarSeriesBar ("hi2", new GraphCellToRender ((Rune)'.'), 100)
|
|
);
|
|
|
|
barSeries.Orientation = Orientation.Vertical;
|
|
|
|
// redraw graph
|
|
graph.SetNeedsDraw ();
|
|
graph.Draw ();
|
|
|
|
// bar should be drawn at BarEvery 1f + offset 0.5f = 3 screen units
|
|
Assert.Equal (3, barSeries.BarScreenStarts [0].X);
|
|
Assert.Equal (3, barSeries.BarScreenEnds [0].X);
|
|
|
|
// second bar should be BarEveryx2 = 2f + offset 0.5f = 5 screen units
|
|
Assert.Equal (5, barSeries.BarScreenStarts [1].X);
|
|
Assert.Equal (5, barSeries.BarScreenEnds [1].X);
|
|
|
|
// both bars should have labels
|
|
Assert.Equal (2, axisX.LabelPoints.Count);
|
|
Assert.Contains (3, axisX.LabelPoints);
|
|
Assert.Contains (5, axisX.LabelPoints);
|
|
|
|
// bars are very tall but should not draw up off top of screen
|
|
Assert.Equal (9, barSeries.BarScreenStarts [0].Y);
|
|
Assert.Equal (0, barSeries.BarScreenEnds [0].Y);
|
|
Assert.Equal (9, barSeries.BarScreenStarts [1].Y);
|
|
Assert.Equal (0, barSeries.BarScreenEnds [1].Y);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestZeroHeightBar_WithName ()
|
|
{
|
|
GraphView graph = GetGraph (out FakeBarSeries barSeries, out FakeHAxis axisX, out FakeVAxis axisY);
|
|
graph.Draw ();
|
|
|
|
// no bars
|
|
Assert.Empty (barSeries.BarScreenStarts);
|
|
Assert.Empty (axisX.LabelPoints);
|
|
Assert.Empty (axisY.LabelPoints);
|
|
|
|
// bar of height 0
|
|
barSeries.Bars.Add (new BarSeriesBar ("hi", new GraphCellToRender ((Rune)'.'), 0));
|
|
barSeries.Orientation = Orientation.Vertical;
|
|
|
|
// redraw graph
|
|
graph.SetNeedsDraw ();
|
|
graph.Draw ();
|
|
|
|
// bar should not be drawn
|
|
Assert.Empty (barSeries.BarScreenStarts);
|
|
|
|
Assert.NotEmpty (axisX.LabelPoints);
|
|
Assert.Empty (axisY.LabelPoints);
|
|
|
|
// but bar name should be
|
|
// Screen position x=2 because bars are drawn every 1f of
|
|
// graph space and CellSize.X is 0.5f
|
|
Assert.Contains (2, axisX.LabelPoints);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
private GraphView GetGraph (out FakeBarSeries series, out FakeHAxis axisX, out FakeVAxis axisY)
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.BeginInit ();
|
|
gv.EndInit ();
|
|
|
|
// y axis goes from 0.1 to 1 across 10 console rows
|
|
// x axis goes from 0 to 10 across 20 console columns
|
|
gv.Viewport = new Rectangle (0, 0, 20, 10);
|
|
//gv.Scheme = new Scheme ();
|
|
gv.CellSize = new PointF (0.5f, 0.1f);
|
|
|
|
gv.Series.Add (series = new FakeBarSeries ());
|
|
|
|
// don't show axis labels that means any labels
|
|
// that appaer are explicitly from the bars
|
|
gv.AxisX = axisX = new FakeHAxis { Increment = 0 };
|
|
gv.AxisY = axisY = new FakeVAxis { Increment = 0 };
|
|
|
|
return gv;
|
|
}
|
|
|
|
private class FakeBarSeries : BarSeries
|
|
{
|
|
public List<Point> BarScreenEnds { get; } = new ();
|
|
public List<Point> BarScreenStarts { get; } = new ();
|
|
public GraphCellToRender FinalColor { get; private set; }
|
|
protected override GraphCellToRender AdjustColor (GraphCellToRender graphCellToRender) { return FinalColor = base.AdjustColor (graphCellToRender); }
|
|
|
|
protected override void DrawBarLine (GraphView graph, Point start, Point end, BarSeriesBar beingDrawn)
|
|
{
|
|
base.DrawBarLine (graph, start, end, beingDrawn);
|
|
|
|
BarScreenStarts.Add (start);
|
|
BarScreenEnds.Add (end);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class AxisTests
|
|
{
|
|
private GraphView GetGraph (out FakeHAxis axis) { return GetGraph (out axis, out _); }
|
|
private GraphView GetGraph (out FakeVAxis axis) { return GetGraph (out _, out axis); }
|
|
|
|
private GraphView GetGraph (out FakeHAxis axisX, out FakeVAxis axisY)
|
|
{
|
|
var gv = new GraphView ();
|
|
gv.Viewport = new Rectangle (0, 0, 50, 30);
|
|
// gv.Scheme = new Scheme ();
|
|
|
|
// graph can't be completely empty or it won't draw
|
|
gv.Series.Add (new ScatterSeries ());
|
|
|
|
axisX = new FakeHAxis ();
|
|
axisY = new FakeVAxis ();
|
|
gv.AxisX = axisX;
|
|
gv.AxisY = axisY;
|
|
|
|
return gv;
|
|
}
|
|
|
|
#region HorizontalAxis Tests
|
|
|
|
/// <summary>Tests that the horizontal axis is computed correctly and does not over spill it's bounds</summary>
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestHAxisLocation_NoMargin ()
|
|
{
|
|
GraphView gv = GetGraph (out FakeHAxis axis);
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
Assert.DoesNotContain (new Point (-1, 29), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (0, 29), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (1, 29), axis.DrawAxisLinePoints);
|
|
|
|
Assert.Contains (new Point (48, 29), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (49, 29), axis.DrawAxisLinePoints);
|
|
Assert.DoesNotContain (new Point (50, 29), axis.DrawAxisLinePoints);
|
|
|
|
Assert.InRange (axis.LabelPoints.Max (), 0, 49);
|
|
Assert.InRange (axis.LabelPoints.Min (), 0, 49);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestHAxisLocation_MarginBottom ()
|
|
{
|
|
GraphView gv = GetGraph (out FakeHAxis axis);
|
|
|
|
gv.MarginBottom = 10;
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
Assert.DoesNotContain (new Point (-1, 19), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (0, 19), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (1, 19), axis.DrawAxisLinePoints);
|
|
|
|
Assert.Contains (new Point (48, 19), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (49, 19), axis.DrawAxisLinePoints);
|
|
Assert.DoesNotContain (new Point (50, 19), axis.DrawAxisLinePoints);
|
|
|
|
Assert.InRange (axis.LabelPoints.Max (), 0, 49);
|
|
Assert.InRange (axis.LabelPoints.Min (), 0, 49);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestHAxisLocation_MarginLeft ()
|
|
{
|
|
GraphView gv = GetGraph (out FakeHAxis axis);
|
|
|
|
gv.MarginLeft = 5;
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
Assert.DoesNotContain (new Point (4, 29), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (5, 29), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (6, 29), axis.DrawAxisLinePoints);
|
|
|
|
Assert.Contains (new Point (48, 29), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (49, 29), axis.DrawAxisLinePoints);
|
|
Assert.DoesNotContain (new Point (50, 29), axis.DrawAxisLinePoints);
|
|
|
|
// Axis lables should not be drawn in the margin
|
|
Assert.InRange (axis.LabelPoints.Max (), 5, 49);
|
|
Assert.InRange (axis.LabelPoints.Min (), 5, 49);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region VerticalAxisTests
|
|
|
|
/// <summary>Tests that the horizontal axis is computed correctly and does not over spill it's bounds</summary>
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestVAxisLocation_NoMargin ()
|
|
{
|
|
GraphView gv = GetGraph (out FakeVAxis axis);
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
Assert.DoesNotContain (new Point (0, -1), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (0, 1), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (0, 2), axis.DrawAxisLinePoints);
|
|
|
|
Assert.Contains (new Point (0, 28), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (0, 29), axis.DrawAxisLinePoints);
|
|
Assert.DoesNotContain (new Point (0, 30), axis.DrawAxisLinePoints);
|
|
|
|
Assert.InRange (axis.LabelPoints.Max (), 0, 29);
|
|
Assert.InRange (axis.LabelPoints.Min (), 0, 29);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestVAxisLocation_MarginBottom ()
|
|
{
|
|
GraphView gv = GetGraph (out FakeVAxis axis);
|
|
|
|
gv.MarginBottom = 10;
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
Assert.DoesNotContain (new Point (0, -1), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (0, 1), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (0, 2), axis.DrawAxisLinePoints);
|
|
|
|
Assert.Contains (new Point (0, 18), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (0, 19), axis.DrawAxisLinePoints);
|
|
Assert.DoesNotContain (new Point (0, 20), axis.DrawAxisLinePoints);
|
|
|
|
// Labels should not be drawn into the axis
|
|
Assert.InRange (axis.LabelPoints.Max (), 0, 19);
|
|
Assert.InRange (axis.LabelPoints.Min (), 0, 19);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestVAxisLocation_MarginLeft ()
|
|
{
|
|
GraphView gv = GetGraph (out FakeVAxis axis);
|
|
|
|
gv.MarginLeft = 5;
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
Assert.DoesNotContain (new Point (5, -1), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (5, 1), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (5, 2), axis.DrawAxisLinePoints);
|
|
|
|
Assert.Contains (new Point (5, 28), axis.DrawAxisLinePoints);
|
|
Assert.Contains (new Point (5, 29), axis.DrawAxisLinePoints);
|
|
Assert.DoesNotContain (new Point (5, 30), axis.DrawAxisLinePoints);
|
|
|
|
Assert.InRange (axis.LabelPoints.Max (), 0, 29);
|
|
Assert.InRange (axis.LabelPoints.Min (), 0, 29);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public class TextAnnotationTests
|
|
{
|
|
private readonly ITestOutputHelper _output;
|
|
public TextAnnotationTests (ITestOutputHelper output) { _output = output; }
|
|
|
|
[Theory]
|
|
[InlineData (null)]
|
|
[InlineData (" ")]
|
|
[InlineData ("\t\t")]
|
|
[AutoInitShutdown]
|
|
public void TestTextAnnotation_EmptyText (string whitespace)
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
|
|
gv.Annotations.Add (
|
|
new TextAnnotation { Text = whitespace, GraphPosition = new PointF (4, 2) }
|
|
);
|
|
|
|
// add a point a bit further along the graph so if the whitespace were rendered
|
|
// the test would pick it up (AssertDriverContentsAre ignores trailing whitespace on lines)
|
|
var points = new ScatterSeries ();
|
|
points.Points.Add (new PointF (7, 2));
|
|
gv.Series.Add (points);
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@$"
|
|
│
|
|
┤ {Glyphs.Dot}
|
|
┤
|
|
0┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestTextAnnotation_GraphUnits ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
|
|
gv.Annotations.Add (
|
|
new TextAnnotation { Text = "hey!", GraphPosition = new PointF (2, 2) }
|
|
);
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@"
|
|
│
|
|
┤ hey!
|
|
┤
|
|
0┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// user scrolls up one unit of graph space
|
|
gv.ScrollOffset = new PointF (0, 1f);
|
|
gv.SetNeedsDraw ();
|
|
gv.SetClipToScreen ();
|
|
gv.Draw ();
|
|
|
|
// we expect the text annotation to go down one line since
|
|
// the scroll offset means that that point of graph space is
|
|
// lower down in the view. Note the 1 on the axis too, our viewport
|
|
// (excluding margins) now shows y of 1 to 4 (previously 0 to 5)
|
|
expected =
|
|
@"
|
|
│
|
|
┤
|
|
┤ hey!
|
|
1┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestTextAnnotation_LongText ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
|
|
gv.Annotations.Add (
|
|
new TextAnnotation
|
|
{
|
|
Text = "hey there partner hows it going boy its great", GraphPosition = new PointF (2, 2)
|
|
}
|
|
);
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.SetNeedsDraw ();
|
|
gv.Draw ();
|
|
|
|
// long text should get truncated
|
|
// margin takes up 1 units
|
|
// the GraphPosition of the anntation is 2
|
|
// Leaving 7 characters of the annotation renderable (including space)
|
|
var expected =
|
|
@"
|
|
│
|
|
┤ hey the
|
|
┤
|
|
0┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestTextAnnotation_Offscreen ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
|
|
gv.Annotations.Add (
|
|
new TextAnnotation
|
|
{
|
|
Text = "hey there partner hows it going boy its great", GraphPosition = new PointF (9, 2)
|
|
}
|
|
);
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
// Text is off the screen (graph x axis runs to 8 not 9)
|
|
var expected =
|
|
@"
|
|
│
|
|
┤
|
|
┤
|
|
0┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void TestTextAnnotation_ScreenUnits ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
|
|
gv.Annotations.Add (
|
|
new TextAnnotation { Text = "hey!", ScreenPosition = new Point (3, 1) }
|
|
);
|
|
gv.LayoutSubViews ();
|
|
gv.SetClipToScreen ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@"
|
|
│
|
|
┤ hey!
|
|
┤
|
|
0┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// user scrolls up one unit of graph space
|
|
gv.ScrollOffset = new PointF (0, 1f);
|
|
gv.SetNeedsDraw ();
|
|
gv.SetClipToScreen ();
|
|
gv.Draw ();
|
|
|
|
// we expect no change in the location of the annotation (only the axis label changes)
|
|
// this is because screen units are constant and do not change as the viewport into
|
|
// graph space scrolls to different areas of the graph
|
|
expected =
|
|
@"
|
|
│
|
|
┤ hey!
|
|
┤
|
|
1┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// user scrolls up one unit of graph space
|
|
gv.ScrollOffset = new PointF (0, 1f);
|
|
gv.SetNeedsDraw ();
|
|
gv.SetClipToScreen ();
|
|
gv.Draw ();
|
|
|
|
// we expect no change in the location of the annotation (only the axis label changes)
|
|
// this is because screen units are constant and do not change as the viewport into
|
|
// graph space scrolls to different areas of the graph
|
|
expected =
|
|
@"
|
|
│
|
|
┤ hey!
|
|
┤
|
|
1┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
}
|
|
|
|
public class LegendTests
|
|
{
|
|
private readonly ITestOutputHelper _output;
|
|
public LegendTests (ITestOutputHelper output) { _output = output; }
|
|
|
|
[Fact]
|
|
public void Constructors_Defaults ()
|
|
{
|
|
var legend = new LegendAnnotation ();
|
|
Assert.Equal (Rectangle.Empty, legend.Viewport);
|
|
Assert.Equal (Rectangle.Empty, legend.Frame);
|
|
Assert.Equal (LineStyle.Single, legend.BorderStyle);
|
|
Assert.False (legend.BeforeSeries);
|
|
|
|
var bounds = new Rectangle (1, 2, 10, 3);
|
|
legend = new LegendAnnotation (bounds);
|
|
Assert.Equal (new Rectangle (0, 0, 8, 1), legend.Viewport);
|
|
Assert.Equal (bounds, legend.Frame);
|
|
Assert.Equal (LineStyle.Single, legend.BorderStyle);
|
|
Assert.False (legend.BeforeSeries);
|
|
legend.BorderStyle = LineStyle.None;
|
|
Assert.Equal (new Rectangle (0, 0, 10, 3), legend.Viewport);
|
|
Assert.Equal (bounds, legend.Frame);
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void LegendNormalUsage_WithBorder ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
var legend = new LegendAnnotation (new Rectangle (2, 0, 5, 3));
|
|
legend.AddEntry (new GraphCellToRender ((Rune)'A'), "Ant");
|
|
legend.AddEntry (new GraphCellToRender ((Rune)'B'), "Bat");
|
|
|
|
gv.Annotations.Add (legend);
|
|
gv.Layout ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@"
|
|
│┌───┐
|
|
┤│AAn│
|
|
┤└───┘
|
|
0┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void LegendNormalUsage_WithoutBorder ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
var legend = new LegendAnnotation (new Rectangle (2, 0, 5, 3));
|
|
legend.AddEntry (new GraphCellToRender ((Rune)'A'), "Ant");
|
|
legend.AddEntry (new GraphCellToRender ((Rune)'B'), "?"); // this will exercise pad
|
|
legend.AddEntry (new GraphCellToRender ((Rune)'C'), "Cat");
|
|
legend.AddEntry (new GraphCellToRender ((Rune)'H'), "Hattter"); // not enough space for this oen
|
|
legend.BorderStyle = LineStyle.None;
|
|
|
|
gv.Annotations.Add (legend);
|
|
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@"
|
|
│AAnt
|
|
┤B?
|
|
┤CCat
|
|
0┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
}
|
|
|
|
public class PathAnnotationTests
|
|
{
|
|
private readonly ITestOutputHelper _output;
|
|
public PathAnnotationTests (ITestOutputHelper output) { _output = output; }
|
|
|
|
[Fact]
|
|
public void MarginBottom_BiggerThanHeight_ExpectBlankGraph ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
gv.Height = 10;
|
|
gv.MarginBottom = 20;
|
|
|
|
gv.Series.Add (
|
|
new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
|
|
);
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@"
|
|
|
|
|
|
";
|
|
DriverAssert.AssertDriverContentsAre (expected, _output, gv.Driver);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
public void MarginLeft_BiggerThanWidth_ExpectBlankGraph ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
gv.Width = 10;
|
|
gv.MarginLeft = 20;
|
|
|
|
gv.Series.Add (
|
|
new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
|
|
);
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@"
|
|
|
|
|
|
";
|
|
DriverAssert.AssertDriverContentsAre (expected, _output, gv.Driver);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void PathAnnotation_Box ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
|
|
var path = new PathAnnotation ();
|
|
path.Points.Add (new PointF (1, 1));
|
|
path.Points.Add (new PointF (1, 3));
|
|
path.Points.Add (new PointF (6, 3));
|
|
path.Points.Add (new PointF (6, 1));
|
|
|
|
// list the starting point again so that it draws a complete square
|
|
// (otherwise it will miss out the last line along the bottom)
|
|
path.Points.Add (new PointF (1, 1));
|
|
|
|
gv.Annotations.Add (path);
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@"
|
|
│......
|
|
┤. .
|
|
┤......
|
|
0┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void PathAnnotation_Diamond ()
|
|
{
|
|
GraphView gv = GraphViewTests.GetGraph ();
|
|
|
|
var path = new PathAnnotation ();
|
|
path.Points.Add (new PointF (1, 2));
|
|
path.Points.Add (new PointF (3, 3));
|
|
path.Points.Add (new PointF (6, 2));
|
|
path.Points.Add (new PointF (3, 1));
|
|
|
|
// list the starting point again to close the shape
|
|
path.Points.Add (new PointF (1, 2));
|
|
|
|
gv.Annotations.Add (path);
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@"
|
|
│ ..
|
|
┤.. ..
|
|
┤ ...
|
|
0┼┬┬┬┬┬┬┬┬
|
|
0 5";
|
|
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Theory]
|
|
[AutoInitShutdown]
|
|
[InlineData (true)]
|
|
[InlineData (false)]
|
|
public void ViewChangeText_RendersCorrectly (bool useFill)
|
|
{
|
|
// create a wide window
|
|
var mount = new View { Width = 100, Height = 100 };
|
|
var top = new Toplevel ();
|
|
|
|
try
|
|
{
|
|
// Create a view with a short text
|
|
var view = new View { Text = "ff", Width = 2, Height = 1 };
|
|
|
|
// Specify that the label should be very wide
|
|
if (useFill)
|
|
{
|
|
view.Width = Dim.Fill ();
|
|
}
|
|
else
|
|
{
|
|
view.Width = 100;
|
|
}
|
|
|
|
//put label into view
|
|
mount.Add (view);
|
|
|
|
//putting mount into Toplevel since changing size
|
|
top.Add (mount);
|
|
Application.Begin (top);
|
|
|
|
// render view
|
|
//view.Scheme = new Scheme ();
|
|
Assert.Equal (1, view.Height);
|
|
mount.SetNeedsDraw ();
|
|
mount.Draw ();
|
|
|
|
// should have the initial text
|
|
DriverAssert.AssertDriverContentsAre ("ff", null);
|
|
|
|
// change the text and redraw
|
|
view.Text = "ff1234";
|
|
mount.SetNeedsDraw ();
|
|
top.SetClipToScreen ();
|
|
mount.Draw ();
|
|
|
|
// should have the new text rendered
|
|
DriverAssert.AssertDriverContentsAre ("ff1234", null);
|
|
}
|
|
finally
|
|
{
|
|
top.Dispose ();
|
|
Application.Shutdown ();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void XAxisLabels_With_MarginLeft ()
|
|
{
|
|
var gv = new GraphView
|
|
{
|
|
App = ApplicationImpl.Instance,
|
|
Viewport = new Rectangle (0, 0, 10, 7)
|
|
};
|
|
|
|
gv.CellSize = new PointF (1, 0.5f);
|
|
gv.AxisY.Increment = 1;
|
|
gv.AxisY.ShowLabelsEvery = 1;
|
|
|
|
gv.Series.Add (
|
|
new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
|
|
);
|
|
|
|
// reserve 3 cells of the left for the margin
|
|
gv.MarginLeft = 3;
|
|
gv.MarginBottom = 1;
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.SetNeedsDraw ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@$"
|
|
│
|
|
2┤
|
|
│
|
|
1┤{Glyphs.Dot}
|
|
│
|
|
0┼┬┬┬┬{Glyphs.Dot}┬
|
|
0 5
|
|
|
|
";
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void YAxisLabels_With_MarginBottom ()
|
|
{
|
|
var gv = new GraphView
|
|
{
|
|
App = ApplicationImpl.Instance,
|
|
Viewport = new Rectangle (0, 0, 10, 7)
|
|
};
|
|
|
|
gv.CellSize = new PointF (1, 0.5f);
|
|
gv.AxisY.Increment = 1;
|
|
gv.AxisY.ShowLabelsEvery = 1;
|
|
|
|
gv.Series.Add (
|
|
new ScatterSeries { Points = { new PointF (1, 1), new PointF (5, 0) } }
|
|
);
|
|
|
|
// reserve 3 cells of the console for the margin
|
|
gv.MarginBottom = 3;
|
|
gv.MarginLeft = 1;
|
|
|
|
gv.LayoutSubViews ();
|
|
gv.SetNeedsDraw ();
|
|
gv.Draw ();
|
|
|
|
var expected =
|
|
@$"
|
|
│
|
|
1┤{Glyphs.Dot}
|
|
│
|
|
0┼┬┬┬┬{Glyphs.Dot}┬┬┬
|
|
0 5
|
|
|
|
";
|
|
DriverAssert.AssertDriverContentsAre (expected, _output);
|
|
|
|
// Shutdown must be called to safely clean up Application if Init has been called
|
|
Application.Shutdown ();
|
|
}
|
|
}
|
|
|
|
public class AxisIncrementToRenderTests
|
|
{
|
|
[Fact]
|
|
public void AxisIncrementToRenderTests_Constructor ()
|
|
{
|
|
var render = new AxisIncrementToRender (Orientation.Horizontal, 1, 6.6f);
|
|
|
|
Assert.Equal (Orientation.Horizontal, render.Orientation);
|
|
Assert.Equal (1, render.ScreenLocation);
|
|
Assert.Equal (6.6f, render.Value);
|
|
}
|
|
}
|