using System.Collections.Generic;
using System.Linq;
using System.Net;
+using System.Reflection;
+using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
+using NSubstitute;
using Xunit;
using Xunit.Extensions;
}
}
+ public ImgAzyobuziNetTest()
+ {
+ this.MyCommonSetup();
+ }
+
+ public void MyCommonSetup()
+ {
+ var mockAssembly = Substitute.For<_Assembly>();
+ mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
+ MyCommon.EntryAssembly = mockAssembly;
+
+ MyCommon.fileVersion = "1.0.0.0";
+ }
+
[Fact]
public void HostFallbackTest()
{
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Reflection;
+using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
+using NSubstitute;
using Xunit;
using Xunit.Extensions;
{
}
- protected override string FetchImageUrl(string url)
+ protected override Task<string> FetchImageUrlAsync(string url, CancellationToken token)
{
- return this.FakeHtml;
+ return Task.FromResult(this.FakeHtml);
}
}
+ public MetaThumbnailServiceTest()
+ {
+ this.MyCommonSetup();
+ }
+
+ public void MyCommonSetup()
+ {
+ var mockAssembly = Substitute.For<_Assembly>();
+ mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
+ MyCommon.EntryAssembly = mockAssembly;
+
+ MyCommon.fileVersion = "1.0.0.0";
+ }
+
[Fact]
public async Task OGPMetaTest()
{
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Reflection;
+using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
+using NSubstitute;
using Xunit;
using Xunit.Extensions;
{
public class SimpleThumbnailServiceTest
{
+ public SimpleThumbnailServiceTest()
+ {
+ this.MyCommonSetup();
+ }
+
+ public void MyCommonSetup()
+ {
+ var mockAssembly = Substitute.For<_Assembly>();
+ mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
+ MyCommon.EntryAssembly = mockAssembly;
+
+ MyCommon.fileVersion = "1.0.0.0";
+ }
+
[Fact]
public async Task RegexMatchTest()
{
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Reflection;
+using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
+using NSubstitute;
using Xunit;
using Xunit.Extensions;
{
}
- protected override XDocument FetchContentInfoApi(string url)
+ protected override Task<XDocument> FetchContentInfoApiAsync(string url, CancellationToken token)
{
Assert.True(Regex.IsMatch(url, @"http://api\.tinami\.com/content/info\?cont_id=.+&api_key=.+"));
- return XDocument.Parse(this.FakeXml);
+ return Task.FromResult(XDocument.Parse(this.FakeXml));
}
}
+ public TinamiTest()
+ {
+ this.MyCommonSetup();
+ }
+
+ public void MyCommonSetup()
+ {
+ var mockAssembly = Substitute.For<_Assembly>();
+ mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
+ MyCommon.EntryAssembly = mockAssembly;
+
+ MyCommon.fileVersion = "1.0.0.0";
+ }
+
[Fact]
public async Task ApiTest()
{
{
}
- public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
+ public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
{
- return Task.Run(() =>
- {
- var pageUrl = this.ReplaceUrl(url);
- if (pageUrl == null) return null;
+ var pageUrl = this.ReplaceUrl(url);
+ if (pageUrl == null) return null;
- try
- {
- var content = this.FetchImageUrl(pageUrl);
+ try
+ {
+ var content = await this.FetchImageUrlAsync(pageUrl, token)
+ .ConfigureAwait(false);
- var thumbnailUrl = this.GetThumbnailUrl(content);
- if (string.IsNullOrEmpty(thumbnailUrl)) return null;
+ var thumbnailUrl = this.GetThumbnailUrl(content);
+ if (string.IsNullOrEmpty(thumbnailUrl)) return null;
- return new ThumbnailInfo(this.http)
- {
- ImageUrl = url,
- ThumbnailUrl = thumbnailUrl,
- TooltipText = null,
- };
- }
- catch (WebException)
+ return new ThumbnailInfo(this.http)
{
- return null;
- }
- }, token);
+ ImageUrl = url,
+ ThumbnailUrl = thumbnailUrl,
+ TooltipText = null,
+ };
+ }
+ catch (WebException)
+ {
+ return null;
+ }
}
protected virtual string GetThumbnailUrl(string html)
return null;
}
- protected virtual string FetchImageUrl(string url)
+ protected virtual async Task<string> FetchImageUrlAsync(string url, CancellationToken token)
{
- using (var client = new OTWebClient())
+ using (var response = await this.http.GetAsync(url, token).ConfigureAwait(false))
{
- return client.DownloadString(url);
+ response.EnsureSuccessStatusCode();
+
+ return await response.Content.ReadAsStringAsync()
+ .ConfigureAwait(false);
}
}
}
{
}
- public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
+ public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
{
- return Task.Run<ThumbnailInfo>(async () =>
- {
- var thumb = await base.GetThumbnailInfoAsync(url, post, token);
- if (thumb == null) return null;
+ var thumb = await base.GetThumbnailInfoAsync(url, post, token)
+ .ConfigureAwait(false);
- return new Pixiv.Thumbnail(this.http)
- {
- ImageUrl = thumb.ImageUrl,
- ThumbnailUrl = thumb.ThumbnailUrl,
- TooltipText = thumb.TooltipText,
- FullSizeImageUrl = thumb.FullSizeImageUrl,
- };
- }, token);
+ if (thumb == null) return null;
+
+ return new Pixiv.Thumbnail(this.http)
+ {
+ ImageUrl = thumb.ImageUrl,
+ ThumbnailUrl = thumb.ThumbnailUrl,
+ TooltipText = thumb.TooltipText,
+ FullSizeImageUrl = thumb.FullSizeImageUrl,
+ };
}
public class Thumbnail : ThumbnailInfo
{
}
- public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
+ public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
{
- return Task.Run(() =>
- {
- var apiUrl = base.ReplaceUrl(url);
- if (apiUrl == null) return null;
+ var apiUrl = base.ReplaceUrl(url);
+ if (apiUrl == null) return null;
- var xdoc = this.FetchContentInfoApi(apiUrl);
+ var xdoc = await this.FetchContentInfoApiAsync(apiUrl, token)
+ .ConfigureAwait(false);
- if (xdoc.XPathSelectElement("/rsp").Attribute("stat").Value == "ok")
+ if (xdoc.XPathSelectElement("/rsp").Attribute("stat").Value == "ok")
+ {
+ var thumbUrlElm = xdoc.XPathSelectElement("/rsp/content/thumbnails/thumbnail_150x150");
+ if (thumbUrlElm != null)
{
- var thumbUrlElm = xdoc.XPathSelectElement("/rsp/content/thumbnails/thumbnail_150x150");
- if (thumbUrlElm != null)
- {
- var descElm = xdoc.XPathSelectElement("/rsp/content/description");
+ var descElm = xdoc.XPathSelectElement("/rsp/content/description");
- return new ThumbnailInfo(this.http)
- {
- ImageUrl = url,
- ThumbnailUrl = thumbUrlElm.Attribute("url").Value,
- TooltipText = descElm == null ? null : descElm.Value,
- };
- }
+ return new ThumbnailInfo(this.http)
+ {
+ ImageUrl = url,
+ ThumbnailUrl = thumbUrlElm.Attribute("url").Value,
+ TooltipText = descElm == null ? null : descElm.Value,
+ };
}
+ }
- return null;
- }, token);
+ return null;
}
- protected virtual XDocument FetchContentInfoApi(string url)
+ protected virtual async Task<XDocument> FetchContentInfoApiAsync(string url, CancellationToken token)
{
- return XDocument.Load(url);
+ using (var response = await this.http.GetAsync(url, token).ConfigureAwait(false))
+ {
+ response.EnsureSuccessStatusCode();
+
+ var xmlStr = await response.Content.ReadAsStringAsync()
+ .ConfigureAwait(false);
+
+ return XDocument.Parse(xmlStr);
+ }
}
}
}
{
}
- public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
+ public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
{
- return Task.Run(() =>
+ var apiUrl = base.ReplaceUrl(url);
+ if (apiUrl == null) return null;
+
+ var json = await this.http.GetByteArrayAsync(apiUrl)
+ .ConfigureAwait(false);
+
+ using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(json, XmlDictionaryReaderQuotas.Max))
{
- var apiUrl = base.ReplaceUrl(url);
- if (apiUrl == null) return null;
+ var xElm = XElement.Load(jsonReader);
- using (var client = new OTWebClient())
- using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(client.DownloadData(apiUrl), XmlDictionaryReaderQuotas.Max))
+ var thumbUrlElm = xElm.XPathSelectElement("/response/post/thumb_url");
+ if (thumbUrlElm == null)
{
- var xElm = XElement.Load(jsonReader);
-
- var thumbUrlElm = xElm.XPathSelectElement("/response/post/thumb_url");
- if (thumbUrlElm == null)
- {
- return null;
- }
+ return null;
+ }
- var textElm = xElm.XPathSelectElement("/response/post/text");
+ var textElm = xElm.XPathSelectElement("/response/post/text");
- return new ThumbnailInfo(this.http)
- {
- ImageUrl = url,
- ThumbnailUrl = thumbUrlElm.Value,
- TooltipText = textElm == null ? null : textElm.Value,
- };
- }
- }, token);
+ return new ThumbnailInfo(this.http)
+ {
+ ImageUrl = url,
+ ThumbnailUrl = thumbUrlElm.Value,
+ TooltipText = textElm == null ? null : textElm.Value,
+ };
+ }
}
}
}
{
}
- public override Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
+ public override async Task<ThumbnailInfo> GetThumbnailInfoAsync(string url, PostClass post, CancellationToken token)
{
- return Task.Run(() =>
- {
- var apiUrl = base.ReplaceUrl(url);
- if (apiUrl == null) return null;
+ var apiUrl = base.ReplaceUrl(url);
+ if (apiUrl == null) return null;
- var xdoc = XDocument.Load(apiUrl);
+ var xmlStr = await this.http.GetStringAsync(apiUrl)
+ .ConfigureAwait(false);
- var thumbUrlElm = xdoc.XPathSelectElement("/oembed/thumbnail_url");
- if (thumbUrlElm != null)
- {
- var titleElm = xdoc.XPathSelectElement("/oembed/title");
- var durationElm = xdoc.XPathSelectElement("/oembed/duration");
+ var xdoc = XDocument.Parse(xmlStr);
- var tooltipText = "";
- if (titleElm != null && durationElm != null)
- {
- var duration = int.Parse(durationElm.Value);
- var minute = duration / 60;
- var second = duration % 60;
- tooltipText = string.Format("{0} ({1:00}:{2:00})", titleElm.Value, minute, second);
- }
+ var thumbUrlElm = xdoc.XPathSelectElement("/oembed/thumbnail_url");
+ if (thumbUrlElm != null)
+ {
+ var titleElm = xdoc.XPathSelectElement("/oembed/title");
+ var durationElm = xdoc.XPathSelectElement("/oembed/duration");
- return new ThumbnailInfo(this.http)
- {
- ImageUrl = url,
- ThumbnailUrl = thumbUrlElm.Value,
- TooltipText = tooltipText,
- };
+ var tooltipText = "";
+ if (titleElm != null && durationElm != null)
+ {
+ var duration = int.Parse(durationElm.Value);
+ var minute = duration / 60;
+ var second = duration % 60;
+ tooltipText = string.Format("{0} ({1:00}:{2:00})", titleElm.Value, minute, second);
}
- return null;
- }, token);
+ return new ThumbnailInfo(this.http)
+ {
+ ImageUrl = url,
+ ThumbnailUrl = thumbUrlElm.Value,
+ TooltipText = tooltipText,
+ };
+ }
+
+ return null;
}
}
}