// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System; using System.Reflection; using System.Text.RegularExpressions; using System.Web; using Moq; using Xunit; using Assert = Microsoft.TestCommon.AssertEx; namespace Microsoft.Web.Helpers.Test { public class VideoTest { private VirtualPathUtilityWrapper _pathUtility = new VirtualPathUtilityWrapper(); [Fact] public void FlashCannotOverrideHtmlAttributes() { 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."); } [Fact] public void FlashDefaults() { string html = Video.Flash(GetContext(), _pathUtility, "http://foo.bar.com/foo.swf").ToString().Replace("\r\n", ""); Assert.True(html.StartsWith( "" )); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.EndsWith("")); } [Fact] public void FlashThrowsWhenPathIsEmpty() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.Flash(GetContext(), _pathUtility, String.Empty); }, "path"); } [Fact] public void FlashThrowsWhenPathIsNull() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.Flash(GetContext(), _pathUtility, null); }, "path"); } [Fact] public void FlashWithExposedOptions() { string html = Video.Flash(GetContext(), _pathUtility, "http://foo.bar.com/foo.swf", width: "100px", height: "100px", play: false, loop: false, menu: false, backgroundColor: "#000", quality: "Q", scale: "S", windowMode: "WM", baseUrl: "http://foo.bar.com/", version: "1.0.0.0", htmlAttributes: new { id = "fl" }, embedName: "efl").ToString().Replace("\r\n", ""); Assert.True(html.StartsWith( "" )); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); var embed = new Regex("").Match(html); Assert.True(embed.Success); Assert.True(embed.Value.StartsWith("")); Assert.True(html.Contains("")); // note - can't guarantee order of optional params: Assert.True( html.Contains("") || html.Contains("") ); } [Fact] public void MediaPlayerCannotOverrideHtmlAttributes() { 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."); } [Fact] public void MediaPlayerDefaults() { string html = Video.MediaPlayer(GetContext(), _pathUtility, "http://foo.bar.com/foo.wmv").ToString().Replace("\r\n", ""); Assert.True(html.StartsWith( "" )); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.EndsWith("")); } [Fact] public void MediaPlayerThrowsWhenPathIsEmpty() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.MediaPlayer(GetContext(), _pathUtility, String.Empty); }, "path"); } [Fact] public void MediaPlayerThrowsWhenPathIsNull() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.MediaPlayer(GetContext(), _pathUtility, null); }, "path"); } [Fact] public void MediaPlayerWithExposedOptions() { string html = Video.MediaPlayer(GetContext(), _pathUtility, "http://foo.bar.com/foo.wmv", width: "100px", height: "100px", autoStart: false, playCount: 2, uiMode: "UIMODE", stretchToFit: true, enableContextMenu: false, mute: true, volume: 1, baseUrl: "http://foo.bar.com/", htmlAttributes: new { id = "mp" }, embedName: "emp").ToString().Replace("\r\n", ""); Assert.True(html.StartsWith( "" )); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); var embed = new Regex("").Match(html); Assert.True(embed.Success); Assert.True(embed.Value.StartsWith("")); Assert.True(html.Contains("")); Assert.True( html.Contains("") || html.Contains("") ); } [Fact] public void SilverlightCannotOverrideHtmlAttributes() { Assert.ThrowsArgument(() => { Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", "100px", "100px", htmlAttributes: new { WIDTH = "CanNotOverride" }); }, "htmlAttributes", "Property \"WIDTH\" cannot be set through this argument."); } [Fact] public void SilverlightDefaults() { string html = Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", "100px", "100px").ToString().Replace("\r\n", ""); Assert.True(html.StartsWith( "" )); Assert.True(html.Contains("")); Assert.True(html.Contains( "" + "\"Get")); Assert.True(html.EndsWith("")); } [Fact] public void SilverlightThrowsWhenPathIsEmpty() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.Silverlight(GetContext(), _pathUtility, String.Empty, "100px", "100px"); }, "path"); } [Fact] public void SilverlightThrowsWhenPathIsNull() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.Silverlight(GetContext(), _pathUtility, null, "100px", "100px"); }, "path"); } [Fact] public void SilverlightThrowsWhenHeightIsEmpty() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", "100px", String.Empty); }, "height"); } [Fact] public void SilverlightThrowsWhenHeightIsNull() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", "100px", null); }, "height"); } [Fact] public void SilverlightThrowsWhenWidthIsEmpty() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", String.Empty, "100px"); }, "width"); } [Fact] public void SilverlightThrowsWhenWidthIsNull() { Assert.ThrowsArgumentNullOrEmptyString(() => { Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", null, "100px"); }, "width"); } [Fact] public void SilverlightWithExposedOptions() { string html = Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", width: "85%", height: "85%", backgroundColor: "red", initParameters: "X=Y", minimumVersion: "1.0.0.0", autoUpgrade: false, htmlAttributes: new { id = "sl" }).ToString().Replace("\r\n", ""); Assert.True(html.StartsWith( "" )); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); Assert.True(html.Contains("")); var embed = new Regex("").Match(html); Assert.False(embed.Success); } [Fact] public void SilverlightWithUnexposedOptions() { string html = Video.Silverlight(GetContext(), _pathUtility, "http://foo.bar.com/foo.xap", width: "50px", height: "50px", options: new { X = "Y", Z = 123 }).ToString().Replace("\r\n", ""); Assert.True(html.Contains("")); Assert.True(html.Contains("")); } [Fact] public void ValidatePathResolvesExistingLocalPath() { string path = Assembly.GetExecutingAssembly().Location; Mock pathUtility = new Mock(); pathUtility.Setup(p => p.Combine(It.IsAny(), It.IsAny())).Returns(path); pathUtility.Setup(p => p.ToAbsolute(It.IsAny())).Returns(path); Mock serverMock = new Mock(); serverMock.Setup(s => s.MapPath(It.IsAny())).Returns(path); HttpContextBase context = GetContext(serverMock.Object); string html = Video.Flash(context, pathUtility.Object, "foo.bar").ToString(); Assert.True(html.StartsWith(" pathUtility = new Mock(); pathUtility.Setup(p => p.Combine(It.IsAny(), It.IsAny())).Returns(path); pathUtility.Setup(p => p.ToAbsolute(It.IsAny())).Returns(path); Mock serverMock = new Mock(); serverMock.Setup(s => s.MapPath(It.IsAny())).Returns(path); HttpContextBase context = GetContext(serverMock.Object); Assert.Throws(() => { Video.Flash(context, pathUtility.Object, "exist.swf"); }, "The media file \"exist.swf\" does not exist."); } private static HttpContextBase GetContext(HttpServerUtilityBase serverUtility = null) { // simple mocked context - won't reference as long as path starts with 'http' Mock requestMock = new Mock(); Mock contextMock = new Mock(); contextMock.Setup(context => context.Request).Returns(requestMock.Object); contextMock.Setup(context => context.Server).Returns(serverUtility); return contextMock.Object; } } }