Пример #1
0
        public void WindowsSearchAPI_ShouldNotReturnResultsWithNullValue_WhenDbResultHasANullColumn()
        {
            // Arrange
            OleDBResult file1 = new OleDBResult(new List <object>()
            {
                "C:/test/path/file1.txt", DBNull.Value, (Int64)0x0
            });
            OleDBResult file2 = new OleDBResult(new List <object>()
            {
                "C:/test/path/file2.txt", "file2.txt", (Int64)0x0
            });

            List <OleDBResult> results = new List <OleDBResult>()
            {
                file1, file2
            };
            var mock = new Mock <ISearch>();

            mock.Setup(x => x.Query(It.IsAny <string>(), It.IsAny <string>())).Returns(results);
            WindowsSearchAPI _api = new WindowsSearchAPI(mock.Object, false);

            // Act
            var windowsSearchAPIResults = _api.Search("FilePath");

            // Assert
            Assert.IsTrue(windowsSearchAPIResults.Count() == 1);
            Assert.IsFalse(windowsSearchAPIResults.Any(x => x.Title == "file1.txt"));
            Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file2.txt"));
        }
Пример #2
0
        public void WindowsSearchAPIShouldReturnEmptyResultsWhenIsFullQueryIsTrueAndTheQueryDoesNotRequireLIKESyntax()
        {
            // Arrange
            OleDBResult file1 = new OleDBResult(new List <object>()
            {
                "C:/test/path/file1.txt", DBNull.Value
            });
            OleDBResult file2 = new OleDBResult(new List <object>()
            {
                "C:/test/path/file2.txt", "file2.txt"
            });

            List <OleDBResult> results = new List <OleDBResult>()
            {
                file1, file2
            };
            var mock = new Mock <ISearch>();

            mock.Setup(x => x.Query(It.IsAny <string>(), It.IsAny <string>())).Returns(results);
            WindowsSearchAPI api = new WindowsSearchAPI(mock.Object, false);
            var searchManager    = GetMockSearchManager();

            // Act
            var windowsSearchAPIResults = api.Search("file", searchManager, true);

            // Assert
            Assert.IsTrue(!windowsSearchAPIResults.Any());
        }
Пример #3
0
        public void WindowsSearchAPI_ShouldNotShowHiddenFiles_WhenDisplayHiddenFilesIsFalse()
        {
            // Arrange
            OleDBResult unHiddenFile = new OleDBResult(new List <object>()
            {
                "C:/test/path/file1.txt", "file1.txt", (Int64)0x0
            });
            OleDBResult hiddenFile = new OleDBResult(new List <object>()
            {
                "C:/test/path/file2.txt", "file2.txt", (Int64)0x2
            });
            List <OleDBResult> results = new List <OleDBResult>()
            {
                hiddenFile, unHiddenFile
            };
            var mock = new Mock <ISearch>();

            mock.Setup(x => x.Query(It.IsAny <string>(), It.IsAny <string>())).Returns(results);
            WindowsSearchAPI _api = new WindowsSearchAPI(mock.Object, false);

            // Act
            var windowsSearchAPIResults = _api.Search("FilePath");

            // Assert
            Assert.IsTrue(windowsSearchAPIResults.Count() == 1);
            Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file1.txt"));
            Assert.IsFalse(windowsSearchAPIResults.Any(x => x.Title == "file2.txt"));
        }
Пример #4
0
        public void WindowsSearchAPIShouldNotReturnResultsWithNullValueWhenDbResultHasANullColumn()
        {
            // Arrange
            OleDBResult unHiddenFile = new OleDBResult(new List <object>()
            {
                "C:/test/path/file1.txt", DBNull.Value
            });
            OleDBResult hiddenFile = new OleDBResult(new List <object>()
            {
                "C:/test/path/file2.txt", "file2.txt"
            });
            List <OleDBResult> results = new List <OleDBResult>()
            {
                hiddenFile, unHiddenFile
            };
            var mock = new Mock <ISearch>();

            mock.Setup(x => x.Query(It.IsAny <string>(), It.IsAny <string>())).Returns(results);
            WindowsSearchAPI api  = new WindowsSearchAPI(mock.Object, false);
            var mockSearchManager = GetMockSearchManager();

            // Act
            var windowsSearchAPIResults = api.Search("FilePath", mockSearchManager);

            // Assert
            Assert.IsTrue(windowsSearchAPIResults.Count() == 1);
            Assert.IsFalse(windowsSearchAPIResults.Any(x => x.Title == "file1.txt"));
            Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file2.txt"));
        }
Пример #5
0
        public void WindowsSearchAPIShouldReturnResultsWhenSearchWasExecuted()
        {
            // Arrange
            OleDBResult unHiddenFile = new OleDBResult(new List <object>()
            {
                "C:/test/path/file1.txt", "file1.txt"
            });
            OleDBResult hiddenFile = new OleDBResult(new List <object>()
            {
                "C:/test/path/file2.txt", "file2.txt"
            });
            List <OleDBResult> results = new List <OleDBResult>()
            {
                hiddenFile, unHiddenFile
            };
            var mock = new Mock <ISearch>();

            mock.Setup(x => x.Query(It.IsAny <string>(), It.IsAny <string>())).Returns(results);
            WindowsSearchAPI api  = new WindowsSearchAPI(mock.Object, true);
            var mockSearchManager = GetMockSearchManager();

            // Act
            var windowsSearchAPIResults = api.Search("FilePath", mockSearchManager);

            // Assert
            Assert.IsTrue(windowsSearchAPIResults.Count() == 2);
            Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file1.txt"));
            Assert.IsTrue(windowsSearchAPIResults.Any(x => x.Title == "file2.txt"));
        }
Пример #6
0
        public void WindowsSearchAPI_ShouldReturnEmptyResults_WhenIsFullQueryIsTrueAndTheQueryDoesNotRequireLIKESyntax()
        {
            // Arrange
            OleDBResult file1 = new OleDBResult(new List <object>()
            {
                "C:/test/path/file1.txt", DBNull.Value, (Int64)0x0
            });
            OleDBResult file2 = new OleDBResult(new List <object>()
            {
                "C:/test/path/file2.txt", "file2.txt", (Int64)0x0
            });

            List <OleDBResult> results = new List <OleDBResult>()
            {
                file1, file2
            };
            var mock = new Mock <ISearch>();

            mock.Setup(x => x.Query(It.IsAny <string>(), It.IsAny <string>())).Returns(results);
            WindowsSearchAPI _api = new WindowsSearchAPI(mock.Object, false);

            // Act
            var windowsSearchAPIResults = _api.Search("file", true);

            // Assert
            Assert.IsTrue(windowsSearchAPIResults.Count() == 0);
        }
Пример #7
0
        public void ExecuteQuery_ShouldDisposeAllConnections_AfterFunctionCall()
        {
            // Arrange
            OleDBSearch      oleDbSearch = new OleDBSearch();
            WindowsSearchAPI _api        = new WindowsSearchAPI(oleDbSearch);

            // Act
            _api.Search("FilePath");

            // Assert
            Assert.IsTrue(oleDbSearch.HaveAllDisposableItemsBeenDisposed());
        }
Пример #8
0
        public List <Result> Query(Query query, bool isFullQuery)
        {
            var results = new List <Result>();

            if (!string.IsNullOrEmpty(query.Search))
            {
                var searchQuery = query.Search;
                if (_settings.MaxSearchCount <= 0)
                {
                    _settings.MaxSearchCount = 30;
                }

                var regexMatch = Regex.Match(searchQuery, reservedStringPattern);

                if (!regexMatch.Success)
                {
                    try
                    {
                        if (_driveDetection.DisplayWarning())
                        {
                            results.Add(new Result
                            {
                                Title    = Properties.Resources.Microsoft_plugin_indexer_drivedetectionwarning,
                                SubTitle = Properties.Resources.Microsoft_plugin_indexer_disable_warning_in_settings,
                                IcoPath  = WarningIconPath,
                                Action   = e =>
                                {
                                    Helper.OpenInShell("ms-settings:cortana-windowssearch");
                                    return(true);
                                },
                            });
                        }

                        // This uses the Microsoft.Search.Interop assembly
                        var searchManager     = new CSearchManager();
                        var searchResultsList = _api.Search(searchQuery, searchManager, maxCount: _settings.MaxSearchCount).ToList();

                        // If the delayed execution query is not required (since the SQL query is fast) return empty results
                        if (searchResultsList.Count == 0 && isFullQuery)
                        {
                            return(new List <Result>());
                        }

                        foreach (var searchResult in searchResultsList)
                        {
                            var path = searchResult.Path;

                            // Using CurrentCulture since this is user facing
                            var    toolTipTitle = string.Format(CultureInfo.CurrentCulture, "{0} : {1}", Properties.Resources.Microsoft_plugin_indexer_name, searchResult.Title);
                            var    toolTipText  = string.Format(CultureInfo.CurrentCulture, "{0} : {1}", Properties.Resources.Microsoft_plugin_indexer_path, path);
                            string workingDir   = null;
                            if (_settings.UseLocationAsWorkingDir)
                            {
                                workingDir = _fileSystem.Path.GetDirectoryName(path);
                            }

                            Result r = new Result();
                            r.Title       = searchResult.Title;
                            r.SubTitle    = Properties.Resources.Microsoft_plugin_indexer_subtitle_header + ": " + path;
                            r.IcoPath     = path;
                            r.ToolTipData = new ToolTipData(toolTipTitle, toolTipText);
                            r.Action      = c =>
                            {
                                bool hide = true;
                                if (!Helper.OpenInShell(path, null, workingDir))
                                {
                                    hide = false;
                                    var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
                                    var msg  = Properties.Resources.Microsoft_plugin_indexer_file_open_failed;
                                    _context.API.ShowMsg(name, msg, string.Empty);
                                }

                                return(hide);
                            };
                            r.ContextData = searchResult;

                            // If the result is a directory, then it's display should show a directory.
                            if (_fileSystem.Directory.Exists(path))
                            {
                                r.QueryTextDisplay = path;
                            }

                            results.Add(r);
                        }
                    }
                    catch (InvalidOperationException)
                    {
                        // The connection has closed, internal error of ExecuteReader()
                        // Not showing this exception to the users
                    }
                    catch (Exception ex)
                    {
                        Log.Exception("Something failed", ex, GetType());
                    }
                }
            }

            return(results);
        }
Пример #9
0
        // This function uses the Windows indexer and returns the list of results obtained
        public List <Result> Query(Query query)
        {
            var results = new List <Result>();

            if (!string.IsNullOrEmpty(query.Search))
            {
                var searchQuery = query.Search;
                if (_settings.MaxSearchCount <= 0)
                {
                    _settings.MaxSearchCount = 50;
                }

                var regexMatch = Regex.Match(searchQuery, ReservedStringPattern);

                if (!regexMatch.Success)
                {
                    try
                    {
                        if (_driveDetection.DisplayWarning())
                        {
                            results.Add(new Result
                            {
                                Title    = _context.API.GetTranslation("Microsoft_plugin_indexer_drivedetectionwarning"),
                                SubTitle = _context.API.GetTranslation("Microsoft_plugin_indexer_disable_warning_in_settings"),
                                IcoPath  = WarningIconPath,
                                Action   = e =>
                                {
                                    try
                                    {
                                        Process.Start(GetWindowsSearchSettingsProcessInfo());
                                    }
                                    catch (Exception ex)
                                    {
                                        Log.Exception("Microsoft.Plugin.Indexer", $"Unable to launch Windows Search Settings: {ex.Message}", ex, "Query");
                                    }
                                    return(true);
                                }
                            });
                        }

                        var searchResultsList = _api.Search(searchQuery, maxCount: _settings.MaxSearchCount).ToList();
                        foreach (var searchResult in searchResultsList)
                        {
                            var    path         = searchResult.Path;
                            var    toolTipTitle = string.Format("{0} : {1}", _context.API.GetTranslation("Microsoft_plugin_indexer_name"), searchResult.Title);
                            var    toolTipText  = string.Format("{0} : {1}", _context.API.GetTranslation("Microsoft_plugin_indexer_path"), path);
                            string workingDir   = null;
                            if (_settings.UseLocationAsWorkingDir)
                            {
                                workingDir = Path.GetDirectoryName(path);
                            }

                            Result r = new Result();
                            r.Title       = searchResult.Title;
                            r.SubTitle    = "Search: " + path;
                            r.IcoPath     = path;
                            r.ToolTipData = new ToolTipData(toolTipTitle, toolTipText);
                            r.Action      = c =>
                            {
                                bool hide;
                                try
                                {
                                    Process.Start(new ProcessStartInfo
                                    {
                                        FileName         = path,
                                        UseShellExecute  = true,
                                        WorkingDirectory = workingDir
                                    });
                                    hide = true;
                                }
                                catch (Win32Exception)
                                {
                                    var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
                                    var msg  = "Can't Open this file";
                                    _context.API.ShowMsg(name, msg, string.Empty);
                                    hide = false;
                                }
                                return(hide);
                            };
                            r.ContextData = searchResult;

                            //If the result is a directory, then it's display should show a directory.
                            if (Directory.Exists(path))
                            {
                                r.QueryTextDisplay = path;
                            }

                            results.Add(r);
                        }
                    }
                    catch (InvalidOperationException)
                    {
                        //The connection has closed, internal error of ExecuteReader()
                        //Not showing this exception to the users
                    }
                    catch (Exception ex)
                    {
                        Log.Info(ex.ToString());
                    }
                }
            }

            return(results);
        }
        // This function uses the Windows indexer and returns the list of results obtained
        public List <Result> Query(Query query)
        {
            var results = new List <Result>();

            if (!string.IsNullOrEmpty(query.Search))
            {
                var searchQuery = query.Search;
                if (_settings.MaxSearchCount <= 0)
                {
                    _settings.MaxSearchCount = 50;
                }

                var regexMatch = Regex.Match(searchQuery, ReservedStringPattern);

                if (!regexMatch.Success)
                {
                    try
                    {
                        var searchResultsList = _api.Search(searchQuery, maxCount: _settings.MaxSearchCount).ToList();
                        foreach (var searchResult in searchResultsList)
                        {
                            var path = searchResult.Path;

                            string workingDir = null;
                            if (_settings.UseLocationAsWorkingDir)
                            {
                                workingDir = Path.GetDirectoryName(path);
                            }

                            Result r = new Result();
                            r.Title    = searchResult.Title;
                            r.SubTitle = "Search: " + path;
                            r.IcoPath  = path;
                            r.Action   = c =>
                            {
                                bool hide;
                                try
                                {
                                    Process.Start(new ProcessStartInfo
                                    {
                                        FileName         = path,
                                        UseShellExecute  = true,
                                        WorkingDirectory = workingDir
                                    });
                                    hide = true;
                                }
                                catch (Win32Exception)
                                {
                                    var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
                                    var msg  = "Can't Open this file";
                                    _context.API.ShowMsg(name, msg, string.Empty);
                                    hide = false;
                                }
                                return(hide);
                            };
                            r.ContextData = searchResult;

                            //If the result is a directory, then it's display should show a directory.
                            if (Directory.Exists(path))
                            {
                                r.QueryTextDisplay = path;
                            }

                            results.Add(r);
                        }
                    }
                    catch (InvalidOperationException)
                    {
                        //The connection has closed, internal error of ExecuteReader()
                        //Not showing this exception to the users
                    }
                    catch (Exception ex)
                    {
                        Log.Info(ex.ToString());
                    }
                }
            }

            return(results);
        }