2020-10-01 05:37:46 +02:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System ;
using System.Collections.Generic ;
2022-10-16 14:23:31 +02:00
using System.IO ;
2020-11-02 18:33:43 +01:00
using System.IO.Abstractions.TestingHelpers ;
2020-10-01 05:37:46 +02:00
using System.Linq ;
2024-09-16 16:09:43 -04:00
2020-10-01 05:37:46 +02:00
using Microsoft.Plugin.Folder.Sources ;
using Microsoft.Plugin.Folder.Sources.Result ;
2021-08-16 15:25:06 +02:00
using Microsoft.VisualStudio.TestTools.UnitTesting ;
2020-10-01 05:37:46 +02:00
namespace Microsoft.Plugin.Folder.UnitTests
{
2021-08-16 15:25:06 +02:00
[TestClass]
2020-10-01 05:37:46 +02:00
public class InternalQueryFolderTests
{
2020-11-02 18:33:43 +01:00
private static IQueryFileSystemInfo _queryFileSystemInfoMock ;
private static MockFileSystem _fileSystem ;
2020-10-01 05:37:46 +02:00
2021-08-16 15:25:06 +02:00
[TestInitialize]
2020-10-01 05:37:46 +02:00
public void SetupMock ( )
{
2020-11-02 18:33:43 +01:00
// Note: This mock filesystem adds a 'c:\temp' directory.
_fileSystem = new MockFileSystem ( new Dictionary < string , MockFileData > ( )
2020-10-01 05:37:46 +02:00
{
2020-11-02 18:33:43 +01:00
{ @"c:\bla.txt" , new MockFileData ( string . Empty ) } ,
{ @"c:\Test\test.txt" , new MockFileData ( string . Empty ) } ,
{ @"c:\Test\more-test.png" , new MockFileData ( string . Empty ) } ,
{ @"c:\Test\A\deep-nested.png" , new MockFileData ( string . Empty ) } ,
{ @"c:\Test\b\" , new MockDirectoryData ( ) } ,
} ) ;
2022-10-16 14:23:31 +02:00
_queryFileSystemInfoMock = new QueryFileSystemInfo ( _fileSystem . DirectoryInfo , MatchType . Simple , FileAttributes . Hidden | FileAttributes . System ) ;
2020-10-01 05:37:46 +02:00
}
2021-08-16 15:25:06 +02:00
[TestMethod]
2020-10-01 05:37:46 +02:00
public void Query_ThrowsException_WhenCalledNull ( )
{
// Setup
2020-11-02 18:33:43 +01:00
var queryInternalDirectory = new QueryInternalDirectory ( new FolderSettings ( ) , _queryFileSystemInfoMock , _fileSystem . Directory ) ;
2020-10-01 05:37:46 +02:00
// Act & Assert
2021-08-16 15:25:06 +02:00
Assert . ThrowsException < ArgumentNullException > ( ( ) = > queryInternalDirectory . Query ( null ) . ToArray ( ) ) ;
2020-10-01 05:37:46 +02:00
}
2021-08-16 15:25:06 +02:00
[DataTestMethod]
[DataRow(@"c", 0, 0, false, DisplayName = "String empty is nothing")]
[DataRow(@"c:", 2, 1, false, DisplayName = "Root without \\")]
[DataRow(@"c:\", 2, 1, false, DisplayName = "Normal root")]
[DataRow(@"c:\Test", 2, 2, false, DisplayName = "Select yourself")]
2024-11-11 10:42:40 +01:00
[DataRow(@"c:\not-exist", 2, 0, false, DisplayName = "Folder not exist, return root")]
2021-08-16 15:25:06 +02:00
[DataRow(@"c:\not-exist\not-exist2", 0, 0, false, DisplayName = "Folder not exist, return root")]
[DataRow(@"c:\bla.t", 2, 1, false, DisplayName = "Partial match file")]
2022-06-02 11:44:52 +02:00
[DataRow(@"c:/bla.t", 2, 1, false, DisplayName = "Partial match file with /")]
2020-10-01 05:37:46 +02:00
public void Query_WhenCalled ( string search , int folders , int files , bool truncated )
{
2020-11-02 18:33:43 +01:00
const int maxFolderSetting = 3 ;
// Setup
var folderSettings = new FolderSettings ( )
{
MaxFileResults = maxFolderSetting ,
MaxFolderResults = maxFolderSetting ,
} ;
var queryInternalDirectory = new QueryInternalDirectory ( folderSettings , _queryFileSystemInfoMock , _fileSystem . Directory ) ;
// Act
var isDriveOrSharedFolder = queryInternalDirectory . Query ( search )
. ToLookup ( r = > r . GetType ( ) ) ;
// Assert
Assert . AreEqual ( files , isDriveOrSharedFolder [ typeof ( FileItemResult ) ] . Count ( ) , "File count doesn't match" ) ;
Assert . AreEqual ( folders , isDriveOrSharedFolder [ typeof ( FolderItemResult ) ] . Count ( ) , "folder count doesn't match" ) ;
// Always check if there is less than max folders
2021-08-16 15:25:06 +02:00
Assert . IsTrue ( isDriveOrSharedFolder [ typeof ( FileItemResult ) ] . Count ( ) < = maxFolderSetting , "Files are not limited" ) ;
Assert . IsTrue ( isDriveOrSharedFolder [ typeof ( FolderItemResult ) ] . Count ( ) < = maxFolderSetting , "Folders are not limited" ) ;
2020-11-02 18:33:43 +01:00
// Checks if CreateOpenCurrentFolder is displayed
Assert . AreEqual ( Math . Min ( folders + files , 1 ) , isDriveOrSharedFolder [ typeof ( CreateOpenCurrentFolderResult ) ] . Count ( ) , "CreateOpenCurrentFolder displaying is incorrect" ) ;
Assert . AreEqual ( truncated , isDriveOrSharedFolder [ typeof ( TruncatedItemResult ) ] . Count ( ) = = 1 , "CreateOpenCurrentFolder displaying is incorrect" ) ;
}
2021-08-16 15:25:06 +02:00
[DataTestMethod]
[DataRow(@"c:\>", 3, 3, true, DisplayName = "Max Folder test recursive")]
2024-11-11 10:42:40 +01:00
[DataRow(@"c:\Test>", 3, 0, true, DisplayName = "2 Folders recursive")]
[DataRow(@"c:\not-exist>", 3, 0, true, DisplayName = "Folder not exist, return root recursive")]
2021-08-16 15:25:06 +02:00
[DataRow(@"c:\not-exist\not-exist2>", 0, 0, false, DisplayName = "Folder not exist, return root recursive")]
2020-11-02 18:33:43 +01:00
public void Query_Recursive_WhenCalled ( string search , int folders , int files , bool truncated )
{
const int maxFolderSetting = 3 ;
2020-10-01 05:37:46 +02:00
// Setup
var folderSettings = new FolderSettings ( )
{
MaxFileResults = maxFolderSetting ,
MaxFolderResults = maxFolderSetting ,
} ;
2020-11-02 18:33:43 +01:00
var queryInternalDirectory = new QueryInternalDirectory ( folderSettings , _queryFileSystemInfoMock , _fileSystem . Directory ) ;
2020-10-01 05:37:46 +02:00
// Act
var isDriveOrSharedFolder = queryInternalDirectory . Query ( search )
. ToLookup ( r = > r . GetType ( ) ) ;
// Assert
Assert . AreEqual ( files , isDriveOrSharedFolder [ typeof ( FileItemResult ) ] . Count ( ) , "File count doesn't match" ) ;
Assert . AreEqual ( folders , isDriveOrSharedFolder [ typeof ( FolderItemResult ) ] . Count ( ) , "folder count doesn't match" ) ;
// Always check if there is less than max folders
2021-08-16 15:25:06 +02:00
Assert . IsTrue ( isDriveOrSharedFolder [ typeof ( FileItemResult ) ] . Count ( ) < = maxFolderSetting , "Files are not limited" ) ;
Assert . IsTrue ( isDriveOrSharedFolder [ typeof ( FolderItemResult ) ] . Count ( ) < = maxFolderSetting , "Folders are not limited" ) ;
2020-10-01 05:37:46 +02:00
// Checks if CreateOpenCurrentFolder is displayed
Assert . AreEqual ( Math . Min ( folders + files , 1 ) , isDriveOrSharedFolder [ typeof ( CreateOpenCurrentFolderResult ) ] . Count ( ) , "CreateOpenCurrentFolder displaying is incorrect" ) ;
Assert . AreEqual ( truncated , isDriveOrSharedFolder [ typeof ( TruncatedItemResult ) ] . Count ( ) = = 1 , "CreateOpenCurrentFolder displaying is incorrect" ) ;
}
}
}