File: LayoutTest.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (716 lines) | stat: -rw-r--r-- 28,590 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Globalization;
using System.Web.WebPages.Resources;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Web.WebPages.Test
{
    public class LayoutTest
    {
        [Fact]
        public void LayoutBasicTest()
        {
            var layoutPath = "~/Layout.cshtml";
            LayoutBasicTestInternal(layoutPath);
        }

        [Fact]
        public void RelativeLayoutPageTest()
        {
            var pagePath = "~/MyApp/index.cshtml";
            var layoutPath = "~/MyFiles/Layout.cshtml";
            var layoutPage = "../MyFiles/Layout.cshtml";
            LayoutBasicTestInternal(layoutPath, pagePath, layoutPage);
        }

        [Fact]
        public void AppRelativeLayoutPageTest()
        {
            var pagePath = "~/MyApp/index.cshtml";
            var layoutPath = "~/MyFiles/Layout.cshtml";
            var layoutPage = "~/MyFiles/Layout.cshtml";
            LayoutBasicTestInternal(layoutPath, pagePath, layoutPage);
        }

        [Fact]
        public void SourceFileWithLayoutPageTest()
        {
            // Arrange
            var pagePath = "~/MyApp/index.cshtml";
            var layoutPath = "~/MyFiles/Layout.cshtml";
            var layoutPage = "~/MyFiles/Layout.cshtml";
            var content = "hello world";
            var title = "MyPage";
            var page = CreatePageWithLayout(
                p =>
                {
                    p.PageData["Title"] = title;
                    p.WriteLiteral(content);
                },
                p =>
                {
                    p.WriteLiteral(p.PageData["Title"]);
                    p.Write(p.RenderBody());
                }, pagePath, layoutPath, layoutPage);
            var request = new Mock<HttpRequestBase>();
            request.SetupGet(c => c.Path).Returns("/myapp/index.cshtml");
            request.SetupGet(c => c.RawUrl).Returns("http://localhost:8080/index.cshtml");
            request.SetupGet(c => c.IsLocal).Returns(true);
            request.Setup(c => c.MapPath(It.IsAny<string>())).Returns<string>(c => c);
            request.Setup(c => c.Browser.IsMobileDevice).Returns(false);
            request.Setup(c => c.Cookies).Returns(new HttpCookieCollection());

            var result = Utils.RenderWebPage(page, request: request.Object);
            Assert.Equal(2, page.PageContext.SourceFiles.Count);
            Assert.True(page.PageContext.SourceFiles.Contains("~/MyApp/index.cshtml"));
            Assert.True(page.PageContext.SourceFiles.Contains("~/MyFiles/Layout.cshtml"));
        }

        private static void LayoutBasicTestInternal(string layoutPath, string pagePath = "~/index.cshtml", string layoutPage = "Layout.cshtml")
        {
            // The page ~/index.cshtml does the following:
            // PageData["Title"] = "MyPage";
            // Layout = "Layout.cshtml";
            // WriteLiteral("hello world");
            // 
            // The layout page ~/Layout.cshtml does the following:
            // WriteLiteral(Title);
            // RenderBody();
            //
            // Expected rendered result is "MyPagehello world"

            var content = "hello world";
            var title = "MyPage";
            var result = RenderPageWithLayout(
                p =>
                {
                    p.PageData["Title"] = title;
                    p.WriteLiteral(content);
                },
                p =>
                {
                    p.WriteLiteral(p.PageData["Title"]);
                    p.Write(p.RenderBody());
                },
                pagePath, layoutPath, layoutPage);

            Assert.Equal(title + content, result);
        }

        [Fact]
        public void LayoutNestedTest()
        {
            // Testing nested layout pages
            //
            // The page ~/index.cshtml does the following:
            // PageData["Title"] = "MyPage";
            // Layout = "Layout1.cshtml";
            // WriteLiteral("hello world");
            // 
            // The first layout page ~/Layout1.cshtml does the following:
            // Layout = "Layout2.cshtml";
            // WriteLiteral("<layout1>");
            // RenderBody();
            // WriteLiteral("</layout1>");
            //
            // The second layout page ~/Layout2.cshtml does the following:
            // WriteLiteral(Title);
            // WriteLiteral("<layout2>");
            // RenderBody();
            // WriteLiteral("</layout2>");
            //
            // Expected rendered result is "MyPage<layout2><layout1>hello world</layout1></layout2>"

            var layout2Path = "~/Layout2.cshtml";
            var layout2 = Utils.CreatePage(
                p =>
                {
                    p.WriteLiteral(p.PageData["Title"]);
                    p.WriteLiteral("<layout2>");
                    p.Write(p.RenderBody());
                    p.WriteLiteral("</layout2>");
                },
                layout2Path);

            var layout1Path = "~/Layout1.cshtml";
            var layout1 = Utils.CreatePage(
                p =>
                {
                    p.Layout = "Layout2.cshtml";
                    p.WriteLiteral("<layout1>");
                    p.Write(p.RenderBody());
                    p.WriteLiteral("</layout1>");
                },
                layout1Path);

            var page = Utils.CreatePage(
                p =>
                {
                    p.PageData["Title"] = "MyPage";
                    p.Layout = "Layout1.cshtml";
                    p.WriteLiteral("hello world");
                });

            Utils.AssignObjectFactoriesAndDisplayModeProvider(page, layout1, layout2);

            var result = Utils.RenderWebPage(page);
            Assert.Equal("MyPage<layout2><layout1>hello world</layout1></layout2>", result);
        }

        [Fact]
        public void LayoutSectionsTest()
        {
            // Testing nested layout pages with sections
            //
            // The page ~/index.cshtml does the following:
            // PageData["Title"] = "MyPage";
            // Layout = "Layout1.cshtml";
            // DefineSection("header1", () => {
            //     WriteLiteral("index header");
            // });
            // WriteLiteral("hello world");
            // DefineSection("footer1", () => {
            //     WriteLiteral("index footer");
            // });
            //
            // The first layout page ~/Layout1.cshtml does the following:
            // Layout = "Layout2.cshtml";
            // DefineSection("header2", () => {
            //     WriteLiteral("<layout1 header>");
            //     RenderSection("header1");
            //     WriteLiteral("</layout1 header>");
            // });
            // WriteLiteral("<layout1>");
            // RenderBody();
            // WriteLiteral("</layout1>");
            // DefineSection("footer2", () => {
            //     WriteLiteral("<layout1 footer>");
            //     RenderSection("header2");
            //     WriteLiteral("</layout1 footer>");
            // });
            //
            // The second layout page ~/Layout2.cshtml does the following:
            // WriteLiteral(Title);
            // WriteLiteral("\n<layout2 header>");
            // RenderSection("header2");
            // WriteLiteral("</layout2 header>\n");
            // WriteLiteral("<layout2>");
            // RenderBody();
            // WriteLiteral("</layout2>\n");
            // WriteLiteral("<layout2 footer>");
            // RenderSection("footer");
            // WriteLiteral("</layout2 footer>");
            //
            // Expected rendered result is:
            // MyPage
            // <layout2 header><layout1 header>index header</layout1 header></layout2 header>
            // <layout2><layout1>hello world</layout1></layout2>"
            // <layout2 footer><layout1 footer>index footer</layout1 footer></layout2 footer>

            var layout2Path = "~/Layout2.cshtml";
            var layout2 = Utils.CreatePage(
                p =>
                {
                    p.WriteLiteral(p.PageData["Title"]);
                    p.WriteLiteral("\r\n");
                    p.WriteLiteral("<layout2 header>");
                    p.Write(p.RenderSection("header2"));
                    p.WriteLiteral("</layout2 header>");
                    p.WriteLiteral("\r\n");

                    p.WriteLiteral("<layout2>");
                    p.Write(p.RenderBody());
                    p.WriteLiteral("</layout2>");
                    p.WriteLiteral("\r\n");

                    p.WriteLiteral("<layout2 footer>");
                    p.Write(p.RenderSection("footer2"));
                    p.WriteLiteral("</layout2 footer>");
                },
                layout2Path);

            var layout1Path = "~/Layout1.cshtml";
            var layout1 = Utils.CreatePage(
                p =>
                {
                    p.Layout = "Layout2.cshtml";
                    p.DefineSection("header2", () =>
                    {
                        p.WriteLiteral("<layout1 header>");
                        p.Write(p.RenderSection("header1"));
                        p.WriteLiteral("</layout1 header>");
                    });

                    p.WriteLiteral("<layout1>");
                    p.Write(p.RenderBody());
                    p.WriteLiteral("</layout1>");

                    p.DefineSection("footer2", () =>
                    {
                        p.WriteLiteral("<layout1 footer>");
                        p.Write(p.RenderSection("footer1"));
                        p.WriteLiteral("</layout1 footer>");
                    });
                },
                layout1Path);

            var page = Utils.CreatePage(
                p =>
                {
                    p.PageData["Title"] = "MyPage";
                    p.Layout = "Layout1.cshtml";
                    p.DefineSection("header1", () => { p.WriteLiteral("index header"); });
                    p.WriteLiteral("hello world");
                    p.DefineSection("footer1", () => { p.WriteLiteral("index footer"); });
                });

            Utils.AssignObjectFactoriesAndDisplayModeProvider(page, layout1, layout2);

            var result = Utils.RenderWebPage(page);
            var expected = @"MyPage
<layout2 header><layout1 header>index header</layout1 header></layout2 header>
<layout2><layout1>hello world</layout1></layout2>
<layout2 footer><layout1 footer>index footer</layout1 footer></layout2 footer>";
            Assert.Equal(expected, result);
        }

        [Fact]
        public void LayoutSectionsNestedNamesTest()
        {
            // Tests nested layout using the same section names at different levels.
            //
            // The page ~/index.cshtml does the following:
            // Layout = "Layout1.cshtml";
            // @section body {
            //     body in index
            // }
            //
            // The page ~/layout1.cshtml does the following:
            // Layout = "Layout2.cshtml";
            // @section body {
            //     body in layout1 
            //     @RenderSection("body")
            // }
            //
            // The page ~/layout2.cshtml does the following:
            // body in layout2
            // @RenderSection("body")
            //
            // Expected rendered result is:
            // body in layout2 body in layout1 body in index
            var layout2Path = "~/Layout2.cshtml";
            var layout2 = Utils.CreatePage(
                p =>
                {
                    p.WriteLiteral("body in layout2 ");
                    p.Write(p.RenderSection("body"));
                },
                layout2Path);
            var layout1Path = "~/Layout1.cshtml";
            var layout1 = Utils.CreatePage(
                p =>
                {
                    p.Layout = "Layout2.cshtml";
                    p.DefineSection("body", () =>
                    {
                        p.WriteLiteral("body in layout1 ");
                        p.Write(p.RenderSection("body"));
                    });
                },
                layout1Path);

            var page = Utils.CreatePage(
                p =>
                {
                    p.Layout = "Layout1.cshtml";
                    p.DefineSection("body", () => { p.WriteLiteral("body in index"); });
                });

            Utils.AssignObjectFactoriesAndDisplayModeProvider(page, layout1, layout2);

            var result = Utils.RenderWebPage(page);
            var expected = "body in layout2 body in layout1 body in index";
            Assert.Equal(expected, result);
        }

        [Fact]
        public void CaseInsensitiveSectionNamesTest()
        {
            var page = CreatePageWithLayout(
                p =>
                {
                    p.Write("123");
                    p.DefineSection("abc", () => { p.Write("abc"); });
                    p.DefineSection("XYZ", () => { p.Write("xyz"); });
                    p.Write("456");
                },
                p =>
                {
                    p.Write(p.RenderSection("AbC"));
                    p.Write(p.RenderSection("xyZ"));
                    p.Write(p.RenderBody());
                });
            var result = Utils.RenderWebPage(page);
            var expected = "abcxyz123456";
            Assert.Equal(expected, result);
        }

        [Fact]
        public void MissingLayoutPageTest()
        {
            var layoutPage = "Layout.cshtml";
            var page = Utils.CreatePage(
                p =>
                {
                    p.PageData["Title"] = "MyPage";
                    p.Layout = layoutPage;
                });
            var layoutPath1 = "~/Layout.cshtml";

            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_LayoutPageNotFound, layoutPage, layoutPath1));
        }

        [Fact]
        public void RenderBodyAlreadyCalledTest()
        {
            // Layout page calls RenderBody more than once.
            var page = CreatePageWithLayout(
                p => { },
                p =>
                {
                    p.Write(p.RenderBody());
                    p.Write(p.RenderBody());
                });

            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page), WebPageResources.WebPage_RenderBodyAlreadyCalled);
        }

        [Fact]
        public void RenderBodyNotCalledTest()
        {
            // Page does not define any sections, but layout page does not call RenderBody
            var layoutPath = "~/Layout.cshtml";
            var page = CreatePageWithLayout(
                p => { },
                p => { },
                layoutPath: layoutPath);

            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_RenderBodyNotCalled, layoutPath));
        }

        [Fact]
        public void RenderBodyCalledDirectlyTest()
        {
            // A Page that is not a layout page calls the RenderBody method
            var page = Utils.CreatePage(p => { p.RenderBody(); });
            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_CannotRequestDirectly, "~/index.cshtml", "RenderBody"));
        }

        [Fact]
        public void RenderSectionCalledDirectlyTest()
        {
            // A Page that is not a layout page calls the RenderBody method
            var page = Utils.CreatePage(p => { p.RenderSection(""); });
            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_CannotRequestDirectly, "~/index.cshtml", "RenderSection"));
        }

        [Fact]
        public void SectionAlreadyDefinedTest()
        {
            // The page calls DefineSection more than once on the same name
            var sectionName = "header";
            var page = Utils.CreatePage(p =>
            {
                p.Layout = "Layout.cshtml";
                p.DefineSection(sectionName, () => { });
                p.DefineSection(sectionName, () => { });
            });

            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.InvariantCulture, WebPageResources.WebPage_SectionAleadyDefined, sectionName));
        }

        [Fact]
        public void SectionAlreadyDefinedCaseInsensitiveTest()
        {
            // The page calls DefineSection more than once on the same name but with different casing
            var name1 = "section1";
            var name2 = "SecTion1";

            var page = Utils.CreatePage(p =>
            {
                p.Layout = "Layout.cshtml";
                p.DefineSection(name1, () => { });
                p.DefineSection(name2, () => { });
            });

            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.InvariantCulture, WebPageResources.WebPage_SectionAleadyDefined, name2));
        }

        [Fact]
        public void SectionNotDefinedTest()
        {
            // Layout page calls RenderSection on a name that has not been defined.
            var sectionName = "NoSuchSection";
            var page = CreatePageWithLayout(
                p => { },
                p => { p.Write(p.RenderSection(sectionName)); });

            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.InvariantCulture, WebPageResources.WebPage_SectionNotDefined, sectionName));
        }

        [Fact]
        public void SectionAlreadyRenderedTest()
        {
            // Layout page calls RenderSection on the same name more than once.
            var sectionName = "header";
            var page = CreatePageWithLayout(
                p =>
                {
                    p.Layout = "Layout.cshtml";
                    p.DefineSection(sectionName, () => { });
                },
                p =>
                {
                    p.Write(p.RenderSection(sectionName));
                    p.Write(p.RenderSection(sectionName));
                });

            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.InvariantCulture, WebPageResources.WebPage_SectionAleadyRendered, sectionName));
        }

        [Fact]
        public void SectionsNotRenderedTest()
        {
            // Layout page does not render all the defined sections.

            var layoutPath = "~/Layout.cshtml";
            var sectionName1 = "section1";
            var sectionName2 = "section2";
            var sectionName3 = "section3";
            var sectionName4 = "section4";
            var sectionName5 = "section5";
            // A dummy section action that does nothing
            SectionWriter sectionAction = () => { };

            // The page defines 5 sections.
            var page = CreatePageWithLayout(
                p =>
                {
                    p.DefineSection(sectionName1, sectionAction);
                    p.DefineSection(sectionName2, sectionAction);
                    p.DefineSection(sectionName3, sectionAction);
                    p.DefineSection(sectionName4, sectionAction);
                    p.DefineSection(sectionName5, sectionAction);
                },
                // The layout page renders only two of the sections
                p =>
                {
                    p.Write(p.RenderSection(sectionName2));
                    p.Write(p.RenderSection(sectionName4));
                },
                layoutPath: layoutPath);

            var sectionsNotRendered = "section1; section3; section5";
            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_SectionsNotRendered, layoutPath, sectionsNotRendered));
        }

        [Fact]
        public void SectionsNotRenderedRenderBodyTest()
        {
            // Layout page does not render all the defined sections, but it calls RenderBody.
            var layoutPath = "~/Layout.cshtml";
            var sectionName1 = "section1";
            var sectionName2 = "section2";
            // A dummy section action that does nothing
            SectionWriter sectionAction = () => { };

            var page = CreatePageWithLayout(
                p =>
                {
                    p.DefineSection(sectionName1, sectionAction);
                    p.DefineSection(sectionName2, sectionAction);
                },
                // The layout page only calls RenderBody
                p => { p.Write(p.RenderBody()); },
                layoutPath: layoutPath);

            var sectionsNotRendered = "section1; section2";
            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_SectionsNotRendered, layoutPath, sectionsNotRendered));
        }

        [Fact]
        public void InvalidPageTypeTest()
        {
            var layoutPath = "~/Layout.js";
            var contents = "hello world";
            var page = Utils.CreatePage(p =>
            {
                p.Layout = layoutPath;
                p.Write(contents);
            });
            var layoutPage = new object();

            var objectFactory = new Mock<IVirtualPathFactory>();
            objectFactory.Setup(c => c.Exists(It.IsAny<string>())).Returns<string>(p => layoutPath.Equals(p, StringComparison.OrdinalIgnoreCase));
            objectFactory.Setup(c => c.CreateInstance(It.IsAny<string>())).Returns<string>(_ => layoutPage as WebPageBase);
            page.VirtualPathFactory = objectFactory.Object;

            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_InvalidPageType, layoutPath));

            Assert.Throws<HttpException>(() => Utils.RenderWebPage(page),
                                                  String.Format(CultureInfo.CurrentCulture, WebPageResources.WebPage_InvalidPageType, layoutPath));
        }

        [Fact]
        public void ValidPageTypeTest()
        {
            var layoutPath = "~/Layout.js";
            var contents = "hello world";
            var page = Utils.CreatePage(p =>
            {
                p.Layout = layoutPath;
                p.Write(contents);
            });
            var layoutPage = Utils.CreatePage(p => p.WriteLiteral(p.RenderBody()), layoutPath);

            Utils.AssignObjectFactoriesAndDisplayModeProvider(page, layoutPage);

            Assert.Equal(contents, Utils.RenderWebPage(page));
        }

        [Fact]
        public void IsSectionDefinedTest()
        {
            // Tests for the IsSectionDefined method

            // Only sections named section1 and section3 are defined.
            var page = CreatePageWithLayout(
                p =>
                {
                    p.DefineSection("section1", () => { });
                    p.DefineSection("section3", () => { });
                },
                p =>
                {
                    p.Write(p.RenderSection("section1"));
                    p.Write(p.RenderSection("section3"));
                    p.Write("section1: " + p.IsSectionDefined("section1") + "; ");
                    p.Write("section2: " + p.IsSectionDefined("section2") + "; ");
                    p.Write("section3: " + p.IsSectionDefined("section3") + "; ");
                    p.Write("section4: " + p.IsSectionDefined("section4") + "; ");
                });
            var result = Utils.RenderWebPage(page);
            var expected = "section1: True; section2: False; section3: True; section4: False; ";
            Assert.Equal(expected, result);
        }

        [Fact]
        public void OptionalSectionsTest()
        {
            // Only sections named section1 and section3 are defined.
            var page = CreatePageWithLayout(
                p =>
                {
                    p.DefineSection("section1", () => { p.Write("section1 "); });
                    p.DefineSection("section3", () => { p.Write("section3"); });
                },
                p =>
                {
                    p.Write(p.RenderSection("section1", required: false));
                    p.Write(p.RenderSection("section2", required: false));
                    p.Write(p.RenderSection("section3", required: false));
                    p.Write(p.RenderSection("section4", required: false));
                });
            var result = Utils.RenderWebPage(page);
            var expected = "section1 section3";
            Assert.Equal(expected, result);
        }

        [Fact]
        public void PageDataTest()
        {
            // Layout page uses items in PageData set by content page
            var contents = "my contents";
            var page = CreatePageWithLayout(
                p =>
                {
                    p.PageData["contents"] = contents;
                    p.Write(" body");
                },
                p =>
                {
                    p.Write(p.PageData["contents"]);
                    p.Write(p.RenderBody());
                });
            var result = Utils.RenderWebPage(page);
            var expected = contents + " body";
            Assert.Equal(expected, result);
        }

        [Fact]
        public void RenderPageAndLayoutPage()
        {
            //Dev10 bug 928341 - a page that has a layout page, and the page calls RenderPage should not cause an error
            var layoutPagePath = "~/layout.cshtml";
            var page = Utils.CreatePage(p =>
            {
                p.DefineSection("foo", () => { p.Write("This is foo"); });
                p.Write(p.RenderPage("bar.cshtml"));
                p.Layout = layoutPagePath;
            });
            var layoutPage = Utils.CreatePage(p =>
            {
                p.Write(p.RenderBody());
                p.Write(" ");
                p.Write(p.RenderSection("foo"));
            }, layoutPagePath);

            var subPage = Utils.CreatePage(p => p.Write("This is bar"), "~/bar.cshtml");
            Utils.AssignObjectFactoriesAndDisplayModeProvider(page, layoutPage, subPage);

            var result = Utils.RenderWebPage(page);
            var expected = "This is bar This is foo";
            Assert.Equal(expected, result);
        }

        public static string RenderPageWithLayout(Action<WebPage> pageExecuteAction, Action<WebPage> layoutExecuteAction,
                                                  string pagePath = "~/index.cshtml", string layoutPath = "~/Layout.cshtml", string layoutPage = "Layout.cshtml")
        {
            var page = CreatePageWithLayout(pageExecuteAction, layoutExecuteAction, pagePath, layoutPath, layoutPage);
            return Utils.RenderWebPage(page);
        }

        public static MockPage CreatePageWithLayout(Action<WebPage> pageExecuteAction, Action<WebPage> layoutExecuteAction,
                                                    string pagePath = "~/index.cshtml", string layoutPath = "~/Layout.cshtml", string layoutPageName = "Layout.cshtml")
        {
            var page = Utils.CreatePage(
                p =>
                {
                    p.Layout = layoutPageName;
                    pageExecuteAction(p);
                },
                pagePath);
            var layoutPage = Utils.CreatePage(
                p => { layoutExecuteAction(p); },
                layoutPath);

            Utils.AssignObjectFactoriesAndDisplayModeProvider(layoutPage, page);

            return page;
        }
    }
}