diff --git a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs index 82bfe86..52b0cb5 100644 --- a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs +++ b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.Async.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Threading.Tasks; using Xunit; @@ -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] @@ -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, @@ -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, @@ -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")); } + /// + /// Tests the method without a callback. + /// + [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() { diff --git a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.cs b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.cs index 52a9057..508d1ee 100644 --- a/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.cs +++ b/AdvancedSharpAdbClient.Tests/DeviceCommands/PackageManagerTests.cs @@ -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] @@ -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, @@ -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, @@ -232,6 +236,48 @@ public void InstallMultiplePackageTest() Assert.True(syncService.UploadedFiles.ContainsKey("/data/local/tmp/split_config.xxhdpi.apk")); } + /// + /// Tests the method without a callback. + /// + [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() { diff --git a/AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs b/AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs index 8764882..9bd8c58 100644 --- a/AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs +++ b/AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs @@ -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, @@ -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]; @@ -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, @@ -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]; diff --git a/AdvancedSharpAdbClient.Tests/SyncServiceTests.cs b/AdvancedSharpAdbClient.Tests/SyncServiceTests.cs index 859d122..c73334a 100644 --- a/AdvancedSharpAdbClient.Tests/SyncServiceTests.cs +++ b/AdvancedSharpAdbClient.Tests/SyncServiceTests.cs @@ -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, @@ -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]; diff --git a/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs b/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs index adf958c..571c8fd 100644 --- a/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs +++ b/AdvancedSharpAdbClient/DeviceCommands/PackageManager.Async.cs @@ -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) @@ -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) @@ -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) @@ -324,10 +339,15 @@ public async Task InstallMultipleRemotePackageAsync(IEnumerable 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) diff --git a/AdvancedSharpAdbClient/Models/FileStatisticsData.V2.cs b/AdvancedSharpAdbClient/Models/FileStatisticsData.V2.cs index 8a57903..291412e 100644 --- a/AdvancedSharpAdbClient/Models/FileStatisticsData.V2.cs +++ b/AdvancedSharpAdbClient/Models/FileStatisticsData.V2.cs @@ -96,13 +96,13 @@ public readonly record struct FileStatisticsDataEx(uint Error, ulong Device, ulo /// /// Gets the timestamp indicating when the item was last modified. /// - [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; /// /// Gets the timestamp indicating when the last change occurred. /// - [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; ///