• 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

開発に使用するリポジトリ


Commit MetaInfo

Revisióncdb63e40b6a0a85cfbe9e9044b19572519509a15 (tree)
Tiempo2012-12-22 01:01:52
AutorKimura Youichi <kim.upsilon@bucy...>
CommiterKimura Youichi

Log Message

OpenTween.Thumbnail.Services.* のテストをいくつか追加

Cambiar Resumen

Diferencia incremental

--- a/OpenTween.Tests/OpenTween.Tests.csproj
+++ b/OpenTween.Tests/OpenTween.Tests.csproj
@@ -8,7 +8,7 @@
88 <ProjectGuid>{18A32642-A8F3-425B-978D-0C6F630EDDE8}</ProjectGuid>
99 <OutputType>Library</OutputType>
1010 <AppDesignerFolder>Properties</AppDesignerFolder>
11- <RootNamespace>OpenTween.Tests</RootNamespace>
11+ <RootNamespace>OpenTween</RootNamespace>
1212 <AssemblyName>OpenTween.Tests</AssemblyName>
1313 <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1414 <FileAlignment>512</FileAlignment>
@@ -58,6 +58,10 @@
5858 <Compile Include="Properties\AssemblyInfo.cs" />
5959 <Compile Include="TabsDialogTest.cs" />
6060 <Compile Include="TestUtils.cs" />
61+ <Compile Include="Thumbnail\Services\ImgAzyobuziNetTest.cs" />
62+ <Compile Include="Thumbnail\Services\MetaThumbnailServiceTest.cs" />
63+ <Compile Include="Thumbnail\Services\SimpleThumbnailServiceTest.cs" />
64+ <Compile Include="Thumbnail\Services\TinamiTest.cs" />
6165 </ItemGroup>
6266 <ItemGroup>
6367 <ProjectReference Include="..\OpenTween\OpenTween.csproj">
--- /dev/null
+++ b/OpenTween.Tests/Thumbnail/Services/ImgAzyobuziNetTest.cs
@@ -0,0 +1,62 @@
1+// OpenTween - Client of Twitter
2+// Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3+// All rights reserved.
4+//
5+// This file is part of OpenTween.
6+//
7+// This program is free software; you can redistribute it and/or modify it
8+// under the terms of the GNU General Public License as published by the Free
9+// Software Foundation; either version 3 of the License, or (at your option)
10+// any later version.
11+//
12+// This program is distributed in the hope that it will be useful, but
13+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+// for more details.
16+//
17+// You should have received a copy of the GNU General Public License along
18+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20+// Boston, MA 02110-1301, USA.
21+
22+using System;
23+using System.Collections.Generic;
24+using System.Linq;
25+using System.Text;
26+using NUnit.Framework;
27+
28+namespace OpenTween.Thumbnail.Services
29+{
30+ [TestFixture]
31+ class ImgAzyobuziNetTest
32+ {
33+ class TestImgAzyobuziNet : ImgAzyobuziNet
34+ {
35+ protected override byte[] FetchRegex(string apiBase)
36+ {
37+ return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
38+ }
39+ }
40+
41+ [Test]
42+ public void MatchTest()
43+ {
44+ var service = new TestImgAzyobuziNet();
45+ var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
46+
47+ Assert.That(thumbinfo, Is.Not.Null);
48+ Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://example.com/abcd"));
49+ Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("http://img.azyobuzi.net/api/redirect?uri=http%3A%2F%2Fexample.com%2Fabcd"));
50+ Assert.That(thumbinfo.TooltipText, Is.Null);
51+ }
52+
53+ [Test]
54+ public void NotMatchTest()
55+ {
56+ var service = new TestImgAzyobuziNet();
57+ var thumbinfo = service.GetThumbnailInfo("http://hogehoge.com/abcd", null);
58+
59+ Assert.That(thumbinfo, Is.Null);
60+ }
61+ }
62+}
--- /dev/null
+++ b/OpenTween.Tests/Thumbnail/Services/MetaThumbnailServiceTest.cs
@@ -0,0 +1,139 @@
1+// OpenTween - Client of Twitter
2+// Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3+// All rights reserved.
4+//
5+// This file is part of OpenTween.
6+//
7+// This program is free software; you can redistribute it and/or modify it
8+// under the terms of the GNU General Public License as published by the Free
9+// Software Foundation; either version 3 of the License, or (at your option)
10+// any later version.
11+//
12+// This program is distributed in the hope that it will be useful, but
13+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+// for more details.
16+//
17+// You should have received a copy of the GNU General Public License along
18+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20+// Boston, MA 02110-1301, USA.
21+
22+using System;
23+using System.Collections.Generic;
24+using System.Linq;
25+using System.Text;
26+using NUnit.Framework;
27+
28+namespace OpenTween.Thumbnail.Services
29+{
30+ [TestFixture]
31+ class MetaThumbnailServiceTest
32+ {
33+ class TestMetaThumbnailService : MetaThumbnailService
34+ {
35+ public string FakeHtml { get; set; }
36+
37+ public TestMetaThumbnailService(string url)
38+ : base(url)
39+ {
40+ }
41+
42+ protected override string FetchImageUrl(string url)
43+ {
44+ return this.FakeHtml;
45+ }
46+ }
47+
48+ [Test]
49+ [Description("Open Graph protocol")]
50+ public void OGPMetaTest()
51+ {
52+ var service = new TestMetaThumbnailService(@"http://example.com/.+");
53+
54+ service.FakeHtml = @"
55+<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML+RDFa 1.0//EN' 'http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd'>
56+<html xmlns='http://www.w3.org/1999/xhtml'>
57+<head>
58+ <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
59+ <meta property='og:image' content='http://img.example.com/abcd'/>
60+ <title>hogehoge</title>
61+</head>
62+<body>
63+ <p>hogehoge</p>
64+</body>
65+</html>
66+";
67+ var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
68+
69+ Assert.That(thumbinfo, Is.Not.Null);
70+ Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://example.com/abcd"));
71+ Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("http://img.example.com/abcd"));
72+ Assert.That(thumbinfo.TooltipText, Is.Null);
73+ }
74+
75+ [Test]
76+ [Description("Twitter Cards")]
77+ public void TwitterMetaTest()
78+ {
79+ var service = new TestMetaThumbnailService(@"http://example.com/.+");
80+
81+ service.FakeHtml = @"
82+<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
83+
84+<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
85+<meta name='twitter:image' content='http://img.example.com/abcd'>
86+<title>hogehoge</title>
87+
88+<p>hogehoge
89+";
90+ var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
91+
92+ Assert.That(thumbinfo, Is.Not.Null);
93+ Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://example.com/abcd"));
94+ Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("http://img.example.com/abcd"));
95+ Assert.That(thumbinfo.TooltipText, Is.Null);
96+ }
97+
98+ [Test]
99+ [Description("Twitpicとか")]
100+ public void InvalidMetaTest()
101+ {
102+ var service = new TestMetaThumbnailService(@"http://example.com/.+");
103+
104+ service.FakeHtml = @"
105+<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
106+
107+<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
108+<meta name='twitter:image' value='http://img.example.com/abcd'>
109+<title>hogehoge</title>
110+
111+<p>hogehoge
112+";
113+ var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
114+
115+ Assert.That(thumbinfo, Is.Not.Null);
116+ Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://example.com/abcd"));
117+ Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("http://img.example.com/abcd"));
118+ Assert.That(thumbinfo.TooltipText, Is.Null);
119+ }
120+
121+ [Test]
122+ public void NoMetaTest()
123+ {
124+ var service = new TestMetaThumbnailService(@"http://example.com/.+");
125+
126+ service.FakeHtml = @"
127+<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
128+
129+<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
130+<title>hogehoge</title>
131+
132+<p>hogehoge
133+";
134+ var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
135+
136+ Assert.That(thumbinfo, Is.Null);
137+ }
138+ }
139+}
--- /dev/null
+++ b/OpenTween.Tests/Thumbnail/Services/SimpleThumbnailServiceTest.cs
@@ -0,0 +1,56 @@
1+// OpenTween - Client of Twitter
2+// Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3+// All rights reserved.
4+//
5+// This file is part of OpenTween.
6+//
7+// This program is free software; you can redistribute it and/or modify it
8+// under the terms of the GNU General Public License as published by the Free
9+// Software Foundation; either version 3 of the License, or (at your option)
10+// any later version.
11+//
12+// This program is distributed in the hope that it will be useful, but
13+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+// for more details.
16+//
17+// You should have received a copy of the GNU General Public License along
18+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20+// Boston, MA 02110-1301, USA.
21+
22+using System;
23+using System.Collections.Generic;
24+using System.Linq;
25+using System.Text;
26+using NUnit.Framework;
27+
28+namespace OpenTween.Thumbnail.Services
29+{
30+ [TestFixture]
31+ class SimpleThumbnailServiceTest
32+ {
33+ [Test]
34+ public void RegexMatchTest()
35+ {
36+ var service = new SimpleThumbnailService(@"http://example.com/(.+)", @"http://img.example.com/$1");
37+
38+ var thumbinfo = service.GetThumbnailInfo("http://example.com/abcd", null);
39+
40+ Assert.That(thumbinfo, Is.Not.Null);
41+ Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://example.com/abcd"));
42+ Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("http://img.example.com/abcd"));
43+ Assert.That(thumbinfo.TooltipText, Is.Null);
44+ }
45+
46+ [Test]
47+ public void RegexNotMatchTest()
48+ {
49+ var service = new SimpleThumbnailService(@"http://example.com/(.+)", @"http://img.example.com/\1");
50+
51+ var thumbinfo = service.GetThumbnailInfo("http://hogehoge.com/abcd", null);
52+
53+ Assert.That(thumbinfo, Is.Null);
54+ }
55+ }
56+}
--- /dev/null
+++ b/OpenTween.Tests/Thumbnail/Services/TinamiTest.cs
@@ -0,0 +1,59 @@
1+using System;
2+using System.Collections.Generic;
3+using System.Linq;
4+using System.Text;
5+using NUnit.Framework;
6+using System.Xml.Linq;
7+
8+namespace OpenTween.Thumbnail.Services
9+{
10+ [TestFixture]
11+ class TinamiTest
12+ {
13+ class TestTinami : Tinami
14+ {
15+ public string FakeXml { get; set; }
16+
17+ public TestTinami(string pattern, string replacement)
18+ : base(pattern, replacement)
19+ {
20+ }
21+
22+ protected override XDocument FetchContentInfoApi(string url)
23+ {
24+ Assert.That(url, Is.StringMatching(@"http://api\.tinami\.com/content/info\?cont_id=.+&api_key=.+"));
25+
26+ return XDocument.Parse(this.FakeXml);
27+ }
28+ }
29+
30+ [Test]
31+ public void ApiTest()
32+ {
33+ var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$",
34+ "http://api.tinami.com/content/info?cont_id=${ContentId}&api_key=" + ApplicationSettings.TINAMIApiKey);
35+
36+ service.FakeXml = @"<?xml version='1.0' encoding='utf-8' ?>
37+<rsp stat='ok'>
38+ <content type='illust' issupport='1' iscollection='0'>
39+ <title>ほげほげ</title>
40+ <description>説明</description>
41+ <thumbnails>
42+ <thumbnail_150x150 url='http://img.tinami.com/hogehoge_150.gif' width='112' height='120'/>
43+ </thumbnails>
44+ <image>
45+ <url>http://img.tinami.com/hogehoge_full.gif</url>
46+ <width>640</width>
47+ <height>480</height>
48+ </image>
49+ </content>
50+</rsp>";
51+ var thumbinfo = service.GetThumbnailInfo("http://www.tinami.com/view/12345", null);
52+
53+ Assert.That(thumbinfo, Is.Not.Null);
54+ Assert.That(thumbinfo.ImageUrl, Is.EqualTo("http://www.tinami.com/view/12345"));
55+ Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("http://img.tinami.com/hogehoge_150.gif"));
56+ Assert.That(thumbinfo.TooltipText, Is.EqualTo("説明"));
57+ }
58+ }
59+}
--- a/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs
+++ b/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs
@@ -44,10 +44,16 @@ namespace OpenTween.Thumbnail.Services
4444
4545 private object LockObj = new object();
4646
47- public ImgAzyobuziNet()
47+ public ImgAzyobuziNet(bool autoupdate = false)
4848 {
4949 this.LoadRegex();
5050
51+ if (autoupdate)
52+ this.StartAutoUpdate();
53+ }
54+
55+ public void StartAutoUpdate()
56+ {
5157 Task.Factory.StartNew(() =>
5258 {
5359 for (;;)
@@ -80,8 +86,7 @@ namespace OpenTween.Thumbnail.Services
8086 {
8187 try
8288 {
83- using (var client = new OTWebClient() { Timeout = 1000 })
84- using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(client.DownloadData(apiBase + "regex.json"), XmlDictionaryReaderQuotas.Max))
89+ using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(this.FetchRegex(apiBase), XmlDictionaryReaderQuotas.Max))
8590 {
8691 var xElm = XElement.Load(jsonReader);
8792
@@ -102,6 +107,14 @@ namespace OpenTween.Thumbnail.Services
102107 return false;
103108 }
104109
110+ protected virtual byte[] FetchRegex(string apiBase)
111+ {
112+ using (var client = new OTWebClient() { Timeout = 1000 })
113+ {
114+ return client.DownloadData(apiBase + "regex.json");
115+ }
116+ }
117+
105118 public override ThumbnailInfo GetThumbnailInfo(string url, PostClass post)
106119 {
107120 lock (this.LockObj)
--- a/OpenTween/Thumbnail/Services/MetaThumbnailService.cs
+++ b/OpenTween/Thumbnail/Services/MetaThumbnailService.cs
@@ -36,7 +36,12 @@ namespace OpenTween.Thumbnail.Services
3636 protected static Regex metaPattern = new Regex("<meta property=[\"'](?<property>.+?)[\"'] content=[\"'](?<content>.+?)[\"']");
3737 protected static string[] propertyNames = { "twitter:image", "og:image" };
3838
39- public MetaThumbnailService(string pattern, string replacement = "${0}")
39+ public MetaThumbnailService(string url)
40+ : base(url, "${0}")
41+ {
42+ }
43+
44+ public MetaThumbnailService(string pattern, string replacement)
4045 : base(pattern, replacement)
4146 {
4247 }
@@ -46,7 +51,9 @@ namespace OpenTween.Thumbnail.Services
4651 var pageUrl = this.ReplaceUrl(url);
4752 if (pageUrl == null) return null;
4853
49- var thumbnailUrl = this.FetchThumbnailUrl(pageUrl);
54+ var content = this.FetchImageUrl(pageUrl);
55+
56+ var thumbnailUrl = this.GetThumbnailUrl(content);
5057 if (string.IsNullOrEmpty(thumbnailUrl)) return null;
5158
5259 return new ThumbnailInfo()
@@ -57,23 +64,27 @@ namespace OpenTween.Thumbnail.Services
5764 };
5865 }
5966
60- protected virtual string FetchThumbnailUrl(string url)
67+ protected virtual string GetThumbnailUrl(string html)
6168 {
62- using (var client = new OTWebClient())
63- {
64- var content = client.DownloadString(url);
65- var matches = MetaThumbnailService.metaPattern.Matches(content);
69+ var matches = MetaThumbnailService.metaPattern.Matches(html);
6670
67- foreach (Match match in matches)
71+ foreach (Match match in matches)
72+ {
73+ var propertyName = match.Groups["property"].Value;
74+ if (MetaThumbnailService.propertyNames.Contains(propertyName))
6875 {
69- var propertyName = match.Groups["property"].Value;
70- if (MetaThumbnailService.propertyNames.Contains(propertyName))
71- {
72- return match.Groups["content"].Value;
73- }
76+ return match.Groups["content"].Value;
7477 }
78+ }
79+
80+ return null;
81+ }
7582
76- return null;
83+ protected virtual string FetchImageUrl(string url)
84+ {
85+ using (var client = new OTWebClient())
86+ {
87+ return client.DownloadString(url);
7788 }
7889 }
7990 }
--- a/OpenTween/Thumbnail/Services/Pixiv.cs
+++ b/OpenTween/Thumbnail/Services/Pixiv.cs
@@ -34,9 +34,9 @@ namespace OpenTween.Thumbnail.Services
3434 {
3535 }
3636
37- protected override string FetchThumbnailUrl(string url)
37+ protected override string FetchImageUrl(string url)
3838 {
39- var thumbnailUrl = base.FetchThumbnailUrl(url);
39+ var thumbnailUrl = base.FetchImageUrl(url);
4040
4141 // og:image のサムネイルURLにそのままアクセスすると403が返ってくるので回避
4242 return Regex.Replace(thumbnailUrl, @"_s(?=\..{3}$)", "_m");
--- a/OpenTween/Thumbnail/Services/SimpleThumbnailService.cs
+++ b/OpenTween/Thumbnail/Services/SimpleThumbnailService.cs
@@ -28,7 +28,7 @@ using System.Text.RegularExpressions;
2828 namespace OpenTween.Thumbnail.Services
2929 {
3030 /// <summary>
31- /// 正規表現による単純な置換でサムネイルURLを生成する
31+ /// 正規表現によるURLの単純な置換でサムネイルURLを生成する
3232 /// </summary>
3333 class SimpleThumbnailService : IThumbnailService
3434 {
--- a/OpenTween/Thumbnail/Services/Tinami.cs
+++ b/OpenTween/Thumbnail/Services/Tinami.cs
@@ -43,7 +43,7 @@ namespace OpenTween.Thumbnail.Services
4343 var apiUrl = base.ReplaceUrl(url);
4444 if (apiUrl == null) return null;
4545
46- var xdoc = XDocument.Load(apiUrl);
46+ var xdoc = this.FetchContentInfoApi(apiUrl);
4747
4848 if (xdoc.XPathSelectElement("/rsp").Attribute("stat").Value == "ok")
4949 {
@@ -63,5 +63,10 @@ namespace OpenTween.Thumbnail.Services
6363
6464 return null;
6565 }
66+
67+ protected virtual XDocument FetchContentInfoApi(string url)
68+ {
69+ return XDocument.Load(url);
70+ }
6671 }
6772 }
--- a/OpenTween/Thumbnail/ThumbnailGenerator.cs
+++ b/OpenTween/Thumbnail/ThumbnailGenerator.cs
@@ -40,7 +40,7 @@ namespace OpenTween.Thumbnail
4040 new SimpleThumbnailService(@"^https?://.*(\.jpg|\.jpeg|\.gif|\.png|\.bmp)$", "${0}"),
4141
4242 // img.azyobuzi.net
43- new ImgAzyobuziNet(),
43+ new ImgAzyobuziNet(autoupdate: true),
4444
4545 // ImgUr
4646 new SimpleThumbnailService(@"^http://(?:i\.)?imgur\.com/(\w+)(?:\..{3})?$", "http://img.imgur.com/${1}l.jpg"),