public void Get_Returns_Null_For_Missing_Extensions() { // Given an empty registry, we should return null for the class of any extension. var registry = new InMemoryRegistry(); var className = FileExtensionClass.Get(registry.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default), ".some_type", false); Assert.That(className, Is.Null); }
[TestCase(".")] // no actual extension public void Invalid_Exceptions_Throw_On_Get(string extension) { try { var registry = new InMemoryRegistry(); FileExtensionClass.Get(registry.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default), extension, false); Assert.Fail($@"An exception should be thrown for extension '{extension}'"); } catch (Exception exception) { // We should have an exception which includes the invalid extension in the message. if (!string.IsNullOrEmpty(extension)) { Assert.That(exception.Message, Contains.Substring(extension)); } } }
/// <summary> /// Determines whether a server is associated with a shell item. /// </summary> /// <param name="server">The server.</param> /// <param name="shellItem">The shell item.</param> /// <returns> /// <c>true</c> if a server is associated with the shell item; otherwise, <c>false</c>. /// </returns> private bool IsServerAssociatedWithShellItem(ISharpShellServer server, ShellItem shellItem) { // If we don't have the server, bail. if (server == null || shellItem == null) { return(false); } // Get the associations. var associationType = COMServerAssociationAttribute.GetAssociationType(server.GetType()); var associations = COMServerAssociationAttribute.GetAssociations(server.GetType()); // TODO: This is potentially a very useful check - maybe it should be moved into the // COMServerAssociationAttribute class so that it can be reused. // We have a special case for icon overlays. if (server is SharpIconOverlayHandler && TestIconOverlayHandler != null && shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM)) { if (((IShellIconOverlayIdentifier)TestIconOverlayHandler).IsMemberOf(shellItem.Path, FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL) == 0) { return(true); } } // Based on the assocation type, we can check the shell item. switch (associationType) { // This is checked for backwards compatibility. #pragma warning disable 618 case AssociationType.FileExtension: #pragma warning restore 618 // File extensions are easy to check. if (shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM)) { return (associations.Any( a => string.Compare(Path.GetExtension(shellItem.DisplayName), a, StringComparison.OrdinalIgnoreCase) == 0)); } break; case AssociationType.ClassOfExtension: // TODO must be tested. if (shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM)) { // Get our class. var registry = ServiceRegistry.GetService <IRegistry>(); using (var classesRoot = registry.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default)) { var fileClass = FileExtensionClass.Get(classesRoot, Path.GetExtension(shellItem.DisplayName), false); // Do we match it? return(associations.Any(a => string.Compare(fileClass, FileExtensionClass.Get(classesRoot, a, false), StringComparison.InvariantCultureIgnoreCase) == 0)); } } break; case AssociationType.Class: // TODO must be tested. break; case AssociationType.AllFiles: // TODO must be tested. return(shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM) && shellItem.IsFolder == false); case AssociationType.Directory: // Directories are filesystem, not streams, and folder. return(shellItem.Attributes.HasFlag(SFGAO.SFGAO_FILESYSTEM) && !shellItem.Attributes.HasFlag(SFGAO.SFGAO_STREAM) && shellItem.IsFolder); case AssociationType.Drive: // TODO must be tested. return(shellItem.Attributes.HasFlag(SFGAO.SFGAO_STORAGEANCESTOR)); case AssociationType.UnknownFiles: // TODO must be tested. break; } return(false); }
void listView_Drop(object sender, DragEventArgs e) { if (this.ItemUnderDragCursor != null) { this.ItemUnderDragCursor = null; } e.Effects = DragDropEffects.Move; if (!e.Data.GetDataPresent(typeof(ItemType))) { return; } // Get the data object which was dropped. ItemType data = e.Data.GetData(typeof(ItemType)) as ItemType; if (data == null) { return; } // Get the ObservableCollection<ItemType> which contains the dropped data object. ObservableCollection <ItemType> itemsDestination = this.listViewDestination.ItemsSource as ObservableCollection <ItemType>; if (itemsDestination == null) { throw new Exception( "A ListView managed by ListViewDragManager must have its ItemsSource set to an ObservableCollection<ItemType>."); } int oldIndex = itemsDestination.IndexOf(data); int newIndex = this.IndexUnderDragCursor; if (newIndex < 0) { // The drag started somewhere else, and our ListView is empty // so make the new item the first in the list. if (itemsDestination.Count == 0) { newIndex = 0; } // The drag started somewhere else, but our ListView has items // so make the new item the last in the list. else if (oldIndex < 0) { newIndex = itemsDestination.Count; } // The user is trying to drop an item from our ListView into // our ListView, but the mouse is not over an item, so don't // let them drop it. else { return; } } // Dropping an item back onto itself is not considered an actual 'drop'. if (oldIndex == newIndex) { return; } if (this.ProcessDrop != null) { // Let the client code process the drop. ProcessDropEventArgs <ItemType> args = new ProcessDropEventArgs <ItemType>(itemsDestination, data, oldIndex, newIndex, e.AllowedEffects); this.ProcessDrop(this, args); e.Effects = args.Effects; } else { foreach (ListView lv in allListViews) { ObservableCollection <InterfaceClass> itemsSource = lv.ItemsSource as ObservableCollection <InterfaceClass>; InterfaceClass obj = null; if (itemsSource != null) { foreach (InterfaceClass item in itemsSource) { obj = data as InterfaceClass; if (item.Addres.Equals(obj.Addres)) { break; } } } if (obj != null && itemsSource != null) { itemsSource.Remove(obj); } } foreach (ListView lv in allListViews) { ObservableCollection <FileExtensionClass> itemsSource = lv.ItemsSource as ObservableCollection <FileExtensionClass>; FileExtensionClass obj = null; if (itemsSource != null) { foreach (FileExtensionClass item in itemsSource) { obj = data as FileExtensionClass; if (item.Extension.Equals(obj.Extension)) { break; } } } if (obj != null && itemsSource != null) { itemsSource.Remove(obj); } } itemsDestination.Insert(newIndex, data); // Set the Effects property so that the call to DoDragDrop will return 'Move'. e.Effects = DragDropEffects.Move; } }