SubtextMasterPageTests.cs :  » Bloggers » SubText » UnitTests » Subtext » SubtextWeb » UI » Pages » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » UnitTests » Subtext » SubtextWeb » UI » Pages » SubtextMasterPageTests.cs
using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.Routing;
using System.Web.UI;
using MbUnit.Framework;
using Moq;
using Subtext.Extensibility;
using Subtext.Framework;
using Subtext.Framework.Components;
using Subtext.Framework.Services.SearchEngine;
using Subtext.Framework.UI.Skinning;
using Subtext.Web.UI.Controls;
using Subtext.Web.UI.Pages;
using UnitTests.Subtext.Framework.Util;

namespace UnitTests.Subtext.SubtextWeb.UI.Pages{
    [TestFixture]
    public class SubtextMasterPageTests
    {
        [Test]
        public void AddControlToBody_WithComments_AddsControlToUpdatePanel()
        {
            // arrange
            var updatePanel = new UpdatePanel();
            var control = new UserControl {Visible = false};
            var bodyControl = new UserControl();
            var page = new SubtextMasterPage();

            // act
            page.AddControlToBody("Comments", control, updatePanel, bodyControl);
            
            // assert
            Assert.AreEqual(control, updatePanel.ContentTemplateContainer.Controls[0]);
            Assert.IsTrue(control.Visible);
        }

        [Test]
        public void AddControlToBody_WithPostComment_AddsControlToUpdatePanelAndUpdatePanelToCenterBodyControl()
        {
            // arrange
            var updatePanel = new UpdatePanel();
            var postCommentControl = new PostComment();
            var bodyControl = new UserControl();
            var page = new SubtextMasterPage();

            // act
            page.AddControlToBody("PostComment", postCommentControl, updatePanel, bodyControl);

            // assert
            Assert.AreEqual(postCommentControl, updatePanel.ContentTemplateContainer.Controls[0]);
            Assert.AreEqual(updatePanel, bodyControl.Controls[0]);
            Assert.IsTrue(postCommentControl.Visible);
        }

        [Test]
        public void AddControlToBody_WithOtherControl_AddsControlToBodyControl()
        {
            // arrange
            var updatePanel = new UpdatePanel();
            var control = new UserControl();
            var bodyControl = new UserControl();
            var page = new SubtextMasterPage();

            // act
            page.AddControlToBody("Other", control, updatePanel, bodyControl);

            // assert
            Assert.AreEqual(control, bodyControl.Controls[0]);
        }

        [RowTest]
        [Row("javascript", "scripts/test.js", "", "", @"<script type=""javascript"" src=""scripts/test.js""></script>")]
        [Row("javascript", "scripts/test.js", "", "/Subtext.Web/MyBlog/",
            @"<script type=""javascript"" src=""/Subtext.Web/MyBlog/scripts/test.js""></script>")]
        [Row("javascript", "~/scripts/test.js", "Subtext.Web", "/Anything/",
            @"<script type=""javascript"" src=""/Subtext.Web/scripts/test.js""></script>")]
        [Row("javascript", "~/scripts/test.js", "", "/Anything/",
            @"<script type=""javascript"" src=""/scripts/test.js""></script>")]
        [Row("javascript", "/scripts/test.js", "Subtext.Web", "/Anything/",
            @"<script type=""javascript"" src=""/scripts/test.js""></script>")]
        public void RenderScriptElementRendersAppropriatePath(string type, string src, string virtualDir,
                                                              string skinPath, string expected)
        {
            UnitTestHelper.SetHttpContextWithBlogRequest("localhost", "Anything", virtualDir);

            var script = new Script {Type = type, Src = src};

            string scriptTag = ScriptElementCollectionRenderer.RenderScriptElement(skinPath, script);
            Assert.AreEqual(scriptTag, expected + Environment.NewLine,
                            "The rendered script tag was not what we expected.");
        }

        [RowTest]
        [Row("style/test.css", "", "", @"style/test.css")]
        [Row("style/test.css", "", "/Subtext.Web/MyBlog/", @"/Subtext.Web/MyBlog/style/test.css")]
        [Row("~/style/test.css", "Subtext.Web", "/Anything/", @"/Subtext.Web/style/test.css")]
        [Row("~/style/test.css", "", "/Anything/", "/style/test.css")]
        [Row("/style/test.css", "Subtext.Web", "/Anything/", "/style/test.css")]
        [Row("http://haacked.com/style/test.css", "Subtext.Web", "/Anything/", "http://haacked.com/style/test.css")]
        public void GetStylesheetHrefPathRendersAppropriatePath(string src, string virtualDir, string skinPath,
                                                                string expected)
        {
            UnitTestHelper.SetHttpContextWithBlogRequest("localhost", "Anything", virtualDir);

            var style = new Style {Href = src};

            string stylePath = StyleSheetElementCollectionRenderer.GetStylesheetHrefPath(skinPath, style);
            Assert.AreEqual(stylePath, expected, "The rendered style path was not what we expected.");
        }
        
        [Test]
        public void InitializeControls_WithControlNames_AddsControlsToBody()
        {
            // arrange
            var page = new SubtextMasterPage();
            var context = new Mock<ISubtextContext>();
            page.SubtextContext = context.Object;
            context.Setup(c => c.HttpContext.Request.UrlReferrer).Returns((Uri)null);
            context.Setup(c => c.HttpContext.Request.IsLocal).Returns(false);
            page.SetControls(new[]{"Test"});
            var loader = new Mock<ISkinControlLoader>();
            loader.Setup(l => l.LoadControl("Test")).Returns(new UserControl());

            // act
            page.InitializeControls(loader.Object);

            // assert
            loader.Verify(l => l.LoadControl("Test"));
        }

        [Test]
        public void InitializeControls_WithReferrer_LoadsMoreResultsControl()
        {
            // arrange
            var page = new SubtextMasterPage();
            var context = new Mock<ISubtextContext>();
            context.Setup(c => c.Blog).Returns(new Blog {Id = 123});
            context.Setup(c => c.HttpContext.Request.UrlReferrer).Returns(new Uri("http://bing.com/?q=test"));
            context.Setup(c => c.HttpContext.Request.IsLocal).Returns(false);
            context.Setup(c => c.HttpContext.Request.Url).Returns(new Uri("http://example.com/"));
            


            var httpContext = new Mock<HttpContextBase>();
            httpContext.FakeRequest("~/archive/the-slug.aspx");

            var routeData = new RouteData();
            routeData.Values.Add("slug", "the-slug");

            context.SetupRequestContext(httpContext, routeData)
                .SetupBlog(new Blog { Id = 1, TimeZoneId = TimeZonesTest.PacificTimeZoneId /* pacific */})
                .Setup(c => c.Repository.GetEntry("the-slug", true, true)).Returns(new Entry(PostType.BlogPost) { Id = 123, EntryName = "the-slug", Title = "Testing 123" });

            page.SubtextContext = context.Object;
            page.SetControls(new[] { "Test" });
            var searchEngine = new Mock<ISearchEngineService>();
            searchEngine.Setup(s => s.Search(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>())).Returns(new[]{new SearchEngineResult()});
            page.SearchEngineService = searchEngine.Object;
            var loader = new Mock<ISkinControlLoader>();
            loader.Setup(l => l.LoadControl("MoreResults")).Returns(new UserControl());

            // act
            page.InitializeControls(loader.Object);

            // assert
            loader.Verify(l => l.LoadControl("Test"));
        }

        [Test]
        public void InitializeControls_WithReferrerButOnlyHomepageControl_DoesNotLoadsMoreResultsControl()
        {
            // arrange
            var page = new SubtextMasterPage();
            var context = new Mock<ISubtextContext>();
            page.SubtextContext = context.Object;
            context.Setup(c => c.HttpContext.Request.UrlReferrer).Returns(new Uri("http://bing.com/?q=test"));
            context.Setup(c => c.HttpContext.Request.IsLocal).Returns(false);
            page.SetControls(new[] { "HomePage" });
            var loader = new Mock<ISkinControlLoader>();
            loader.Setup(l => l.LoadControl("MoreResults")).Throws(new InvalidOperationException());

            // act, assert
            page.InitializeControls(loader.Object);
        }

        [Test]
        public void InitializeControls_WithReferrerButNoControls_DoesNotLoadMoreResultsControl()
        {
            // arrange
            var page = new SubtextMasterPage();
            var context = new Mock<ISubtextContext>();
            page.SubtextContext = context.Object;
            context.Setup(c => c.HttpContext.Request.UrlReferrer).Returns(new Uri("http://bing.com/?q=test"));
            context.Setup(c => c.HttpContext.Request.IsLocal).Returns(false);
            page.SetControls(null);
            var loader = new Mock<ISkinControlLoader>();
            loader.Setup(l => l.LoadControl("MoreResults")).Throws(new InvalidOperationException());

            // act, assert
            page.InitializeControls(loader.Object);
        }

        [Test]
        public void InitializeControls_WithLocalRequestAndReferrerInQueryString_LoadsMoreResultsControl()
        {
            // arrange
            var page = new SubtextMasterPage();
            var context = new Mock<ISubtextContext>();
            page.SubtextContext = context.Object;
            context.Setup(c => c.Blog).Returns(new Blog { Id = 123 });
            context.Setup(c => c.HttpContext.Request.UrlReferrer).Returns((Uri)null);
            context.Setup(c => c.HttpContext.Request.IsLocal).Returns(true);
            context.Setup(c => c.HttpContext.Request.Url).Returns(new Uri("http://example.com/"));
            var queryString = new NameValueCollection { { "referrer", "http://bing.com/?q=test" } };
            context.Setup(c => c.HttpContext.Request.QueryString).Returns(queryString);

            var httpContext = new Mock<HttpContextBase>();
            httpContext.FakeRequest("~/archive/the-slug.aspx");

            var routeData = new RouteData();
            routeData.Values.Add("slug", "the-slug");

            context.SetupRequestContext(httpContext, routeData)
                .SetupBlog(new Blog { Id = 1, TimeZoneId = TimeZonesTest.PacificTimeZoneId /* pacific */})
                .Setup(c => c.Repository.GetEntry("the-slug", true, true)).Returns(new Entry(PostType.BlogPost) { Id = 123, EntryName = "the-slug", Title = "Testing 123" });

            page.SetControls(new[] { "Test" });
            var loader = new Mock<ISkinControlLoader>();
            loader.Setup(l => l.LoadControl("MoreResults")).Returns(new UserControl());
            var searchEngine = new Mock<ISearchEngineService>();
            searchEngine.Setup(s => s.Search(It.IsAny<string>(), It.IsAny<int>(), It.IsAny<int>())).Returns(new[] { new SearchEngineResult() });
            page.SearchEngineService = searchEngine.Object;

            // act
            page.InitializeControls(loader.Object);

            // assert
            loader.Verify(l => l.LoadControl("Test"));
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.