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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -24,10 +25,10 @@ public async Task InstallRemotePackageAsyncTest()

adbClient.ReceivedCommands.Clear();

await manager.InstallRemotePackageAsync("/data/base.apk", new InstallProgress(PackageInstallProgressState.Installing), TestContext.Current.CancellationToken, new string[] { "-r", "-t" });
await manager.InstallRemotePackageAsync("/data/base.apk", new InstallProgress(PackageInstallProgressState.Installing), TestContext.Current.CancellationToken, ["-r", "-t"]);

Assert.Single(adbClient.ReceivedCommands);
Assert.Equal("shell:pm install -r -t \"/data/base.apk\"", adbClient.ReceivedCommands[0]);
string item = Assert.Single(adbClient.ReceivedCommands);
Assert.Equal("shell:pm install -r -t \"/data/base.apk\"", item);
}

[Fact]
Expand Down Expand Up @@ -156,7 +157,9 @@ public async Task InstallMultiplePackageAsyncTest()

PackageManager manager = new(adbClient, Device, (c, d) => syncService);

await manager.InstallMultiplePackageAsync("Assets/TestApp/base.apk", ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
await manager.InstallMultiplePackageAsync(
"Assets/TestApp/base.apk",
["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
new InstallProgress(
PackageInstallProgressState.Preparing,
PackageInstallProgressState.Uploading,
Expand Down Expand Up @@ -185,7 +188,9 @@ await manager.InstallMultiplePackageAsync("Assets/TestApp/base.apk", ["Assets/Te
syncService.UploadedFiles.Clear();
adbClient.ReceivedCommands.Clear();

await manager.InstallMultiplePackageAsync(["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], "com.google.android.gms",
await manager.InstallMultiplePackageAsync(
["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
"com.google.android.gms",
new InstallProgress(
PackageInstallProgressState.Preparing,
PackageInstallProgressState.Uploading,
Expand All @@ -209,6 +214,50 @@ await manager.InstallMultiplePackageAsync(["Assets/TestApp/split_config.arm64_v8
Assert.True(syncService.UploadedFiles.ContainsKey("/data/local/tmp/split_config.xxhdpi.apk"));
}

/// <summary>
/// Tests the <see cref="PackageManager.InstallMultiplePackageAsync(string, System.Collections.Generic.IEnumerable{string}, System.Action{InstallProgressEventArgs}?, System.Threading.CancellationToken, string[])"/> method without a callback.
/// </summary>
[Fact]
public async Task InstallMultiplePackageWithoutCallbackAsyncTest()
{
DummySyncService syncService = new();

DummyAdbClient adbClient = new();

adbClient.Commands["shell:pm list packages -f"] = "package:/system/app/Gallery2/Gallery2.apk=com.android.gallery3d";
adbClient.Commands["shell:pm install-create"] = "Success: created install session [936013062]";
adbClient.Commands["shell:pm install-create -p com.google.android.gms"] = "Success: created install session [936013062]";
adbClient.Commands["shell:pm install-write 936013062 base.apk \"/data/local/tmp/base.apk\""] = "Success";
adbClient.Commands["shell:pm install-write 936013062 split0.apk \"/data/local/tmp/split_config.arm64_v8a.apk\""] = "Success";
adbClient.Commands["shell:pm install-write 936013062 split1.apk \"/data/local/tmp/split_config.xxhdpi.apk\""] = "Success";
adbClient.Commands["shell:pm install-commit 936013062"] = "Success";
adbClient.Commands["shell:rm \"/data/local/tmp/base.apk\""] = string.Empty;
adbClient.Commands["shell:rm \"/data/local/tmp/split_config.arm64_v8a.apk\""] = string.Empty;
adbClient.Commands["shell:rm \"/data/local/tmp/split_config.xxhdpi.apk\""] = string.Empty;

PackageManager manager = new(adbClient, Device, (c, d) => syncService);

// The split counts must not depend on a callback being there to report them
await manager.InstallMultiplePackageAsync(
"Assets/TestApp/base.apk",
["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
callback: null,
TestContext.Current.CancellationToken);

Assert.Equal(9, adbClient.ReceivedCommands.Count);
Assert.Equal("shell:rm \"/data/local/tmp/base.apk\"", adbClient.ReceivedCommands[8]);

adbClient.ReceivedCommands.Clear();

await manager.InstallMultiplePackageAsync(
["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
"com.google.android.gms",
callback: null,
TestContext.Current.CancellationToken);

Assert.Equal(6, adbClient.ReceivedCommands.Count);
}

[Fact]
public async Task UninstallPackageAsyncTest()
{
Expand Down
54 changes: 50 additions & 4 deletions AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void InstallRemotePackageTest()

manager.InstallRemotePackage("/data/base.apk", new InstallProgress(PackageInstallProgressState.Installing), "-r", "-t");

Assert.Single(adbClient.ReceivedCommands);
Assert.Equal("shell:pm install -r -t \"/data/base.apk\"", adbClient.ReceivedCommands[0]);
string item = Assert.Single(adbClient.ReceivedCommands);
Assert.Equal("shell:pm install -r -t \"/data/base.apk\"", item);
}

[Fact]
Expand Down Expand Up @@ -181,7 +181,9 @@ public void InstallMultiplePackageTest()

PackageManager manager = new(adbClient, Device, (c, d) => syncService);

manager.InstallMultiplePackage("Assets/TestApp/base.apk", ["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
manager.InstallMultiplePackage(
"Assets/TestApp/base.apk",
["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
new InstallProgress(
PackageInstallProgressState.Preparing,
PackageInstallProgressState.Uploading,
Expand Down Expand Up @@ -209,7 +211,9 @@ public void InstallMultiplePackageTest()
syncService.UploadedFiles.Clear();
adbClient.ReceivedCommands.Clear();

manager.InstallMultiplePackage(["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"], "com.google.android.gms",
manager.InstallMultiplePackage(
["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
"com.google.android.gms",
new InstallProgress(
PackageInstallProgressState.Preparing,
PackageInstallProgressState.Uploading,
Expand All @@ -232,6 +236,48 @@ public void InstallMultiplePackageTest()
Assert.True(syncService.UploadedFiles.ContainsKey("/data/local/tmp/split_config.xxhdpi.apk"));
}

/// <summary>
/// Tests the <see cref="PackageManager.InstallMultiplePackage(string, System.Collections.Generic.IEnumerable{string}, System.Action{InstallProgressEventArgs}?, string[])"/> method without a callback.
/// </summary>
[Fact]
public void InstallMultiplePackageWithoutCallbackTest()
{
DummySyncService syncService = new();

DummyAdbClient adbClient = new();

adbClient.Commands["shell:pm list packages -f"] = "package:/system/app/Gallery2/Gallery2.apk=com.android.gallery3d";
adbClient.Commands["shell:pm install-create"] = "Success: created install session [936013062]";
adbClient.Commands["shell:pm install-create -p com.google.android.gms"] = "Success: created install session [936013062]";
adbClient.Commands["shell:pm install-write 936013062 base.apk \"/data/local/tmp/base.apk\""] = "Success";
adbClient.Commands["shell:pm install-write 936013062 split0.apk \"/data/local/tmp/split_config.arm64_v8a.apk\""] = "Success";
adbClient.Commands["shell:pm install-write 936013062 split1.apk \"/data/local/tmp/split_config.xxhdpi.apk\""] = "Success";
adbClient.Commands["shell:pm install-commit 936013062"] = "Success";
adbClient.Commands["shell:rm \"/data/local/tmp/base.apk\""] = string.Empty;
adbClient.Commands["shell:rm \"/data/local/tmp/split_config.arm64_v8a.apk\""] = string.Empty;
adbClient.Commands["shell:rm \"/data/local/tmp/split_config.xxhdpi.apk\""] = string.Empty;

PackageManager manager = new(adbClient, Device, (c, d) => syncService);

// The split counts must not depend on a callback being there to report them
manager.InstallMultiplePackage(
"Assets/TestApp/base.apk",
["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
callback: null);

Assert.Equal(9, adbClient.ReceivedCommands.Count);
Assert.Equal("shell:rm \"/data/local/tmp/base.apk\"", adbClient.ReceivedCommands[8]);

adbClient.ReceivedCommands.Clear();

manager.InstallMultiplePackage(
["Assets/TestApp/split_config.arm64_v8a.apk", "Assets/TestApp/split_config.xxhdpi.apk"],
"com.google.android.gms",
callback: null);

Assert.Equal(6, adbClient.ReceivedCommands.Count);
}

[Fact]
public void UninstallPackageTest()
{
Expand Down
18 changes: 10 additions & 8 deletions AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ public async Task GetListingExAsyncTest()
208, 7, 0, 0,
13, 39, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
151, 130, 56, 86, 0, 0, 0, 0,
152, 130, 56, 86, 0, 0, 0, 0,
152, 130, 56, 86, 0, 0, 0, 0,
152, 130, 56, 86, 0, 0, 0, 0
153, 130, 56, 86, 0, 0, 0, 0
],
[
0, 0, 0, 0,
Expand Down Expand Up @@ -247,9 +247,10 @@ public async Task GetListingExAsyncTest()
Assert.Equal(0u, dir.Size);
Assert.Equal(2000u, dir.UserId);
Assert.Equal(9997u, dir.GroupId);
Assert.Equal(time, dir.AccessTime);
// Distinct times, so a field read from another's offset fails
Assert.Equal(time.AddSeconds(-1), dir.AccessTime);
Assert.Equal(time, dir.ModifiedTime);
Assert.Equal(time, dir.ChangedTime);
Assert.Equal(time.AddSeconds(1), dir.ChangedTime);
Assert.Equal($"drwxr-x--x\t0\t{dir.ModifiedTime}\t.", dir.ToString());

FileStatisticsEx parentDir = value[1];
Expand Down Expand Up @@ -382,9 +383,9 @@ public async Task GetAsyncListingExTest()
208, 7, 0, 0,
13, 39, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
151, 130, 56, 86, 0, 0, 0, 0,
152, 130, 56, 86, 0, 0, 0, 0,
152, 130, 56, 86, 0, 0, 0, 0,
152, 130, 56, 86, 0, 0, 0, 0
153, 130, 56, 86, 0, 0, 0, 0
],
[
0, 0, 0, 0,
Expand Down Expand Up @@ -451,9 +452,10 @@ public async Task GetAsyncListingExTest()
Assert.Equal(0u, dir.Size);
Assert.Equal(2000u, dir.UserId);
Assert.Equal(9997u, dir.GroupId);
Assert.Equal(time, dir.AccessTime);
// Distinct times, so a field read from another's offset fails
Assert.Equal(time.AddSeconds(-1), dir.AccessTime);
Assert.Equal(time, dir.ModifiedTime);
Assert.Equal(time, dir.ChangedTime);
Assert.Equal(time.AddSeconds(1), dir.ChangedTime);
Assert.Equal($"drwxr-x--x\t0\t{dir.ModifiedTime}\t.", dir.ToString());

FileStatisticsEx parentDir = value[1];
Expand Down
9 changes: 5 additions & 4 deletions AdvancedSharpAdbClient.Tests/SyncServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ public void GetListingExTest()
208, 7, 0, 0,
13, 39, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
151, 130, 56, 86, 0, 0, 0, 0,
152, 130, 56, 86, 0, 0, 0, 0,
152, 130, 56, 86, 0, 0, 0, 0,
152, 130, 56, 86, 0, 0, 0, 0
153, 130, 56, 86, 0, 0, 0, 0
],
[
0, 0, 0, 0,
Expand Down Expand Up @@ -253,9 +253,10 @@ public void GetListingExTest()
Assert.Equal(0u, dir.Size);
Assert.Equal(2000u, dir.UserId);
Assert.Equal(9997u, dir.GroupId);
Assert.Equal(time, dir.AccessTime);
// Distinct times, so a field read from another's offset fails
Assert.Equal(time.AddSeconds(-1), dir.AccessTime);
Assert.Equal(time, dir.ModifiedTime);
Assert.Equal(time, dir.ChangedTime);
Assert.Equal(time.AddSeconds(1), dir.ChangedTime);
Assert.Equal($"drwxr-x--x\t0\t{dir.ModifiedTime}\t.", dir.ToString());

FileStatisticsEx parentDir = value[1];
Expand Down
28 changes: 24 additions & 4 deletions AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,16 @@ void OnSplitSyncProgressChanged(string? sender, SyncProgressChangedEventArgs arg
await InstallMultipleRemotePackageAsync(baseRemoteFilePath, splitRemoteFilePaths, callback, cancellationToken, arguments);

int count = 0;
object locker = new();
callback?.Invoke(new InstallProgressEventArgs(0, splitRemoteFilePaths.Length + 1, PackageInstallProgressState.PostInstall));
await splitRemoteFilePaths.Select(async x =>
{
await RemoveRemotePackageAsync(x, cancellationToken).ConfigureAwait(false);
callback?.Invoke(new InstallProgressEventArgs(++count, splitRemoteFilePaths.Length + 1, PackageInstallProgressState.PostInstall));
lock (locker)
{
count++;
callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFilePaths.Length + 1, PackageInstallProgressState.PostInstall));
}
}).WhenAll().ConfigureAwait(false);

if (count < splitRemoteFilePaths.Length)
Expand Down Expand Up @@ -237,11 +242,16 @@ void OnSyncProgressChanged(string? sender, SyncProgressChangedEventArgs args)
await InstallMultipleRemotePackageAsync(splitRemoteFilePaths, packageName, callback, cancellationToken, arguments);

int count = 0;
object locker = new();
callback?.Invoke(new InstallProgressEventArgs(0, splitRemoteFilePaths.Length, PackageInstallProgressState.PostInstall));
await splitRemoteFilePaths.Select(async x =>
{
await RemoveRemotePackageAsync(x, cancellationToken).ConfigureAwait(false);
callback?.Invoke(new InstallProgressEventArgs(++count, splitRemoteFilePaths.Length, PackageInstallProgressState.PostInstall));
lock (locker)
{
count++;
callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFilePaths.Length, PackageInstallProgressState.PostInstall));
}
}).WhenAll().ConfigureAwait(false);

if (count < splitRemoteFilePaths.Length)
Expand Down Expand Up @@ -279,10 +289,15 @@ public async Task InstallMultipleRemotePackageAsync(string baseRemoteFilePath, I
callback?.Invoke(new InstallProgressEventArgs(1, splitRemoteFileCount + 1, PackageInstallProgressState.WriteSession));

int count = 0;
object locker = new();
await splitRemoteFilePaths.Select(async (splitRemoteFilePath, index) =>
{
await WriteInstallSessionAsync(session, $"split{index}", splitRemoteFilePath, cancellationToken).ConfigureAwait(false);
callback?.Invoke(new InstallProgressEventArgs(++count, splitRemoteFileCount + 1, PackageInstallProgressState.WriteSession));
lock (locker)
{
count++;
callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFileCount + 1, PackageInstallProgressState.WriteSession));
}
}).WhenAll().ConfigureAwait(false);

if (count < splitRemoteFileCount)
Expand Down Expand Up @@ -324,10 +339,15 @@ public async Task InstallMultipleRemotePackageAsync(IEnumerable<string> splitRem
callback?.Invoke(new InstallProgressEventArgs(0, splitRemoteFileCount, PackageInstallProgressState.WriteSession));

int count = 0;
object locker = new();
await splitRemoteFilePaths.Select(async (splitRemoteFilePath, index) =>
{
await WriteInstallSessionAsync(session, $"split{index}", splitRemoteFilePath, cancellationToken).ConfigureAwait(false);
callback?.Invoke(new InstallProgressEventArgs(++count, splitRemoteFileCount, PackageInstallProgressState.WriteSession));
lock (locker)
{
count++;
callback?.Invoke(new InstallProgressEventArgs(count, splitRemoteFileCount, PackageInstallProgressState.WriteSession));
}
}).WhenAll().ConfigureAwait(false);

if (count < splitRemoteFileCount)
Expand Down
4 changes: 2 additions & 2 deletions AdvancedSharpAdbClient/Models/FileStatisticsData.V2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ public readonly record struct FileStatisticsDataEx(uint Error, ulong Device, ulo
/// <summary>
/// Gets the timestamp indicating when the item was last modified.
/// </summary>
[field: FieldOffset((5 * sizeof(uint)) + (2 * sizeof(ulong)) + sizeof(ulong))]
[field: FieldOffset((5 * sizeof(uint)) + (3 * sizeof(ulong)) + sizeof(long))]
public long ModifiedTime { get; init; } = ModifiedTime;

/// <summary>
/// Gets the timestamp indicating when the last change occurred.
/// </summary>
[field: FieldOffset((5 * sizeof(uint)) + (2 * sizeof(ulong)) + (2 * sizeof(ulong)))]
[field: FieldOffset((5 * sizeof(uint)) + (3 * sizeof(ulong)) + (2 * sizeof(long)))]
public long ChangedTime { get; init; } = ChangedTime;

/// <summary>
Expand Down