Skip to content

Commit

Permalink
Merge pull request #89 from SharpAdb/improve/winsdkfeatures
Browse files Browse the repository at this point in the history
Add WinRT extension for AdbClient and SyncService
  • Loading branch information
wherewhere authored Dec 25, 2023
2 parents 84cda8d + 0e22f9b commit 855bca8
Show file tree
Hide file tree
Showing 45 changed files with 2,333 additions and 482 deletions.
248 changes: 244 additions & 4 deletions AdvancedSharpAdbClient.Tests/AdbClientTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ public async void UnrootAsyncTest()
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallAsync(DeviceData, Stream, IProgress{InstallProgressEventArgs}?, CancellationToken, string[])"/> method.
/// Tests the <see cref="AdbClient.InstallAsync(DeviceData, Stream, Action{InstallProgressEventArgs}?, CancellationToken, string[])"/> method.
/// </summary>
[Fact]
public async void InstallAsyncTest()
Expand Down Expand Up @@ -688,7 +688,7 @@ await RunTestAsync(
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallMultipleAsync(DeviceData, IEnumerable{Stream}, string, IProgress{InstallProgressEventArgs}?, CancellationToken, string[])"/> method.
/// Tests the <see cref="AdbClient.InstallMultipleAsync(DeviceData, IEnumerable{Stream}, string, Action{InstallProgressEventArgs}?, CancellationToken, string[])"/> method.
/// </summary>
[Fact]
public async void InstallMultipleAsyncTest()
Expand Down Expand Up @@ -747,7 +747,7 @@ await RunTestAsync(
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallMultipleAsync(DeviceData, Stream, IEnumerable{Stream}, IProgress{InstallProgressEventArgs}?, CancellationToken, string[])"/> method.
/// Tests the <see cref="AdbClient.InstallMultipleAsync(DeviceData, Stream, IEnumerable{Stream}, Action{InstallProgressEventArgs}?, CancellationToken, string[])"/> method.
/// </summary>
[Fact]
public async void InstallMultipleWithBaseAsyncTest()
Expand Down Expand Up @@ -847,7 +847,7 @@ public async void InstallCreateAsyncTest()
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallWriteAsync(DeviceData, Stream, string, string, IProgress{double}?, CancellationToken)"/> method.
/// Tests the <see cref="AdbClient.InstallWriteAsync(DeviceData, Stream, string, string, Action{double}?, CancellationToken)"/> method.
/// </summary>
[Fact]
public async void InstallWriteAsyncTest()
Expand Down Expand Up @@ -920,6 +920,246 @@ await RunTestAsync(
() => TestClient.InstallCommitAsync(Device, "936013062"));
}

#if WINDOWS10_0_17763_0_OR_GREATER
/// <summary>
/// Tests the <see cref="AdbClient.InstallAsync(DeviceData, IRandomAccessStream, Action{InstallProgressEventArgs}?, CancellationToken, string[])"/> method.
/// </summary>
[Fact]
public async void InstallWinRTAsyncTest()
{
// The app data is sent in chunks of 32 kb
List<byte[]> applicationDataChunks = [];

await using (FileStream stream = File.OpenRead("Assets/TestApp/base.apk"))
{
byte[] buffer = new byte[32 * 1024];
int read = 0;

while ((read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length))) > 0)
{
byte[] array = buffer.AsSpan(0, read).ToArray();
applicationDataChunks.Add(array);
}
}

byte[] response = "Success\n"u8.ToArray();

StorageFile storageFile = await StorageFile.GetFileFromPathAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Assets\TestApp\base.apk"));
using (IRandomAccessStreamWithContentType stream = await storageFile.OpenReadAsync())
{
string[] requests =
[
"host:transport:169.254.109.177:5555",
$"exec:cmd package 'install' -S {stream.Size}"
];

await RunTestAsync(
OkResponses(2),
NoResponseMessages,
requests,
NoSyncRequests,
NoSyncResponses,
[response],
applicationDataChunks,
() => TestClient.InstallAsync(Device, stream,
new InstallProgress(
PackageInstallProgressState.Preparing,
PackageInstallProgressState.Uploading,
PackageInstallProgressState.Installing,
PackageInstallProgressState.Finished)));
}
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallMultipleAsync(DeviceData, IEnumerable{IRandomAccessStream}, string, Action{InstallProgressEventArgs}?, CancellationToken, string[])"/> method.
/// </summary>
[Fact]
public async void InstallMultipleWinRTAsyncTest()
{
// The app data is sent in chunks of 32 kb
List<byte[]> applicationDataChunks = [];

await using (FileStream stream = File.OpenRead("Assets/TestApp/split_config.arm64_v8a.apk"))
{
byte[] buffer = new byte[32 * 1024];
int read = 0;

while ((read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length))) > 0)
{
byte[] array = buffer.AsSpan(0, read).ToArray();
applicationDataChunks.Add(array);
}
}

StorageFile storageFile = await StorageFile.GetFileFromPathAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Assets\TestApp\split_config.arm64_v8a.apk"));
using IRandomAccessStreamWithContentType abiStream = await storageFile.OpenReadAsync();

string[] requests =
[
"host:transport:169.254.109.177:5555",
"exec:cmd package 'install-create' -p com.google.android.gms",
"host:transport:169.254.109.177:5555",
$"exec:cmd package 'install-write' -S {abiStream.Size} 936013062 splitAPK0.apk",
"host:transport:169.254.109.177:5555",
"exec:cmd package 'install-commit' 936013062"
];

byte[][] responses =
[
Encoding.ASCII.GetBytes($"Success: streamed {abiStream.Size} bytes\n")
];

await using MemoryStream sessionStream = new(Encoding.ASCII.GetBytes("Success: created install session [936013062]\r\n"));
await using MemoryStream commitStream = new("Success\n"u8.ToArray());

await RunTestAsync(
OkResponses(6),
NoResponseMessages,
requests,
NoSyncRequests,
NoSyncResponses,
responses,
applicationDataChunks,
[sessionStream, commitStream],
() => TestClient.InstallMultipleAsync(Device, [abiStream], "com.google.android.gms",
new InstallProgress(
PackageInstallProgressState.Preparing,
PackageInstallProgressState.CreateSession,
PackageInstallProgressState.Uploading,
PackageInstallProgressState.Installing,
PackageInstallProgressState.Finished)));
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallMultipleAsync(DeviceData, IRandomAccessStream, IEnumerable{IRandomAccessStream}, Action{InstallProgressEventArgs}?, CancellationToken, string[])"/> method.
/// </summary>
[Fact]
public async void InstallMultipleWinRTWithBaseAsyncTest()
{
// The app data is sent in chunks of 32 kb
List<byte[]> applicationDataChunks = [];

await using (FileStream stream = File.OpenRead("Assets/TestApp/base.apk"))
{
byte[] buffer = new byte[32 * 1024];
int read = 0;

while ((read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length))) > 0)
{
byte[] array = buffer.AsSpan(0, read).ToArray();
applicationDataChunks.Add(array);
}
}

await using (FileStream stream = File.OpenRead("Assets/TestApp/split_config.arm64_v8a.apk"))
{
byte[] buffer = new byte[32 * 1024];
int read = 0;

while ((read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length))) > 0)
{
byte[] array = buffer.AsSpan(0, read).ToArray();
applicationDataChunks.Add(array);
}
}

StorageFile storageFile = await StorageFile.GetFileFromPathAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Assets\TestApp\base.apk"));
using IRandomAccessStreamWithContentType baseStream = await storageFile.OpenReadAsync();
storageFile = await StorageFile.GetFileFromPathAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Assets\TestApp\split_config.arm64_v8a.apk"));
using IRandomAccessStreamWithContentType abiStream = await storageFile.OpenReadAsync();

string[] requests =
[
"host:transport:169.254.109.177:5555",
"exec:cmd package 'install-create'",
"host:transport:169.254.109.177:5555",
$"exec:cmd package 'install-write' -S {baseStream.Size} 936013062 baseAPK.apk",
"host:transport:169.254.109.177:5555",
$"exec:cmd package 'install-write' -S {abiStream.Size} 936013062 splitAPK0.apk",
"host:transport:169.254.109.177:5555",
"exec:cmd package 'install-commit' 936013062"
];

byte[][] responses =
[
Encoding.ASCII.GetBytes($"Success: streamed {baseStream.Size} bytes\n"),
Encoding.ASCII.GetBytes($"Success: streamed {abiStream.Size} bytes\n")
];

using MemoryStream sessionStream = new(Encoding.ASCII.GetBytes("Success: created install session [936013062]\r\n"));
using MemoryStream commitStream = new("Success\n"u8.ToArray());

await RunTestAsync(
OkResponses(8),
NoResponseMessages,
requests,
NoSyncRequests,
NoSyncResponses,
responses,
applicationDataChunks,
[sessionStream, commitStream],
() => TestClient.InstallMultipleAsync(Device, baseStream, [abiStream],
new InstallProgress(
PackageInstallProgressState.Preparing,
PackageInstallProgressState.CreateSession,
PackageInstallProgressState.Uploading,
PackageInstallProgressState.Installing,
PackageInstallProgressState.Finished)));
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallWriteAsync(DeviceData, IRandomAccessStream, string, string, Action{double}?, CancellationToken)"/> method.
/// </summary>
[Fact]
public async void InstallWriteWinRTAsyncTest()
{
// The app data is sent in chunks of 32 kb
List<byte[]> applicationDataChunks = [];

await using (FileStream stream = File.OpenRead("Assets/TestApp/base.apk"))
{
byte[] buffer = new byte[32 * 1024];
int read = 0;

while ((read = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length))) > 0)
{
byte[] array = buffer.AsSpan(0, read).ToArray();
applicationDataChunks.Add(array);
}
}

StorageFile storageFile = await StorageFile.GetFileFromPathAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Assets\TestApp\base.apk"));
using (IRandomAccessStreamWithContentType stream = await storageFile.OpenReadAsync())
{
string[] requests =
[
"host:transport:169.254.109.177:5555",
$"exec:cmd package 'install-write' -S {stream.Size} 936013062 base.apk"
];

byte[] response = Encoding.ASCII.GetBytes($"Success: streamed {stream.Size} bytes\n");

double temp = 0;
Progress<double> progress = new();
progress.ProgressChanged += (sender, args) =>
{
Assert.True(temp <= args, $"{nameof(args)}: {args} is less than {temp}.");
temp = args;
};

await RunTestAsync(
OkResponses(2),
NoResponseMessages,
requests,
NoSyncRequests,
NoSyncResponses,
[response],
applicationDataChunks,
() => TestClient.InstallWriteAsync(Device, stream, "base", "936013062", progress));
}
}
#endif

/// <summary>
/// Tests the <see cref="AdbClientExtensions.UninstallAsync(IAdbClient, DeviceData, string, string[])"/> method.
/// </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 @@ -748,7 +748,7 @@ public void UnrootTest()
}

/// <summary>
/// Tests the <see cref="AdbClient.Install(DeviceData, Stream, IProgress{InstallProgressEventArgs}?, string[])"/> method.
/// Tests the <see cref="AdbClient.Install(DeviceData, Stream, Action{InstallProgressEventArgs}?, string[])"/> method.
/// </summary>
[Fact]
public void InstallTest()
Expand Down Expand Up @@ -796,7 +796,7 @@ public void InstallTest()
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallMultiple(DeviceData, IEnumerable{Stream}, string, IProgress{InstallProgressEventArgs}?, string[])"/> method.
/// Tests the <see cref="AdbClient.InstallMultiple(DeviceData, IEnumerable{Stream}, string, Action{InstallProgressEventArgs}?, string[])"/> method.
/// </summary>
[Fact]
public void InstallMultipleTest()
Expand Down Expand Up @@ -871,7 +871,7 @@ public void InstallMultipleTest()
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallMultiple(DeviceData, Stream, IEnumerable{Stream}, IProgress{InstallProgressEventArgs}?, string[])"/> method.
/// Tests the <see cref="AdbClient.InstallMultiple(DeviceData, Stream, IEnumerable{Stream}, Action{InstallProgressEventArgs}?, string[])"/> method.
/// </summary>
[Fact]
public void InstallMultipleWithBaseTest()
Expand Down Expand Up @@ -987,7 +987,7 @@ public void InstallCreateTest()
}

/// <summary>
/// Tests the <see cref="AdbClient.InstallWrite(DeviceData, Stream, string, string, IProgress{double}?)"/> method.
/// Tests the <see cref="AdbClient.InstallWrite(DeviceData, Stream, string, string, Action{double}?)"/> method.
/// </summary>
[Fact]
public void InstallWriteTest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5" PrivateAssets="all">
<PackageReference Include="xunit" Version="2.6.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6" PrivateAssets="all">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void TryAsVersionTest(int versionCode, string versionName, bool expected)

#if WINDOWS10_0_17763_0_OR_GREATER
/// <summary>
/// Tests the <see cref="VersionInfo.TryAsPackageVersion(out Windows.ApplicationModel.PackageVersion)"/> method.
/// Tests the <see cref="VersionInfo.TryAsPackageVersion(out PackageVersion)"/> method.
/// </summary>
[Theory]
[InlineData(1231, "1.2.3.1", true)]
Expand All @@ -45,7 +45,7 @@ public void TryAsVersionTest(int versionCode, string versionName, bool expected)
[InlineData(098765456, "Unknown", false)]
public void TryAsPackageVersionTest(int versionCode, string versionName, bool expected)
{
bool result = new VersionInfo(versionCode, versionName).TryAsPackageVersion(out Windows.ApplicationModel.PackageVersion version);
bool result = new VersionInfo(versionCode, versionName).TryAsPackageVersion(out PackageVersion version);
Assert.Equal(expected, result);
if (expected)
{
Expand Down
Loading

0 comments on commit 855bca8

Please sign in to comment.