Skip to content

Commit

Permalink
Merge pull request #26 from pavkam/backend-consolidation
Browse files Browse the repository at this point in the history
Support for NCurses 6+ mouse ABI and loading from HOMEBREW on Mac
  • Loading branch information
pavkam authored Dec 24, 2022
2 parents 6e45cbc + 2a1f9a8 commit 34936bd
Show file tree
Hide file tree
Showing 37 changed files with 1,207 additions and 689 deletions.
2 changes: 1 addition & 1 deletion Demos/Sharpie.Demos.Bounce/Sharpie.Demos.Bounce.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj" />
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Demos/Sharpie.Demos.Events/Sharpie.Demos.Events.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj" />
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Demos/Sharpie.Demos.Font/Sharpie.Demos.Font.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


<ItemGroup>
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj" />
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Demos/Sharpie.Demos.Slk/Sharpie.Demos.Slk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj" />
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Demos/Sharpie.Demos.Snake/Sharpie.Demos.Snake.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj" />
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Demos/Sharpie.Demos.Windows/Sharpie.Demos.Windows.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj" />
<ProjectReference Include="..\..\Sharpie\Sharpie.csproj"/>
</ItemGroup>

</Project>
121 changes: 104 additions & 17 deletions Sharpie.Tests/CursesBackendTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ private void MockLoadResult(string lib, bool result)
});
}

private void VerifyAttempts(params string[] candidates)
{
foreach (var c in candidates)
{
_dotNetSystemAdapterMock.Verify(
s => s.TryLoadNativeLibrary(c, out It.Ref<IntPtr>.IsAny), Times.Once);
}

_dotNetSystemAdapterMock.Verify(
s => s.TryLoadNativeLibrary(It.IsAny<string>(), out It.Ref<IntPtr>.IsAny), Times.Exactly(candidates.Length));
}

[TestInitialize]
public void TestInitialize()
{
Expand Down Expand Up @@ -147,35 +159,110 @@ public void NCurses2_CallsNCurses1()
out It.Ref<IntPtr>.IsAny), Times.Once);
}

[TestMethod]
public void NCurses2_ForLinux_HasManyOptionsForNCurses()
[TestMethod, DataRow("linux"), DataRow("freebsd")]
public void NCurses2_ForLinuxOrFreeBsd_HasSpecificOptions(string op)
{
_dotNetSystemAdapterMock.Setup(s => s.IsLinux)
.Returns(true);
.Returns(op == "linux");
_dotNetSystemAdapterMock.Setup(s => s.IsFreeBsd)
.Returns(op == "freebsd");

var options = new[]
{
"ncursesw",
"libncursesw.so",
"libncursesw.so.5",
"libncursesw.so.6",
"ncurses",
"libncurses.so",
"libncurses.so.5",
"libncursesw.so.5",
"libncursesw.so",
"ncursesw",
"libncurses.so.6",
"libncurses.so.5",
"libncurses.so",
"ncurses",
};

Should.Throw<CursesInitializationException>(() => CursesBackend.NCurses(_dotNetSystemAdapterMock.Object));

foreach (var option in options)
VerifyAttempts(options);
}

[TestMethod]
public void NCurses2_ForMacOs_TriesToLoadDefault_IfNoHomeBrewFound()
{
_dotNetSystemAdapterMock.Setup(s => s.IsMacOs)
.Returns(true);

Should.Throw<CursesInitializationException>(() => CursesBackend.NCurses(_dotNetSystemAdapterMock.Object));

VerifyAttempts("ncurses");
}

[TestMethod]
public void NCurses2_ForMacOs_ScansTheLibraryDirectory_IfNoHomeBrewFound()
{
_dotNetSystemAdapterMock.Setup(s => s.IsMacOs)
.Returns(true);

_dotNetSystemAdapterMock.Setup(s => s.CombinePaths(It.IsAny<string[]>()))
.Returns((string[] ps) => string.Join("+", ps));
_dotNetSystemAdapterMock.Setup(s => s.GetEnvironmentVariable("HOMEBREW_PREFIX")).Returns("/h");
_dotNetSystemAdapterMock.Setup(s => s.DirectoryExists("/h+lib")).Returns(true);
_dotNetSystemAdapterMock.Setup(s => s.EnumerateFiles("/h+lib")).Returns(new[]
{
_dotNetSystemAdapterMock.Verify(
s => s.TryLoadNativeLibrary(option, It.IsAny<Assembly>(), It.IsAny<DllImportSearchPath?>(),
out It.Ref<IntPtr>.IsAny), Times.Once);
}
"dummy.txt",
"libncurses.10",
"libncurses.dylib",
"libncurses.a.dylib",
"libncurses.1.dylib",
"libncurses.10.dylib"
});

_dotNetSystemAdapterMock.Verify(
s => s.TryLoadNativeLibrary(It.IsAny<string>(), It.IsAny<Assembly>(), It.IsAny<DllImportSearchPath?>(),
out It.Ref<IntPtr>.IsAny), Times.Exactly(options.Length));
Should.Throw<CursesInitializationException>(() => CursesBackend.NCurses(_dotNetSystemAdapterMock.Object));

VerifyAttempts("/h+lib+libncurses.1.dylib", "/h+lib+libncurses.10.dylib", "ncurses");
}

[TestMethod]
public void NCurses2_ForMacOs_ScansTheCellarDirectories_IfNoHomeBrewFound()
{
_dotNetSystemAdapterMock.Setup(s => s.IsMacOs)
.Returns(true);

_dotNetSystemAdapterMock.Setup(s => s.CombinePaths(It.IsAny<string[]>()))
.Returns((string[] ps) => string.Join("+", ps));
_dotNetSystemAdapterMock.Setup(s => s.GetEnvironmentVariable("HOMEBREW_CELLAR")).Returns("/h");
_dotNetSystemAdapterMock.Setup(s => s.DirectoryExists("/h+ncurses")).Returns(true);
_dotNetSystemAdapterMock.Setup(s => s.DirectoryExists("/h+ncurses-one+lib")).Returns(true);
_dotNetSystemAdapterMock.Setup(s => s.DirectoryExists("/h+ncurses-2+lib")).Returns(true);
_dotNetSystemAdapterMock.Setup(s => s.EnumerateDirectories("/h+ncurses")).Returns(new[]
{
"/h+ncurses-one",
"/h+ncurses-2"
});

_dotNetSystemAdapterMock.Setup(s => s.EnumerateFiles("/h+ncurses-one+lib")).Returns(new[]
{
"dummy.txt",
"libncurses.10",
"libncurses.dylib",
"libncurses.a.dylib",
"libncurses.1.dylib",
"libncurses.10.dylib"
});

_dotNetSystemAdapterMock.Setup(s => s.EnumerateFiles("/h+ncurses-2+lib")).Returns(new[]
{
"dummy.txt",
"libncurses.2.dylib",
"libncurses.12.dylib"
});

Should.Throw<CursesInitializationException>(() => CursesBackend.NCurses(_dotNetSystemAdapterMock.Object));

VerifyAttempts(
"/h+ncurses-one+lib+libncurses.1.dylib",
"/h+ncurses-2+lib+libncurses.2.dylib",
"/h+ncurses-one+lib+libncurses.10.dylib",
"/h+ncurses-2+lib+libncurses.12.dylib",
"ncurses");
}

}
192 changes: 192 additions & 0 deletions Sharpie.Tests/CursesMouseEventParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
Copyright (c) 2022, Alexandru Ciobanu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

namespace Sharpie.Tests;

[TestClass]
public class CursesMouseEventParserTests
{
[TestMethod]
public void Get_ReturnsObject_ForAbi1()
{
var r = CursesMouseEventParser.Get(1);
r.ShouldNotBeNull();

r.ReportPosition.ShouldBe(8u << 24);
r.All.ShouldBe((8u << 24) - 1);
}

[TestMethod]
public void Get_ReturnsObject_ForAbi2()
{
var r = CursesMouseEventParser.Get(2);
r.ShouldNotBeNull();

r.ReportPosition.ShouldBe(8u << 25);
r.All.ShouldBe((8u << 25) - 1);
}

[TestMethod]
public void Get_ReturnsAbi1_ForUnknownAbi()
{
CursesMouseEventParser.Get(-1).ShouldBe(CursesMouseEventParser.Get(1));
}

[TestMethod,
DataRow(1u << ((1 - 1) * 6), MouseButton.Button1, MouseButtonState.Released),
DataRow(2u << ((1 - 1) * 6), MouseButton.Button1, MouseButtonState.Pressed),
DataRow(4u << ((1 - 1) * 6), MouseButton.Button1, MouseButtonState.Clicked),
DataRow(8u << ((1 - 1) * 6), MouseButton.Button1, MouseButtonState.DoubleClicked),
DataRow(16u << ((1 - 1) * 6), MouseButton.Button1, MouseButtonState.TripleClicked),

DataRow(1u << ((2 - 1) * 6), MouseButton.Button2, MouseButtonState.Released),
DataRow(2u << ((2 - 1) * 6), MouseButton.Button2, MouseButtonState.Pressed),
DataRow(4u << ((2 - 1) * 6), MouseButton.Button2, MouseButtonState.Clicked),
DataRow(8u << ((2 - 1) * 6), MouseButton.Button2, MouseButtonState.DoubleClicked),
DataRow(16u << ((2 - 1) * 6), MouseButton.Button2, MouseButtonState.TripleClicked),

DataRow(1u << ((3 - 1) * 6), MouseButton.Button3, MouseButtonState.Released),
DataRow(2u << ((3 - 1) * 6), MouseButton.Button3, MouseButtonState.Pressed),
DataRow(4u << ((3 - 1) * 6), MouseButton.Button3, MouseButtonState.Clicked),
DataRow(8u << ((3 - 1) * 6), MouseButton.Button3, MouseButtonState.DoubleClicked),
DataRow(16u << ((3 - 1) * 6), MouseButton.Button3, MouseButtonState.TripleClicked),

DataRow(1u << ((4 - 1) * 6), MouseButton.Button4, MouseButtonState.Released),
DataRow(2u << ((4 - 1) * 6), MouseButton.Button4, MouseButtonState.Pressed),
DataRow(4u << ((4 - 1) * 6), MouseButton.Button4, MouseButtonState.Clicked),
DataRow(8u << ((4 - 1) * 6), MouseButton.Button4, MouseButtonState.DoubleClicked),
DataRow(16u << ((4 - 1) * 6), MouseButton.Button4, MouseButtonState.TripleClicked)]
public void Parse_ParsesTheButtonAndState_ForAbi1(uint raw, MouseButton expButton, MouseButtonState expState)
{
var parser = CursesMouseEventParser.Get(1);
var p = parser.Parse(raw);

p.ShouldBe((expButton, expState, ModifierKey.None));
}


[TestMethod,
DataRow(1u << ((1 - 1) * 5), MouseButton.Button1, MouseButtonState.Released),
DataRow(2u << ((1 - 1) * 5), MouseButton.Button1, MouseButtonState.Pressed),
DataRow(4u << ((1 - 1) * 5), MouseButton.Button1, MouseButtonState.Clicked),
DataRow(8u << ((1 - 1) * 5), MouseButton.Button1, MouseButtonState.DoubleClicked),
DataRow(16u << ((1 - 1) * 5), MouseButton.Button1, MouseButtonState.TripleClicked),

DataRow(1u << ((2 - 1) * 5), MouseButton.Button2, MouseButtonState.Released),
DataRow(2u << ((2 - 1) * 5), MouseButton.Button2, MouseButtonState.Pressed),
DataRow(4u << ((2 - 1) * 5), MouseButton.Button2, MouseButtonState.Clicked),
DataRow(8u << ((2 - 1) * 5), MouseButton.Button2, MouseButtonState.DoubleClicked),
DataRow(16u << ((2 - 1) * 5), MouseButton.Button2, MouseButtonState.TripleClicked),

DataRow(1u << ((3 - 1) * 5), MouseButton.Button3, MouseButtonState.Released),
DataRow(2u << ((3 - 1) * 5), MouseButton.Button3, MouseButtonState.Pressed),
DataRow(4u << ((3 - 1) * 5), MouseButton.Button3, MouseButtonState.Clicked),
DataRow(8u << ((3 - 1) * 5), MouseButton.Button3, MouseButtonState.DoubleClicked),
DataRow(16u << ((3 - 1) * 5), MouseButton.Button3, MouseButtonState.TripleClicked),

DataRow(1u << ((4 - 1) * 5), MouseButton.Button4, MouseButtonState.Released),
DataRow(2u << ((4 - 1) * 5), MouseButton.Button4, MouseButtonState.Pressed),
DataRow(4u << ((4 - 1) * 5), MouseButton.Button4, MouseButtonState.Clicked),
DataRow(8u << ((4 - 1) * 5), MouseButton.Button4, MouseButtonState.DoubleClicked),
DataRow(16u << ((4 - 1) * 5), MouseButton.Button4, MouseButtonState.TripleClicked),

DataRow(1u << ((5 - 1) * 5), MouseButton.Button5, MouseButtonState.Released),
DataRow(2u << ((5 - 1) * 5), MouseButton.Button5, MouseButtonState.Pressed),
DataRow(4u << ((5 - 1) * 5), MouseButton.Button5, MouseButtonState.Clicked),
DataRow(8u << ((5 - 1) * 5), MouseButton.Button5, MouseButtonState.DoubleClicked),
DataRow(16u << ((5 - 1) * 5), MouseButton.Button5, MouseButtonState.TripleClicked),
]
public void Parse_ParsesTheButtonAndState_ForAbi2(uint raw, MouseButton expButton, MouseButtonState expState)
{
var parser = CursesMouseEventParser.Get(2);
var p = parser.Parse(raw);

p.ShouldBe((expButton, expState, ModifierKey.None));
}

[TestMethod,
DataRow(0u, ModifierKey.None),
DataRow(1u << 24, ModifierKey.Ctrl),
DataRow(2u << 24, ModifierKey.Shift),
DataRow(4u << 24, ModifierKey.Alt),
DataRow((4u << 24) | (1u << 24), ModifierKey.Alt | ModifierKey.Ctrl),
DataRow((2u << 24) | (1u << 24), ModifierKey.Shift | ModifierKey.Ctrl),
DataRow((4u << 24) | (2u << 24), ModifierKey.Alt | ModifierKey.Shift),
DataRow((1u << 24) | (2u << 24), ModifierKey.Ctrl | ModifierKey.Shift),
DataRow((4u << 24) | (1u << 24) | (2u << 24),
ModifierKey.Alt | ModifierKey.Shift | ModifierKey.Ctrl)]
public void Parse_ParsesTheModifiers_ForAbi1(uint raw, ModifierKey expMod)
{
var parser = CursesMouseEventParser.Get(1);
var p = parser.Parse(raw | 1u);

p.ShouldBe((MouseButton.Button1, MouseButtonState.Released,expMod));
}


[TestMethod,
DataRow(0u, ModifierKey.None),
DataRow(1u << 25, ModifierKey.Ctrl),
DataRow(2u << 25, ModifierKey.Shift),
DataRow(4u << 25, ModifierKey.Alt),
DataRow((4u << 25) | (1u << 25), ModifierKey.Alt | ModifierKey.Ctrl),
DataRow((2u << 25) | (1u << 25), ModifierKey.Shift | ModifierKey.Ctrl),
DataRow((4u << 25) | (2u << 25), ModifierKey.Alt | ModifierKey.Shift),
DataRow((1u << 25) | (2u << 25), ModifierKey.Ctrl | ModifierKey.Shift),
DataRow((4u << 25) | (1u << 25) | (2u << 25),
ModifierKey.Alt | ModifierKey.Shift | ModifierKey.Ctrl)]
public void Parse_ParsesTheModifiers_ForAbi2(uint raw, ModifierKey expMod)
{
var parser = CursesMouseEventParser.Get(2);
var p = parser.Parse(raw | 1u);

p.ShouldBe((MouseButton.Button1, MouseButtonState.Released,expMod));
}

[TestMethod,
DataRow(0u), DataRow(8u << 24), DataRow(1u << 24)]
public void Parse_ReturnsNullIfNoButtonPresent_ForAbi1(uint raw)
{
var parser = CursesMouseEventParser.Get(1);
var p = parser.Parse(raw);

p.ShouldBeNull();
}

[TestMethod,
DataRow(0u), DataRow(8u << 25), DataRow(1u << 25)]
public void Parse_ReturnsNullIfNoButtonPresent_ForAbi2(uint raw)
{
var parser = CursesMouseEventParser.Get(2);
var p = parser.Parse(raw);

p.ShouldBeNull();
}
}
Loading

0 comments on commit 34936bd

Please sign in to comment.