PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/webpages/test/Microsoft.Web.Helpers.Test/VideoTest.cs

https://github.com/jeanlyn/ASP.NET-Mvc-3
C# | 300 lines | 261 code | 37 blank | 2 comment | 0 complexity | ce4eeba12e9f0e0570f6c3d981357514 MD5 | raw file
  1. namespace Microsoft.Web.Helpers.Test {
  2. using System;
  3. using System.Text.RegularExpressions;
  4. using System.Web;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using System.Web.WebPages.TestUtils;
  7. using Moq;
  8. [TestClass]
  9. public class VideoTest {
  10. private VirtualPathUtilityWrapper _pathUtility = new VirtualPathUtilityWrapper();
  11. [TestMethod]
  12. public void FlashCannotOverrideHtmlAttributes() {
  13. ExceptionAssert.ThrowsArgumentException(() => {
  14. Video.Flash(GetContext(), _pathUtility, "http://foo.bar.com/foo.swf", htmlAttributes: new { cLASSid = "CanNotOverride" });
  15. }, "htmlAttributes", "Property \"cLASSid\" cannot be set through this argument.");
  16. }
  17. [TestMethod]
  18. public void FlashDefaults() {
  19. string html = Video.Flash(GetContext(), _pathUtility, "http://foo.bar.com/foo.swf").ToString().Replace("\r\n", "");
  20. Assert.IsTrue(html.StartsWith(
  21. "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" " +
  22. "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab\" type=\"application/x-oleobject\" >"
  23. ));
  24. Assert.IsTrue(html.Contains("<param name=\"movie\" value=\"http://foo.bar.com/foo.swf\" />"));
  25. Assert.IsTrue(html.Contains("<embed src=\"http://foo.bar.com/foo.swf\" type=\"application/x-shockwave-flash\" />"));
  26. Assert.IsTrue(html.EndsWith("</object>"));
  27. }
  28. [TestMethod]
  29. public void FlashThrowsWhenPathIsEmpty() {
  30. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  31. Video.Flash(GetContext(), _pathUtility, String.Empty);
  32. }, "path");
  33. }
  34. [TestMethod]
  35. public void FlashThrowsWhenPathIsNull() {
  36. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  37. Video.Flash(GetContext(), _pathUtility, null);
  38. }, "path");
  39. }
  40. [TestMethod]
  41. public void FlashWithExposedOptions() {
  42. string html = Video.Flash(GetContext(), _pathUtility, "http://foo.bar.com/foo.swf", width: "100px", height: "100px",
  43. play: false, loop: false, menu: false, bgColor: "#000", quality: "Q", scale: "S", windowMode: "WM",
  44. baseUrl: "http://foo.bar.com/", version: "1.0.0.0", htmlAttributes: new { id = "fl" }, embedName: "efl").ToString().Replace("\r\n", "");
  45. Assert.IsTrue(html.StartsWith(
  46. "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" " +
  47. "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=1,0,0,0\" " +
  48. "height=\"100px\" id=\"fl\" type=\"application/x-oleobject\" width=\"100px\" >"
  49. ));
  50. Assert.IsTrue(html.Contains("<param name=\"play\" value=\"False\" />"));
  51. Assert.IsTrue(html.Contains("<param name=\"loop\" value=\"False\" />"));
  52. Assert.IsTrue(html.Contains("<param name=\"menu\" value=\"False\" />"));
  53. Assert.IsTrue(html.Contains("<param name=\"bgColor\" value=\"#000\" />"));
  54. Assert.IsTrue(html.Contains("<param name=\"quality\" value=\"Q\" />"));
  55. Assert.IsTrue(html.Contains("<param name=\"scale\" value=\"S\" />"));
  56. Assert.IsTrue(html.Contains("<param name=\"wmode\" value=\"WM\" />"));
  57. Assert.IsTrue(html.Contains("<param name=\"base\" value=\"http://foo.bar.com/\" />"));
  58. var embed = new Regex("<embed.*/>").Match(html);
  59. Assert.IsTrue(embed.Success);
  60. Assert.IsTrue(embed.Value.StartsWith("<embed src=\"http://foo.bar.com/foo.swf\" width=\"100px\" height=\"100px\" name=\"efl\" type=\"application/x-shockwave-flash\" "));
  61. Assert.IsTrue(embed.Value.Contains("play=\"False\""));
  62. Assert.IsTrue(embed.Value.Contains("loop=\"False\""));
  63. Assert.IsTrue(embed.Value.Contains("menu=\"False\""));
  64. Assert.IsTrue(embed.Value.Contains("bgColor=\"#000\""));
  65. Assert.IsTrue(embed.Value.Contains("quality=\"Q\""));
  66. Assert.IsTrue(embed.Value.Contains("scale=\"S\""));
  67. Assert.IsTrue(embed.Value.Contains("wmode=\"WM\""));
  68. Assert.IsTrue(embed.Value.Contains("base=\"http://foo.bar.com/\""));
  69. }
  70. [TestMethod]
  71. public void FlashWithUnexposedOptions() {
  72. string html = Video.Flash(GetContext(), _pathUtility, "http://foo.bar.com/foo.swf", options: new { X = "Y", Z = 123 }).ToString().Replace("\r\n", "");
  73. Assert.IsTrue(html.Contains("<param name=\"X\" value=\"Y\" />"));
  74. Assert.IsTrue(html.Contains("<param name=\"Z\" value=\"123\" />"));
  75. // note - can't guarantee order of optional params:
  76. Assert.IsTrue(
  77. html.Contains("<embed src=\"http://foo.bar.com/foo.swf\" type=\"application/x-shockwave-flash\" X=\"Y\" Z=\"123\" />") ||
  78. html.Contains("<embed src=\"http://foo.bar.com/foo.swf\" type=\"application/x-shockwave-flash\" Z=\"123\" X=\"Y\" />")
  79. );
  80. }
  81. [TestMethod]
  82. public void MediaPlayerCannotOverrideHtmlAttributes() {
  83. ExceptionAssert.ThrowsArgumentException(() => {
  84. Video.MediaPlayer(GetContext(), _pathUtility, "http://foo.bar.com/foo.wmv", htmlAttributes: new { cODEbase = "CanNotOverride" });
  85. }, "htmlAttributes", "Property \"cODEbase\" cannot be set through this argument.");
  86. }
  87. [TestMethod]
  88. public void MediaPlayerDefaults() {
  89. string html = Video.MediaPlayer(GetContext(), _pathUtility, "http://foo.bar.com/foo.wmv").ToString().Replace("\r\n", "");
  90. Assert.IsTrue(html.StartsWith(
  91. "<object classid=\"clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6\" >"
  92. ));
  93. Assert.IsTrue(html.Contains("<param name=\"URL\" value=\"http://foo.bar.com/foo.wmv\" />"));
  94. Assert.IsTrue(html.Contains("<embed src=\"http://foo.bar.com/foo.wmv\" type=\"application/x-mplayer2\" />"));
  95. Assert.IsTrue(html.EndsWith("</object>"));
  96. }
  97. [TestMethod]
  98. public void MediaPlayerThrowsWhenPathIsEmpty() {
  99. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  100. Video.MediaPlayer(GetContext(), _pathUtility, String.Empty);
  101. }, "path");
  102. }
  103. [TestMethod]
  104. public void MediaPlayerThrowsWhenPathIsNull() {
  105. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  106. Video.MediaPlayer(GetContext(), _pathUtility, null);
  107. }, "path");
  108. }
  109. [TestMethod]
  110. public void MediaPlayerWithExposedOptions() {
  111. string html = Video.MediaPlayer(GetContext(), _pathUtility, "http://foo.bar.com/foo.wmv", width: "100px", height: "100px",
  112. autoStart: false, playCount: 2, uiMode: "UIMODE", stretchToFit: true, enableContextMenu: false, mute: true,
  113. volume: 1, baseUrl: "http://foo.bar.com/", htmlAttributes: new { id = "mp" }, embedName: "emp").ToString().Replace("\r\n", "");
  114. Assert.IsTrue(html.StartsWith(
  115. "<object classid=\"clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6\" height=\"100px\" id=\"mp\" width=\"100px\" >"
  116. ));
  117. Assert.IsTrue(html.Contains("<param name=\"URL\" value=\"http://foo.bar.com/foo.wmv\" />"));
  118. Assert.IsTrue(html.Contains("<param name=\"autoStart\" value=\"False\" />"));
  119. Assert.IsTrue(html.Contains("<param name=\"playCount\" value=\"2\" />"));
  120. Assert.IsTrue(html.Contains("<param name=\"uiMode\" value=\"UIMODE\" />"));
  121. Assert.IsTrue(html.Contains("<param name=\"stretchToFit\" value=\"True\" />"));
  122. Assert.IsTrue(html.Contains("<param name=\"enableContextMenu\" value=\"False\" />"));
  123. Assert.IsTrue(html.Contains("<param name=\"mute\" value=\"True\" />"));
  124. Assert.IsTrue(html.Contains("<param name=\"volume\" value=\"1\" />"));
  125. Assert.IsTrue(html.Contains("<param name=\"baseURL\" value=\"http://foo.bar.com/\" />"));
  126. var embed = new Regex("<embed.*/>").Match(html);
  127. Assert.IsTrue(embed.Success);
  128. Assert.IsTrue(embed.Value.StartsWith("<embed src=\"http://foo.bar.com/foo.wmv\" width=\"100px\" height=\"100px\" name=\"emp\" type=\"application/x-mplayer2\" "));
  129. Assert.IsTrue(embed.Value.Contains("autoStart=\"False\""));
  130. Assert.IsTrue(embed.Value.Contains("playCount=\"2\""));
  131. Assert.IsTrue(embed.Value.Contains("uiMode=\"UIMODE\""));
  132. Assert.IsTrue(embed.Value.Contains("stretchToFit=\"True\""));
  133. Assert.IsTrue(embed.Value.Contains("enableContextMenu=\"False\""));
  134. Assert.IsTrue(embed.Value.Contains("mute=\"True\""));
  135. Assert.IsTrue(embed.Value.Contains("volume=\"1\""));
  136. Assert.IsTrue(embed.Value.Contains("baseURL=\"http://foo.bar.com/\""));
  137. }
  138. [TestMethod]
  139. public void MediaPlayerWithUnexposedOptions() {
  140. string html = Video.MediaPlayer(GetContext(), _pathUtility, "http://foo.bar.com/foo.wmv", options: new { X = "Y", Z = 123 }).ToString().Replace("\r\n", "");
  141. Assert.IsTrue(html.Contains("<param name=\"X\" value=\"Y\" />"));
  142. Assert.IsTrue(html.Contains("<param name=\"Z\" value=\"123\" />"));
  143. Assert.IsTrue(
  144. html.Contains("<embed src=\"http://foo.bar.com/foo.wmv\" type=\"application/x-mplayer2\" X=\"Y\" Z=\"123\" />") ||
  145. html.Contains("<embed src=\"http://foo.bar.com/foo.wmv\" type=\"application/x-mplayer2\" Z=\"123\" X=\"Y\" />")
  146. );
  147. }
  148. [TestMethod]
  149. public void SilverlightCannotOverrideHtmlAttributes() {
  150. ExceptionAssert.ThrowsArgumentException(() => {
  151. Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", "100px", "100px",
  152. htmlAttributes: new { WIDTH = "CanNotOverride" });
  153. }, "htmlAttributes", "Property \"WIDTH\" cannot be set through this argument.");
  154. }
  155. [TestMethod]
  156. public void SilverlightDefaults() {
  157. string html = Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", "100px", "100px").ToString().Replace("\r\n", "");
  158. Assert.IsTrue(html.StartsWith(
  159. "<object data=\"data:application/x-silverlight-2,\" height=\"100px\" type=\"application/x-silverlight-2\" " +
  160. "width=\"100px\" >"
  161. ));
  162. Assert.IsTrue(html.Contains("<param name=\"source\" value=\"http://foo.bar.com/foo.xap\" />"));
  163. Assert.IsTrue(html.Contains(
  164. "<a href=\"http://go.microsoft.com/fwlink/?LinkID=149156\" style=\"text-decoration:none\">" +
  165. "<img src=\"http://go.microsoft.com/fwlink?LinkId=108181\" alt=\"Get Microsoft Silverlight\" " +
  166. "style=\"border-style:none\"/></a>"));
  167. Assert.IsTrue(html.EndsWith("</object>"));
  168. }
  169. [TestMethod]
  170. public void SilverlightThrowsWhenPathIsEmpty() {
  171. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  172. Video.Silverlight(GetContext(), _pathUtility, String.Empty, "100px", "100px");
  173. }, "path");
  174. }
  175. [TestMethod]
  176. public void SilverlightThrowsWhenPathIsNull() {
  177. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  178. Video.Silverlight(GetContext(), _pathUtility, null, "100px", "100px");
  179. }, "path");
  180. }
  181. [TestMethod]
  182. public void SilverlightThrowsWhenHeightIsEmpty() {
  183. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  184. Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", "100px", String.Empty);
  185. }, "height");
  186. }
  187. [TestMethod]
  188. public void SilverlightThrowsWhenHeightIsNull() {
  189. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  190. Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", "100px", null);
  191. }, "height");
  192. }
  193. [TestMethod]
  194. public void SilverlightThrowsWhenWidthIsEmpty() {
  195. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  196. Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", String.Empty, "100px");
  197. }, "width");
  198. }
  199. [TestMethod]
  200. public void SilverlightThrowsWhenWidthIsNull() {
  201. ExceptionAssert.ThrowsArgNullOrEmpty(() => {
  202. Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", null, "100px");
  203. }, "width");
  204. }
  205. [TestMethod]
  206. public void SilverlightWithExposedOptions() {
  207. string html = Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", width: "85%", height: "85%",
  208. bgColor: "red", initParameters: "X=Y", minimumVersion: "1.0.0.0", autoUpgrade: false,
  209. htmlAttributes: new { id = "sl" }).ToString().Replace("\r\n", "");
  210. Assert.IsTrue(html.StartsWith(
  211. "<object data=\"data:application/x-silverlight-2,\" height=\"85%\" id=\"sl\" " +
  212. "type=\"application/x-silverlight-2\" width=\"85%\" >"
  213. ));
  214. Assert.IsTrue(html.Contains("<param name=\"background\" value=\"red\" />"));
  215. Assert.IsTrue(html.Contains("<param name=\"initparams\" value=\"X=Y\" />"));
  216. Assert.IsTrue(html.Contains("<param name=\"minruntimeversion\" value=\"1.0.0.0\" />"));
  217. Assert.IsTrue(html.Contains("<param name=\"autoUpgrade\" value=\"False\" />"));
  218. var embed = new Regex("<embed.*/>").Match(html);
  219. Assert.IsFalse(embed.Success);
  220. }
  221. [TestMethod]
  222. public void SilverlightWithUnexposedOptions() {
  223. string html = Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", width: "50px", height: "50px",
  224. options: new { X = "Y", Z = 123 } ).ToString().Replace("\r\n", "");
  225. Assert.IsTrue(html.Contains("<param name=\"X\" value=\"Y\" />"));
  226. Assert.IsTrue(html.Contains("<param name=\"Z\" value=\"123\" />"));
  227. }
  228. [TestMethod]
  229. public void ValidatePathResolvesExistingLocalPath() {
  230. string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
  231. Mock<VirtualPathUtilityBase> pathUtility = new Mock<VirtualPathUtilityBase>();
  232. pathUtility.Setup(p => p.Combine(It.IsAny<string>(), It.IsAny<string>())).Returns(path);
  233. pathUtility.Setup(p => p.ToAbsolute(It.IsAny<string>())).Returns(path);
  234. Mock<HttpServerUtilityBase> serverMock = new Mock<HttpServerUtilityBase>();
  235. serverMock.Setup(s => s.MapPath(It.IsAny<string>())).Returns(path);
  236. HttpContextBase context = GetContext(serverMock.Object);
  237. string html = Video.Flash(context, pathUtility.Object, "foo.bar").ToString();
  238. Assert.IsTrue(html.StartsWith("<object"));
  239. Assert.IsTrue(html.Contains(HttpUtility.HtmlAttributeEncode(HttpUtility.UrlPathEncode(path))));
  240. }
  241. [TestMethod]
  242. public void ValidatePathThrowsForNonExistingLocalPath() {
  243. string path = "c:\\does\\not\\exist.swf";
  244. Mock<VirtualPathUtilityBase> pathUtility = new Mock<VirtualPathUtilityBase>();
  245. pathUtility.Setup(p => p.Combine(It.IsAny<string>(), It.IsAny<string>())).Returns(path);
  246. pathUtility.Setup(p => p.ToAbsolute(It.IsAny<string>())).Returns(path);
  247. Mock<HttpServerUtilityBase> serverMock = new Mock<HttpServerUtilityBase>();
  248. serverMock.Setup(s => s.MapPath(It.IsAny<string>())).Returns(path);
  249. HttpContextBase context = GetContext(serverMock.Object);
  250. ExceptionAssert.Throws<InvalidOperationException>(() => {
  251. Video.Flash(context, pathUtility.Object, "exist.swf");
  252. }, "The media file \"exist.swf\" does not exist.");
  253. }
  254. private static HttpContextBase GetContext(HttpServerUtilityBase serverUtility = null) {
  255. // simple mocked context - won't reference as long as path starts with 'http'
  256. Mock<HttpRequestBase> requestMock = new Mock<HttpRequestBase>();
  257. Mock<HttpContextBase> contextMock = new Mock<HttpContextBase>();
  258. contextMock.Setup(context => context.Request).Returns(requestMock.Object);
  259. contextMock.Setup(context => context.Server).Returns(serverUtility);
  260. return contextMock.Object;
  261. }
  262. }
  263. }