1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 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;
27 using System.Windows.Forms;
30 using Xunit.Extensions;
34 public class TweenMainTest
37 public void GetUrlFromDataObject_XMozUrlTest()
39 var dataBytes = Encoding.Unicode.GetBytes("https://twitter.com/\nTwitter\0");
40 using (var memstream = new MemoryStream(dataBytes))
42 var data = new DataObject("text/x-moz-url", memstream);
44 var expected = new Tuple<string, string>("https://twitter.com/", "Twitter");
45 Assert.Equal(expected, TweenMain.GetUrlFromDataObject(data));
50 public void GetUrlFromDataObject_IESiteModeToUrlTest()
52 var dataBytes = Encoding.Unicode.GetBytes("https://twitter.com/\0Twitter\0");
53 using (var memstream = new MemoryStream(dataBytes))
55 var data = new DataObject("IESiteModeToUrl", memstream);
57 var expected = new Tuple<string, string>("https://twitter.com/", "Twitter");
58 Assert.Equal(expected, TweenMain.GetUrlFromDataObject(data));
63 public void GetUrlFromDataObject_UniformResourceLocatorWTest()
65 var dataBytes = Encoding.Unicode.GetBytes("https://twitter.com/\0");
66 using (var memstream = new MemoryStream(dataBytes))
68 var data = new DataObject("UniformResourceLocatorW", memstream);
70 var expected = new Tuple<string, string>("https://twitter.com/", null);
71 Assert.Equal(expected, TweenMain.GetUrlFromDataObject(data));
76 public void GetUrlFromDataObject_UnknownFormatTest()
78 using (var memstream = new MemoryStream(new byte[0]))
80 var data = new DataObject("application/x-hogehoge", memstream);
82 Assert.Throws<NotSupportedException>(() => TweenMain.GetUrlFromDataObject(data));
87 public void CreateRetweetUnofficial_UrlTest()
89 var statusText = "<a href=\"http://t.co/KYi7vMZzRt\" title=\"http://twitter.com/\">twitter.com</a>";
91 Assert.Equal("http://twitter.com/", TweenMain.CreateRetweetUnofficial(statusText, false));
95 public void CreateRetweetUnofficial_MentionTest()
97 var statusText = "<a class=\"mention\" href=\"https://twitter.com/twitterapi\">@TwitterAPI</a>";
99 Assert.Equal("@TwitterAPI", TweenMain.CreateRetweetUnofficial(statusText, false));
103 public void CreateRetweetUnofficial_HashtagTest()
105 var statusText = "<a class=\"hashtag\" href=\"https://twitter.com/search?q=%23OpenTween\">#OpenTween</a>";
107 Assert.Equal("#OpenTween", TweenMain.CreateRetweetUnofficial(statusText, false));
111 public void CreateRetweetUnofficial_SingleLineTest()
113 var statusText = "123<br>456<br>789";
115 Assert.Equal("123 456 789", TweenMain.CreateRetweetUnofficial(statusText, false));
119 public void CreateRetweetUnofficial_MultiLineTest()
121 var statusText = "123<br>456<br>789";
123 Assert.Equal("123" + Environment.NewLine + "456" + Environment.NewLine + "789", TweenMain.CreateRetweetUnofficial(statusText, true));
127 public void CreateRetweetUnofficial_DecodeTest()
129 var statusText = "<>"' ";
131 Assert.Equal("<>\"' ", TweenMain.CreateRetweetUnofficial(statusText, false));
135 public void CreateRetweetUnofficial_WithFormatterTest()
137 // TweetFormatterでHTMLに整形 → CreateRetweetUnofficialで復元 までの動作が正しく行えているか
139 var text = "#てすと @TwitterAPI \n http://t.co/KYi7vMZzRt";
140 var entities = new TwitterEntity[]
142 new TwitterEntityHashtag
144 Indices = new[] { 0, 4 },
147 new TwitterEntityMention
149 Indices = new[] { 5, 16 },
151 Name = "Twitter API",
152 ScreenName = "twitterapi",
156 Indices = new[] { 19, 41 },
157 DisplayUrl = "twitter.com",
158 ExpandedUrl = "http://twitter.com/",
159 Url = "http://t.co/KYi7vMZzRt",
163 var html = TweetFormatter.AutoLinkHtml(text, entities);
165 var expected = "#てすと @TwitterAPI " + Environment.NewLine + " http://twitter.com/";
166 Assert.Equal(expected, TweenMain.CreateRetweetUnofficial(html, true));
170 public void FormatQuoteTweetHtml_PostClassTest()
172 var post = new PostClass
175 Nickname = "upsilon",
176 ScreenName = "kim_upsilon",
177 Text = "<a href=\"https://twitter.com/twitterapi\">@twitterapi</a> hogehoge",
178 CreatedAt = new DateTime(2015, 3, 30, 3, 30, 0),
181 // PostClass.Text はリンクを除去するのみでエスケープは行わない
182 // (TweetFormatter によって既にエスケープされた文字列が格納されているため)
184 var expected = "<a class=\"quote-tweet-link\" href=\"https://twitter.com/status/status/12345\">" +
185 "<blockquote class=\"quote-tweet\">" +
186 "<p>@twitterapi hogehoge</p> — upsilon (@kim_upsilon) " + DateTime.Parse("2015/03/30 3:30:00") +
188 Assert.Equal(expected, TweenMain.FormatQuoteTweetHtml(post));
192 public void FormatQuoteTweetHtml_HtmlTest()
194 var statusId = 12345L; // リンク先のステータスID
195 var html = "<marquee>hogehoge</marquee>"; // HTMLをそのまま出力する (エスケープしない)
197 var expected = "<a class=\"quote-tweet-link\" href=\"https://twitter.com/status/status/12345\">" +
198 "<blockquote class=\"quote-tweet\"><marquee>hogehoge</marquee></blockquote>" +
200 Assert.Equal(expected, TweenMain.FormatQuoteTweetHtml(statusId, html));
204 public void StripLinkTagHtml_Test()
206 var html = "<a href=\"https://twitter.com/twitterapi\">@twitterapi</a>";
208 var expected = "@twitterapi";
209 Assert.Equal(expected, TweenMain.StripLinkTagHtml(html));