Skip to content

Commit

Permalink
Add tests for DeviceExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Dec 30, 2023
1 parent d3a38e6 commit c1764a2
Show file tree
Hide file tree
Showing 27 changed files with 392 additions and 122 deletions.
8 changes: 4 additions & 4 deletions AdvancedSharpAdbClient.Tests/AdbClientTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async void GetDevicesAsyncTest()
[Fact]
public async void CreateForwardAsyncTest() =>
await RunCreateForwardAsyncTest(
(device) => TestClient.CreateForwardAsync(device, "tcp:1", "tcp:2", true),
device => TestClient.CreateForwardAsync(device, "tcp:1", "tcp:2", true),
"tcp:1;tcp:2");

/// <summary>
Expand All @@ -92,7 +92,7 @@ await RunCreateForwardAsyncTest(
[Fact]
public async void CreateReverseAsyncTest() =>
await RunCreateReverseAsyncTest(
(device) => TestClient.CreateReverseForwardAsync(device, "tcp:1", "tcp:2", true),
device => TestClient.CreateReverseForwardAsync(device, "tcp:1", "tcp:2", true),
"tcp:1;tcp:2");

/// <summary>
Expand All @@ -101,7 +101,7 @@ await RunCreateReverseAsyncTest(
[Fact]
public async void CreateTcpForwardAsyncTest() =>
await RunCreateForwardAsyncTest(
(device) => TestClient.CreateForwardAsync(device, 3, 4),
device => TestClient.CreateForwardAsync(device, 3, 4),
"tcp:3;tcp:4");

/// <summary>
Expand All @@ -110,7 +110,7 @@ await RunCreateForwardAsyncTest(
[Fact]
public async void CreateSocketForwardAsyncTest() =>
await RunCreateForwardAsyncTest(
(device) => TestClient.CreateForwardAsync(device, 5, "/socket/1"),
device => TestClient.CreateForwardAsync(device, 5, "/socket/1"),
"tcp:5;local:/socket/1");

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions AdvancedSharpAdbClient.Tests/AdbClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void SetDeviceOtherException()
[Fact]
public void CreateForwardTest() =>
RunCreateForwardTest(
(device) => TestClient.CreateForward(device, "tcp:1", "tcp:2", true),
device => TestClient.CreateForward(device, "tcp:1", "tcp:2", true),
"tcp:1;tcp:2");

/// <summary>
Expand All @@ -201,7 +201,7 @@ public void CreateForwardTest() =>
[Fact]
public void CreateReverseTest() =>
RunCreateReverseTest(
(device) => TestClient.CreateReverseForward(device, "tcp:1", "tcp:2", true),
device => TestClient.CreateReverseForward(device, "tcp:1", "tcp:2", true),
"tcp:1;tcp:2");

/// <summary>
Expand All @@ -210,7 +210,7 @@ public void CreateReverseTest() =>
[Fact]
public void CreateTcpForwardTest() =>
RunCreateForwardTest(
(device) => TestClient.CreateForward(device, 3, 4),
device => TestClient.CreateForward(device, 3, 4),
"tcp:3;tcp:4");

/// <summary>
Expand All @@ -219,7 +219,7 @@ public void CreateTcpForwardTest() =>
[Fact]
public void CreateSocketForwardTest() =>
RunCreateForwardTest(
(device) => TestClient.CreateForward(device, 5, "/socket/1"),
device => TestClient.CreateForward(device, 5, "/socket/1"),
"tcp:5;local:/socket/1");

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions AdvancedSharpAdbClient.Tests/AdbServerTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async void GetStatusAsyncNotRunningTest()
IAdbSocket adbSocketMock = Substitute.For<IAdbSocket>();
adbSocketMock.SendAdbRequestAsync("host:version", Arg.Any<CancellationToken>()).Throws(new AggregateException(new SocketException(AdbServer.ConnectionRefused)));

AdbServer adbServer = new((endPoint) => adbSocketMock, adbCommandLineClientFactory);
AdbServer adbServer = new(endPoint => adbSocketMock, adbCommandLineClientFactory);

AdbServerStatus status = await adbServer.GetStatusAsync();
Assert.False(status.IsRunning);
Expand Down Expand Up @@ -51,7 +51,7 @@ public async void GetStatusAsyncRunningTest()
[Fact]
public async void GetStatusAsyncOtherSocketExceptionTest()
{
adbSocketFactory = (endPoint) => throw new SocketException();
adbSocketFactory = endPoint => throw new SocketException();

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

Expand All @@ -64,7 +64,7 @@ public async void GetStatusAsyncOtherSocketExceptionTest()
[Fact]
public async void GetStatusAsyncOtherExceptionTest()
{
adbSocketFactory = (endPoint) => throw new Exception();
adbSocketFactory = endPoint => throw new Exception();

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

Expand Down Expand Up @@ -107,7 +107,7 @@ public async void StartServerAsyncOutdatedRunningNoExecutableTest()
[Fact]
public async void StartServerAsyncNotRunningNoExecutableTest()
{
adbSocketFactory = (endPoint) => throw new SocketException(AdbServer.ConnectionRefused);
adbSocketFactory = endPoint => throw new SocketException(AdbServer.ConnectionRefused);

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

Expand Down Expand Up @@ -141,7 +141,7 @@ public async void StartServerAsyncOutdatedRunningTest()
[Fact]
public async void StartServerAsyncNotRunningTest()
{
adbSocketFactory = (endPoint) => throw new SocketException(AdbServer.ConnectionRefused);
adbSocketFactory = endPoint => throw new SocketException(AdbServer.ConnectionRefused);

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

Expand Down
14 changes: 7 additions & 7 deletions AdvancedSharpAdbClient.Tests/AdbServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public partial class AdbServerTests
public AdbServerTests()
{
socket = new DummyAdbSocket();
adbSocketFactory = (endPoint) => socket;
adbSocketFactory = endPoint => socket;

commandLineClient = new DummyAdbCommandLineClient();
adbCommandLineClientFactory = (version) => commandLineClient;
adbCommandLineClientFactory = version => commandLineClient;

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);
}
Expand All @@ -38,7 +38,7 @@ public void GetStatusNotRunningTest()
IAdbSocket adbSocketMock = Substitute.For<IAdbSocket>();
adbSocketMock.When(x => x.SendAdbRequest("host:version")).Do(x => throw new SocketException(AdbServer.ConnectionRefused));

AdbServer adbServer = new((endPoint) => adbSocketMock, adbCommandLineClientFactory);
AdbServer adbServer = new(endPoint => adbSocketMock, adbCommandLineClientFactory);

AdbServerStatus status = adbServer.GetStatus();
Assert.False(status.IsRunning);
Expand Down Expand Up @@ -71,7 +71,7 @@ public void GetStatusRunningTest()
[Fact]
public void GetStatusOtherSocketExceptionTest()
{
adbSocketFactory = (endPoint) => throw new SocketException();
adbSocketFactory = endPoint => throw new SocketException();

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

Expand All @@ -84,7 +84,7 @@ public void GetStatusOtherSocketExceptionTest()
[Fact]
public void GetStatusOtherExceptionTest()
{
adbSocketFactory = (endPoint) => throw new Exception();
adbSocketFactory = endPoint => throw new Exception();

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

Expand Down Expand Up @@ -127,7 +127,7 @@ public void StartServerOutdatedRunningNoExecutableTest()
[Fact]
public void StartServerNotRunningNoExecutableTest()
{
adbSocketFactory = (endPoint) => throw new SocketException(AdbServer.ConnectionRefused);
adbSocketFactory = endPoint => throw new SocketException(AdbServer.ConnectionRefused);

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

Expand Down Expand Up @@ -161,7 +161,7 @@ public void StartServerOutdatedRunningTest()
[Fact]
public void StartServerNotRunningTest()
{
adbSocketFactory = (endPoint) => throw new SocketException(AdbServer.ConnectionRefused);
adbSocketFactory = endPoint => throw new SocketException(AdbServer.ConnectionRefused);

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

Expand Down
11 changes: 6 additions & 5 deletions AdvancedSharpAdbClient.Tests/AdbSocketTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class AdbSocketTests
[Fact]
public async void SendSyncDATARequestAsyncTest() =>
await RunTestAsync(
(socket) => socket.SendSyncRequestAsync(SyncCommand.DATA, 2, default),
socket => socket.SendSyncRequestAsync(SyncCommand.DATA, 2, default),
[(byte)'D', (byte)'A', (byte)'T', (byte)'A', 2, 0, 0, 0]);

/// <summary>
Expand All @@ -24,7 +24,7 @@ await RunTestAsync(
[Fact]
public async void SendSyncSENDRequestAsyncTest() =>
await RunTestAsync(
(socket) => socket.SendSyncRequestAsync(SyncCommand.SEND, "/test", default),
socket => socket.SendSyncRequestAsync(SyncCommand.SEND, "/test", default),
[(byte)'S', (byte)'E', (byte)'N', (byte)'D', 5, 0, 0, 0, (byte)'/', (byte)'t', (byte)'e', (byte)'s', (byte)'t']);

/// <summary>
Expand All @@ -33,15 +33,16 @@ await RunTestAsync(
[Fact]
public async void SendSyncDENTRequestAsyncTest() =>
await RunTestAsync(
(socket) => socket.SendSyncRequestAsync(SyncCommand.DENT, "/data", 633, default),
socket => socket.SendSyncRequestAsync(SyncCommand.DENT, "/data", 633, default),
[(byte)'D', (byte)'E', (byte)'N', (byte)'T', 9, 0, 0, 0, (byte)'/', (byte)'d', (byte)'a', (byte)'t', (byte)'a', (byte)',', (byte)'6', (byte)'3', (byte)'3']);

/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequestAsync(SyncCommand, string, CancellationToken)"/> method.
/// </summary>
[Fact]
public async void SendSyncNullRequestAsyncTest() =>
_ = await Assert.ThrowsAsync<ArgumentNullException>(() => RunTestAsync((socket) => socket.SendSyncRequestAsync(SyncCommand.DATA, null, default), []));
_ = await Assert.ThrowsAsync<ArgumentNullException>(() =>
RunTestAsync(socket => socket.SendSyncRequestAsync(SyncCommand.DATA, null, default), []));

/// <summary>
/// Tests the <see cref="AdbSocket.ReadSyncResponseAsync(CancellationToken)"/> method.
Expand Down Expand Up @@ -218,7 +219,7 @@ public async void ReadAsyncMemoryTest()
[Fact]
public async void SendAdbRequestAsyncTest() =>
await RunTestAsync(
(socket) => socket.SendAdbRequestAsync("Test", default),
socket => socket.SendAdbRequestAsync("Test", default),
"0004Test"u8.ToArray());

private static async Task RunTestAsync(Func<IAdbSocket, Task> test, byte[] expectedDataSent)
Expand Down
11 changes: 6 additions & 5 deletions AdvancedSharpAdbClient.Tests/AdbSocketTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void IsOkayTest()
[Fact]
public void SendSyncDATARequestTest() =>
RunTest(
(socket) => socket.SendSyncRequest(SyncCommand.DATA, 2),
socket => socket.SendSyncRequest(SyncCommand.DATA, 2),
[(byte)'D', (byte)'A', (byte)'T', (byte)'A', 2, 0, 0, 0]);

/// <summary>
Expand All @@ -69,7 +69,7 @@ public void SendSyncDATARequestTest() =>
[Fact]
public void SendSyncSENDRequestTest() =>
RunTest(
(socket) => socket.SendSyncRequest(SyncCommand.SEND, "/test"),
socket => socket.SendSyncRequest(SyncCommand.SEND, "/test"),
[(byte)'S', (byte)'E', (byte)'N', (byte)'D', 5, 0, 0, 0, (byte)'/', (byte)'t', (byte)'e', (byte)'s', (byte)'t']);

/// <summary>
Expand All @@ -78,15 +78,16 @@ public void SendSyncSENDRequestTest() =>
[Fact]
public void SendSyncDENTRequestTest() =>
RunTest(
(socket) => socket.SendSyncRequest(SyncCommand.DENT, "/data", 633),
socket => socket.SendSyncRequest(SyncCommand.DENT, "/data", 633),
[(byte)'D', (byte)'E', (byte)'N', (byte)'T', 9, 0, 0, 0, (byte)'/', (byte)'d', (byte)'a', (byte)'t', (byte)'a', (byte)',', (byte)'6', (byte)'3', (byte)'3']);

/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequest(SyncCommand, string)"/> method.
/// </summary>
[Fact]
public void SendSyncNullRequestTest() =>
_ = Assert.Throws<ArgumentNullException>(() => RunTest((socket) => socket.SendSyncRequest(SyncCommand.DATA, null), []));
_ = Assert.Throws<ArgumentNullException>(() =>
RunTest(socket => socket.SendSyncRequest(SyncCommand.DATA, null), []));

/// <summary>
/// Tests the <see cref="AdbSocket.ReadSyncResponse"/> method.
Expand Down Expand Up @@ -263,7 +264,7 @@ public void ReadSpanTest()
[Fact]
public void SendAdbRequestTest() =>
RunTest(
(socket) => socket.SendAdbRequest("Test"),
socket => socket.SendAdbRequest("Test"),
"0004Test"u8.ToArray());

/// <summary>
Expand Down
Loading

0 comments on commit c1764a2

Please sign in to comment.