Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coenm committed Sep 13, 2023
1 parent d886113 commit 4e4437e
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/RepoM.Plugin.WebBrowser/Services/WebBrowserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public bool ProfileExist(string name)

public void OpenUrl(string url)
{
ProcessHelper.StartProcess(url, string.Empty);
StartProcess(url, string.Empty);
}

public void OpenUrl(string url, string profile)
Expand All @@ -40,7 +40,7 @@ public void OpenUrl(string url, string profile)

if (string.IsNullOrWhiteSpace(profileConfig.CommandLineArguments))
{
ProcessHelper.StartProcess(browser, url);
StartProcess(browser, url);
return;
}

Expand All @@ -54,6 +54,12 @@ public void OpenUrl(string url, string profile)
commandLinesArgs += " " + url;
}

ProcessHelper.StartProcess(browser, commandLinesArgs);
StartProcess(browser, commandLinesArgs);
}

// virtual because of testing
protected virtual void StartProcess(string process, string arguments)
{
ProcessHelper.StartProcess(process, arguments);
}
}
120 changes: 120 additions & 0 deletions tests/RepoM.Plugin.WebBrowser.Tests/WebBrowserServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace RepoM.Plugin.WebBrowser.Tests;

using System;
using System.Collections.Generic;
using FluentAssertions;
using RepoM.Plugin.WebBrowser.Services;
using VerifyXunit;
Expand All @@ -20,4 +21,123 @@ public void Ctor_ShouldThrow_WhenArgumentNull()
// assert
act1.Should().Throw<ArgumentNullException>();
}

[Fact]
public void ProfileExist_ShouldReturnFalse_WhenNoProfilesDefined()
{

// arrange
var config = new WebBrowserConfiguration();
var sut = new WebBrowserService(config);

// act
var result = sut.ProfileExist("name");

// assert
_ = result.Should().BeFalse();
}

[Fact]
public void ProfileExist_ShouldReturnFalse_WhenNoProfileNameDoesNotMatchCase()
{

// arrange
var config = new WebBrowserConfiguration
{
Profiles = new()
{
{ "Name", new BrowserProfileConfig() },
},
};
var sut = new WebBrowserService(config);

// act
var result = sut.ProfileExist("name");

// assert
_ = result.Should().BeFalse();
}

[Fact]
public void ProfileExist_ShouldReturnTrue_WhenProfileExists()
{

// arrange
var config = new WebBrowserConfiguration
{
Profiles = new()
{
{ "name1", new BrowserProfileConfig() },
{ "name2", new BrowserProfileConfig() },
{ "name3", new BrowserProfileConfig() },
},
};
var sut = new WebBrowserService(config);

// act
var result = sut.ProfileExist("name2");

// assert
_ = result.Should().BeTrue();
}

[Fact]
public void OpenUrl_ShouldStartProcess()
{
// arrange
var config = new WebBrowserConfiguration
{
Profiles = new()
{
{ "name1", new BrowserProfileConfig() },
{ "name2", new BrowserProfileConfig() },
{ "name3", new BrowserProfileConfig() },
},
};
var sut = new DummyWebBrowserService(config);

// act
sut.OpenUrl("https://google.com");

// assert
sut.StartProcessCalled.Should().BeEquivalentTo("https://google.com - ");
}

[Fact]
public void OpenUrl_WithProfile_ShouldStartProcess()
{
// arrange
var config = new WebBrowserConfiguration
{
Profiles = new()
{
{ "Private", new BrowserProfileConfig { BrowserName = "Edge", CommandLineArguments = "\"--profile 23 \" {url}", } },
},
Browsers = new()
{
{ "Edge", "msedge.exe" },
},
};
var sut = new DummyWebBrowserService(config);

// act
sut.OpenUrl("https://google.com", "Private");

// assert
sut.StartProcessCalled.Should().BeEquivalentTo("msedge.exe - \"--profile 23 \" https://google.com");
}
}

file class DummyWebBrowserService : WebBrowserService
{
public DummyWebBrowserService(WebBrowserConfiguration configuration) : base(configuration)
{
}

public List<string> StartProcessCalled { get; } = new(1);

protected override void StartProcess(string process, string arguments)
{
StartProcessCalled.Add(process + " - " + arguments);
}
}

0 comments on commit 4e4437e

Please sign in to comment.