1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
25 using System.Reflection;
26 using System.Runtime.InteropServices;
28 using System.Text.RegularExpressions;
29 using System.Threading;
30 using System.Threading.Tasks;
31 using System.Xml.Linq;
34 using Xunit.Extensions;
36 namespace OpenTween.Thumbnail.Services
38 public class TinamiTest
40 class TestTinami : Tinami
42 public string FakeXml { get; set; }
44 public TestTinami(string pattern, string replacement)
45 : base(pattern, replacement)
49 protected override Task<XDocument> FetchContentInfoApiAsync(string url, CancellationToken token)
51 Assert.True(Regex.IsMatch(url, @"http://api\.tinami\.com/content/info\?cont_id=.+&api_key=.+"));
53 return Task.FromResult(XDocument.Parse(this.FakeXml));
62 public void MyCommonSetup()
64 var mockAssembly = Substitute.For<_Assembly>();
65 mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
66 MyCommon.EntryAssembly = mockAssembly;
68 MyCommon.fileVersion = "1.0.0.0";
72 public async Task ApiTest()
74 var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$",
75 "http://api.tinami.com/content/info?cont_id=${ContentId}&api_key=" + ApplicationSettings.TINAMIApiKey);
77 service.FakeXml = @"<?xml version='1.0' encoding='utf-8' ?>
79 <content type='illust' issupport='1' iscollection='0'>
81 <description>説明</description>
83 <thumbnail_150x150 url='http://img.tinami.com/hogehoge_150.gif' width='112' height='120'/>
86 <url>http://img.tinami.com/hogehoge_full.gif</url>
92 var thumbinfo = await service.GetThumbnailInfoAsync("http://www.tinami.com/view/12345", null, CancellationToken.None);
94 Assert.NotNull(thumbinfo);
95 Assert.Equal("http://www.tinami.com/view/12345", thumbinfo.ImageUrl);
96 Assert.Equal("http://img.tinami.com/hogehoge_150.gif", thumbinfo.ThumbnailUrl);
97 Assert.Equal("説明", thumbinfo.TooltipText);
101 public async Task ApiErrorTest()
103 var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$",
104 "http://api.tinami.com/content/info?cont_id=${ContentId}&api_key=" + ApplicationSettings.TINAMIApiKey);
106 service.FakeXml = @"<?xml version='1.0' encoding='utf-8'?>
107 <rsp stat='user_only'>
108 <err msg='この作品は登録ユーザー限定の作品です。'/>
110 var thumbinfo = await service.GetThumbnailInfoAsync("http://www.tinami.com/view/12345", null, CancellationToken.None);
112 Assert.Null(thumbinfo);