Skip to content

Commit

Permalink
Enable dependency injection of most factories - enables parallel testing
Browse files Browse the repository at this point in the history
  • Loading branch information
qmfrederik committed Apr 18, 2017
1 parent 6499995 commit 72d112e
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 94 deletions.
23 changes: 22 additions & 1 deletion SharpAdbClient.Tests/AdbClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,28 @@ public class AdbClientTests : SocketBasedTests
[ExpectedException(typeof(ArgumentNullException))]
public void ConstructorNullTest()
{
new AdbClient(null);
new AdbClient(null, Factories.AdbSocketFactory);
}

[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public void ConstructorInvalidEndPointTest()
{
new AdbClient(new CustomEndPoint(), Factories.AdbSocketFactory);
}

[TestMethod]
public void ConstructorTest()
{
var adbClient = new AdbClient();
Assert.IsNotNull(adbClient);
Assert.IsNotNull(adbClient.EndPoint);
Assert.IsInstanceOfType(adbClient.EndPoint, typeof(IPEndPoint));

var endPoint = (IPEndPoint)adbClient.EndPoint;

Assert.AreEqual(IPAddress.Loopback, endPoint.Address);
Assert.AreEqual(AdbClient.AdbServerPort, endPoint.Port);
}

[TestMethod]
Expand Down
90 changes: 44 additions & 46 deletions SharpAdbClient.Tests/AdbServerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SharpAdbClient.Exceptions;
using System;
using System.Net;
Expand All @@ -9,30 +10,36 @@ namespace SharpAdbClient.Tests
[TestClass]
public class AdbServerTests
{
private Func<string, IAdbCommandLineClient> adbCommandLineClientFactory;
private DummyAdbSocket socket;
private DummyAdbCommandLineClient commandLineClient;
private Func<EndPoint, IAdbSocket> adbSocketFactory;
private AdbClient adbClient;
private AdbServer adbServer;

[TestInitialize]
public void Initialize()
{
Factories.Reset();

this.socket = new DummyAdbSocket();
Factories.AdbSocketFactory = (endPoint) => this.socket;
this.adbSocketFactory = (endPoint) => this.socket;

this.commandLineClient = new DummyAdbCommandLineClient();
Factories.AdbCommandLineClientFactory = (version) => this.commandLineClient;
this.adbCommandLineClientFactory = (version) => this.commandLineClient;

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

[TestMethod]
public void GetStatusNotRunningTest()
{
Factories.AdbSocketFactory = (endPoint) =>
{
throw new SocketException(AdbServer.ConnectionRefused);
};
var adbClientMock = new Mock<IAdbClient>();
adbClientMock.Setup(c => c.GetAdbVersion())
.Throws(new SocketException(AdbServer.ConnectionRefused));

var status = AdbServer.Instance.GetStatus();
var adbServer = new AdbServer(adbClientMock.Object, this.adbCommandLineClientFactory);

var status = adbServer.GetStatus();
Assert.IsFalse(status.IsRunning);
Assert.IsNull(status.Version);
}
Expand All @@ -43,7 +50,7 @@ public void GetStatusRunningTest()
this.socket.Responses.Enqueue(AdbResponse.OK);
this.socket.ResponseMessages.Enqueue("0020");

var status = AdbServer.Instance.GetStatus();
var status = this.adbServer.GetStatus();

Assert.AreEqual(0, this.socket.Responses.Count);
Assert.AreEqual(0, this.socket.ResponseMessages.Count);
Expand All @@ -58,24 +65,30 @@ public void GetStatusRunningTest()
[ExpectedException(typeof(SocketException))]
public void GetStatusOtherSocketExceptionTest()
{
Factories.AdbSocketFactory = (endPoint) =>
this.adbSocketFactory = (endPoint) =>
{
throw new SocketException();
};

var status = AdbServer.Instance.GetStatus();
this.adbClient = new AdbClient(AdbClient.DefaultEndPoint, this.adbSocketFactory);
this.adbServer = new AdbServer(this.adbClient, this.adbCommandLineClientFactory);

var status = this.adbServer.GetStatus();
}

[TestMethod]
[ExpectedException(typeof(Exception))]
public void GetStatusOtherExceptionTest()
{
Factories.AdbSocketFactory = (endPoint) =>
this.adbSocketFactory = (endPoint) =>
{
throw new Exception();
};

var status = AdbServer.Instance.GetStatus();
this.adbClient = new AdbClient(AdbClient.DefaultEndPoint, this.adbSocketFactory);
this.adbServer = new AdbServer(this.adbClient, this.adbCommandLineClientFactory);

var status = this.adbServer.GetStatus();
}

[TestMethod]
Expand All @@ -85,7 +98,7 @@ public void StartServerAlreadyRunningTest()
this.socket.Responses.Enqueue(AdbResponse.OK);
this.socket.ResponseMessages.Enqueue("0020");

var result = AdbServer.Instance.StartServer(null, false);
var result = this.adbServer.StartServer(null, false);

Assert.AreEqual(StartServerResult.AlreadyRunning, result);

Expand All @@ -100,7 +113,7 @@ public void StartServerOutdatedRunningNoExecutableTest()
this.socket.Responses.Enqueue(AdbResponse.OK);
this.socket.ResponseMessages.Enqueue("0010");

var result = AdbServer.Instance.StartServer(null, false);
var result = this.adbServer.StartServer(null, false);

Assert.AreEqual(1, this.socket.Requests.Count);
Assert.AreEqual("host:version", this.socket.Requests[0]);
Expand All @@ -110,12 +123,15 @@ public void StartServerOutdatedRunningNoExecutableTest()
[ExpectedException(typeof(AdbException))]
public void StartServerNotRunningNoExecutableTest()
{
Factories.AdbSocketFactory = (endPoint) =>
this.adbSocketFactory = (endPoint) =>
{
throw new SocketException(AdbServer.ConnectionRefused);
};

var result = AdbServer.Instance.StartServer(null, false);
this.adbClient = new AdbClient(AdbClient.DefaultEndPoint, this.adbSocketFactory);
this.adbServer = new AdbServer(this.adbClient, this.adbCommandLineClientFactory);

var result = this.adbServer.StartServer(null, false);
}

[TestMethod]
Expand All @@ -128,7 +144,7 @@ public void StartServerOutdatedRunningTest()

Assert.IsFalse(this.commandLineClient.ServerStarted);

var result = AdbServer.Instance.StartServer("adb.exe", false);
var result = this.adbServer.StartServer("adb.exe", false);

Assert.IsTrue(this.commandLineClient.ServerStarted);

Expand All @@ -140,16 +156,19 @@ public void StartServerOutdatedRunningTest()
[TestMethod]
public void StartServerNotRunningTest()
{
Factories.AdbSocketFactory = (endPoint) =>
this.adbSocketFactory = (endPoint) =>
{
throw new SocketException(AdbServer.ConnectionRefused);
};

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

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

Assert.IsFalse(this.commandLineClient.ServerStarted);

var result = AdbServer.Instance.StartServer("adb.exe", false);
var result = this.adbServer.StartServer("adb.exe", false);

Assert.IsTrue(this.commandLineClient.ServerStarted);
}
Expand All @@ -164,7 +183,7 @@ public void StartServerIntermediateRestartRequestedRunningTest()

Assert.IsFalse(this.commandLineClient.ServerStarted);

var result = AdbServer.Instance.StartServer("adb.exe", true);
var result = this.adbServer.StartServer("adb.exe", true);

Assert.IsTrue(this.commandLineClient.ServerStarted);

Expand All @@ -183,40 +202,19 @@ public void StartServerIntermediateRestartNotRequestedRunningTest()

Assert.IsFalse(this.commandLineClient.ServerStarted);

var result = AdbServer.Instance.StartServer("adb.exe", false);
var result = this.adbServer.StartServer("adb.exe", false);

Assert.IsFalse(this.commandLineClient.ServerStarted);

Assert.AreEqual(1, this.socket.Requests.Count);
Assert.AreEqual("host:version", this.socket.Requests[0]);
}

[TestMethod]
public void ConstructorTest()
{
var adbServer = new AdbServer();
Assert.IsNotNull(adbServer);
Assert.IsNotNull(adbServer.EndPoint);
Assert.IsInstanceOfType(adbServer.EndPoint, typeof(IPEndPoint));

var endPoint = (IPEndPoint)adbServer.EndPoint;

Assert.AreEqual(IPAddress.Loopback, endPoint.Address);
Assert.AreEqual(AdbServer.AdbServerPort, endPoint.Port);
}

[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public void ConstructorInvalidEndPointTest()
{
var adbServer = new AdbServer(new CustomEndPoint());
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ConstructorNullEndPointTest()
public void ConstructorAdbClientNullTest()
{
var adbServer = new AdbServer(null);
var adbServer = new AdbServer(null, this.adbCommandLineClientFactory);
}
}
}
6 changes: 6 additions & 0 deletions SharpAdbClient.Tests/DeviceCommands/DeviceExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ public void ListProcessesTest()
@"1 (init) S 0 0 0 0 -1 1077944576 2680 83280 0 179 0 67 16 39 20 0 1 0 2 17735680 143 18446744073709551615 134512640 135145076 4288071392 4288070744 134658736 0 0 0 65536 18446744071580117077 0 0 17 1 0 0 0 0 0 135152736 135165080 142131200 4288073690 4288073696 4288073696 4288073714 0
2 (kthreadd) S 0 0 0 0 -1 2129984 0 0 0 0 0 0 0 0 20 0 1 0 2 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744071579254310 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 (ksoftirqd/0) S 2 0 0 0 -1 69238848 0 0 0 0 0 23 0 0 20 0 1 0 7 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744071579284070 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0");
adbClient.Commands.Add("cat /proc/1/cmdline /proc/1/stat /proc/2/cmdline /proc/2/stat /proc/3/cmdline /proc/3/stat ",
@"
1 (init) S 0 0 0 0 -1 1077944576 2680 83280 0 179 0 67 16 39 20 0 1 0 2 17735680 143 18446744073709551615 134512640 135145076 4288071392 4288070744 134658736 0 0 0 65536 18446744071580117077 0 0 17 1 0 0 0 0 0 135152736 135165080 142131200 4288073690 4288073696 4288073696 4288073714 0
2 (kthreadd) S 0 0 0 0 -1 2129984 0 0 0 0 0 0 0 0 20 0 1 0 2 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744071579254310 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 (ksoftirqd/0) S 2 0 0 0 -1 69238848 0 0 0 0 0 23 0 0 20 0 1 0 7 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 18446744071579284070 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0");

var device = new DeviceData();
var processes = device.ListProcesses().ToArray();
Expand Down
6 changes: 5 additions & 1 deletion SharpAdbClient.Tests/DeviceMonitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public void DeviceConnectedTest()
[TestMethod]
public void StartInitialDeviceListTest()
{
this.Socket.WaitForNewData = true;

using (DeviceMonitor monitor = new DeviceMonitor(this.Socket))
{
DeviceMonitorSink sink = new DeviceMonitorSink(monitor);
Expand Down Expand Up @@ -226,6 +228,8 @@ public void AdbKilledTest()
var dummyAdbServer = new DummyAdbServer();
AdbServer.Instance = dummyAdbServer;

this.Socket.WaitForNewData = true;

using (DeviceMonitor monitor = new DeviceMonitor(this.Socket))
{
base.RunTest(
Expand All @@ -250,7 +254,7 @@ public void AdbKilledTest()
[TestCategory("IntegrationTest")]
public void DisposeMonitorTest()
{
using (DeviceMonitor monitor = new DeviceMonitor(new AdbSocket(AdbServer.Instance.EndPoint)))
using (DeviceMonitor monitor = new DeviceMonitor(new AdbSocket(AdbClient.Instance.EndPoint)))
{
monitor.Start();
Thread.Sleep(1000);
Expand Down
3 changes: 3 additions & 0 deletions SharpAdbClient.Tests/DummyAdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public Dictionary<string, string> Commands
public Collection<string> ReceivedCommands
{ get; private set; } = new Collection<string>();

public EndPoint EndPoint
{ get; private set; }

public void Connect(DnsEndPoint endpoint)
{
throw new NotImplementedException();
Expand Down
4 changes: 2 additions & 2 deletions SharpAdbClient.Tests/SocketBasedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ protected void Initialize(bool integrationTest, bool doDispose)
this.IntegrationTest = false;
#endif

this.EndPoint = AdbServer.Instance.EndPoint;
this.EndPoint = AdbClient.Instance.EndPoint;
this.Socket = (IDummyAdbSocket)Factories.AdbSocketFactory(this.EndPoint);

AdbClient.Instance = new AdbClient(AdbServer.Instance.EndPoint);
AdbClient.Instance = new AdbClient();
}

/// <summary>
Expand Down
Loading

0 comments on commit 72d112e

Please sign in to comment.