OSDN Git Service

イベントのテストにAssert.Raisesを使用する
[opentween/open-tween.git] / OpenTween.Tests / TweetThumbnailTest.cs
index 7b44f37..3fd0dcc 100644 (file)
@@ -23,13 +23,15 @@ using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+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 NSubstitute;
+using Moq;
+using OpenTween.Models;
 using OpenTween.Thumbnail;
 using OpenTween.Thumbnail.Services;
 using Xunit;
@@ -52,25 +54,28 @@ namespace OpenTween
                 this.replaceTooltip = replaceTooltip;
             }
 
-            public override ThumbnailInfo GetThumbnailInfo(string url, PostClass post)
+            public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
             {
                 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);
+
                 return new MockThumbnailInfo
                 {
-                    ImageUrl = url,
-                    ThumbnailUrl = match.Result(this.replaceUrl),
+                    MediaPageUrl = url,
+                    ThumbnailImageUrl = match.Result(this.replaceUrl),
                     TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
                 };
             }
 
             class MockThumbnailInfo : ThumbnailInfo
             {
-                public override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
+                public override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
                 {
-                    return Task.Run(() => MemoryImage.CopyFromBytes(File.ReadAllBytes("Resources/" + this.ThumbnailUrl)), token);
+                    return Task.FromResult(TestUtils.CreateDummyImage());
                 }
             }
         }
@@ -86,18 +91,18 @@ namespace OpenTween
             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),
             });
         }
 
         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;
         }
 
         [Fact]
@@ -123,10 +128,10 @@ namespace OpenTween
         {
             var post = new PostClass
             {
-                TextFromApi = "てすと http://foo.example.com/abcd",
-                Media = new Dictionary<string, string>
+                TextFromApi = "てすと http://slow.example.com/abcd",
+                Media = new List<MediaInfo>
                 {
-                    {"http://foo.example.com/abcd", "http://foo.example.com/abcd"},
+                    new MediaInfo("http://slow.example.com/abcd"),
                 },
             };
 
@@ -138,7 +143,7 @@ namespace OpenTween
 
                 tokenSource.Cancel();
 
-                await TestUtils.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
+                await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
                 Assert.True(task.IsCanceled);
             }
         }
@@ -180,9 +185,9 @@ namespace OpenTween
             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"),
                 },
             };
 
@@ -200,8 +205,8 @@ namespace OpenTween
                 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("dot.gif", 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]));
             }
@@ -213,10 +218,10 @@ namespace OpenTween
             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"),
                 },
             };
 
@@ -235,14 +240,14 @@ namespace OpenTween
                 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("dot.gif", 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("dot.gif", 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]));
@@ -256,36 +261,35 @@ namespace OpenTween
             {
                 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;
-                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());
 
                 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;
-                await thumbbox.ShowThumbnailAsync(post2);
 
-                Assert.True(eventCalled);
+                await Assert.RaisesAsync<EventArgs>(
+                    x => thumbbox.ThumbnailLoading += x,
+                    x => thumbbox.ThumbnailLoading -= x,
+                    () => thumbbox.ShowThumbnailAsync(post2)
+                );
             }
         }
 
@@ -295,10 +299,10 @@ namespace OpenTween
             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"),
                 },
             };
 
@@ -312,22 +316,22 @@ namespace OpenTween
 
                 thumbbox.scrollBar.Value = 0;
 
-                thumbbox.ScrollUp();
+                thumbbox.ScrollDown();
                 Assert.Equal(1, thumbbox.scrollBar.Value);
                 Assert.False(thumbbox.pictureBox[0].Visible);
                 Assert.True(thumbbox.pictureBox[1].Visible);
 
-                thumbbox.ScrollUp();
+                thumbbox.ScrollDown();
                 Assert.Equal(1, thumbbox.scrollBar.Value);
                 Assert.False(thumbbox.pictureBox[0].Visible);
                 Assert.True(thumbbox.pictureBox[1].Visible);
 
-                thumbbox.ScrollDown();
+                thumbbox.ScrollUp();
                 Assert.Equal(0, thumbbox.scrollBar.Value);
                 Assert.True(thumbbox.pictureBox[0].Visible);
                 Assert.False(thumbbox.pictureBox[1].Visible);
 
-                thumbbox.ScrollDown();
+                thumbbox.ScrollUp();
                 Assert.Equal(0, thumbbox.scrollBar.Value);
                 Assert.True(thumbbox.pictureBox[0].Visible);
                 Assert.False(thumbbox.pictureBox[1].Visible);