OSDN Git Service

サムネイル画像の読み込み処理をThumbnailInfoクラスに移動
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 12 Jan 2014 05:30:29 +0000 (14:30 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sun, 12 Jan 2014 22:46:38 +0000 (07:46 +0900)
OpenTween.Tests/OpenTween.Tests.csproj
OpenTween.Tests/TweetThumbnailTest.cs
OpenTween/Thumbnail/ThumbnailInfo.cs
OpenTween/TweetThumbnail.cs

index 1bbfb88..3df4f4e 100644 (file)
@@ -86,6 +86,9 @@
   <ItemGroup>
     <Content Include="dlls\NSubstitute.dll" />
     <Content Include="dlls\nunit.framework.dll" />
+    <Content Include="Resources\dot.gif">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
     <Content Include="Resources\re.gif">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
index 78c00cc..663f09c 100644 (file)
 
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 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 OpenTween.Thumbnail;
@@ -36,27 +39,39 @@ namespace OpenTween
 {
     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)
             {
-                var thumbinfo = base.GetThumbnailInfo(url, post);
+                var match = this.regex.Match(url);
 
-                if (thumbinfo != null && this.tooltip != null)
+                if (!match.Success) return null;
+
+                return new MockThumbnailInfo
                 {
-                    var match = this.regex.Match(url);
-                    thumbinfo.TooltipText = match.Result(this.tooltip);
-                }
+                    ImageUrl = url,
+                    ThumbnailUrl = match.Result(this.replaceUrl),
+                    TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null,
+                };
+            }
 
-                return thumbinfo;
+            class MockThumbnailInfo : ThumbnailInfo
+            {
+                protected override Task<MemoryImage> LoadThumbnailImageAsync()
+                {
+                    return Task.Factory.StartNew(() => MemoryImage.CopyFromBytes(File.ReadAllBytes("Resources/" + this.ThumbnailUrl)));
+                }
             }
         }
 
@@ -168,9 +183,9 @@ namespace OpenTween
                 Assert.False(thumbbox.scrollBar.Enabled);
 
                 Assert.Equal(1, thumbbox.pictureBox.Count);
-                Assert.Equal("dot.gif", thumbbox.pictureBox[0].ImageLocation);
+                Assert.NotNull(thumbbox.pictureBox[0].Image);
 
-                Assert.IsType<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
+                Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
                 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
 
                 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
@@ -202,16 +217,16 @@ namespace OpenTween
                 Assert.True(thumbbox.scrollBar.Enabled);
 
                 Assert.Equal(2, thumbbox.pictureBox.Count);
-                Assert.Equal("dot.gif", thumbbox.pictureBox[0].ImageLocation);
-                Assert.Equal("dot.gif", thumbbox.pictureBox[1].ImageLocation);
+                Assert.NotNull(thumbbox.pictureBox[0].Image);
+                Assert.NotNull(thumbbox.pictureBox[1].Image);
 
-                Assert.IsType<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
+                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.IsType<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
+                Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
                 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
 
                 Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
index c1f9300..1edf7bd 100644 (file)
@@ -23,6 +23,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Threading.Tasks;
 
 namespace OpenTween.Thumbnail
 {
@@ -32,5 +33,24 @@ namespace OpenTween.Thumbnail
         public string ThumbnailUrl { get; set; }
         public string TooltipText { get; set; }
         public string FullSizeImageUrl { get; set; }
+
+        public readonly Lazy<Task<MemoryImage>> ThumbnailImageTask;
+
+        public ThumbnailInfo()
+        {
+            this.ThumbnailImageTask = new Lazy<Task<MemoryImage>>(this.LoadThumbnailImageAsync);
+        }
+
+        protected virtual Task<MemoryImage> LoadThumbnailImageAsync()
+        {
+            var client = new OTWebClient();
+
+            var task = client.DownloadDataAsync(new Uri(this.ThumbnailUrl))
+                .ContinueWith(t => MemoryImage.CopyFromBytes(t.Result));
+
+            task.ContinueWith(_ => client.Dispose());
+
+            return task;
+        }
     }
 }
index fbee8cb..832a46e 100644 (file)
@@ -85,13 +85,8 @@ namespace OpenTween
                             picbox.ContextMenu = CreateContextMenu(thumb);
 
                             picbox.ShowInitialImage();
-
-                            var client = new OTWebClient();
-                            client.DownloadDataAsync(new Uri(thumb.ThumbnailUrl))
-                                .ContinueWith(t2 => MemoryImage.CopyFromBytes(t2.Result))
-                                .ContinueWith(t2 =>
+                            thumb.ThumbnailImageTask.Value.ContinueWith(t2 =>
                                 {
-                                    client.Dispose();
                                     if (t2.IsFaulted)
                                     {
                                         t2.Exception.Flatten().Handle(x => x is WebException || x is InvalidImageException);
@@ -99,7 +94,8 @@ namespace OpenTween
                                         return;
                                     }
                                     picbox.Image = t2.Result;
-                                }, uiScheduler);
+                                },
+                                cancelToken, TaskContinuationOptions.AttachedToParent, uiScheduler);
 
                             var tooltipText = thumb.TooltipText;
                             if (!string.IsNullOrEmpty(tooltipText))