OSDN Git Service

サムネイル関係のテストコード追加
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 30 Dec 2012 08:16:42 +0000 (17:16 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sun, 30 Dec 2012 10:25:05 +0000 (19:25 +0900)
OpenTween.Tests/OpenTween.Tests.csproj
OpenTween.Tests/Resources/dot.gif [new file with mode: 0644]
OpenTween.Tests/Thumbnail/Services/ImgAzyobuziNetTest.cs
OpenTween.Tests/Thumbnail/Services/TinamiTest.cs
OpenTween.Tests/TweetThumbnailTest.cs [new file with mode: 0644]
OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs
OpenTween/Thumbnail/Services/MetaThumbnailService.cs
OpenTween/TweetThumbnail.Designer.cs
OpenTween/TweetThumbnail.cs

index fdd9b59..f620af6 100644 (file)
@@ -62,6 +62,7 @@
     <Compile Include="Thumbnail\Services\MetaThumbnailServiceTest.cs" />\r
     <Compile Include="Thumbnail\Services\SimpleThumbnailServiceTest.cs" />\r
     <Compile Include="Thumbnail\Services\TinamiTest.cs" />\r
+    <Compile Include="TweetThumbnailTest.cs" />\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ProjectReference Include="..\OpenTween\OpenTween.csproj">\r
diff --git a/OpenTween.Tests/Resources/dot.gif b/OpenTween.Tests/Resources/dot.gif
new file mode 100644 (file)
index 0000000..e232d4a
Binary files /dev/null and b/OpenTween.Tests/Resources/dot.gif differ
index 94e4319..9dc6d98 100644 (file)
@@ -24,6 +24,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using NUnit.Framework;
+using System.Net;
 
 namespace OpenTween.Thumbnail.Services
 {
@@ -32,13 +33,42 @@ namespace OpenTween.Thumbnail.Services
     {
         class TestImgAzyobuziNet : ImgAzyobuziNet
         {
+            public TestImgAzyobuziNet()
+                : base()
+            {
+                this.ApiHosts = new[] { "http://img.azyobuzi.net/api/" };
+            }
+
+            public TestImgAzyobuziNet(string[] apiHosts)
+                : base()
+            {
+                this.ApiHosts = apiHosts;
+            }
+
+            public string GetApiBase()
+            {
+                return this.ApiBase;
+            }
+
             protected override byte[] FetchRegex(string apiBase)
             {
+                if (apiBase == "http://down.example.com/api/")
+                    throw new WebException();
+
                 return Encoding.UTF8.GetBytes("[{\"name\": \"hogehoge\", \"regex\": \"^https?://example.com/(.+)$\"}]");
             }
         }
 
         [Test]
+        public void HostFallbackTest()
+        {
+            var service = new TestImgAzyobuziNet(new[] { "http://down.example.com/api/", "http://avail.example.com/api/" });
+
+            service.LoadRegex();
+            Assert.That(service.GetApiBase(), Is.EqualTo("http://avail.example.com/api/"));
+        }
+
+        [Test]
         public void MatchTest()
         {
             var service = new TestImgAzyobuziNet();
index 69268b4..94a5e37 100644 (file)
@@ -55,5 +55,20 @@ namespace OpenTween.Thumbnail.Services
             Assert.That(thumbinfo.ThumbnailUrl, Is.EqualTo("http://img.tinami.com/hogehoge_150.gif"));
             Assert.That(thumbinfo.TooltipText, Is.EqualTo("説明"));
         }
+
+        [Test]
+        public void ApiErrorTest()
+        {
+            var service = new TestTinami(@"^http://www\.tinami\.com/view/(?<ContentId>\d+)$",
+                "http://api.tinami.com/content/info?cont_id=${ContentId}&api_key=" + ApplicationSettings.TINAMIApiKey);
+
+            service.FakeXml = @"<?xml version='1.0' encoding='utf-8'?>
+<rsp stat='user_only'>
+  <err msg='この作品は登録ユーザー限定の作品です。'/>
+</rsp>";
+            var thumbinfo = service.GetThumbnailInfo("http://www.tinami.com/view/12345", null);
+
+            Assert.That(thumbinfo, Is.Null);
+        }
     }
 }
diff --git a/OpenTween.Tests/TweetThumbnailTest.cs b/OpenTween.Tests/TweetThumbnailTest.cs
new file mode 100644 (file)
index 0000000..e229ecc
--- /dev/null
@@ -0,0 +1,108 @@
+// OpenTween - Client of Twitter
+// Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+// 
+// This file is part of OpenTween.
+// 
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+// 
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details. 
+// 
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+using System.Reflection;
+using System.Windows.Forms;
+using OpenTween.Thumbnail;
+
+namespace OpenTween
+{
+    [TestFixture]
+    class TweetThumbnailTest
+    {
+        [Test]
+        public void CreatePictureBoxTest()
+        {
+            using (var thumbBox = new TweetThumbnail())
+            {
+                var method = typeof(TweetThumbnail).GetMethod("CreatePictureBox", BindingFlags.Instance | BindingFlags.NonPublic);
+                var picbox = method.Invoke(thumbBox, new[] { "pictureBox1" }) as PictureBox;
+
+                Assert.That(picbox, Is.Not.Null);
+                Assert.That(picbox.Name, Is.EqualTo("pictureBox1"));
+                Assert.That(picbox.SizeMode, Is.EqualTo(PictureBoxSizeMode.Zoom));
+                Assert.That(picbox.WaitOnLoad, Is.False);
+                Assert.That(picbox.Dock, Is.EqualTo(DockStyle.Fill));
+                Assert.That(picbox.Visible, Is.False);
+
+                picbox.Dispose();
+            }
+        }
+
+        class TestTweetThumbnail : TweetThumbnail
+        {
+            protected override List<ThumbnailInfo> GetThumbailInfo(PostClass post)
+            {
+                return new List<ThumbnailInfo>
+                {
+                    new ThumbnailInfo
+                    {
+                        ImageUrl = "http://example.com/abcd",
+                        ThumbnailUrl = "dot.gif", // 1x1の黒画像
+                        TooltipText = "ほげほげ",
+                    },
+                };
+            }
+        }
+
+        [Test]
+        public void CancelAsyncTest()
+        {
+            using (var thumbbox = new TestTweetThumbnail())
+            {
+                var post = new PostClass();
+                var task = thumbbox.ShowThumbnailAsync(post);
+
+                thumbbox.CancelAsync();
+
+                Assert.That(task.IsCanceled, Is.True);
+            }
+        }
+
+        [Test]
+        public void SetThumbnailCountTest(
+            [Values(0, 1, 2)] int count)
+        {
+            using (var thumbbox = new TweetThumbnail())
+            {
+                var method = typeof(TweetThumbnail).GetMethod("SetThumbnailCount", BindingFlags.Instance | BindingFlags.NonPublic);
+                method.Invoke(thumbbox, new[] { (object)count });
+
+                Assert.That(thumbbox.pictureBox.Count, Is.EqualTo(count));
+
+                var num = 0;
+                foreach (var picbox in thumbbox.pictureBox)
+                {
+                    Assert.That(picbox.Name, Is.EqualTo("pictureBox" + num));
+                    num++;
+                }
+
+                Assert.That(thumbbox.scrollBar.Minimum, Is.EqualTo(0));
+                Assert.That(thumbbox.scrollBar.Maximum, Is.EqualTo(count));
+            }
+        }
+    }
+}
index 4b1cc58..9c0c74f 100644 (file)
@@ -37,10 +37,10 @@ namespace OpenTween.Thumbnail.Services
 {
     class ImgAzyobuziNet : IThumbnailService
     {
-        private readonly string[] ApiHosts = { "http://img.azyobuzi.net/api/", "http://img.opentween.org/api/" };
+        protected string[] ApiHosts = { "http://img.azyobuzi.net/api/", "http://img.opentween.org/api/" };
 
-        private string ApiBase;
-        private IEnumerable<Regex> UrlRegex = new Regex[] {};
+        protected string ApiBase;
+        protected IEnumerable<Regex> UrlRegex = new Regex[] {};
 
         private object LockObj = new object();
 
index daef29a..6ff29f0 100644 (file)
@@ -37,7 +37,7 @@ namespace OpenTween.Thumbnail.Services
         protected static string[] propertyNames = { "twitter:image", "og:image" };
 
         public MetaThumbnailService(string url)
-            : base(url, "${0}")
+            : this(url, "${0}")
         {
         }
 
index beb4a9b..0cd2ae9 100644 (file)
@@ -58,7 +58,7 @@
 
         #endregion
 
-        private System.Windows.Forms.VScrollBar scrollBar;
         private System.Windows.Forms.ToolTip toolTip;
+        protected internal System.Windows.Forms.VScrollBar scrollBar;
     }
 }
index 69aeba3..294db80 100644 (file)
@@ -36,7 +36,7 @@ namespace OpenTween
 {
     public partial class TweetThumbnail : UserControl
     {
-        private List<PictureBox> pictureBox = new List<PictureBox>();
+        protected internal List<PictureBox> pictureBox = new List<PictureBox>();
 
         private Task task = null;
         private CancellationTokenSource cancelTokenSource;
@@ -63,7 +63,7 @@ namespace OpenTween
             this.cancelTokenSource = new CancellationTokenSource();
             var cancelToken = this.cancelTokenSource.Token;
 
-            this.task = Task.Factory.StartNew(() => ThumbnailGenerator.GetThumbnails(post), cancelToken)
+            this.task = Task.Factory.StartNew(() => this.GetThumbailInfo(post), cancelToken)
                 .ContinueWith( /* await使いたい */
                     t =>
                     {
@@ -106,6 +106,11 @@ namespace OpenTween
             return this.task;
         }
 
+        protected virtual List<ThumbnailInfo> GetThumbailInfo(PostClass post)
+        {
+            return ThumbnailGenerator.GetThumbnails(post);
+        }
+
         public void CancelAsync()
         {
             if (this.task != null && !this.task.IsCompleted)
@@ -141,14 +146,7 @@ namespace OpenTween
 
             for (int i = 0; i < count; i++)
             {
-                var picbox = new PictureBox()
-                {
-                    Name = "pictureBox" + i,
-                    SizeMode = PictureBoxSizeMode.Zoom,
-                    WaitOnLoad = false,
-                    Dock = DockStyle.Fill,
-                    Visible = false,
-                };
+                var picbox = CreatePictureBox("pictureBox" + i);
                 picbox.DoubleClick += this.pictureBox_DoubleClick;
 
                 this.Controls.Add(picbox);
@@ -158,6 +156,18 @@ namespace OpenTween
             this.ResumeLayout(false);
         }
 
+        protected virtual PictureBox CreatePictureBox(string name)
+        {
+            return new PictureBox()
+            {
+                Name = name,
+                SizeMode = PictureBoxSizeMode.Zoom,
+                WaitOnLoad = false,
+                Dock = DockStyle.Fill,
+                Visible = false,
+            };
+        }
+
         public void ScrollUp()
         {
             var newval = this.scrollBar.Value + this.scrollBar.SmallChange;