PageRenderTime 1498ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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