Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,007 changes: 1,007 additions & 0 deletions AdvancedSharpAdbClient.Tests/AdbClientTests.Async.cs

Large diffs are not rendered by default.

877 changes: 659 additions & 218 deletions AdvancedSharpAdbClient.Tests/AdbClientTests.cs

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions AdvancedSharpAdbClient.Tests/AdbCommandLineClientTests.Async.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using AdvancedSharpAdbClient.Exceptions;
using System;
using Xunit;

namespace AdvancedSharpAdbClient.Tests
{
public partial class AdbCommandLineClientTests
{
[Fact]
public async void GetVersionAsyncTest()
{
DummyAdbCommandLineClient commandLine = new()
{
Version = new Version(1, 0, 32)
};

Assert.Equal(new Version(1, 0, 32), await commandLine.GetVersionAsync());
}

[Fact]
public async void GetVersionAsyncNullTest()
{
DummyAdbCommandLineClient commandLine = new()
{
Version = null
};
_ = await Assert.ThrowsAsync<AdbException>(() => commandLine.GetVersionAsync());
}

[Fact]
public async void GetOutdatedVersionAsyncTest()
{
DummyAdbCommandLineClient commandLine = new()
{
Version = new Version(1, 0, 1)
};

_ = await Assert.ThrowsAsync<AdbException>(() => commandLine.GetVersionAsync());
}

[Fact]
public async void StartServerAsyncTest()
{
DummyAdbCommandLineClient commandLine = new();
Assert.False(commandLine.ServerStarted);
await commandLine.StartServerAsync();
Assert.True(commandLine.ServerStarted);
}
}
}
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient.Tests/AdbCommandLineClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace AdvancedSharpAdbClient.Tests
/// <summary>
/// Tests the <see cref="AdbCommandLineClient"/> class.
/// </summary>
public class AdbCommandLineClientTests
public partial class AdbCommandLineClientTests
{
[Fact]
public void GetVersionTest()
Expand Down
187 changes: 187 additions & 0 deletions AdvancedSharpAdbClient.Tests/AdbServerTests.Async.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using AdvancedSharpAdbClient.Exceptions;
using Moq;
using System;
using System.Net.Sockets;
using System.Threading;
using Xunit;

namespace AdvancedSharpAdbClient.Tests
{
public partial class AdbServerTests
{
[Fact]
public async void GetStatusAsyncNotRunningTest()
{
Mock<IAdbClient> adbClientMock = new();
adbClientMock.Setup(c => c.GetAdbVersionAsync(It.IsAny<CancellationToken>()))
.Throws(new AggregateException(new SocketException(AdbServer.ConnectionRefused)));

AdbServer adbServer = new(adbClientMock.Object, adbCommandLineClientFactory);

AdbServerStatus status = await adbServer.GetStatusAsync();
Assert.False(status.IsRunning);
Assert.Null(status.Version);
}

[Fact]
public async void GetStatusAsyncRunningTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0020");

AdbServerStatus status = await adbServer.GetStatusAsync();

Assert.Empty(socket.Responses);
Assert.Empty(socket.ResponseMessages);
Assert.Single(socket.Requests);
Assert.Equal("host:version", socket.Requests[0]);

Assert.True(status.IsRunning);
Assert.Equal(new Version(1, 0, 32), status.Version);
}

[Fact]
public async void GetStatusAsyncOtherSocketExceptionTest()
{
adbSocketFactory = (endPoint) => throw new SocketException();

adbClient = new AdbClient(AdbClient.DefaultEndPoint, adbSocketFactory);
adbServer = new AdbServer(adbClient, adbCommandLineClientFactory);

_ = await Assert.ThrowsAsync<SocketException>(async () => await adbServer.GetStatusAsync());
}

[Fact]
public async void GetStatusAsyncOtherExceptionTest()
{
adbSocketFactory = (endPoint) => throw new Exception();

adbClient = new AdbClient(AdbClient.DefaultEndPoint, adbSocketFactory);
adbServer = new AdbServer(adbClient, adbCommandLineClientFactory);

_ = await Assert.ThrowsAsync<Exception>(async () => await adbServer.GetStatusAsync());
}

[Fact]
public async void StartServerAsyncAlreadyRunningTest()
{
commandLineClient.Version = new Version(1, 0, 20);
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0020");

StartServerResult result = await adbServer.StartServerAsync(null, false);

Assert.Equal(StartServerResult.AlreadyRunning, result);

Assert.Single(socket.Requests);
Assert.Equal("host:version", socket.Requests[0]);
}

[Fact]
public async void StartServerAsyncOutdatedRunningNoExecutableTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0010");

_ = await Assert.ThrowsAsync<AdbException>(async () => await adbServer.StartServerAsync(null, false));
}

[Fact]
public async void StartServerAsyncNotRunningNoExecutableTest()
{
adbSocketFactory = (endPoint) => throw new SocketException(AdbServer.ConnectionRefused);

adbClient = new AdbClient(AdbClient.DefaultEndPoint, adbSocketFactory);
adbServer = new AdbServer(adbClient, adbCommandLineClientFactory);

_ = await Assert.ThrowsAsync<AdbException>(async () => await adbServer.StartServerAsync(null, false));
}

[Fact]
public async void StartServerAsyncOutdatedRunningTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0010");

commandLineClient.Version = new Version(1, 0, 32);

Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.StartServerAsync(ServerName, false);

Assert.True(commandLineClient.ServerStarted);

Assert.Equal(2, socket.Requests.Count);
Assert.Equal("host:version", socket.Requests[0]);
Assert.Equal("host:kill", socket.Requests[1]);
}

[Fact]
public async void StartServerAsyncNotRunningTest()
{
adbSocketFactory = (endPoint) => throw new SocketException(AdbServer.ConnectionRefused);

adbClient = new AdbClient(AdbClient.DefaultEndPoint, adbSocketFactory);
adbServer = new AdbServer(adbClient, adbCommandLineClientFactory);

commandLineClient.Version = new Version(1, 0, 32);

Assert.False(commandLineClient.ServerStarted);

StartServerResult result = await adbServer.StartServerAsync(ServerName, false);

Assert.True(commandLineClient.ServerStarted);
}

[Fact]
public async void StartServerAsyncIntermediateRestartRequestedRunningTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("001f");

commandLineClient.Version = new Version(1, 0, 32);

Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.StartServerAsync(ServerName, true);

Assert.True(commandLineClient.ServerStarted);

Assert.Equal(2, socket.Requests.Count);
Assert.Equal("host:version", socket.Requests[0]);
Assert.Equal("host:kill", socket.Requests[1]);
}

[Fact]
public async void StartServerAsyncIntermediateRestartNotRequestedRunningTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("001f");

commandLineClient.Version = new Version(1, 0, 32);

Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.StartServerAsync(ServerName, false);

Assert.False(commandLineClient.ServerStarted);

Assert.Single(socket.Requests);
Assert.Equal("host:version", socket.Requests[0]);
}

[Fact]
public async void RestartServerAsyncTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("001f");

commandLineClient.Version = new Version(1, 0, 32);

Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.RestartServerAsync(ServerName);

Assert.False(commandLineClient.ServerStarted);

Assert.Single(socket.Requests);
Assert.Equal("host:version", socket.Requests[0]);
}
}
}
20 changes: 19 additions & 1 deletion AdvancedSharpAdbClient.Tests/AdbServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AdvancedSharpAdbClient.Tests
/// <summary>
/// Tests the <see cref="AdbServer"/> class.
/// </summary>
public class AdbServerTests
public partial class AdbServerTests
{
private readonly Func<string, IAdbCommandLineClient> adbCommandLineClientFactory;
private readonly DummyAdbSocket socket;
Expand All @@ -26,6 +26,7 @@ public AdbServerTests()
adbSocketFactory = (endPoint) => socket;

commandLineClient = new DummyAdbCommandLineClient();
AdbServer.IsValidAdbFile = commandLineClient.IsValidAdbFile;
adbCommandLineClientFactory = (version) => commandLineClient;

adbClient = new AdbClient(AdbClient.DefaultEndPoint, adbSocketFactory);
Expand Down Expand Up @@ -190,6 +191,23 @@ public void StartServerIntermediateRestartNotRequestedRunningTest()
Assert.Equal("host:version", socket.Requests[0]);
}

[Fact]
public void RestartServerTest()
{
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("001f");

commandLineClient.Version = new Version(1, 0, 32);

Assert.False(commandLineClient.ServerStarted);
_ = adbServer.RestartServer(ServerName);

Assert.False(commandLineClient.ServerStarted);

Assert.Single(socket.Requests);
Assert.Equal("host:version", socket.Requests[0]);
}

[Fact]
public void ConstructorAdbClientNullTest() =>
_ = Assert.Throws<ArgumentNullException>(() => new AdbServer(null, adbCommandLineClientFactory));
Expand Down
Loading