releases/tests/Paca.Tests/Browser/ChromeOpenSearchAndCustomEngineTests.cs

70 lines
2.7 KiB
C#

using System;
using System.Linq;
using Paca.Browser;
using Paca.Browser.Search;
using Paca.Browser.Tabs;
using Xunit;
namespace Paca.Tests.Browser;
public class ChromeOpenSearchAndCustomEngineTests
{
[Fact]
public void OpenSearchParser_Parses_Standard_Xml_Description()
{
var xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<OpenSearchDescription xmlns=""http://a9.com/-/spec/opensearch/1.1/"">
<ShortName>GitHub</ShortName>
<Description>GitHub Code Search</Description>
<Url type=""text/html"" template=""https://github.com/search?q={searchTerms}""/>
</OpenSearchDescription>";
var engine = OpenSearchParser.Parse(xml, fallbackKeyword: "gh");
Assert.NotNull(engine);
Assert.Equal("GitHub", engine.ShortName);
Assert.Equal("GitHub Code Search", engine.Description);
Assert.Equal("gh", engine.Keyword);
Assert.Equal("https://github.com/search?q=%s", engine.TemplateUrl);
}
[Theory]
[InlineData("https://example.com?q=%s", "hello world", "https://example.com?q=hello+world")]
[InlineData("https://example.com?q={0}", "hello world", "https://example.com?q=hello+world")]
[InlineData("https://example.com?q={searchTerms}", "hello world", "https://example.com?q=hello+world")]
[InlineData("https://example.com?q=%s", "C# & .NET", "https://example.com?q=C%23+%26+.NET")]
public void SearchEngineTemplateFormatter_Formats_Correctly(string template, string query, string expected)
{
var result = SearchEngineTemplateFormatter.FormatUrl(template, query);
Assert.Equal(expected, result);
}
[Fact]
public void SearchEngineResolver_Supports_PercentS_Templates()
{
var custom = new[]
{
new SearchEngine("DevSearch", "ds", "https://devsearch.org/?q=%s")
};
var resolver = new SearchEngineResolver("Google", customEngines: custom);
var intent = resolver.Resolve("ds async task");
Assert.NotNull(intent);
Assert.False(intent.IsUrl);
Assert.Equal("https://devsearch.org/?q=async+task", intent.Target);
Assert.Equal("DevSearch", intent.EngineName);
}
[Fact]
public void OmniboxActionEvaluator_Matches_Search_Engines()
{
var actionsKr = OmniboxActionEvaluator.GetMatchingActions("검색엔진");
Assert.Contains(actionsKr, a => a.ActionId == "action:search-engines");
var actionsEn = OmniboxActionEvaluator.GetMatchingActions("search engines");
Assert.Contains(actionsEn, a => a.ActionId == "action:search-engines");
var action = OmniboxActionEvaluator.AllActions.FirstOrDefault(a => a.ActionId == "action:search-engines");
Assert.NotNull(action);
Assert.Contains("검색엔진", action.DisplayTitle);
}
}