Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions Example/FetcherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand All @@ -19,34 +20,59 @@ public FetcherTest()
InitializeComponent();
}

private void btnFetch_Click(object sender, EventArgs e)
private bool _isFetching = false;
private CancellationTokenSource _cancellationTokenSource = null;

private async void btnFetch_Click(object sender, EventArgs e)
{
if (_isFetching)
{
_cancellationTokenSource?.Cancel();
return;
}

_isFetching = true;
((Button)sender).Text = "Cancel";

try
{
var uri = new Uri(txtUri.Text);
var minSize = (int)numMinSize.Value;
var maxSize = (int)numMaxSize.Value;
var perfectSize = (int)numPerfectSize.Value;

_cancellationTokenSource = new CancellationTokenSource();

picIcon.Size = new Size(16, 16);
picIcon.Image = null;

var image = new Fetcher().Fetch(uri, new FetchOptions
{
MinimumSize = new IconSize(minSize, minSize),
MaximumSize = new IconSize(maxSize, maxSize),
PerfectSize = new IconSize(perfectSize, perfectSize)
});
var image = await new Fetcher().Fetch(
uri,
new FetchOptions
{
MinimumSize = new IconSize(minSize, minSize),
MaximumSize = new IconSize(maxSize, maxSize),
PerfectSize = new IconSize(perfectSize, perfectSize)
},
_cancellationTokenSource);

if (image != null)
{
picIcon.Size = new Size(image.Size.Width, image.Size.Height);
picIcon.Image = image.ToSKBitmap().ToBitmap();
}
}
catch (TaskCanceledException) { }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
_cancellationTokenSource?.Dispose();
_isFetching = false;
((Button)sender).Text = "Fetch";
}
}
}
}
27 changes: 25 additions & 2 deletions Example/ScannerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand All @@ -27,26 +28,48 @@ private void lstResults_Resize(object sender, EventArgs e)
_ExpandLocationColumn();
}

private void btnScan_Click(object sender, EventArgs e)
private bool _isScanning = false;
private CancellationTokenSource _cancellationTokenSource = null;

private async void btnScan_Click(object sender, EventArgs e)
{
try
{
if (_isScanning)
{
_cancellationTokenSource?.Cancel();
return;
}

_isScanning = true;
((Button)sender).Text = "Cancel";

var uri = new Uri(txtUri.Text);

lstResults.Items.Clear();

foreach (var result in new Scanner().Scan(uri))
_cancellationTokenSource = new CancellationTokenSource();

foreach (var result in await new Scanner().Scan(uri, _cancellationTokenSource))
{
lstResults.Items.Add(new ListViewItem(new[]{
result.ExpectedSize.ToString(),
result.Location.ToString()
}));
}
}
catch (TaskCanceledException) { }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
_cancellationTokenSource?.Dispose();
_isScanning = false;
((Button)sender).Text = "Scan";
_cancellationTokenSource?.Dispose();
}
}

private void _ExpandLocationColumn()
Expand Down
6 changes: 3 additions & 3 deletions FaviconFetcher.Tests/BrowserConfigXmlScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Start_ValidXml_Parse()
</browserconfig>");

var scanner = new BrowserconfigXmlScanner(source, uri);
scanner.Start();
scanner.Start().GetAwaiter();

Assert.AreEqual(4, scanner.Results.Count);
}
Expand All @@ -43,7 +43,7 @@ public void Start_InvalidXml_Skip()
<browserconfig>>");

var scanner = new BrowserconfigXmlScanner(source, uri);
scanner.Start();
scanner.Start().GetAwaiter();

Assert.AreEqual(0, scanner.Results.Count);
}
Expand All @@ -68,7 +68,7 @@ public void Start_ContainsInvalidUri_Skip()
</browserconfig>");

var scanner = new BrowserconfigXmlScanner(source, uri);
scanner.Start();
scanner.Start().GetAwaiter();

Assert.AreEqual(3, scanner.Results.Count);
}
Expand Down
Loading