Skip to content

Commit

Permalink
Optimized parameter enumerations and fixed bug in UpdateProgress event (
Browse files Browse the repository at this point in the history
  • Loading branch information
dahall committed Jul 12, 2020
1 parent 26f61b9 commit b153e67
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
65 changes: 49 additions & 16 deletions UnitTests/Windows.Shell/ShellFileOperationsTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Vanara.PInvoke;
using Vanara.PInvoke.Tests;
using Vanara.Windows.Shell;
using static Vanara.PInvoke.Ole32;
Expand All @@ -14,25 +16,52 @@ public class ShellFileOperationsTests
{
[Test]
public void CopyItemTest()
{
ShellFileOperations.Copy(new ShellItem(@"C:\Users\dahall\Downloads\lubuntu-16.04.2-desktop-amd64.iso"), ShellFolder.Desktop);
Assert.That(File.Exists(@"C:\Users\dahall\Desktop\lubuntu-16.04.2-desktop-amd64.iso"), Is.True);
File.Delete(@"C:\Users\dahall\Desktop\lubuntu-16.04.2-desktop-amd64.iso");
{
ShellFileOperations.Copy(new ShellItem(TestCaseSources.LargeFile), ShellFolder.Desktop);
var dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Path.GetFileName(TestCaseSources.LargeFile));
Assert.That(File.Exists(dest), Is.True);
File.Delete(dest);
}

[Test]
public void CopyItemsTest()
{
var l = Directory.EnumerateFiles(@"C:\Users\dahall\Downloads", "h*.zip").Select(s => new ShellItem(s)).ToList();
var l = Directory.EnumerateFiles(KNOWNFOLDERID.FOLDERID_Downloads.FullPath(), "h*.zip").Select(s => new ShellItem(s)).ToList();
ShellFileOperations.Copy(l, ShellFolder.Desktop);
foreach (var i in l)
{
var fn = Path.Combine(@"C:\Users\dahall\Desktop", i.Name);
var fn = Path.Combine(ShellFolder.Desktop.FileSystemPath, i.Name);
Assert.That(File.Exists(fn), Is.True);
File.Delete(fn);
}
}

[Test]
public void CopyWithProgressTest()
{
// Setup hidden copy op with progress handler
bool progressShown = false;
using var op = new ShellFileOperations();
op.Options = ShellFileOperations.OperationFlags.AddUndoRecord | ShellFileOperations.OperationFlags.NoConfirmMkDir | ShellFileOperations.OperationFlags.Silent;
op.UpdateProgress += Op_UpdateProgress;

// Run the operation
using var shi = new ShellItem(TestCaseSources.LargeFile);
op.QueueCopyOperation(shi, ShellFolder.Desktop);
op.PerformOperations();

// Asert and clean
Assert.IsTrue(progressShown);
var dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Path.GetFileName(TestCaseSources.LargeFile));
File.Delete(dest);

void Op_UpdateProgress(object sender, System.ComponentModel.ProgressChangedEventArgs args)
{
Debug.WriteLine($"{args.UserState}: {args.ProgressPercentage}%");
progressShown = true;
}
}

[Test]
public void MoveItemTest()
{
Expand All @@ -54,25 +83,29 @@ public void MoveItemTest()
[Test]
public void MultOpsTest()
{
const string newLargeFile = "MuchLongerNameForTheFile.bin";
using (var op = new ShellFileOperations())
{
op.Options |= ShellFileOperations.OperationFlags.NoMinimizeBox;
var shi = new ShellItem(@"C:\Users\dahall\Downloads\lubuntu-16.04.2-desktop-amd64.iso");
var shi = new ShellItem(TestCaseSources.LargeFile);
op.PostCopyItem += HandleEvent;
op.QueueCopyOperation(shi, ShellFolder.Desktop);
shi = new ShellItem(@"C:\Users\dahall\Desktop\lubuntu-16.04.2-desktop-amd64.iso");
var dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Path.GetFileName(TestCaseSources.LargeFile));
shi = new ShellItem(dest);
op.QueueMoveOperation(shi, new ShellFolder(KNOWNFOLDERID.FOLDERID_Documents));
op.PostMoveItem += HandleEvent;
shi = new ShellItem(@"C:\Users\dahall\Documents\lubuntu-16.04.2-desktop-amd64.iso");
op.QueueRenameOperation(shi, "MuchLongerNameForTheFile.iso");
dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.GetFileName(TestCaseSources.LargeFile));
shi = new ShellItem(dest);
op.QueueRenameOperation(shi, newLargeFile);
op.PostRenameItem += HandleEvent;
shi = new ShellItem(@"C:\Users\dahall\Documents\MuchLongerNameForTheFile.iso");
dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), newLargeFile);
shi = new ShellItem(dest);
op.QueueDeleteOperation(shi);
op.PostDeleteItem += HandleEvent;
op.PerformOperations();
}

void HandleEvent(object sender, ShellFileOperations.ShellFileOpEventArgs args)
static void HandleEvent(object sender, ShellFileOperations.ShellFileOpEventArgs args)
{
Debug.WriteLine(args);
Assert.That(args.Result.Succeeded, Is.True);
Expand All @@ -92,12 +125,12 @@ public void NewItemTest()
}
foreach (var file in files)
{
var fn = Path.Combine(@"C:\Users\dahall\Desktop", file);
var fn = Path.Combine(ShellFolder.Desktop.FileSystemPath, file);
Assert.That(File.Exists(fn), Is.True);
File.Delete(fn);
}

void HandleEvent(object sender, ShellFileOperations.ShellFileOpEventArgs args)
static void HandleEvent(object sender, ShellFileOperations.ShellFileOpEventArgs args)
{
Debug.WriteLine(args);
Assert.That(args.Result.Succeeded, Is.True);
Expand All @@ -114,14 +147,14 @@ public void SetPropsTest2()
using (var op = new ShellFileOperations())
{
op.PostNewItem += HandleEvent;
op.QueueNewItemOperation(ShellFolder.Desktop, fn, template: @"C:\Users\dahall\Documents\Custom Office Templates\blank.dotx");
op.QueueNewItemOperation(ShellFolder.Desktop, fn, template: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"Custom Office Templates\blank.dotx"));
op.QueueApplyPropertiesOperation(new ShellItem(fp), p);
op.PerformOperations();
}
Assert.That(new ShellItem(fp).Properties[PROPERTYKEY.System.Author], Is.EquivalentTo(authors));
File.Delete(fp);

void HandleEvent(object sender, ShellFileOperations.ShellFileOpEventArgs args)
static void HandleEvent(object sender, ShellFileOperations.ShellFileOpEventArgs args)
{
Debug.WriteLine(args);
Assert.That(args.Result.Succeeded, Is.True);
Expand Down
14 changes: 8 additions & 6 deletions Windows.Shell/ShellFileOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public void QueueApplyPropertiesOperation(ShellItem item, ShellItemPropertyUpdat
public void QueueApplyPropertiesOperation(IEnumerable<ShellItem> items, ShellItemPropertyUpdates props)
{
op.SetProperties(props.IPropertyChangeArray);
op.ApplyPropertiesToItems(new ShellItemArray(items).IShellItemArray);
op.ApplyPropertiesToItems(GetSHArray(items));
QueuedOperations++;
}

Expand All @@ -644,7 +644,7 @@ public void QueueCopyOperation(ShellItem source, ShellFolder dest, string newNam
/// <param name="dest">A <see cref="ShellFolder"/> that specifies the destination folder to contain the copy of the items.</param>
public void QueueCopyOperation(IEnumerable<ShellItem> sourceItems, ShellFolder dest)
{
op.CopyItems(new ShellItemArray(sourceItems).IShellItemArray, dest.IShellItem);
op.CopyItems(GetSHArray(sourceItems), dest.IShellItem);
QueuedOperations++;
}

Expand All @@ -662,7 +662,7 @@ public void QueueDeleteOperation(ShellItem item)
/// </param>
public void QueueDeleteOperation(IEnumerable<ShellItem> items)
{
op.DeleteItems(new ShellItemArray(items).IShellItemArray);
op.DeleteItems(GetSHArray(items));
QueuedOperations++;
}

Expand All @@ -686,7 +686,7 @@ public void QueueMoveOperation(ShellItem source, ShellFolder dest, string newNam
/// <param name="dest">A <see cref="ShellFolder"/> that specifies the destination folder to contain the moved items.</param>
public void QueueMoveOperation(IEnumerable<ShellItem> sourceItems, ShellFolder dest)
{
op.MoveItems(new ShellItemArray(sourceItems).IShellItemArray, dest.IShellItem);
op.MoveItems(GetSHArray(sourceItems), dest.IShellItem);
QueuedOperations++;
}

Expand Down Expand Up @@ -735,7 +735,7 @@ public void QueueRenameOperation(ShellItem source, string newName)
/// <param name="newName">The new display name of the items.</param>
public void QueueRenameOperation(IEnumerable<ShellItem> sourceItems, string newName)
{
op.RenameItems(new ShellItemArray(sourceItems).IShellItemArray, newName);
op.RenameItems(GetSHArray(sourceItems), newName);
QueuedOperations++;
}

Expand All @@ -762,6 +762,8 @@ protected virtual void Dispose(bool disposing)
}
}

private IShellItemArray GetSHArray(IEnumerable<ShellItem> items) => items is ShellItemArray a ? a.IShellItemArray : GetSHArray(items);

/// <summary>Arguments supplied to the <see cref="PostNewItem"/> event.</summary>
/// <seealso cref="Vanara.Windows.Shell.ShellFileOperations.ShellFileOpEventArgs"/>
public class ShellFileNewOpEventArgs : ShellFileOpEventArgs
Expand Down Expand Up @@ -881,7 +883,7 @@ public void ResumeTimer()

public void StartOperations() => parent.StartOperations?.Invoke(parent, EventArgs.Empty);

public void UpdateProgress(uint iWorkTotal, uint iWorkSoFar) => parent.UpdateProgress?.Invoke(parent, new System.ComponentModel.ProgressChangedEventArgs((int)(iWorkSoFar * 100 / iWorkTotal), null));
public void UpdateProgress(uint iWorkTotal, uint iWorkSoFar) => parent.UpdateProgress?.Invoke(parent, new System.ComponentModel.ProgressChangedEventArgs(iWorkTotal == 0 ? 0 : (int)(iWorkSoFar * 100 / iWorkTotal), null));
}
}

Expand Down

0 comments on commit b153e67

Please sign in to comment.