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); }