dev
Revision | 77c2a7c7bd89a1db0cde817a9dce3a47af715bac (tree) |
---|---|
Time | 2014-01-13 07:46:38 |
Author | Kimura Youichi <kim.upsilon@bucy...> |
Commiter | Kimura Youichi |
サムネイル画像の読み込み処理をThumbnailInfoクラスに移動
@@ -86,6 +86,9 @@ | ||
86 | 86 | <ItemGroup> |
87 | 87 | <Content Include="dlls\NSubstitute.dll" /> |
88 | 88 | <Content Include="dlls\nunit.framework.dll" /> |
89 | + <Content Include="Resources\dot.gif"> | |
90 | + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | |
91 | + </Content> | |
89 | 92 | <Content Include="Resources\re.gif"> |
90 | 93 | <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |
91 | 94 | </Content> |
@@ -21,10 +21,13 @@ | ||
21 | 21 | |
22 | 22 | using System; |
23 | 23 | using System.Collections.Generic; |
24 | +using System.IO; | |
24 | 25 | using System.Linq; |
25 | 26 | using System.Reflection; |
26 | 27 | using System.Runtime.InteropServices; |
28 | +using System.Text.RegularExpressions; | |
27 | 29 | using System.Threading; |
30 | +using System.Threading.Tasks; | |
28 | 31 | using System.Windows.Forms; |
29 | 32 | using NSubstitute; |
30 | 33 | using OpenTween.Thumbnail; |
@@ -36,27 +39,39 @@ namespace OpenTween | ||
36 | 39 | { |
37 | 40 | public class TweetThumbnailTest |
38 | 41 | { |
39 | - class TestThumbnailService : SimpleThumbnailService | |
42 | + class TestThumbnailService : IThumbnailService | |
40 | 43 | { |
41 | - protected string tooltip; | |
44 | + private readonly Regex regex; | |
45 | + private readonly string replaceUrl; | |
46 | + private readonly string replaceTooltip; | |
42 | 47 | |
43 | - public TestThumbnailService(string pattern, string replacement, string tooltip) | |
44 | - : base(pattern, replacement) | |
48 | + public TestThumbnailService(string pattern, string replaceUrl, string replaceTooltip) | |
45 | 49 | { |
46 | - this.tooltip = tooltip; | |
50 | + this.regex = new Regex(pattern); | |
51 | + this.replaceUrl = replaceUrl; | |
52 | + this.replaceTooltip = replaceTooltip; | |
47 | 53 | } |
48 | 54 | |
49 | 55 | public override ThumbnailInfo GetThumbnailInfo(string url, PostClass post) |
50 | 56 | { |
51 | - var thumbinfo = base.GetThumbnailInfo(url, post); | |
57 | + var match = this.regex.Match(url); | |
52 | 58 | |
53 | - if (thumbinfo != null && this.tooltip != null) | |
59 | + if (!match.Success) return null; | |
60 | + | |
61 | + return new MockThumbnailInfo | |
54 | 62 | { |
55 | - var match = this.regex.Match(url); | |
56 | - thumbinfo.TooltipText = match.Result(this.tooltip); | |
57 | - } | |
63 | + ImageUrl = url, | |
64 | + ThumbnailUrl = match.Result(this.replaceUrl), | |
65 | + TooltipText = this.replaceTooltip != null ? match.Result(this.replaceTooltip) : null, | |
66 | + }; | |
67 | + } | |
58 | 68 | |
59 | - return thumbinfo; | |
69 | + class MockThumbnailInfo : ThumbnailInfo | |
70 | + { | |
71 | + protected override Task<MemoryImage> LoadThumbnailImageAsync() | |
72 | + { | |
73 | + return Task.Factory.StartNew(() => MemoryImage.CopyFromBytes(File.ReadAllBytes("Resources/" + this.ThumbnailUrl))); | |
74 | + } | |
60 | 75 | } |
61 | 76 | } |
62 | 77 |
@@ -168,9 +183,9 @@ namespace OpenTween | ||
168 | 183 | Assert.False(thumbbox.scrollBar.Enabled); |
169 | 184 | |
170 | 185 | Assert.Equal(1, thumbbox.pictureBox.Count); |
171 | - Assert.Equal("dot.gif", thumbbox.pictureBox[0].ImageLocation); | |
186 | + Assert.NotNull(thumbbox.pictureBox[0].Image); | |
172 | 187 | |
173 | - Assert.IsType<ThumbnailInfo>(thumbbox.pictureBox[0].Tag); | |
188 | + Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag); | |
174 | 189 | var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag; |
175 | 190 | |
176 | 191 | Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl); |
@@ -202,16 +217,16 @@ namespace OpenTween | ||
202 | 217 | Assert.True(thumbbox.scrollBar.Enabled); |
203 | 218 | |
204 | 219 | Assert.Equal(2, thumbbox.pictureBox.Count); |
205 | - Assert.Equal("dot.gif", thumbbox.pictureBox[0].ImageLocation); | |
206 | - Assert.Equal("dot.gif", thumbbox.pictureBox[1].ImageLocation); | |
220 | + Assert.NotNull(thumbbox.pictureBox[0].Image); | |
221 | + Assert.NotNull(thumbbox.pictureBox[1].Image); | |
207 | 222 | |
208 | - Assert.IsType<ThumbnailInfo>(thumbbox.pictureBox[0].Tag); | |
223 | + Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag); | |
209 | 224 | var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag; |
210 | 225 | |
211 | 226 | Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl); |
212 | 227 | Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl); |
213 | 228 | |
214 | - Assert.IsType<ThumbnailInfo>(thumbbox.pictureBox[1].Tag); | |
229 | + Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag); | |
215 | 230 | thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag; |
216 | 231 | |
217 | 232 | Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl); |
@@ -23,6 +23,7 @@ using System; | ||
23 | 23 | using System.Collections.Generic; |
24 | 24 | using System.Linq; |
25 | 25 | using System.Text; |
26 | +using System.Threading.Tasks; | |
26 | 27 | |
27 | 28 | namespace OpenTween.Thumbnail |
28 | 29 | { |
@@ -32,5 +33,24 @@ namespace OpenTween.Thumbnail | ||
32 | 33 | public string ThumbnailUrl { get; set; } |
33 | 34 | public string TooltipText { get; set; } |
34 | 35 | public string FullSizeImageUrl { get; set; } |
36 | + | |
37 | + public readonly Lazy<Task<MemoryImage>> ThumbnailImageTask; | |
38 | + | |
39 | + public ThumbnailInfo() | |
40 | + { | |
41 | + this.ThumbnailImageTask = new Lazy<Task<MemoryImage>>(this.LoadThumbnailImageAsync); | |
42 | + } | |
43 | + | |
44 | + protected virtual Task<MemoryImage> LoadThumbnailImageAsync() | |
45 | + { | |
46 | + var client = new OTWebClient(); | |
47 | + | |
48 | + var task = client.DownloadDataAsync(new Uri(this.ThumbnailUrl)) | |
49 | + .ContinueWith(t => MemoryImage.CopyFromBytes(t.Result)); | |
50 | + | |
51 | + task.ContinueWith(_ => client.Dispose()); | |
52 | + | |
53 | + return task; | |
54 | + } | |
35 | 55 | } |
36 | 56 | } |
@@ -85,13 +85,8 @@ namespace OpenTween | ||
85 | 85 | picbox.ContextMenu = CreateContextMenu(thumb); |
86 | 86 | |
87 | 87 | picbox.ShowInitialImage(); |
88 | - | |
89 | - var client = new OTWebClient(); | |
90 | - client.DownloadDataAsync(new Uri(thumb.ThumbnailUrl)) | |
91 | - .ContinueWith(t2 => MemoryImage.CopyFromBytes(t2.Result)) | |
92 | - .ContinueWith(t2 => | |
88 | + thumb.ThumbnailImageTask.Value.ContinueWith(t2 => | |
93 | 89 | { |
94 | - client.Dispose(); | |
95 | 90 | if (t2.IsFaulted) |
96 | 91 | { |
97 | 92 | t2.Exception.Flatten().Handle(x => x is WebException || x is InvalidImageException); |
@@ -99,7 +94,8 @@ namespace OpenTween | ||
99 | 94 | return; |
100 | 95 | } |
101 | 96 | picbox.Image = t2.Result; |
102 | - }, uiScheduler); | |
97 | + }, | |
98 | + cancelToken, TaskContinuationOptions.AttachedToParent, uiScheduler); | |
103 | 99 | |
104 | 100 | var tooltipText = thumb.TooltipText; |
105 | 101 | if (!string.IsNullOrEmpty(tooltipText)) |