using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
-using System.Runtime.InteropServices;
-using System.Text;
-using NSubstitute;
-using NUnit.Framework;
+using System.Net.Http;
using System.Reflection;
+using System.Runtime.InteropServices;
+using System.Text.RegularExpressions;
+using System.Threading;
+using System.Threading.Tasks;
using System.Windows.Forms;
+using Moq;
+using OpenTween.Models;
using OpenTween.Thumbnail;
using OpenTween.Thumbnail.Services;
-using System.Threading;
-using System.Threading.Tasks;
+using Xunit;
+using Xunit.Extensions;
namespace OpenTween
{
- [TestFixture]
- class TweetThumbnailTest
+ public class TweetThumbnailTest
{
- class TestThumbnailService : SimpleThumbnailService
+ class TestThumbnailService : IThumbnailService
{
- protected string tooltip;
+ private readonly Regex regex;
+ private readonly string replaceUrl;
+ private readonly string replaceTooltip;
- public TestThumbnailService(string pattern, string replacement, string tooltip)
- : base(pattern, replacement)
+ public TestThumbnailService(string pattern, string replaceUrl, string replaceTooltip)
{
- this.tooltip = tooltip;
+ this.regex = new Regex(pattern);
+ this.replaceUrl = replaceUrl;
+ this.replaceTooltip = replaceTooltip;
}
- public override ThumbnailInfo GetThumbnailInfo(string url, PostClass post)
+ public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
{
- var thumbinfo = base.GetThumbnailInfo(url, post);
+ var match = this.regex.Match(url);
+
+ if (!match.Success) return null;
+
+ if (url.StartsWith("http://slow.example.com/", StringComparison.Ordinal))
+ await Task.Delay(1000, token).ConfigureAwait(false);
- if (thumbinfo != null && this.tooltip != null)
+ return new MockThumbnailInfo
{
- var match = this.regex.Match(url);
- thumbinfo.TooltipText = match.Result(this.tooltip);
- }
+ MediaPageUrl = url,
+ ThumbnailImageUrl = match.Result(this.replaceUrl),
+ TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
+ };
+ }
- return thumbinfo;
+ class MockThumbnailInfo : ThumbnailInfo
+ {
+ public override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
+ {
+ return Task.FromResult(TestUtils.CreateDummyImage());
+ }
}
}
- [TestFixtureSetUp]
+ public TweetThumbnailTest()
+ {
+ this.ThumbnailGeneratorSetup();
+ this.MyCommonSetup();
+ }
+
public void ThumbnailGeneratorSetup()
{
ThumbnailGenerator.Services.Clear();
ThumbnailGenerator.Services.AddRange(new[]
{
- new TestThumbnailService(@"^https?://foo.example.com/(.+)$", @"dot.gif", null),
- new TestThumbnailService(@"^https?://bar.example.com/(.+)$", @"dot.gif", @"${1}"),
+ new TestThumbnailService(@"^https?://foo.example.com/(.+)$", @"http://img.example.com/${1}.png", null),
+ new TestThumbnailService(@"^https?://bar.example.com/(.+)$", @"http://img.example.com/${1}.png", @"${1}"),
+ new TestThumbnailService(@"^https?://slow.example.com/(.+)$", @"http://img.example.com/${1}.png", null),
});
}
- [TestFixtureSetUp]
public void MyCommonSetup()
{
- var mockAssembly = Substitute.For<_Assembly>();
- mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
- MyCommon.EntryAssembly = mockAssembly;
+ var mockAssembly = new Mock<_Assembly>();
+ mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
- MyCommon.fileVersion = "1.0.0.0";
+ MyCommon.EntryAssembly = mockAssembly.Object;
}
- [Test]
+ [Fact]
public void CreatePictureBoxTest()
{
using (var thumbBox = new TweetThumbnail())
var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
- Assert.That(picbox, Is.Not.Null);
- Assert.That(picbox.Name, Is.EqualTo("pictureBox1"));
- Assert.That(picbox.SizeMode, Is.EqualTo(PictureBoxSizeMode.Zoom));
- Assert.That(picbox.WaitOnLoad, Is.False);
- Assert.That(picbox.Dock, Is.EqualTo(DockStyle.Fill));
+ Assert.NotNull(picbox);
+ Assert.Equal("pictureBox1", picbox.Name);
+ Assert.Equal(PictureBoxSizeMode.Zoom, picbox.SizeMode);
+ Assert.False(picbox.WaitOnLoad);
+ Assert.Equal(DockStyle.Fill, picbox.Dock);
picbox.Dispose();
}
}
- [Test]
- public void CancelAsyncTest()
+ [Fact]
+ public async Task CancelAsyncTest()
{
- using (var thumbbox = new TweetThumbnail())
+ var post = new PostClass
{
- var post = new PostClass();
+ TextFromApi = "てすと http://slow.example.com/abcd",
+ Media = new List<MediaInfo>
+ {
+ new MediaInfo("http://slow.example.com/abcd"),
+ },
+ };
+ using (var thumbbox = new TweetThumbnail())
+ using (var tokenSource = new CancellationTokenSource())
+ {
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
- var task = thumbbox.ShowThumbnailAsync(post);
+ var task = thumbbox.ShowThumbnailAsync(post, tokenSource.Token);
- thumbbox.CancelAsync();
+ tokenSource.Cancel();
- Assert.Throws<AggregateException>(() => task.Wait());
- Assert.That(task.IsCanceled, Is.True);
+ await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
+ Assert.True(task.IsCanceled);
}
}
- [Test]
- public void SetThumbnailCountTest(
- [Values(0, 1, 2)] int count)
+ [Theory]
+ [InlineData(0)]
+ [InlineData(1)]
+ [InlineData(2)]
+ public void SetThumbnailCountTest(int count)
{
using (var thumbbox = new TweetThumbnail())
{
var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
method.Invoke(thumbbox, new[] { (object)count });
- Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(count));
+ Assert.Equal(count, thumbbox.pictureBox.Count);
var num = 0;
foreach (var picbox in thumbbox.pictureBox)
{
- Assert.That(picbox.Name, Is.EqualTo("pictureBox" + num));
+ Assert.Equal("pictureBox" + num, picbox.Name);
num++;
}
- Assert.That(thumbbox.panelPictureBox.Controls, Is.EquivalentTo(thumbbox.pictureBox));
+ Assert.Equal(thumbbox.pictureBox, thumbbox.panelPictureBox.Controls.Cast<OTPictureBox>());
- Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
- Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(count));
+ Assert.Equal(0, thumbbox.scrollBar.Minimum);
+
+ if (count == 0)
+ Assert.Equal(0, thumbbox.scrollBar.Maximum);
+ else
+ Assert.Equal(count - 1, thumbbox.scrollBar.Maximum);
}
}
- [Test]
- public void ShowThumbnailAsyncTest()
+ [Fact]
+ public async Task ShowThumbnailAsyncTest()
{
var post = new PostClass
{
TextFromApi = "てすと http://foo.example.com/abcd",
- Media = new Dictionary<string, string>
+ Media = new List<MediaInfo>
{
- {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
+ new MediaInfo("http://foo.example.com/abcd"),
},
};
using (var thumbbox = new TweetThumbnail())
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
- thumbbox.ShowThumbnailAsync(post).Wait();
+ await thumbbox.ShowThumbnailAsync(post);
+
+ Assert.Equal(0, thumbbox.scrollBar.Maximum);
+ Assert.False(thumbbox.scrollBar.Enabled);
- Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(0));
- Assert.That(thumbbox.scrollBar.Enabled, Is.False);
+ Assert.Equal(1, thumbbox.pictureBox.Count);
+ Assert.NotNull(thumbbox.pictureBox[0].Image);
- Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(1));
- Assert.That(thumbbox.pictureBox[0].ImageLocation, Is.EqualTo("dot.gif"));
+ Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
+ var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
- var thumbinfo = thumbbox.pictureBox[0].Tag as ThumbnailInfo;
- Assert.That(thumbinfo, Is.Not.Null);
- Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://foo.example.com/abcd"));
- Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("dot.gif"));
+ Assert.Equal("http://foo.example.com/abcd", thumbinfo.MediaPageUrl);
+ Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailImageUrl);
- Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
+ Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
}
}
- [Test]
- public void ShowThumbnailAsyncTest2()
+ [Fact]
+ public async Task ShowThumbnailAsyncTest2()
{
var post = new PostClass
{
TextFromApi = "てすと http://foo.example.com/abcd http://bar.example.com/efgh",
- Media = new Dictionary<string, string>
+ Media = new List<MediaInfo>
{
- {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
- {"http://bar.example.com/efgh", "http://bar.example.com/efgh"},
+ new MediaInfo("http://foo.example.com/abcd"),
+ new MediaInfo("http://bar.example.com/efgh"),
},
};
using (var thumbbox = new TweetThumbnail())
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
- thumbbox.ShowThumbnailAsync(post).Wait();
+ await thumbbox.ShowThumbnailAsync(post);
+
+ Assert.Equal(1, thumbbox.scrollBar.Maximum);
+ Assert.True(thumbbox.scrollBar.Enabled);
- Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
- Assert.That(thumbbox.scrollBar.Enabled, Is.True);
+ Assert.Equal(2, thumbbox.pictureBox.Count);
+ Assert.NotNull(thumbbox.pictureBox[0].Image);
+ Assert.NotNull(thumbbox.pictureBox[1].Image);
- Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(2));
- Assert.That(thumbbox.pictureBox[0].ImageLocation, Is.EqualTo("dot.gif"));
- Assert.That(thumbbox.pictureBox[1].ImageLocation, Is.EqualTo("dot.gif"));
+ Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
+ var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
- var thumbinfo = thumbbox.pictureBox[0].Tag as ThumbnailInfo;
- Assert.That(thumbinfo, Is.Not.Null);
- Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://foo.example.com/abcd"));
- Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("dot.gif"));
+ Assert.Equal("http://foo.example.com/abcd", thumbinfo.MediaPageUrl);
+ Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailImageUrl);
- thumbinfo = thumbbox.pictureBox[1].Tag as ThumbnailInfo;
- Assert.That(thumbinfo, Is.Not.Null);
- Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://bar.example.com/efgh"));
- Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("dot.gif"));
+ Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
+ thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
- Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]), Is.EqualTo(""));
- Assert.That(thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]), Is.EqualTo("efgh"));
+ Assert.Equal("http://bar.example.com/efgh", thumbinfo.MediaPageUrl);
+ Assert.Equal("http://img.example.com/efgh.png", thumbinfo.ThumbnailImageUrl);
+
+ Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
+ Assert.Equal("efgh", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[1]));
}
}
- [Test]
- public void ThumbnailLoadingEventTest()
+ [Fact]
+ public async Task ThumbnailLoadingEventTest()
{
using (var thumbbox = new TweetThumbnail())
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
- bool eventCalled;
- thumbbox.ThumbnailLoading +=
- (s, e) => { eventCalled = true; };
-
var post = new PostClass
{
TextFromApi = "てすと",
- Media = new Dictionary<string, string>
+ Media = new List<MediaInfo>
{
},
};
- eventCalled = false;
- thumbbox.ShowThumbnailAsync(post).Wait();
+ await TestUtils.NotRaisesAsync<EventArgs>(
+ x => thumbbox.ThumbnailLoading += x,
+ x => thumbbox.ThumbnailLoading -= x,
+ () => thumbbox.ShowThumbnailAsync(post)
+ );
- Assert.That(eventCalled, Is.False);
+ SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
var post2 = new PostClass
{
TextFromApi = "てすと http://foo.example.com/abcd",
- Media = new Dictionary<string, string>
+ Media = new List<MediaInfo>
{
- {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
+ new MediaInfo("http://foo.example.com/abcd"),
},
};
- eventCalled = false;
- thumbbox.ShowThumbnailAsync(post2).Wait();
- Assert.That(eventCalled, Is.True);
+ await Assert.RaisesAsync<EventArgs>(
+ x => thumbbox.ThumbnailLoading += x,
+ x => thumbbox.ThumbnailLoading -= x,
+ () => thumbbox.ShowThumbnailAsync(post2)
+ );
}
}
- [Test]
- public void ScrollTest()
+ [Fact]
+ public async Task ScrollTest()
{
var post = new PostClass
{
TextFromApi = "てすと http://foo.example.com/abcd http://foo.example.com/efgh",
- Media = new Dictionary<string, string>
+ Media = new List<MediaInfo>
{
- {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
- {"http://foo.example.com/efgh", "http://foo.example.com/efgh"},
+ new MediaInfo("http://foo.example.com/abcd"),
+ new MediaInfo("http://foo.example.com/efgh"),
},
};
using (var thumbbox = new TweetThumbnail())
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
- thumbbox.ShowThumbnailAsync(post).Wait();
+ await thumbbox.ShowThumbnailAsync(post);
- Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
- Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(1));
+ Assert.Equal(0, thumbbox.scrollBar.Minimum);
+ Assert.Equal(1, thumbbox.scrollBar.Maximum);
thumbbox.scrollBar.Value = 0;
- thumbbox.ScrollUp();
- Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(1));
- Assert.That(thumbbox.pictureBox[0].Visible, Is.False);
- Assert.That(thumbbox.pictureBox[1].Visible, Is.True);
-
- thumbbox.ScrollUp();
- Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(1));
- Assert.That(thumbbox.pictureBox[0].Visible, Is.False);
- Assert.That(thumbbox.pictureBox[1].Visible, Is.True);
-
thumbbox.ScrollDown();
- Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(0));
- Assert.That(thumbbox.pictureBox[0].Visible, Is.True);
- Assert.That(thumbbox.pictureBox[1].Visible, Is.False);
+ Assert.Equal(1, thumbbox.scrollBar.Value);
+ Assert.False(thumbbox.pictureBox[0].Visible);
+ Assert.True(thumbbox.pictureBox[1].Visible);
thumbbox.ScrollDown();
- Assert.That(thumbbox.scrollBar.Value, Is.EqualTo(0));
- Assert.That(thumbbox.pictureBox[0].Visible, Is.True);
- Assert.That(thumbbox.pictureBox[1].Visible, Is.False);
+ Assert.Equal(1, thumbbox.scrollBar.Value);
+ Assert.False(thumbbox.pictureBox[0].Visible);
+ Assert.True(thumbbox.pictureBox[1].Visible);
+
+ thumbbox.ScrollUp();
+ Assert.Equal(0, thumbbox.scrollBar.Value);
+ Assert.True(thumbbox.pictureBox[0].Visible);
+ Assert.False(thumbbox.pictureBox[1].Visible);
+
+ thumbbox.ScrollUp();
+ Assert.Equal(0, thumbbox.scrollBar.Value);
+ Assert.True(thumbbox.pictureBox[0].Visible);
+ Assert.False(thumbbox.pictureBox[1].Visible);
}
}
}