File: RazorViewTest.cs

package info (click to toggle)
mono 5.18.0.240%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,253,216 kB
  • sloc: cs: 10,925,936; xml: 2,804,987; ansic: 643,970; cpp: 120,384; perl: 59,272; asm: 21,383; sh: 20,162; makefile: 18,157; python: 4,715; pascal: 924; sql: 859; sed: 16; php: 1
file content (250 lines) | stat: -rw-r--r-- 11,195 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.WebPages;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Web.Mvc.Test
{
    public class RazorViewTest
    {
        [Fact]
        public void Constructor_RunViewStartPagesParam()
        {
            var context = new ControllerContext();
            Assert.True(new RazorView(context, "~/view", "~/master", runViewStartPages: true, viewStartFileExtensions: null).RunViewStartPages);
            Assert.False(new RazorView(context, "~/view", "~/master", runViewStartPages: false, viewStartFileExtensions: null).RunViewStartPages);
            Assert.True(new RazorView(context, "~/view", "~/master", runViewStartPages: true, viewStartFileExtensions: null, viewPageActivator: new Mock<IViewPageActivator>().Object).RunViewStartPages);
            Assert.False(new RazorView(context, "~/view", "~/master", runViewStartPages: false, viewStartFileExtensions: null, viewPageActivator: new Mock<IViewPageActivator>().Object).RunViewStartPages);
        }

        [Fact]
        public void ConstructorWithEmptyViewPathThrows()
        {
            // Act & Assert
            Assert.ThrowsArgumentNullOrEmpty(
                () => new RazorView(new ControllerContext(), String.Empty, "~/master", false, Enumerable.Empty<string>()),
                "viewPath"
                );
        }

        [Fact]
        public void ConstructorWithNullViewPathThrows()
        {
            // Act & Assert
            Assert.ThrowsArgumentNullOrEmpty(
                () => new RazorView(new ControllerContext(), null, "~/master", false, Enumerable.Empty<string>()),
                "viewPath"
                );
        }

        [Fact]
        public void ConstructorWithNullControllerContextThrows()
        {
            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => new RazorView(null, "view path", "~/master", false, Enumerable.Empty<string>()),
                "controllerContext"
                );
        }

        [Fact]
        public void LayoutPathProperty()
        {
            //Arrange
            ControllerContext controllerContext = new ControllerContext();

            // Act
            RazorView view = new RazorView(new ControllerContext(), "view path", "master path", false, Enumerable.Empty<string>());

            // Assert
            Assert.Equal("master path", view.LayoutPath);
        }

        [Fact]
        public void LayoutPathPropertyReturnsEmptyStringIfNullLayoutSpecified()
        {
            // Act
            RazorView view = new RazorView(new ControllerContext(), "view path", null, false, Enumerable.Empty<string>());

            // Assert
            Assert.Equal(String.Empty, view.LayoutPath);
        }

        [Fact]
        public void LayoutPathPropertyReturnsEmptyStringIfLayoutNotSpecified()
        {
            // Act
            RazorView view = new RazorView(new ControllerContext(), "view path", null, false, Enumerable.Empty<string>());

            // Assert
            Assert.Equal(String.Empty, view.LayoutPath);
        }

        [Fact]
        public void RenderWithNullWriterThrows()
        {
            // Arrange
            RazorView view = new RazorView(new ControllerContext(), "~/viewPath", null, false, Enumerable.Empty<string>());
            Mock<ViewContext> viewContextMock = new Mock<ViewContext>();

            MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object));
            view.BuildManager = buildManager;

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => view.Render(viewContextMock.Object, null),
                "writer"
                );
        }

        [Fact]
        public void RenderWithUnsupportedTypeThrows()
        {
            // Arrange
            ViewContext context = new Mock<ViewContext>().Object;
            MockBuildManager buildManagerMock = new MockBuildManager("view path", typeof(object));
            RazorView view = new RazorView(new ControllerContext(), "view path", null, false, Enumerable.Empty<string>());
            view.BuildManager = buildManagerMock;

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => view.Render(context, new Mock<TextWriter>().Object),
                "The view at 'view path' must derive from WebViewPage, or WebViewPage<TModel>."
                );
        }

        [Fact]
        public void RenderWithViewPageAndNoStartPageLookupRendersView()
        {
            // Arrange
            StubWebViewPage viewPage = new StubWebViewPage();
            Mock<ViewContext> viewContextMock = new Mock<ViewContext>();
            viewContextMock.Setup(vc => vc.HttpContext.Items).Returns(new Dictionary<object, object>());
            viewContextMock.Setup(vc => vc.HttpContext.Request.IsLocal).Returns(false);
            MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object));
            Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict);
            ControllerContext controllerContext = new ControllerContext();
            activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage);
            RazorView view = new RazorView(controllerContext, "~/viewPath", null, false, Enumerable.Empty<string>(), activator.Object);
            view.StartPageLookup = (WebPageRenderingBase p, string n, IEnumerable<string> e) =>
            {
                Assert.True(false, "ViewStart page lookup should not be called");
                return null;
            };
            view.BuildManager = buildManager;

            // Act
            view.Render(viewContextMock.Object, new Mock<TextWriter>().Object);

            // Assert
            Assert.Null(viewPage.Layout);
            Assert.Equal("", viewPage.OverridenLayoutPath);
            Assert.Same(viewContextMock.Object, viewPage.ViewContext);
            Assert.Equal("~/viewPath", viewPage.VirtualPath);
        }

        [Fact]
        public void RenderWithViewPageAndStartPageLookupExecutesStartPage()
        {
            // Arrange
            StubWebViewPage viewPage = new StubWebViewPage();
            Mock<ViewContext> viewContextMock = new Mock<ViewContext>();
            viewContextMock.Setup(vc => vc.HttpContext.Items).Returns(new Dictionary<object, object>());
            MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object));
            Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict);
            ControllerContext controllerContext = new ControllerContext();
            activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage);
            RazorView view = new RazorView(controllerContext, "~/viewPath", null, true, new[] { "cshtml" }, activator.Object);
            Mock<ViewStartPage> startPage = new Mock<ViewStartPage>();
            startPage.Setup(sp => sp.ExecutePageHierarchy()).Verifiable();
            view.StartPageLookup = (WebPageRenderingBase page, string fileName, IEnumerable<string> extensions) =>
            {
                Assert.Equal(viewPage, page);
                Assert.Equal("_ViewStart", fileName);
                Assert.Equal(new[] { "cshtml" }, extensions.ToArray());
                return startPage.Object;
            };
            view.BuildManager = buildManager;

            // Act
            view.Render(viewContextMock.Object, new Mock<TextWriter>().Object);

            // Assert
            startPage.Verify(sp => sp.ExecutePageHierarchy(), Times.Once());
        }

        // TODO: This throws in WebPages and needs to be tracked down.
        [Fact]
        public void RenderWithViewPageAndLayoutPageRendersView()
        {
            // Arrange
            StubWebViewPage viewPage = new StubWebViewPage();
            Mock<ViewContext> viewContext = new Mock<ViewContext>();
            Mock<HttpContextBase> httpContext = new Mock<HttpContextBase>();
            Mock<HttpRequestBase> httpRequest = new Mock<HttpRequestBase>();

            httpRequest.SetupGet(r => r.IsLocal).Returns(false);
            httpRequest.SetupGet(r => r.Browser.IsMobileDevice).Returns(false);
            httpRequest.SetupGet(r => r.Cookies).Returns(new HttpCookieCollection());

            httpContext.SetupGet(c => c.Request).Returns(httpRequest.Object);
            httpContext.SetupGet(c => c.Response.Cookies).Returns(new HttpCookieCollection());
            httpContext.SetupGet(c => c.Items).Returns(new Hashtable());

            viewContext.SetupGet(v => v.HttpContext).Returns(httpContext.Object);

            MockBuildManager buildManager = new MockBuildManager("~/viewPath", typeof(object));
            Mock<IViewPageActivator> activator = new Mock<IViewPageActivator>(MockBehavior.Strict);

            Mock<WebPage> layoutPage = new Mock<WebPage> { CallBase = true };
            layoutPage.Setup(c => c.Execute()).Callback(() => layoutPage.Object.RenderBody());
            Mock<IVirtualPathFactory> virtualPathFactory = new Mock<IVirtualPathFactory>(MockBehavior.Strict);
            virtualPathFactory.Setup(f => f.Exists("~/layoutPath")).Returns(true);
            virtualPathFactory.Setup(f => f.CreateInstance("~/layoutPath")).Returns(layoutPage.Object);
            ControllerContext controllerContext = new ControllerContext();
            activator.Setup(l => l.Create(controllerContext, typeof(object))).Returns(viewPage);
            RazorView view = new RazorView(controllerContext, "~/viewPath", "~/layoutPath", false, Enumerable.Empty<string>(), activator.Object);
            view.BuildManager = buildManager;
            view.VirtualPathFactory = virtualPathFactory.Object;
            view.DisplayModeProvider = DisplayModeProvider.Instance;

            // Act
            view.Render(viewContext.Object, TextWriter.Null);

            // Assert
            Assert.Equal("~/layoutPath", viewPage.Layout);
            Assert.Equal("~/layoutPath", viewPage.OverridenLayoutPath);
            Assert.Same(viewContext.Object, viewPage.ViewContext);
            Assert.Equal("~/viewPath", viewPage.VirtualPath);
        }

        public class StubWebViewPage : WebViewPage
        {
            public bool InitHelpersCalled;
            public string ResultLayoutPage;
            public string ResultOverridenLayoutPath;
            public ViewContext ResultViewContext;
            public string ResultVirtualPath;

            public override void Execute()
            {
                ResultLayoutPage = Layout;
                ResultOverridenLayoutPath = OverridenLayoutPath;
                ResultViewContext = ViewContext;
                ResultVirtualPath = VirtualPath;
            }

            public override void InitHelpers()
            {
                base.InitHelpers();
                InitHelpersCalled = true;
            }
        }
    }
}