Exemplo n.º 1
0
 public List<Result> ExecuteQueryFunc(Query query, ITokenSource cancelToken)
 {
     if (QueryFunc != null) {
         return QueryFunc(query, cancelToken);
     }
     return new List<Result>();
 }
Exemplo n.º 2
0
 public ProviderInterest ExecuteIsInterestedFunc(Query query)
 {
     if (IsInterestedFunc != null) {
         return IsInterestedFunc(query);
     }
     return ProviderInterest.None;
 }
Exemplo n.º 3
0
 public void TestQueryWithPath()
 {
     var query = new Query(@"C:\\Program Files\\Directory\\File");
     Assert.That(query.Raw, Is.EqualTo(@"C:\\Program Files\\Directory\\File"));
     Assert.That(query.Keyword, Is.Empty);
     Assert.That(query.Arguments, Is.Empty);
     Assert.IsFalse(query.KeywordComplete);
     Assert.IsFalse(query.Empty);
     Assert.IsFalse(query.HasArguments);
     Assert.IsTrue(query.IsPath);
 }
Exemplo n.º 4
0
 public void TestQueryWithKeywordAndArguments()
 {
     var query = new Query("google search argument");
     Assert.That(query.Raw, Is.EqualTo("google search argument"));
     Assert.That(query.Keyword, Is.EqualTo("google"));
     Assert.That(query.Arguments, Is.EqualTo("search argument"));
     Assert.IsTrue(query.KeywordComplete);
     Assert.IsFalse(query.Empty);
     Assert.IsTrue(query.HasArguments);
     Assert.IsFalse(query.IsPath);
 }
Exemplo n.º 5
0
 public void TestQueryWithKeyword()
 {
     var query = new Query("the_keyword");
     Assert.That(query.Raw, Is.EqualTo("the_keyword"));
     Assert.That(query.Keyword, Is.EqualTo("the_keyword"));
     Assert.That(query.Arguments, Is.Empty);
     Assert.IsFalse(query.KeywordComplete);
     Assert.IsFalse(query.Empty);
     Assert.IsFalse(query.HasArguments);
     Assert.IsFalse(query.IsPath);
 }
Exemplo n.º 6
0
        public void TestResultLaunchWithCompleteArguments()
        {
            using (var mock = AutoMock.GetLoose()) {
                var command = mock.Create<CommandProvider>();
                command.Keyword = "test_keyword";
                command.RequiresArguments = true;

                // query with complete keyword and arguments
                var launchMock = new Mock<Action<Query>>();
                command.Launch = launchMock.Object;
                var query = new Query("test_keyword argument1 argument 2");

                // result should still be returned
                var results = command.QueryFunc(query, mock.Create<ITokenSource>());
                Assert.That(results.Count, Is.EqualTo(1));

                // call launch action (it should execute mock launch action)
                results.First().Launch(query);
                launchMock.Verify(x => x(query), Times.Once);
            }
        }
Exemplo n.º 7
0
        private List<Result> Query(Query query, ITokenSource cancelToken)
        {
            if (query.KeywordComplete && query.HasArguments) {
                // check the cache for a matching result
                var cacheKey = query.Arguments;
                var cachedSuggestions = MemoryCache.Default.Get(cacheKey) as List<string>;

                // if cached result is found, return it.
                if (cachedSuggestions != null) {
                    // convert the list of suggestion strings to a List<Result>
                    if (cachedSuggestions.Any()) {
                        var results = cachedSuggestions.Select(suggestion => new Result
                        {
                            Title = suggestion,
                            Icon = _icon,
                            SubTitle = "Search google for " + suggestion,
                            Launch = query1 =>
                            {
                                Process.Start($"http://google.co.uk/search?q={suggestion}");
                                AppCommands.HideWindow();

                            }
                        }).ToList();
                        return results;
                    }
                    // no suggestions were received from the server
                    return new List<Result>
                    {
                        new Result
                        {
                            Title = "No search suggestions found.",
                            Icon = _icon
                        }
                    };
                }

                // Cache miss, begin the background query to fill the cache
                // create a local cancel token for passing to httpclient..
                var cancellable = new CancellationToken();
                cancellable.Register(() =>
                {
                    cancelToken.Cancel();
                    cancelToken.Dispose();
                });
                var x = GetSuggestionsAsync(query.Arguments, cancellable);
                return new List<Result>
                {
                    new Result
                    {
                        Title = "Retrieving search suggestions...",
                        Icon = _icon
                    }
                };
            }
            // otherwise the query has not been provided yet, running the action will autocomplete the query
            return new List<Result>
            {
                new Result
                {
                    Title = "Search Google",
                    SubTitle = "Search Google with Suggestions",
                    Icon = _icon,
                    Launch = query1 => AppCommands.RewriteQuery(Keyword + ' ')
                }
            };
        }
Exemplo n.º 8
0
 /// <summary>
 /// Wraps the command Launch method.  If the keyword is complete, Launch is invoked, otherwise rewrite the query to autocomplete the keyword.
 /// </summary>
 private void HandleLaunch(Query query)
 {
     if (_requiresArguments) {
         if (!query.KeywordComplete || string.IsNullOrEmpty(query.Arguments)) {
             // auto complete the query
             _appCommands.RewriteQuery(_keyword + ' ');
         }
         else {
             _launch(query);
         }
     }
     else {
         _launch(query);
     }
 }
Exemplo n.º 9
0
 public void Invoke(Query query)
 {
     Func?.Invoke(query);
 }
Exemplo n.º 10
0
        public void TestResultLaunchWithIncompleteArguments()
        {
            using (var mock = AutoMock.GetLoose()) {
                // create mock AppCommands (so we can test that the plugin calls it)
                mock.Mock<IAppCommands>().Setup(x => x.RewriteQuery("test_key "));

                var command = mock.Create<CommandProvider>();
                command.Keyword = "test_keyword";
                command.RequiresArguments = true;

                // query with incomplete keyword
                var query = new Query("test_keyw");
                var results = command.QueryFunc(query, mock.Create<ITokenSource>());

                // result should still be returned
                Assert.That(results.Count, Is.EqualTo(1));

                // call launch action (it should rewrite the query)
                results.First().Launch(query);
                mock.Mock<IAppCommands>().Verify(x => x.RewriteQuery("test_keyword "), Times.Once);
            }
        }