Skip to content

Commit 75964a3

Browse files
committed
Add sync v2 tests
1 parent 5ec1ea6 commit 75964a3

27 files changed

Lines changed: 1326 additions & 385 deletions

AdvancedSharpAdbClient.Tests/AdbClientTests.Async.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ public async Task GetDevicesAsyncTest()
5858
string[] responseMessages = ["169.254.109.177:5555 device product:VS Emulator 5\" KitKat (4.4) XXHDPI Phone model:5__KitKat__4_4__XXHDPI_Phone device:donatello\n"];
5959
string[] requests = ["host:devices-l"];
6060

61-
DeviceData[] devices = await RunTestAsync(
61+
List<DeviceData> devices = await RunTestAsync(
6262
OkResponse,
6363
responseMessages,
6464
requests,
65-
ctx => TestClient.GetDevicesAsync(ctx).ToArrayAsync(),
65+
ctx => TestClient.GetDevicesAsync(ctx).ToListAsync(),
6666
TestContext.Current.CancellationToken);
6767

6868
// Make sure and the correct value is returned.
@@ -216,15 +216,15 @@ public async Task ListForwardAsyncTest()
216216
string[] responseMessages = ["169.254.109.177:5555 tcp:1 tcp:2\n169.254.109.177:5555 tcp:3 tcp:4\n169.254.109.177:5555 tcp:5 local:/socket/1\n"];
217217
string[] requests = ["host-serial:169.254.109.177:5555:list-forward"];
218218

219-
ForwardData[] forwards = await RunTestAsync(
219+
List<ForwardData> forwards = await RunTestAsync(
220220
OkResponse,
221221
responseMessages,
222222
requests,
223-
ctx => TestClient.ListForwardAsync(Device, ctx).ToArrayAsync(),
223+
ctx => TestClient.ListForwardAsync(Device, ctx).ToListAsync(),
224224
TestContext.Current.CancellationToken);
225225

226226
Assert.NotNull(forwards);
227-
Assert.Equal(3, forwards.Length);
227+
Assert.Equal(3, forwards.Count);
228228
Assert.Equal("169.254.109.177:5555", forwards[0].SerialNumber);
229229
Assert.Equal("tcp:1", forwards[0].Local);
230230
Assert.Equal("tcp:2", forwards[0].Remote);
@@ -244,15 +244,15 @@ public async Task ListReverseForwardAsyncTest()
244244
"reverse:list-forward"
245245
];
246246

247-
ForwardData[] forwards = await RunTestAsync(
247+
List<ForwardData> forwards = await RunTestAsync(
248248
OkResponses(2),
249249
responseMessages,
250250
requests,
251-
ctx => TestClient.ListReverseForwardAsync(Device, ctx).ToArrayAsync(),
251+
ctx => TestClient.ListReverseForwardAsync(Device, ctx).ToListAsync(),
252252
TestContext.Current.CancellationToken);
253253

254254
Assert.NotNull(forwards);
255-
Assert.Equal(3, forwards.Length);
255+
Assert.Equal(3, forwards.Count);
256256
Assert.Equal("(reverse)", forwards[0].SerialNumber);
257257
Assert.Equal("localabstract:scrcpy", forwards[0].Local);
258258
Assert.Equal("tcp:100", forwards[0].Remote);
@@ -1258,14 +1258,14 @@ public async Task GetFeatureSetAsyncTest()
12581258
string[] requests = ["host-serial:169.254.109.177:5555:features"];
12591259
string[] responses = ["sendrecv_v2_brotli,remount_shell,sendrecv_v2,abb_exec,fixed_push_mkdir,fixed_push_symlink_timestamp,abb,shell_v2,cmd,ls_v2,apex,stat_v2\r\n"];
12601260

1261-
string[] features = await RunTestAsync(
1261+
List<string> features = await RunTestAsync(
12621262
OkResponse,
12631263
responses,
12641264
requests,
1265-
ctx => TestClient.GetFeatureSetAsync(Device, ctx).ToArrayAsync(),
1265+
ctx => TestClient.GetFeatureSetAsync(Device, ctx).ToListAsync(),
12661266
TestContext.Current.CancellationToken);
12671267

1268-
Assert.Equal(12, features.Length);
1268+
Assert.Equal(12, features.Count);
12691269
Assert.Equal("sendrecv_v2_brotli", features.FirstOrDefault());
12701270
Assert.Equal("stat_v2", features.LastOrDefault());
12711271
}

AdvancedSharpAdbClient.Tests/DeviceCommands/DeviceClientTexts.Async.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,13 @@ public async Task FindElementsAsyncTest()
387387
DummyAdbClient client = new();
388388
client.Commands["shell:uiautomator dump /dev/tty"] = File.ReadAllText(@"Assets/DumpScreen.txt");
389389

390-
Element[] elements = await new DeviceClient(client, Device).FindElementsAsync(cancellationToken: TestContext.Current.CancellationToken).ToArrayAsync();
390+
List<Element> elements = await new DeviceClient(client, Device).FindElementsAsync(cancellationToken: TestContext.Current.CancellationToken).ToListAsync();
391391

392392
Assert.Single(client.ReceivedCommands);
393393
Assert.Equal("shell:uiautomator dump /dev/tty", client.ReceivedCommands[0]);
394394

395-
int childCount = elements.Length;
396-
Array.ForEach(elements, x => childCount += x.GetChildCount());
395+
int childCount = elements.Count;
396+
elements.ForEach(x => childCount += x.GetChildCount());
397397
Assert.Equal(145, childCount);
398398
Element element = elements[0][0][0][0][0][0][0][0][0][2][1][0][0];
399399
Assert.Equal("where-where", element.Text);

AdvancedSharpAdbClient.Tests/DeviceCommands/DeviceExtensionsTests.Async.cs

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,45 @@ public async Task StatAsyncTest()
112112

113113
Factories.SyncServiceFactory = (c, d) =>
114114
{
115-
Factories.Reset();
116115
Assert.Equal(d, Device);
117116
return mock;
118117
};
119118

120-
Assert.Equal(stats, await client.StatAsync(Device, remotePath, cancellationToken: TestContext.Current.CancellationToken));
119+
Assert.Equal(stats, await client.StatAsync(Device, remotePath, TestContext.Current.CancellationToken));
120+
Assert.Equal(stats, await client.StatAsync(Device, remotePath, false, TestContext.Current.CancellationToken));
121+
122+
Factories.Reset();
123+
}
124+
125+
/// <summary>
126+
/// Tests the <see cref="DeviceExtensions.StatExAsync(IAdbClient, DeviceData, string, CancellationToken)"/> method.
127+
/// </summary>
128+
[Fact]
129+
public async Task StatExAsyncTest()
130+
{
131+
const string remotePath = "/test";
132+
FileStatisticsEx stats = new(default);
133+
134+
IAdbClient client = Substitute.For<IAdbClient>();
135+
ISyncService mock = Substitute.For<ISyncService>();
136+
mock.StatExAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
137+
.Returns(x =>
138+
{
139+
Assert.Equal(remotePath, x.ArgAt<string>(0));
140+
Assert.Equal(TestContext.Current.CancellationToken, x.ArgAt<CancellationToken>(1));
141+
return stats;
142+
});
143+
144+
Factories.SyncServiceFactory = (c, d) =>
145+
{
146+
Assert.Equal(d, Device);
147+
return mock;
148+
};
149+
150+
Assert.Equal(stats, await client.StatExAsync(Device, remotePath, TestContext.Current.CancellationToken));
151+
Assert.Equal(stats, await client.StatAsync(Device, remotePath, true, TestContext.Current.CancellationToken));
152+
153+
Factories.Reset();
121154
}
122155

123156
/// <summary>
@@ -141,12 +174,45 @@ public async Task GetDirectoryListingAsyncTest()
141174

142175
Factories.SyncServiceFactory = (c, d) =>
143176
{
144-
Factories.Reset();
145177
Assert.Equal(d, Device);
146178
return mock;
147179
};
148180

149-
Assert.Equal(stats, await client.GetDirectoryListingAsync(Device, remotePath, cancellationToken: TestContext.Current.CancellationToken));
181+
Assert.Equal(stats, await client.GetDirectoryListingAsync(Device, remotePath, TestContext.Current.CancellationToken));
182+
Assert.Equal(stats, await client.GetDirectoryListingAsync(Device, remotePath, false, TestContext.Current.CancellationToken));
183+
184+
Factories.Reset();
185+
}
186+
187+
/// <summary>
188+
/// Tests the <see cref="DeviceExtensions.GetDirectoryListingExAsync(IAdbClient, DeviceData, string, CancellationToken)"/> method.
189+
/// </summary>
190+
[Fact]
191+
public async Task GetDirectoryListingExAsyncTest()
192+
{
193+
const string remotePath = "/test";
194+
List<FileStatisticsEx> stats = [new(default)];
195+
196+
IAdbClient client = Substitute.For<IAdbClient>();
197+
ISyncService mock = Substitute.For<ISyncService>();
198+
mock.GetDirectoryListingExAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
199+
.Returns(x =>
200+
{
201+
Assert.Equal(remotePath, x.ArgAt<string>(0));
202+
Assert.Equal(TestContext.Current.CancellationToken, x.ArgAt<CancellationToken>(1));
203+
return stats;
204+
});
205+
206+
Factories.SyncServiceFactory = (c, d) =>
207+
{
208+
Assert.Equal(d, Device);
209+
return mock;
210+
};
211+
212+
Assert.Equal(stats, await client.GetDirectoryListingExAsync(Device, remotePath, TestContext.Current.CancellationToken));
213+
Assert.Equal(stats, await client.GetDirectoryListingAsync(Device, remotePath, true, TestContext.Current.CancellationToken));
214+
215+
Factories.Reset();
150216
}
151217

152218
/// <summary>
@@ -165,17 +231,50 @@ public async Task GetDirectoryAsyncListingTest()
165231
{
166232
Assert.Equal(remotePath, x.ArgAt<string>(0));
167233
Assert.Equal(TestContext.Current.CancellationToken, x.ArgAt<CancellationToken>(1));
168-
return stats.ToAsyncEnumerable(x.ArgAt<CancellationToken>(1));
234+
return stats.ToAsyncEnumerable();
169235
});
170236

171237
Factories.SyncServiceFactory = (c, d) =>
172238
{
173-
Factories.Reset();
174239
Assert.Equal(d, Device);
175240
return mock;
176241
};
177242

178-
Assert.Equal(stats, await client.GetDirectoryAsyncListing(Device, remotePath, cancellationToken: TestContext.Current.CancellationToken).ToListAsync(cancellationToken: TestContext.Current.CancellationToken));
243+
Assert.Equal(stats, await client.GetDirectoryAsyncListing(Device, remotePath, TestContext.Current.CancellationToken).ToListAsync(cancellationToken: TestContext.Current.CancellationToken));
244+
Assert.Equal(stats, await client.GetDirectoryAsyncListing(Device, remotePath, false, TestContext.Current.CancellationToken).ToListAsync(cancellationToken: TestContext.Current.CancellationToken));
245+
246+
Factories.Reset();
247+
}
248+
249+
/// <summary>
250+
/// Tests the <see cref="DeviceExtensions.GetDirectoryAsyncListingEx(IAdbClient, DeviceData, string, CancellationToken)"/> method.
251+
/// </summary>
252+
[Fact]
253+
public async Task GetDirectoryAsyncListingExTest()
254+
{
255+
const string remotePath = "/test";
256+
List<FileStatisticsEx> stats = [new(default)];
257+
258+
IAdbClient client = Substitute.For<IAdbClient>();
259+
ISyncService mock = Substitute.For<ISyncService>();
260+
mock.GetDirectoryAsyncListingEx(Arg.Any<string>(), Arg.Any<CancellationToken>())
261+
.Returns(x =>
262+
{
263+
Assert.Equal(remotePath, x.ArgAt<string>(0));
264+
Assert.Equal(TestContext.Current.CancellationToken, x.ArgAt<CancellationToken>(1));
265+
return stats.ToAsyncEnumerable();
266+
});
267+
268+
Factories.SyncServiceFactory = (c, d) =>
269+
{
270+
Assert.Equal(d, Device);
271+
return mock;
272+
};
273+
274+
Assert.Equal(stats, await client.GetDirectoryAsyncListingEx(Device, remotePath, TestContext.Current.CancellationToken).ToListAsync(cancellationToken: TestContext.Current.CancellationToken));
275+
Assert.Equal(stats, await client.GetDirectoryAsyncListing(Device, remotePath, true, TestContext.Current.CancellationToken).ToListAsync(cancellationToken: TestContext.Current.CancellationToken));
276+
277+
Factories.Reset();
179278
}
180279

181280
/// <summary>

AdvancedSharpAdbClient.Tests/DeviceCommands/DeviceExtensionsTests.cs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,44 @@ public void StatTest()
111111

112112
Factories.SyncServiceFactory = (c, d) =>
113113
{
114-
Factories.Reset();
115114
Assert.Equal(d, Device);
116115
return mock;
117116
};
118117

119118
Assert.Equal(stats, client.Stat(Device, remotePath));
119+
Assert.Equal(stats, client.Stat(Device, remotePath, false));
120+
121+
Factories.Reset();
122+
}
123+
124+
/// <summary>
125+
/// Tests the <see cref="DeviceExtensions.StatEx(IAdbClient, DeviceData, string)"/> method.
126+
/// </summary>
127+
[Fact]
128+
public void StatExTest()
129+
{
130+
const string remotePath = "/test";
131+
FileStatisticsEx stats = new(default);
132+
133+
IAdbClient client = Substitute.For<IAdbClient>();
134+
ISyncService mock = Substitute.For<ISyncService>();
135+
mock.StatEx(Arg.Any<string>())
136+
.Returns(x =>
137+
{
138+
Assert.Equal(remotePath, x.ArgAt<string>(0));
139+
return stats;
140+
});
141+
142+
Factories.SyncServiceFactory = (c, d) =>
143+
{
144+
Assert.Equal(d, Device);
145+
return mock;
146+
};
147+
148+
Assert.Equal(stats, client.StatEx(Device, remotePath));
149+
Assert.Equal(stats, client.Stat(Device, remotePath, true));
150+
151+
Factories.Reset();
120152
}
121153

122154
/// <summary>
@@ -139,12 +171,44 @@ public void GetDirectoryListingTest()
139171

140172
Factories.SyncServiceFactory = (c, d) =>
141173
{
142-
Factories.Reset();
143174
Assert.Equal(d, Device);
144175
return mock;
145176
};
146177

147178
Assert.Equal(stats, client.GetDirectoryListing(Device, remotePath));
179+
Assert.Equal(stats, client.GetDirectoryListing(Device, remotePath, false));
180+
181+
Factories.Reset();
182+
}
183+
184+
/// <summary>
185+
/// Tests the <see cref="DeviceExtensions.GetDirectoryListingEx(IAdbClient, DeviceData, string)"/> method.
186+
/// </summary>
187+
[Fact]
188+
public void GetDirectoryListingExTest()
189+
{
190+
const string remotePath = "/test";
191+
IEnumerable<FileStatisticsEx> stats = [new(default)];
192+
193+
IAdbClient client = Substitute.For<IAdbClient>();
194+
ISyncService mock = Substitute.For<ISyncService>();
195+
mock.GetDirectoryListingEx(Arg.Any<string>())
196+
.Returns(x =>
197+
{
198+
Assert.Equal(remotePath, x.ArgAt<string>(0));
199+
return stats;
200+
});
201+
202+
Factories.SyncServiceFactory = (c, d) =>
203+
{
204+
Assert.Equal(d, Device);
205+
return mock;
206+
};
207+
208+
Assert.Equal(stats, client.GetDirectoryListingEx(Device, remotePath));
209+
Assert.Equal(stats, client.GetDirectoryListing(Device, remotePath, true));
210+
211+
Factories.Reset();
148212
}
149213

150214
/// <summary>

AdvancedSharpAdbClient.Tests/Extensions/AdbClientExtensionsTests.Async.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public async Task ExecuteServerCommandAsyncTest()
5151
Assert.Equal(command, x.ArgAt<string>(1));
5252
Assert.Equal(encoding, x.ArgAt<Encoding>(2));
5353
Assert.Equal(TestContext.Current.CancellationToken, x.ArgAt<CancellationToken>(3));
54-
return result.ToAsyncEnumerable(x.ArgAt<CancellationToken>(3));
54+
return result.ToAsyncEnumerable();
5555
});
5656
_ = client.ExecuteServerEnumerableAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<IAdbSocket>(), Arg.Any<Encoding>(), Arg.Any<CancellationToken>())
5757
.Returns(x =>
@@ -61,7 +61,7 @@ public async Task ExecuteServerCommandAsyncTest()
6161
Assert.Equal(socket, x.ArgAt<IAdbSocket>(2));
6262
Assert.Equal(encoding, x.ArgAt<Encoding>(3));
6363
Assert.Equal(TestContext.Current.CancellationToken, x.ArgAt<CancellationToken>(4));
64-
return result.ToAsyncEnumerable(x.ArgAt<CancellationToken>(4));
64+
return result.ToAsyncEnumerable();
6565
});
6666

6767
await client.ExecuteServerCommandAsync(target, command, receiver, cancellationToken: TestContext.Current.CancellationToken);
@@ -102,7 +102,7 @@ public async Task ExecuteRemoteCommandAsyncTest()
102102
Assert.Equal(device, x.ArgAt<DeviceData>(1));
103103
Assert.Equal(encoding, x.ArgAt<Encoding>(2));
104104
Assert.Equal(TestContext.Current.CancellationToken, x.ArgAt<CancellationToken>(3));
105-
return result.ToAsyncEnumerable(x.ArgAt<CancellationToken>(3));
105+
return result.ToAsyncEnumerable();
106106
});
107107

108108
await client.ExecuteRemoteCommandAsync(command, device, receiver, cancellationToken: TestContext.Current.CancellationToken);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// <copyright file="EnumerableExtensions.cs" company="The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere">
2+
// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion, yungd1plomat, wherewhere. All rights reserved.
3+
// </copyright>
4+
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace AdvancedSharpAdbClient.Tests
10+
{
11+
/// <summary>
12+
/// Provides extension methods for the <see cref="Enumerable"/> class.
13+
/// </summary>
14+
public static class EnumerableExtensions
15+
{
16+
/// <summary>
17+
/// Asynchronously creates an list from a <see cref="IEnumerable{T}"/> of <see cref="Task{TSource}"/>.
18+
/// </summary>
19+
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
20+
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="Task{TSource}"/> to create an list from.</param>
21+
/// <returns>A <see cref="Task{Array}"/> which returns an list that contains the elements from the input sequence.</returns>
22+
public static Task<List<TSource>> ToListAsync<TSource>(this Task<IEnumerable<TSource>> source) =>
23+
source.ContinueWith(x => x.Result.ToList());
24+
25+
/// <summary>
26+
/// Asynchronously creates an array from a <see cref="IEnumerable{T}"/> of <see cref="Task{TSource}"/>.
27+
/// </summary>
28+
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
29+
/// <param name="source">An <see cref="IEnumerable{T}"/> of <see cref="Task{TSource}"/> to create an array from.</param>
30+
/// <returns>A <see cref="Task{Array}"/> which returns an array that contains the elements from the input sequence.</returns>
31+
public static Task<TSource[]> ToArrayAsync<TSource>(this IEnumerable<Task<TSource>> source) =>
32+
source.WhenAll();
33+
}
34+
}

0 commit comments

Comments
 (0)