using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
-using NSubstitute;
+using Moq;
+using OpenTween.Models;
using OpenTween.Thumbnail;
using OpenTween.Thumbnail.Services;
using Xunit;
this.replaceTooltip = replaceTooltip;
}
- public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
+ public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
{
- return Task.Run<ThumbnailInfo>(() =>
- {
- var match = this.regex.Match(url);
+ var match = this.regex.Match(url);
- if (!match.Success) return null;
+ if (!match.Success) return null;
- return new MockThumbnailInfo
- {
- ImageUrl = url,
- ThumbnailUrl = match.Result(this.replaceUrl),
- TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
- };
- });
+ if (url.StartsWith("http://slow.example.com/", StringComparison.Ordinal))
+ await Task.Delay(1000, token).ConfigureAwait(false);
+
+ return new MockThumbnailInfo
+ {
+ MediaPageUrl = url,
+ ThumbnailImageUrl = match.Result(this.replaceUrl),
+ TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
+ };
}
class MockThumbnailInfo : ThumbnailInfo
{
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),
});
}
public void MyCommonSetup()
{
- var mockAssembly = Substitute.For<_Assembly>();
- mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
+ var mockAssembly = new Mock<_Assembly>();
+ mockAssembly.Setup(m => m.GetName()).Returns(new AssemblyName("OpenTween"));
- MyCommon.EntryAssembly = mockAssembly;
+ MyCommon.EntryAssembly = mockAssembly.Object;
}
[Fact]
{
var post = new PostClass
{
- TextFromApi = "てすと http://foo.example.com/abcd",
+ TextFromApi = "てすと http://slow.example.com/abcd",
Media = new List<MediaInfo>
{
- new MediaInfo("http://foo.example.com/abcd"),
+ new MediaInfo("http://slow.example.com/abcd"),
},
};
tokenSource.Cancel();
- await TestUtils.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
+ await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
Assert.True(task.IsCanceled);
}
}
Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
- Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
- Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailUrl);
+ Assert.Equal("http://foo.example.com/abcd", thumbinfo.MediaPageUrl);
+ Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailImageUrl);
Assert.Equal("", thumbbox.toolTip.GetToolTip(thumbbox.pictureBox[0]));
}
Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
- Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
- Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailUrl);
+ Assert.Equal("http://foo.example.com/abcd", thumbinfo.MediaPageUrl);
+ Assert.Equal("http://img.example.com/abcd.png", thumbinfo.ThumbnailImageUrl);
Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
- Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
- Assert.Equal("http://img.example.com/efgh.png", thumbinfo.ThumbnailUrl);
+ 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]));
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
- bool eventCalled;
- thumbbox.ThumbnailLoading +=
- (s, e) => { eventCalled = true; };
-
var post = new PostClass
{
TextFromApi = "てすと",
{
},
};
- eventCalled = false;
- await thumbbox.ShowThumbnailAsync(post);
-
- Assert.False(eventCalled);
+ await TestUtils.NotRaisesAsync<EventArgs>(
+ x => thumbbox.ThumbnailLoading += x,
+ x => thumbbox.ThumbnailLoading -= x,
+ () => thumbbox.ShowThumbnailAsync(post)
+ );
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
new MediaInfo("http://foo.example.com/abcd"),
},
};
- eventCalled = false;
- await thumbbox.ShowThumbnailAsync(post2);
- Assert.True(eventCalled);
+ await Assert.RaisesAsync<EventArgs>(
+ x => thumbbox.ThumbnailLoading += x,
+ x => thumbbox.ThumbnailLoading -= x,
+ () => thumbbox.ShowThumbnailAsync(post2)
+ );
}
}