Skip to content

Commit 33ab62c

Browse files
committed
Set ICloneable to out T
Add clone tests
1 parent 4714c48 commit 33ab62c

File tree

18 files changed

+200
-66
lines changed

18 files changed

+200
-66
lines changed

AdvancedSharpAdbClient.Tests/AdbClientTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,23 @@ public void GetFeatureSetTest()
11321132
Assert.Equal("stat_v2", features.LastOrDefault());
11331133
}
11341134

1135+
/// <summary>
1136+
/// Tests the <see cref="AdbClient.Clone()"/> method.
1137+
/// </summary>
1138+
[Fact]
1139+
public void CloneTest()
1140+
{
1141+
Assert.True(TestClient is ICloneable<IAdbClient>);
1142+
#if WINDOWS10_0_17763_0_OR_GREATER
1143+
Assert.True(TestClient is ICloneable<IAdbClient.IWinRT>);
1144+
#endif
1145+
AdbClient client = TestClient.Clone();
1146+
Assert.Equal(TestClient.EndPoint, client.EndPoint);
1147+
DnsEndPoint endPoint = new("localhost", 5555);
1148+
client = TestClient.Clone(endPoint);
1149+
Assert.Equal(endPoint, client.EndPoint);
1150+
}
1151+
11351152
private void RunConnectTest(Action test, string connectString)
11361153
{
11371154
string[] requests = [$"host:connect:{connectString}"];

AdvancedSharpAdbClient.Tests/AdbServerTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,20 @@ public void StopServerTest()
248248
Assert.Equal("host:kill", socket.Requests[0]);
249249
}
250250

251+
/// <summary>
252+
/// Tests the <see cref="AdbServer.Clone()"/> method.
253+
/// </summary>
254+
[Fact]
255+
public void CloneTest()
256+
{
257+
DnsEndPoint endPoint = new("localhost", 5555);
258+
Assert.True(adbServer is ICloneable<IAdbServer>);
259+
AdbServer server = adbServer.Clone();
260+
Assert.Equal(adbServer.EndPoint, server.EndPoint);
261+
server = adbServer.Clone(endPoint);
262+
Assert.Equal(endPoint, server.EndPoint);
263+
}
264+
251265
/// <summary>
252266
/// Tests the <see cref="AdbServer(EndPoint, Func{EndPoint, IAdbSocket}, Func{string, IAdbCommandLineClient})"/> method.
253267
/// </summary>

AdvancedSharpAdbClient.Tests/AdbSocketTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,21 @@ public void GetShellStreamTest()
282282
Assert.Equal(tcpSocket.OutputStream, shellStream.Inner);
283283
}
284284

285+
286+
/// <summary>
287+
/// Tests the <see cref="AdbSocket.Clone()"/> method.
288+
/// </summary>
289+
[Fact]
290+
public void CloneTest()
291+
{
292+
using DummyTcpSocket tcpSocket = new();
293+
using AdbSocket adbSocket = new(tcpSocket);
294+
Assert.True(adbSocket is ICloneable<IAdbSocket>);
295+
using AdbSocket socket = adbSocket.Clone();
296+
Assert.NotEqual(adbSocket.Socket, socket.Socket);
297+
Assert.Equal(adbSocket.Connected, socket.Connected);
298+
}
299+
285300
private static void RunTest(Action<IAdbSocket> test, byte[] expectedDataSent)
286301
{
287302
using DummyTcpSocket tcpSocket = new();

AdvancedSharpAdbClient.Tests/DeviceMonitorTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,17 @@ public void AdbKilledTest()
283283
Assert.True(Socket.DidReconnect);
284284
Assert.True(dummyAdbServer.WasRestarted);
285285
}
286+
287+
/// <summary>
288+
/// Tests the <see cref="DeviceMonitor.Clone()"/> method.
289+
/// </summary>
290+
[Fact]
291+
public void CloneTest()
292+
{
293+
using DeviceMonitor deviceMonitor = new(Socket);
294+
Assert.True(deviceMonitor is ICloneable<IDeviceMonitor>);
295+
using DeviceMonitor monitor = deviceMonitor.Clone();
296+
Assert.NotEqual(deviceMonitor.Socket, monitor.Socket);
297+
}
286298
}
287299
}

AdvancedSharpAdbClient.Tests/Dummys/DummyAdbSocket.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace AdvancedSharpAdbClient.Tests
1111
/// <summary>
1212
/// A mock implementation of the <see cref="IAdbSocket"/> class.
1313
/// </summary>
14-
internal class DummyAdbSocket : IDummyAdbSocket
14+
internal class DummyAdbSocket : IDummyAdbSocket, ICloneable<DummyAdbSocket>
1515
{
1616
/// <summary>
1717
/// Use this message to cause <see cref="ReadString"/> and <see cref="ReadStringAsync(CancellationToken)"/> to throw
@@ -21,21 +21,21 @@ internal class DummyAdbSocket : IDummyAdbSocket
2121

2222
public DummyAdbSocket() => IsConnected = true;
2323

24-
public Queue<AdbResponse> Responses { get; } = new Queue<AdbResponse>();
24+
public Queue<AdbResponse> Responses { get; init; } = new Queue<AdbResponse>();
2525

26-
public Queue<SyncCommand> SyncResponses { get; } = new Queue<SyncCommand>();
26+
public Queue<SyncCommand> SyncResponses { get; init; } = new Queue<SyncCommand>();
2727

28-
public Queue<byte[]> SyncDataReceived { get; } = new Queue<byte[]>();
28+
public Queue<byte[]> SyncDataReceived { get; init; } = new Queue<byte[]>();
2929

30-
public Queue<byte[]> SyncDataSent { get; } = new Queue<byte[]>();
30+
public Queue<byte[]> SyncDataSent { get; init; } = new Queue<byte[]>();
3131

32-
public Queue<string> ResponseMessages { get; } = new Queue<string>();
32+
public Queue<string> ResponseMessages { get; init; } = new Queue<string>();
3333

34-
public List<string> Requests { get; } = [];
34+
public List<string> Requests { get; init; } = [];
3535

36-
public List<(SyncCommand, string)> SyncRequests { get; } = [];
36+
public List<(SyncCommand, string)> SyncRequests { get; init; } = [];
3737

38-
public Queue<Stream> ShellStreams { get; } = new Queue<Stream>();
38+
public Queue<Stream> ShellStreams { get; init; } = new Queue<Stream>();
3939

4040
public bool IsConnected { get; set; }
4141

@@ -295,5 +295,19 @@ public async Task ReconnectAsync(bool isForce, CancellationToken cancellationTok
295295
await Task.Yield();
296296
DidReconnect = true;
297297
}
298+
299+
public DummyAdbSocket Clone() => new()
300+
{
301+
Responses = Responses,
302+
SyncResponses = SyncResponses,
303+
SyncDataReceived = SyncDataReceived,
304+
SyncDataSent = SyncDataSent,
305+
ResponseMessages = ResponseMessages,
306+
Requests = Requests,
307+
SyncRequests = SyncRequests,
308+
ShellStreams = ShellStreams
309+
};
310+
311+
object ICloneable.Clone() => Clone();
298312
}
299313
}

AdvancedSharpAdbClient.Tests/Dummys/DummyTcpSocket.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace AdvancedSharpAdbClient.Tests
1010
/// <summary>
1111
/// A mock implementation of the <see cref="ITcpSocket"/> class.
1212
/// </summary>
13-
internal class DummyTcpSocket : ITcpSocket
13+
internal class DummyTcpSocket : ITcpSocket, ICloneable<DummyTcpSocket>
1414
{
1515
/// <summary>
1616
/// The stream from which the <see cref="DummyTcpSocket"/> reads.
@@ -113,5 +113,17 @@ public async ValueTask<int> SendAsync(ReadOnlyMemory<byte> buffer, SocketFlags s
113113
}
114114

115115
public byte[] GetBytesSent() => OutputStream.ToArray();
116+
117+
public DummyTcpSocket Clone()
118+
{
119+
DummyTcpSocket socket = new()
120+
{
121+
Connected = true,
122+
ReceiveBufferSize = ReceiveBufferSize
123+
};
124+
return socket;
125+
}
126+
127+
object ICloneable.Clone() => Clone();
116128
}
117129
}

AdvancedSharpAdbClient.Tests/Dummys/TracingAdbSocket.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace AdvancedSharpAdbClient.Tests
1010
{
1111
internal class TracingAdbSocket(EndPoint endPoint) : AdbSocket(endPoint), IDummyAdbSocket
1212
{
13+
private readonly EndPoint endPoint = endPoint;
14+
1315
public bool DoDispose { get; set; }
1416

1517
public Queue<AdbResponse> Responses { get; } = new Queue<AdbResponse>();
@@ -178,5 +180,7 @@ public override async Task<string> ReadStringAsync(CancellationToken cancellatio
178180
Reconnect(isForce);
179181
DidReconnect = true;
180182
}
183+
184+
public override AdbSocket Clone() => new TracingAdbSocket(endPoint);
181185
}
182186
}

AdvancedSharpAdbClient.Tests/SyncServiceTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,35 @@ public void IsProcessingTest()
210210
Assert.True(service.IsOutdate);
211211
});
212212
}
213+
214+
/// <summary>
215+
/// Tests the <see cref="SyncService.Clone()"/> method.
216+
/// </summary>
217+
[Fact]
218+
public void CloneTest()
219+
{
220+
DummyAdbSocket socket = new()
221+
{
222+
Requests =
223+
{
224+
"host:transport:169.254.109.177:5555",
225+
"sync:",
226+
"host:transport:169.254.109.177:5555",
227+
"sync:"
228+
}
229+
};
230+
socket.Responses.Enqueue(AdbResponse.OK);
231+
socket.Responses.Enqueue(AdbResponse.OK);
232+
socket.Responses.Enqueue(AdbResponse.OK);
233+
socket.Responses.Enqueue(AdbResponse.OK);
234+
using SyncService syncService = new(socket, Device);
235+
Assert.True(syncService is ICloneable<ISyncService>);
236+
#if WINDOWS10_0_17763_0_OR_GREATER
237+
Assert.True(syncService is ICloneable<ISyncService.IWinRT>);
238+
#endif
239+
using SyncService service = syncService.Clone();
240+
Assert.NotEqual(syncService.Socket, service.Socket);
241+
Assert.Equal(syncService.Device, service.Device);
242+
}
213243
}
214244
}

AdvancedSharpAdbClient.Tests/TcpSocketTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,20 @@ public void CreateUnsupportedSocketTest()
9090
using TcpSocket socket = new();
9191
_ = Assert.Throws<NotSupportedException>(() => socket.Connect(new CustomEndPoint()));
9292
}
93+
94+
/// <summary>
95+
/// Tests the <see cref="TcpSocket.Clone()"/> method.
96+
/// </summary>
97+
[Fact]
98+
public void CloneTest()
99+
{
100+
using TcpSocket tcpSocket = new();
101+
Assert.True(tcpSocket is ICloneable<ITcpSocket>);
102+
Assert.Throws<ArgumentNullException>(tcpSocket.Clone);
103+
tcpSocket.Connect(new DnsEndPoint("www.bing.com", 80));
104+
using TcpSocket socket = tcpSocket.Clone();
105+
Assert.Equal(tcpSocket.EndPoint, socket.EndPoint);
106+
Assert.True(socket.Connected);
107+
}
93108
}
94109
}

AdvancedSharpAdbClient/AdbClient.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ namespace AdvancedSharpAdbClient
3030
/// <para><seealso href="https://github.com/android/platform_system_core/blob/master/adb/adb.c">adb.c</seealso></para>
3131
/// </remarks>
3232
[DebuggerDisplay($"{nameof(AdbClient)} \\{{ {nameof(EndPoint)} = {{{nameof(EndPoint)}}} }}")]
33-
public partial class AdbClient : IAdbClient, ICloneable<IAdbClient>, ICloneable
33+
public partial class AdbClient : IAdbClient, ICloneable<AdbClient>, ICloneable
3434
#if WINDOWS_UWP || WINDOWS10_0_17763_0_OR_GREATER
35-
, IAdbClient.IWinRT, ICloneable<IAdbClient.IWinRT>
35+
, IAdbClient.IWinRT
3636
#endif
3737
{
3838
/// <summary>
@@ -1133,20 +1133,14 @@ public IEnumerable<string> GetFeatureSet(DeviceData device)
11331133
public override string ToString() => $"The {nameof(AdbClient)} communicate with adb server at {EndPoint}";
11341134

11351135
/// <summary>
1136-
/// Creates a new <see cref="IAdbClient"/> object that is a copy of the current instance with new <see cref="EndPoint"/>.
1136+
/// Creates a new <see cref="AdbClient"/> object that is a copy of the current instance with new <see cref="EndPoint"/>.
11371137
/// </summary>
11381138
/// <param name="endPoint">The new <see cref="EndPoint"/> to use.</param>
1139-
/// <returns>A new <see cref="IAdbClient"/> object that is a copy of this instance with new <see cref="EndPoint"/>.</returns>
1140-
public virtual IAdbClient Clone(EndPoint endPoint) => new AdbClient(endPoint, AdbSocketFactory);
1139+
/// <returns>A new <see cref="AdbClient"/> object that is a copy of this instance with new <see cref="EndPoint"/>.</returns>
1140+
public virtual AdbClient Clone(EndPoint endPoint) => new(endPoint, AdbSocketFactory);
11411141

11421142
/// <inheritdoc/>
1143-
public IAdbClient Clone() => Clone(EndPoint);
1144-
1145-
#if WINDOWS_UWP || WINDOWS10_0_17763_0_OR_GREATER
1146-
/// <inheritdoc/>
1147-
IAdbClient.IWinRT ICloneable<IAdbClient.IWinRT>.Clone() => Clone(EndPoint) is IAdbClient.IWinRT client ? client
1148-
: throw new NotSupportedException($"The {nameof(Clone)} method does not return a {nameof(IAdbClient.IWinRT)} object.");
1149-
#endif
1143+
public AdbClient Clone() => Clone(EndPoint);
11501144

11511145
/// <inheritdoc/>
11521146
object ICloneable.Clone() => Clone();

0 commit comments

Comments
 (0)