using System.Collections.Generic;
using System.IO;
using System.Linq;
+using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
class MockThumbnailInfo : ThumbnailInfo
{
- public override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
+ public override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
{
var image = MemoryImage.CopyFromBytes(File.ReadAllBytes("Resources/" + this.ThumbnailUrl));
return Task.FromResult(image);
public TweetThumbnailTest()
{
this.ThumbnailGeneratorSetup();
+ this.MyCommonSetup();
}
public void ThumbnailGeneratorSetup()
});
}
+ public void MyCommonSetup()
+ {
+ var mockAssembly = Substitute.For<_Assembly>();
+ mockAssembly.GetName().Returns(new AssemblyName("OpenTween"));
+
+ MyCommon.EntryAssembly = mockAssembly;
+ }
+
[Fact]
public void CreatePictureBoxTest()
{
var map = MapThumb.GetDefaultInstance();
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = map.CreateMapLinkUrl(location.Latitude, location.Longitude),
ThumbnailUrl = map.CreateStaticMapUrl(location.Latitude, location.Longitude),
{
if (regex.IsMatch(url))
{
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = this.ApiBase + "redirect?size=large&uri=" + Uri.EscapeDataString(url),
var thumbnailUrl = this.GetThumbnailUrl(content);
if (string.IsNullOrEmpty(thumbnailUrl)) return null;
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = thumbnailUrl,
if (!string.IsNullOrEmpty(imgurl))
{
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = imgurl,
{
class PhotoShareShortlink : IThumbnailService
{
- protected readonly HttpClient http;
protected Regex regex;
public PhotoShareShortlink(string pattern)
- : this(null, pattern)
{
- }
-
- public PhotoShareShortlink(HttpClient http, string pattern)
- {
- this.http = http ?? MyCommon.CreateHttpClient();
this.regex = new Regex(pattern);
}
if (!match.Success) return null;
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = "http://images.bcphotoshare.com/storages/" + RadixConvert.ToInt32(match.Result("${1}"), 36) + "/thumb180.jpg",
if (thumb == null) return null;
- return new Pixiv.Thumbnail(this.http)
+ return new Pixiv.Thumbnail
{
ImageUrl = thumb.ImageUrl,
ThumbnailUrl = thumb.ThumbnailUrl,
public class Thumbnail : ThumbnailInfo
{
- public Thumbnail(HttpClient http)
- : base(http)
- {
- }
-
- public async override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
+ public async override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
{
var request = new HttpRequestMessage(HttpMethod.Get, this.ThumbnailUrl);
request.Headers.Add("User-Agent", MyCommon.GetUserAgentString(fakeMSIE: true));
request.Headers.Referrer = new Uri(this.ImageUrl);
- using (var response = await this.http.SendAsync(request, token).ConfigureAwait(false))
+ using (var response = await http.SendAsync(request, cancellationToken).ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
var thumbnailUrl = this.ReplaceUrl(url);
if (thumbnailUrl == null) return null;
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = thumbnailUrl,
{
var descElm = xdoc.XPathSelectElement("/rsp/content/description");
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = thumbUrlElm.Attribute("url").Value,
using System.IO;
using System.Linq;
using System.Net;
+using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
public class Thumbnail : ThumbnailInfo
{
- public override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
+ public override Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
{
+ // TODO: HttpClient を使用したコードに置き換えたい
return Task.Run(() =>
{
var oauth = new HttpOAuthApiProxy();
else
throw new WebException(statusCode.ToString(), WebExceptionStatus.ProtocolError);
}
- }, token);
+ }, cancellationToken);
}
}
}
return null;
}
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = imgurl,
var textElm = xElm.XPathSelectElement("/response/post/text");
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = thumbUrlElm.Value,
tooltipText = string.Format("{0} ({1:00}:{2:00})", titleElm.Value, minute, second);
}
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = thumbUrlElm.Value,
}
- return new ThumbnailInfo(this.http)
+ return new ThumbnailInfo
{
ImageUrl = url,
ThumbnailUrl = imgUrl,
public string TooltipText { get; set; }
public string FullSizeImageUrl { get; set; }
- protected readonly HttpClient http;
-
- public ThumbnailInfo()
- : this(null)
+ public Task<MemoryImage> LoadThumbnailImageAsync()
{
+ return this.LoadThumbnailImageAsync(CancellationToken.None);
}
- public ThumbnailInfo(HttpClient http)
+ public async Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken cancellationToken)
{
- this.http = http ?? MyCommon.CreateHttpClient();
- }
-
- public Task<MemoryImage> LoadThumbnailImageAsync()
- {
- return this.LoadThumbnailImageAsync(CancellationToken.None);
+ using (var http = MyCommon.CreateHttpClient())
+ {
+ return await this.LoadThumbnailImageAsync(http, cancellationToken)
+ .ConfigureAwait(false);
+ }
}
- public async virtual Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
+ public async virtual Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
{
- using (var response = await this.http.GetAsync(this.ThumbnailUrl, token).ConfigureAwait(false))
+ using (var response = await http.GetAsync(this.ThumbnailUrl, cancellationToken).ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();