• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

dev


Commit MetaInfo

Revision77c2a7c7bd89a1db0cde817a9dce3a47af715bac (tree)
Time2014-01-13 07:46:38
AuthorKimura Youichi <kim.upsilon@bucy...>
CommiterKimura Youichi

Log Message

サムネイル画像の読み込み処理をThumbnailInfoクラスに移動

Change Summary

Incremental Difference

--- a/OpenTween.Tests/OpenTween.Tests.csproj
+++ b/OpenTween.Tests/OpenTween.Tests.csproj
@@ -86,6 +86,9 @@
8686 <ItemGroup>
8787 <Content Include="dlls\NSubstitute.dll" />
8888 <Content Include="dlls\nunit.framework.dll" />
89+ <Content Include="Resources\dot.gif">
90+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
91+ </Content>
8992 <Content Include="Resources\re.gif">
9093 <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
9194 </Content>
--- a/OpenTween.Tests/TweetThumbnailTest.cs
+++ b/OpenTween.Tests/TweetThumbnailTest.cs
@@ -21,10 +21,13 @@
2121
2222 using System;
2323 using System.Collections.Generic;
24+using System.IO;
2425 using System.Linq;
2526 using System.Reflection;
2627 using System.Runtime.InteropServices;
28+using System.Text.RegularExpressions;
2729 using System.Threading;
30+using System.Threading.Tasks;
2831 using System.Windows.Forms;
2932 using NSubstitute;
3033 using OpenTween.Thumbnail;
@@ -36,27 +39,39 @@ namespace OpenTween
3639 {
3740 public class TweetThumbnailTest
3841 {
39- class TestThumbnailService : SimpleThumbnailService
42+ class TestThumbnailService : IThumbnailService
4043 {
41- protected string tooltip;
44+ private readonly Regex regex;
45+ private readonly string replaceUrl;
46+ private readonly string replaceTooltip;
4247
43- public TestThumbnailService(string pattern, string replacement, string tooltip)
44- : base(pattern, replacement)
48+ public TestThumbnailService(string pattern, string replaceUrl, string replaceTooltip)
4549 {
46- this.tooltip = tooltip;
50+ this.regex = new Regex(pattern);
51+ this.replaceUrl = replaceUrl;
52+ this.replaceTooltip = replaceTooltip;
4753 }
4854
4955 public override ThumbnailInfo GetThumbnailInfo(string url, PostClass post)
5056 {
51- var thumbinfo = base.GetThumbnailInfo(url, post);
57+ var match = this.regex.Match(url);
5258
53- if (thumbinfo != null && this.tooltip != null)
59+ if (!match.Success) return null;
60+
61+ return new MockThumbnailInfo
5462 {
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+ }
5868
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+ }
6075 }
6176 }
6277
@@ -168,9 +183,9 @@ namespace OpenTween
168183 Assert.False(thumbbox.scrollBar.Enabled);
169184
170185 Assert.Equal(1, thumbbox.pictureBox.Count);
171- Assert.Equal("dot.gif", thumbbox.pictureBox[0].ImageLocation);
186+ Assert.NotNull(thumbbox.pictureBox[0].Image);
172187
173- Assert.IsType<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
188+ Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
174189 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
175190
176191 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
@@ -202,16 +217,16 @@ namespace OpenTween
202217 Assert.True(thumbbox.scrollBar.Enabled);
203218
204219 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);
207222
208- Assert.IsType<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
223+ Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[0].Tag);
209224 var thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[0].Tag;
210225
211226 Assert.Equal("http://foo.example.com/abcd", thumbinfo.ImageUrl);
212227 Assert.Equal("dot.gif", thumbinfo.ThumbnailUrl);
213228
214- Assert.IsType<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
229+ Assert.IsAssignableFrom<ThumbnailInfo>(thumbbox.pictureBox[1].Tag);
215230 thumbinfo = (ThumbnailInfo)thumbbox.pictureBox[1].Tag;
216231
217232 Assert.Equal("http://bar.example.com/efgh", thumbinfo.ImageUrl);
--- a/OpenTween/Thumbnail/ThumbnailInfo.cs
+++ b/OpenTween/Thumbnail/ThumbnailInfo.cs
@@ -23,6 +23,7 @@ using System;
2323 using System.Collections.Generic;
2424 using System.Linq;
2525 using System.Text;
26+using System.Threading.Tasks;
2627
2728 namespace OpenTween.Thumbnail
2829 {
@@ -32,5 +33,24 @@ namespace OpenTween.Thumbnail
3233 public string ThumbnailUrl { get; set; }
3334 public string TooltipText { get; set; }
3435 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+ }
3555 }
3656 }
--- a/OpenTween/TweetThumbnail.cs
+++ b/OpenTween/TweetThumbnail.cs
@@ -85,13 +85,8 @@ namespace OpenTween
8585 picbox.ContextMenu = CreateContextMenu(thumb);
8686
8787 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 =>
9389 {
94- client.Dispose();
9590 if (t2.IsFaulted)
9691 {
9792 t2.Exception.Flatten().Handle(x => x is WebException || x is InvalidImageException);
@@ -99,7 +94,8 @@ namespace OpenTween
9994 return;
10095 }
10196 picbox.Image = t2.Result;
102- }, uiScheduler);
97+ },
98+ cancelToken, TaskContinuationOptions.AttachedToParent, uiScheduler);
10399
104100 var tooltipText = thumb.TooltipText;
105101 if (!string.IsNullOrEmpty(tooltipText))