diff --git a/src/modules/launcher/Plugins/Wox.Plugin.Indexer/SearchHelper/WindowsSearchAPI.cs b/src/modules/launcher/Plugins/Wox.Plugin.Indexer/SearchHelper/WindowsSearchAPI.cs index b51bf1e4c7..64875595a1 100644 --- a/src/modules/launcher/Plugins/Wox.Plugin.Indexer/SearchHelper/WindowsSearchAPI.cs +++ b/src/modules/launcher/Plugins/Wox.Plugin.Indexer/SearchHelper/WindowsSearchAPI.cs @@ -7,9 +7,9 @@ namespace Wox.Plugin.Indexer.SearchHelper { public class WindowsSearchAPI { - private OleDbConnection conn; - private OleDbCommand command; - private OleDbDataReader WDSResults; + public OleDbConnection conn; + public OleDbCommand command; + public OleDbDataReader WDSResults; public IEnumerable ExecuteQuery(ISearchQueryHelper queryHelper, string keyword) { @@ -43,7 +43,8 @@ namespace Wox.Plugin.Indexer.SearchHelper } } - public void setQueryHelper(ref ISearchQueryHelper queryHelper, string pattern) + + public void ModifyQueryHelper(ref ISearchQueryHelper queryHelper, string pattern) { // convert file pattern if it is not '*'. Don't create restriction for '*' as it includes all files. if (pattern != "*") @@ -63,7 +64,7 @@ namespace Wox.Plugin.Indexer.SearchHelper } } - public IEnumerable Search(string keyword, string pattern = "*", int maxCount = 100) + public void InitQueryHelper(out ISearchQueryHelper queryHelper, int maxCount) { // This uses the Microsoft.Search.Interop assembly CSearchManager manager = new CSearchManager(); @@ -72,7 +73,7 @@ namespace Wox.Plugin.Indexer.SearchHelper ISearchCatalogManager catalogManager = manager.GetCatalog("SystemIndex"); // Get the ISearchQueryHelper which will help us to translate AQS --> SQL necessary to query the indexer - ISearchQueryHelper queryHelper = catalogManager.GetQueryHelper(); + queryHelper = catalogManager.GetQueryHelper(); // Set the number of results we want. Don't set this property if all results are needed. queryHelper.QueryMaxResults = maxCount; @@ -85,9 +86,13 @@ namespace Wox.Plugin.Indexer.SearchHelper // Set sorting order queryHelper.QuerySorting = "System.DateModified DESC"; + } - setQueryHelper(ref queryHelper, pattern); - + public IEnumerable Search(string keyword, string pattern = "*", int maxCount = 100) + { + ISearchQueryHelper queryHelper; + InitQueryHelper(out queryHelper, maxCount); + ModifyQueryHelper(ref queryHelper, pattern); return ExecuteQuery(queryHelper, keyword); } } diff --git a/src/modules/launcher/Plugins/Wox.Plugin.Indexer/Wox.Plugin.Indexer.csproj b/src/modules/launcher/Plugins/Wox.Plugin.Indexer/Wox.Plugin.Indexer.csproj index c4c78bf4d4..1a7bdbe8c7 100644 --- a/src/modules/launcher/Plugins/Wox.Plugin.Indexer/Wox.Plugin.Indexer.csproj +++ b/src/modules/launcher/Plugins/Wox.Plugin.Indexer/Wox.Plugin.Indexer.csproj @@ -9,9 +9,10 @@ Properties Wox.Plugin.Indexer Wox.Plugin.Indexer - v4.7.2 + v4.5.2 512 true + true diff --git a/src/modules/launcher/Wox.Test/Plugins/WindowsIndexerTest.cs b/src/modules/launcher/Wox.Test/Plugins/WindowsIndexerTest.cs new file mode 100644 index 0000000000..9df2aa5892 --- /dev/null +++ b/src/modules/launcher/Wox.Test/Plugins/WindowsIndexerTest.cs @@ -0,0 +1,145 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Data.OleDb; +using Microsoft.Search.Interop; +using Wox.Plugin.Indexer.SearchHelper; + +namespace Wox.Test.Plugins +{ + + [TestFixture] + public class WindowsIndexerTest + { + private WindowsSearchAPI _api = new WindowsSearchAPI(); + + [Test] + public void InitQueryHelper_ShouldInitialize_WhenFunctionIsCalled() + { + // Arrange + int maxCount = 10; + ISearchQueryHelper queryHelper = null; + + // Act + _api.InitQueryHelper(out queryHelper, maxCount); + + // Assert + Assert.IsNotNull(queryHelper); + Assert.AreEqual(queryHelper.QueryMaxResults, maxCount); + } + + [Test] + public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternIsAsterisk() + { + // Arrange + ISearchQueryHelper queryHelper; + String pattern = "*"; + _api.InitQueryHelper(out queryHelper, 10); + + // Act + _api.ModifyQueryHelper(ref queryHelper, pattern); + + // Assert + Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("LIKE")); + Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains")); + } + + [Test] + public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternContainsAsterisk() + { + // Arrange + ISearchQueryHelper queryHelper; + String pattern = "tt*^&)"; + _api.InitQueryHelper(out queryHelper, 10); + + // Act + _api.ModifyQueryHelper(ref queryHelper, pattern); + + // Assert + Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("LIKE")); + Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains")); + } + + [Test] + public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternContainsPercent() + { + // Arrange + ISearchQueryHelper queryHelper; + String pattern = "tt%^&)"; + _api.InitQueryHelper(out queryHelper, 10); + + // Act + _api.ModifyQueryHelper(ref queryHelper, pattern); + + // Assert + Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("LIKE")); + Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains")); + } + + [Test] + public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternContainsUnderScore() + { + // Arrange + ISearchQueryHelper queryHelper; + String pattern = "tt_^&)"; + _api.InitQueryHelper(out queryHelper, 10); + + // Act + _api.ModifyQueryHelper(ref queryHelper, pattern); + + // Assert + Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("LIKE")); + Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains")); + } + + [Test] + public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternContainsQuestionMark() + { + // Arrange + ISearchQueryHelper queryHelper; + String pattern = "tt?^&)"; + _api.InitQueryHelper(out queryHelper, 10); + + // Act + _api.ModifyQueryHelper(ref queryHelper, pattern); + + // Assert + Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("LIKE")); + Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("Contains")); + } + + [Test] + public void ModifyQueryHelper_ShouldSetQueryHelper_WhenPatternDoesNotContainSplSymbols() + { + // Arrange + ISearchQueryHelper queryHelper; + String pattern = "tt^&)bc"; + _api.InitQueryHelper(out queryHelper, 10); + + // Act + _api.ModifyQueryHelper(ref queryHelper, pattern); + + // Assert + Assert.IsFalse(queryHelper.QueryWhereRestrictions.Contains("LIKE")); + Assert.IsTrue(queryHelper.QueryWhereRestrictions.Contains("Contains")); + } + + [Test] + public void ExecuteQuery_ShouldDisposeAllConnections_AfterFunctionCall() + { + // Arrange + ISearchQueryHelper queryHelper; + _api.InitQueryHelper(out queryHelper, 10); + _api.ModifyQueryHelper(ref queryHelper, "*"); + string keyword = "test"; + + // Act + _api.ExecuteQuery(queryHelper, keyword); + + // Assert + Assert.IsNull(_api.conn); + Assert.IsNull(_api.command); + Assert.IsNull(_api.WDSResults); + } + } +} diff --git a/src/modules/launcher/Wox.Test/Wox.Test.csproj b/src/modules/launcher/Wox.Test/Wox.Test.csproj index 1de1a7880b..e656fca226 100644 --- a/src/modules/launcher/Wox.Test/Wox.Test.csproj +++ b/src/modules/launcher/Wox.Test/Wox.Test.csproj @@ -38,6 +38,7 @@ + @@ -45,10 +46,15 @@ + + + {63c3cea8-51fe-472e-b97c-b58f8b17dd51} + Wox.Plugin.Indexer + {B749F0DB-8E75-47DB-9E5E-265D16D0C0D2} Wox.Core @@ -72,6 +78,9 @@ 3.15.1 + + 1.0.0 +