/MindMate.Tests/View/NoteEditing/ImageLocalSaverTests.cs

https://github.com/chenzhongfang/MindMate · C# · 328 lines · 93 code · 38 blank · 197 comment · 0 complexity · 600f0a93514b53918cc3e7a00ad8f1d0 MD5 · raw file

  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using MindMate.Serialization;
  3. using MindMate.View.NoteEditing;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace MindMate.Tests.View
  13. {
  14. [TestClass()]
  15. public class ImageLocalSaverTests
  16. {
  17. [TestMethod()]
  18. public void ImageLocalSaver_ctor_ImagesAreProcessed()
  19. {
  20. var p = new PersistenceManager();
  21. var tree = p.OpenTree(@"Resources\Websites.mm");
  22. string html = null;
  23. System.Threading.Thread t = new System.Threading.Thread(() =>
  24. {
  25. var editor = new NoteEditor();
  26. var form = CreateForm();
  27. form.Controls.Add(editor);
  28. new ImageLocalSaver(editor, p);
  29. form.Shown += (sender, args) =>
  30. {
  31. editor.UpdateHtmlSource(tree.RootNode.FirstChild.NoteText);
  32. html = editor.HTML;
  33. form.Close();
  34. };
  35. form.ShowDialog();
  36. });
  37. t.SetApartmentState(System.Threading.ApartmentState.STA);
  38. t.Start();
  39. t.Join();
  40. Assert.IsTrue(html.Contains("srcOrig"));
  41. int imgUpdated = Regex.Matches(html, "srcOrig", RegexOptions.IgnoreCase).Count;
  42. Assert.AreEqual(79, imgUpdated);
  43. int imgCount = Regex.Matches(html, "<img", RegexOptions.IgnoreCase).Count;
  44. Assert.IsTrue(imgCount > imgUpdated);
  45. }
  46. [TestMethod()]
  47. public void ProcessImages_BBC_CheckImgTags()
  48. {
  49. var p = new PersistenceManager();
  50. var tree = p.OpenTree(@"Resources\Websites.mm");
  51. string html = null;
  52. System.Threading.Thread t = new System.Threading.Thread(() =>
  53. {
  54. var editor = new NoteEditor();
  55. var form = CreateForm();
  56. form.Controls.Add(editor);
  57. form.Shown += (sender, args) =>
  58. {
  59. editor.HTML = tree.RootNode.FirstChild.NoteText;
  60. ImageLocalSaver.ProcessImages(editor.Document, p.CurrentTree);
  61. html = editor.HTML;
  62. form.Close();
  63. };
  64. form.ShowDialog();
  65. });
  66. t.SetApartmentState(System.Threading.ApartmentState.STA);
  67. t.Start();
  68. t.Join();
  69. Assert.IsTrue(html.Contains("srcOrig"));
  70. int imgUpdated = Regex.Matches(html, "srcOrig", RegexOptions.IgnoreCase).Count;
  71. Assert.AreEqual(79, imgUpdated);
  72. int imgCount = Regex.Matches(html, "<img", RegexOptions.IgnoreCase).Count;
  73. Assert.IsTrue(imgCount > imgUpdated);
  74. }
  75. //TODO: Fix the following tests
  76. //[TestMethod()]
  77. //public void ImageTagProcessor_Wikipedia()
  78. //{
  79. // var p = new PersistenceManager();
  80. // var tree = p.OpenTree(@"Resources\Websites.mm").Tree;
  81. // HtmlProcessor sut = null;
  82. // System.Threading.Thread t = new System.Threading.Thread(() =>
  83. // {
  84. // var editor = new NoteEditor();
  85. // sut = new HtmlProcessor(editor, tree.RootNode.FirstChild.Next.NoteText);
  86. // });
  87. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  88. // t.Start();
  89. // t.Join();
  90. // Assert.IsTrue(sut.ProcessedHtml.Contains("srcOrig"));
  91. // Assert.AreEqual(12, sut.ImageSourceChanges.Count());
  92. // int imgCount = Regex.Matches(sut.ProcessedHtml, "<img", RegexOptions.IgnoreCase).Count;
  93. // Assert.IsTrue(imgCount == sut.ImageSourceChanges.Count());
  94. //}
  95. //[TestMethod()]
  96. //public void ImageTagProcessor_Yahoo()
  97. //{
  98. // var p = new PersistenceManager();
  99. // var tree = p.OpenTree(@"Resources\Websites.mm").Tree;
  100. // HtmlProcessor sut = null;
  101. // System.Threading.Thread t = new System.Threading.Thread(() =>
  102. // {
  103. // var editor = new NoteEditor();
  104. // sut = new HtmlProcessor(editor, tree.RootNode.LastChild.NoteText);
  105. // });
  106. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  107. // t.Start();
  108. // t.Join();
  109. // Assert.IsTrue(sut.ProcessedHtml.Contains("srcOrig"));
  110. // Assert.AreEqual(35, sut.ImageSourceChanges.Count());
  111. // int imgCount = Regex.Matches(sut.ProcessedHtml, "<img", RegexOptions.IgnoreCase).Count;
  112. // Assert.IsTrue(imgCount == sut.ImageSourceChanges.Count());
  113. //}
  114. //[TestMethod()]
  115. //public void ImageTagProcessor_SingleQuotes()
  116. //{
  117. // HtmlProcessor sut = null;
  118. // System.Threading.Thread t = new System.Threading.Thread(() =>
  119. // {
  120. // var editor = new NoteEditor();
  121. // sut = new HtmlProcessor(editor, "<img alt='abc' src='http://test.com/image1.jpg'>");
  122. // });
  123. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  124. // t.Start();
  125. // t.Join();
  126. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  127. //}
  128. //[TestMethod()]
  129. //public void ImageTagProcessor_DoubleQuotes()
  130. //{
  131. // HtmlProcessor sut = null;
  132. // System.Threading.Thread t = new System.Threading.Thread(() =>
  133. // {
  134. // var editor = new NoteEditor();
  135. // sut = new HtmlProcessor(editor, "<img alt='abc' src=\"http://test.com/image1.jpg\">");
  136. // });
  137. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  138. // t.Start();
  139. // t.Join();
  140. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  141. //}
  142. //[TestMethod()]
  143. //public void ImageTagProcessor_Newline()
  144. //{
  145. // HtmlProcessor sut = null;
  146. // System.Threading.Thread t = new System.Threading.Thread(() =>
  147. // {
  148. // var editor = new NoteEditor();
  149. // sut = new HtmlProcessor(editor, "<img alt='abc' \n src='http://test.com/image1.jpg'>");
  150. // });
  151. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  152. // t.Start();
  153. // t.Join();
  154. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  155. //}
  156. //[TestMethod()]
  157. //public void ImageTagProcessor_NewlineInSrc()
  158. //{
  159. // HtmlProcessor sut = null;
  160. // System.Threading.Thread t = new System.Threading.Thread(() =>
  161. // {
  162. // var editor = new NoteEditor();
  163. // sut = new HtmlProcessor(editor, "<img alt='abc' src='http://test.com/image1.jpg\n'>");
  164. // });
  165. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  166. // t.Start();
  167. // t.Join();
  168. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  169. //}
  170. //[TestMethod()]
  171. //public void ImageTagProcessor_SpacesInAttribute()
  172. //{
  173. // HtmlProcessor sut = null;
  174. // System.Threading.Thread t = new System.Threading.Thread(() =>
  175. // {
  176. // var editor = new NoteEditor();
  177. // sut = new HtmlProcessor(editor, "<img alt='abc' \n src = 'http://test.com/image1.jpg'>");
  178. // });
  179. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  180. // t.Start();
  181. // t.Join();
  182. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  183. //}
  184. //[TestMethod()]
  185. //public void ImageTagProcessor_NewlineInAttribute()
  186. //{
  187. // HtmlProcessor sut = null;
  188. // System.Threading.Thread t = new System.Threading.Thread(() =>
  189. // {
  190. // var editor = new NoteEditor();
  191. // sut = new HtmlProcessor(editor, "<img alt='abc' \n src =\n 'http://test.com/image1.jpg'>");
  192. // });
  193. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  194. // t.Start();
  195. // t.Join();
  196. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  197. //}
  198. //[TestMethod()]
  199. //public void ImageTagProcessor_SrcFirstAttribute()
  200. //{
  201. // HtmlProcessor sut = null;
  202. // System.Threading.Thread t = new System.Threading.Thread(() =>
  203. // {
  204. // var editor = new NoteEditor();
  205. // sut = new HtmlProcessor(editor, "<img src='http://test.com/image1.jpg' alt='abc'>");
  206. // });
  207. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  208. // t.Start();
  209. // t.Join();
  210. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  211. //}
  212. //[TestMethod()]
  213. //public void ImageTagProcessor_SrcInCaps()
  214. //{
  215. // HtmlProcessor sut = null;
  216. // System.Threading.Thread t = new System.Threading.Thread(() =>
  217. // {
  218. // var editor = new NoteEditor();
  219. // sut = new HtmlProcessor(editor, "<IMG SRC='http://test.com/image1.jpg' alt='abc'>");
  220. // });
  221. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  222. // t.Start();
  223. // t.Join();
  224. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  225. //}
  226. //[TestMethod()]
  227. //public void ImageTagProcessor_ClosingBracketOnNewline()
  228. //{
  229. // HtmlProcessor sut = null;
  230. // System.Threading.Thread t = new System.Threading.Thread(() =>
  231. // {
  232. // var editor = new NoteEditor();
  233. // sut = new HtmlProcessor(editor, "<img src='http://test.com/image1.jpg' alt='abc' \n>");
  234. // });
  235. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  236. // t.Start();
  237. // t.Join();
  238. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  239. //}
  240. //[TestMethod()]
  241. //public void ImageTagProcessor_TwoNewLines()
  242. //{
  243. // HtmlProcessor sut = null;
  244. // System.Threading.Thread t = new System.Threading.Thread(() =>
  245. // {
  246. // var editor = new NoteEditor();
  247. // sut = new HtmlProcessor(editor, "<img alt='abc' \n src =\n 'http://test.com/image1.jpg'>");
  248. // });
  249. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  250. // t.Start();
  251. // t.Join();
  252. // Assert.AreEqual(1, sut.ImageSourceChanges.Count());
  253. //}
  254. //[TestMethod]
  255. //public void ScriptPreProcessor_BBC()
  256. //{
  257. // var p = new PersistenceManager();
  258. // var tree = p.OpenTree(@"Resources\Websites.mm").Tree;
  259. // HtmlProcessor sut = null;
  260. // System.Threading.Thread t = new System.Threading.Thread(() =>
  261. // {
  262. // var editor = new NoteEditor();
  263. // sut = new HtmlProcessor(editor, tree.RootNode.FirstChild.NoteText);
  264. // });
  265. // t.SetApartmentState(System.Threading.ApartmentState.STA);
  266. // t.Start();
  267. // t.Join();
  268. // Assert.IsTrue(sut.ProcessedHtml.Contains("script"));
  269. // int scriptCount = Regex.Matches(sut.ProcessedHtml, "<script", RegexOptions.IgnoreCase).Count;
  270. // Assert.AreEqual(0, scriptCount);
  271. //}
  272. private Form CreateForm()
  273. {
  274. Form form = new Form();
  275. form.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  276. form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  277. form.ClientSize = new System.Drawing.Size(415, 304);
  278. form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
  279. form.KeyPreview = true;
  280. form.MaximizeBox = false;
  281. form.MinimizeBox = false;
  282. form.Name = "TestForm";
  283. form.ShowInTaskbar = false;
  284. form.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  285. form.Text = "Test Form";
  286. form.Size = new Size(320, 320);
  287. return form;
  288. }
  289. }
  290. }