From 89c686dae30af2d8866421da2b8a058fc7646941 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 17 Feb 2023 17:15:26 +0100 Subject: [PATCH 01/28] sections support --- .../Components/src/PublicAPI.Unshipped.txt | 15 ++++++- .../Components/src/Sections/SectionContent.cs | 2 +- .../Components/src/Sections/SectionOutlet.cs | 2 +- src/Components/Web/src/Head/HeadOutlet.cs | 4 +- .../test/E2ETest/Tests/SectionsTest.cs | 41 +++++++++++++++++++ .../BasicTestApp/BasicTestApp.csproj | 5 +-- .../test/testassets/BasicTestApp/Index.razor | 1 + .../BasicTestApp/SectionsTest/Counter.razor | 20 +++++++++ .../SectionsTest/ParentComponent.razor | 7 ++++ 9 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 src/Components/test/E2ETest/Tests/SectionsTest.cs create mode 100644 src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor create mode 100644 src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor diff --git a/src/Components/Components/src/PublicAPI.Unshipped.txt b/src/Components/Components/src/PublicAPI.Unshipped.txt index 34aa8535011c..1df4dfff861b 100644 --- a/src/Components/Components/src/PublicAPI.Unshipped.txt +++ b/src/Components/Components/src/PublicAPI.Unshipped.txt @@ -2,4 +2,17 @@ Microsoft.AspNetCore.Components.ComponentBase.DispatchExceptionAsync(System.Exception! exception) -> System.Threading.Tasks.Task! Microsoft.AspNetCore.Components.RenderHandle.DispatchExceptionAsync(System.Exception! exception) -> System.Threading.Tasks.Task! *REMOVED*Microsoft.AspNetCore.Components.NavigationManager.ToAbsoluteUri(string! relativeUri) -> System.Uri! -Microsoft.AspNetCore.Components.NavigationManager.ToAbsoluteUri(string? relativeUri) -> System.Uri! \ No newline at end of file +Microsoft.AspNetCore.Components.NavigationManager.ToAbsoluteUri(string? relativeUri) -> System.Uri! +Microsoft.AspNetCore.Components.Sections.SectionContent +Microsoft.AspNetCore.Components.Sections.SectionContent.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment? +Microsoft.AspNetCore.Components.Sections.SectionContent.ChildContent.set -> void +Microsoft.AspNetCore.Components.Sections.SectionContent.Dispose() -> void +Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.get -> bool +Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.set -> void +Microsoft.AspNetCore.Components.Sections.SectionContent.Name.get -> string! +Microsoft.AspNetCore.Components.Sections.SectionContent.Name.set -> void +Microsoft.AspNetCore.Components.Sections.SectionOutlet +Microsoft.AspNetCore.Components.Sections.SectionOutlet.Dispose() -> void +Microsoft.AspNetCore.Components.Sections.SectionOutlet.Name.get -> string! +Microsoft.AspNetCore.Components.Sections.SectionOutlet.Name.set -> void +Microsoft.AspNetCore.Components.Sections.SectionOutlet.SectionOutlet() -> void \ No newline at end of file diff --git a/src/Components/Components/src/Sections/SectionContent.cs b/src/Components/Components/src/Sections/SectionContent.cs index e8342597c59c..6f4385d240d6 100644 --- a/src/Components/Components/src/Sections/SectionContent.cs +++ b/src/Components/Components/src/Sections/SectionContent.cs @@ -6,7 +6,7 @@ namespace Microsoft.AspNetCore.Components.Sections; /// /// Provides content to components with matching s. /// -internal sealed class SectionContent : ISectionContentProvider, IComponent, IDisposable +public sealed class SectionContent : ISectionContentProvider, IComponent, IDisposable { private string? _registeredName; private SectionRegistry _registry = default!; diff --git a/src/Components/Components/src/Sections/SectionOutlet.cs b/src/Components/Components/src/Sections/SectionOutlet.cs index e9a7e2d06739..43b451f48c7e 100644 --- a/src/Components/Components/src/Sections/SectionOutlet.cs +++ b/src/Components/Components/src/Sections/SectionOutlet.cs @@ -6,7 +6,7 @@ namespace Microsoft.AspNetCore.Components.Sections; /// /// Renders content provided by components with matching s. /// -internal sealed class SectionOutlet : ISectionContentSubscriber, IComponent, IDisposable +public sealed class SectionOutlet : ISectionContentSubscriber, IComponent, IDisposable { private static readonly RenderFragment _emptyRenderFragment = _ => { }; diff --git a/src/Components/Web/src/Head/HeadOutlet.cs b/src/Components/Web/src/Head/HeadOutlet.cs index 472c3970bf94..62b25162b272 100644 --- a/src/Components/Web/src/Head/HeadOutlet.cs +++ b/src/Components/Web/src/Head/HeadOutlet.cs @@ -14,8 +14,8 @@ public sealed class HeadOutlet : ComponentBase { private const string GetAndRemoveExistingTitle = "Blazor._internal.PageTitle.getAndRemoveExistingTitle"; - internal const string HeadSectionOutletName = "head"; - internal const string TitleSectionOutletName = "title"; + internal const string HeadSectionOutletName = "blazor_head"; + internal const string TitleSectionOutletName = "blazor_title"; private string? _defaultTitle; diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs new file mode 100644 index 000000000000..14c59d016190 --- /dev/null +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -0,0 +1,41 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using BasicTestApp; +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; +using Microsoft.AspNetCore.E2ETesting; +using Xunit.Abstractions; +using Microsoft.AspNetCore.Components.E2ETest; +using OpenQA.Selenium; + +namespace Microsoft.AspNetCore.Components.E2ETests.Tests; +public class SectionsTest : ServerTestBase> +{ + private IWebElement _appElement; + + public SectionsTest + (BrowserFixture browserFixture, + ToggleExecutionModeServerFixture serverFixture, + ITestOutputHelper output) + : base(browserFixture, serverFixture, output) + { + } + + protected override void InitializeAsyncCore() + { + Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Client); + _appElement = Browser.MountTestComponent(); + } + + [Fact] + public void SectionOutletInParentComponentRendersSectionContentOfChildComponent() + { + var counter = _appElement.FindElement(By.Id("counter")); + Assert.Equal("0", counter.Text); + + var incrememntButton = _appElement.FindElement(By.Id("increment_button")); + incrememntButton.Click(); + Assert.Equal("1", counter.Text); + } +} diff --git a/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj b/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj index bc6052a3bcdf..4490ee8be6db 100644 --- a/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj +++ b/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj @@ -49,10 +49,7 @@ - + diff --git a/src/Components/test/testassets/BasicTestApp/Index.razor b/src/Components/test/testassets/BasicTestApp/Index.razor index f9c3814bd069..efbf78276cca 100644 --- a/src/Components/test/testassets/BasicTestApp/Index.razor +++ b/src/Components/test/testassets/BasicTestApp/Index.razor @@ -104,6 +104,7 @@ + @System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor new file mode 100644 index 000000000000..e3632c872f98 --- /dev/null +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor @@ -0,0 +1,20 @@ +@using Microsoft.AspNetCore.Components.Sections + +Counter + +

Counter

+ + +

@currentCount

+
+ + + +@code { + int currentCount = 0; + + void IncrementCount() + { + currentCount++; + } +} diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor new file mode 100644 index 000000000000..de34c7c3e8c3 --- /dev/null +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor @@ -0,0 +1,7 @@ +@using Microsoft.AspNetCore.Components.Sections + +

Parent Component

+ + + + From 7e99bde29eed30c038e8fbb249719e3ced78d48b Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 17 Feb 2023 18:14:15 +0100 Subject: [PATCH 02/28] change SecionsTest --- .../test/E2ETest/Tests/SectionsTest.cs | 41 ++++++++++++++++++- .../SectionsTest/ParentComponent.razor | 19 ++++++++- .../SectionsTest/SimpleComponent.razor | 6 +++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/Components/test/testassets/BasicTestApp/SectionsTest/SimpleComponent.razor diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index 14c59d016190..14fa979afbad 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -31,11 +31,48 @@ protected override void InitializeAsyncCore() [Fact] public void SectionOutletInParentComponentRendersSectionContentOfChildComponent() { - var counter = _appElement.FindElement(By.Id("counter")); - Assert.Equal("0", counter.Text); + //Nothing is chosen yet + Assert.False(TryGetElementById(out _, "counter")); + var options = _appElement.FindElement(By.Id("child-component")); + + // Choose Counter + options.FindElement(By.Name("counter")).Click(); + Assert.True(TryGetElementById(out var counter, "counter")); + Assert.Equal("0", counter.Text); var incrememntButton = _appElement.FindElement(By.Id("increment_button")); + incrememntButton.Click(); Assert.Equal("1", counter.Text); } + + [Fact] + public void SectionOutletInParentComponentRendersSectionContentOfAnotherChildComponent() + { + var options = _appElement.FindElement(By.Id("child-component")); + + // Choose Counter + options.FindElement(By.Name("counter")).Click(); + Assert.True(TryGetElementById(out _, "counter")); + + // Choose Simple Component + options.FindElement(By.Name("simple-component")).Click(); + Assert.True(TryGetElementById(out var simpleComponentText, "text")); + Assert.Equal("Hello!", simpleComponentText.Text); + Assert.False(TryGetElementById(out _, "counter")); + } + + private bool TryGetElementById(out IWebElement counter, string id) + { + try + { + counter = _appElement.FindElement(By.Id(id)); + return true; + } + catch (OpenQA.Selenium.NoSuchElementException) + { + counter = null; + return false; + } + } } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor index de34c7c3e8c3..58e3f9a3bf5e 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor @@ -4,4 +4,21 @@ - + + +@code { + string SelectedComponent { get; set; } = "none"; +} + +@if (SelectedComponent == "counter") +{ + +} +else if (SelectedComponent == "simple-component") +{ + +} diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/SimpleComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/SimpleComponent.razor new file mode 100644 index 000000000000..41f5d418d1ea --- /dev/null +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/SimpleComponent.razor @@ -0,0 +1,6 @@ +@using Microsoft.AspNetCore.Components.Sections +

SimpleComponent

+ + +

Hello!

+
From cbb63aa7638625639b22e4ef503e28180eda472c Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Fri, 17 Feb 2023 19:14:18 +0100 Subject: [PATCH 03/28] fix --- src/Components/Components/src/PublicAPI.Unshipped.txt | 1 + .../test/testassets/BasicTestApp/BasicTestApp.csproj | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Components/Components/src/PublicAPI.Unshipped.txt b/src/Components/Components/src/PublicAPI.Unshipped.txt index 1df4dfff861b..2db9631edcec 100644 --- a/src/Components/Components/src/PublicAPI.Unshipped.txt +++ b/src/Components/Components/src/PublicAPI.Unshipped.txt @@ -11,6 +11,7 @@ Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.get -> Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.set -> void Microsoft.AspNetCore.Components.Sections.SectionContent.Name.get -> string! Microsoft.AspNetCore.Components.Sections.SectionContent.Name.set -> void +Microsoft.AspNetCore.Components.Sections.SectionContent.SectionContent() -> void Microsoft.AspNetCore.Components.Sections.SectionOutlet Microsoft.AspNetCore.Components.Sections.SectionOutlet.Dispose() -> void Microsoft.AspNetCore.Components.Sections.SectionOutlet.Name.get -> string! diff --git a/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj b/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj index 4490ee8be6db..bc6052a3bcdf 100644 --- a/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj +++ b/src/Components/test/testassets/BasicTestApp/BasicTestApp.csproj @@ -49,7 +49,10 @@ - + From 397b91a2a1b3fa6fe469f42411e79145a8b99edc Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Mon, 20 Feb 2023 13:02:23 +0100 Subject: [PATCH 04/28] use Browser.Exists in tests; move code block to the end of .razor file --- .../test/E2ETest/Tests/SectionsTest.cs | 24 ++++--------------- .../SectionsTest/ParentComponent.razor | 8 +++---- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index 14fa979afbad..c9f1e86d82ce 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -32,12 +32,12 @@ protected override void InitializeAsyncCore() public void SectionOutletInParentComponentRendersSectionContentOfChildComponent() { //Nothing is chosen yet - Assert.False(TryGetElementById(out _, "counter")); + Browser.DoesNotExist(By.Id("counter")); var options = _appElement.FindElement(By.Id("child-component")); // Choose Counter options.FindElement(By.Name("counter")).Click(); - Assert.True(TryGetElementById(out var counter, "counter")); + var counter = Browser.Exists(By.Id("counter")); Assert.Equal("0", counter.Text); var incrememntButton = _appElement.FindElement(By.Id("increment_button")); @@ -53,26 +53,12 @@ public void SectionOutletInParentComponentRendersSectionContentOfAnotherChildCom // Choose Counter options.FindElement(By.Name("counter")).Click(); - Assert.True(TryGetElementById(out _, "counter")); + Browser.Exists(By.Id("counter")); // Choose Simple Component options.FindElement(By.Name("simple-component")).Click(); - Assert.True(TryGetElementById(out var simpleComponentText, "text")); + var simpleComponentText = Browser.Exists(By.Id("text")); Assert.Equal("Hello!", simpleComponentText.Text); - Assert.False(TryGetElementById(out _, "counter")); - } - - private bool TryGetElementById(out IWebElement counter, string id) - { - try - { - counter = _appElement.FindElement(By.Id(id)); - return true; - } - catch (OpenQA.Selenium.NoSuchElementException) - { - counter = null; - return false; - } + Browser.DoesNotExist(By.Id("counter")); } } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor index 58e3f9a3bf5e..93be12d39833 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor @@ -10,10 +10,6 @@ -@code { - string SelectedComponent { get; set; } = "none"; -} - @if (SelectedComponent == "counter") { @@ -22,3 +18,7 @@ else if (SelectedComponent == "simple-component") { } + +@code { + string SelectedComponent { get; set; } = "none"; +} From 0e7d9c5f0a74f73369cc25e1980d221931f80239 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Tue, 21 Feb 2023 18:05:51 +0100 Subject: [PATCH 05/28] E2E tests --- .../test/E2ETest/Tests/SectionsTest.cs | 149 ++++++++++++++++-- .../test/testassets/BasicTestApp/Index.razor | 2 +- .../BasicTestApp/SectionsTest/Counter.razor | 32 +++- .../SectionsTest/ParentComponent.razor | 24 --- .../ParentComponentWithTwoChildren.razor | 97 ++++++++++++ .../SectionsTest/SimpleComponent.razor | 6 - .../SectionsTest/TextComponent.razor | 29 ++++ 7 files changed, 285 insertions(+), 54 deletions(-) delete mode 100644 src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor create mode 100644 src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor delete mode 100644 src/Components/test/testassets/BasicTestApp/SectionsTest/SimpleComponent.razor create mode 100644 src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index c9f1e86d82ce..8b84886b7fdf 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -25,40 +25,155 @@ public SectionsTest protected override void InitializeAsyncCore() { Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Client); - _appElement = Browser.MountTestComponent(); + _appElement = Browser.MountTestComponent(); } [Fact] - public void SectionOutletInParentComponentRendersSectionContentOfChildComponent() + public void NoExistingSectionContents_SectionOutletsRenderNothing() { - //Nothing is chosen yet + // At the beginning no SectionContents are rendered Browser.DoesNotExist(By.Id("counter")); - var options = _appElement.FindElement(By.Id("child-component")); + Browser.DoesNotExist(By.Id("text")); + } - // Choose Counter - options.FindElement(By.Name("counter")).Click(); - var counter = Browser.Exists(By.Id("counter")); + [Fact] + public void RenderOneSectionContent_MatchingSectionOutletRendersContentSuccessfully() + { + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + var counter = Browser.Exists(By.Id("counter")); Assert.Equal("0", counter.Text); - var incrememntButton = _appElement.FindElement(By.Id("increment_button")); - incrememntButton.Click(); + _appElement.FindElement(By.Id("increment_button")).Click(); + Assert.Equal("1", counter.Text); } [Fact] - public void SectionOutletInParentComponentRendersSectionContentOfAnotherChildComponent() + public void RenderTwoSectionContentsWithSameName_LastRenderedOverridesSectionOutletContent() + { + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + + Browser.Exists(By.Id("counter")); + + _appElement.FindElement(By.Id("text_render_section_content")).Click(); + + Browser.Exists(By.Id("text")); + Browser.DoesNotExist(By.Id("counter")); + } + + [Fact] + public void SecondSectionContentGetsDisposed_SectionOutletRendersFirstSectionContent() { - var options = _appElement.FindElement(By.Id("child-component")); + //Render Counter and TextComponent SectionContents with same Name + // TextComponent's SectionContent overrides Counter SectionContent + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _appElement.FindElement(By.Id("text_render_section_content")).Click(); + + _appElement.FindElement(By.Id("text_dispose_section_content")).Click(); - // Choose Counter - options.FindElement(By.Name("counter")).Click(); Browser.Exists(By.Id("counter")); + } + + [Fact] + public void BothSectionContentsGetDisposed_SectionOutletsRenderNothing() + { + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _appElement.FindElement(By.Id("text_render_section_content")).Click(); + + _appElement.FindElement(By.Id("counter_dispose_section_content")).Click(); + _appElement.FindElement(By.Id("text_dispose_section_content")).Click(); + + Browser.DoesNotExist(By.Id("counter")); + Browser.DoesNotExist(By.Id("text")); + } + + [Fact] + public void SectionContentNameChanges_MatchingSectionOutletRendersContent() + { + // Render Counter and TextComponent SectionContents with same Name + // TextComponent's SectionContent overrides Counter SectionContent + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _appElement.FindElement(By.Id("text_render_section_content")).Click(); + + _appElement.FindElement(By.Id("counter_change_section_content_name")).Click(); + + Browser.Exists(By.Id("counter")); + } + + [Fact] + public void SectionContentNameChangesToNonExisting_NoMatchingSectionOutletResultingNoRendering() + { + // Render Counter and TextComponent SectionContents with same Name + // TextComponent's SectionContent overrides Counter SectionContent + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _appElement.FindElement(By.Id("text_render_section_content")).Click(); + + _appElement.FindElement(By.Id("counter_change_section_content_name_nonexisting")).Click(); - // Choose Simple Component - options.FindElement(By.Name("simple-component")).Click(); - var simpleComponentText = Browser.Exists(By.Id("text")); - Assert.Equal("Hello!", simpleComponentText.Text); Browser.DoesNotExist(By.Id("counter")); } + + [Fact] + public void SectionOutletGetsDisposed_NoContentsRendered() + { + // Render Counter and TextComponent SectionContents with same Name + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _appElement.FindElement(By.Id("text_render_section_content")).Click(); + + // TextComponent's SectionContent overrides Counter SectionContent + Browser.Exists(By.Id("text")); + + _appElement.FindElement(By.Id("section_outlet_dispose")).Click(); + + Browser.DoesNotExist(By.Id("counter")); + Browser.DoesNotExist(By.Id("text")); + } + + [Fact] + public void DefaultSectionContent_DoesNotOverrideAnotherSectionContent() + { + _appElement.FindElement(By.Id("text_section_content_make_default")).Click(); + + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _appElement.FindElement(By.Id("text_render_section_content")).Click(); + + // TextComponent's SectionContent IsDefaultContent=true does not override Counter's SectionContent + Browser.DoesNotExist(By.Id("text")); + Browser.Exists(By.Id("counter")); + } + + [Fact] + public void BothDefaultSectionContents_() + { + //TODO + } + + [Fact] + public void DefaultSectionContent_RendersWhenAnotherSectionContentGetsDisposed() + { + _appElement.FindElement(By.Id("text_section_content_make_default")).Click(); + + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _appElement.FindElement(By.Id("text_render_section_content")).Click(); + + _appElement.FindElement(By.Id("counter_dispose_section_content")).Click(); + + Browser.DoesNotExist(By.Id("counter")); + Browser.Exists(By.Id("text")); + } + + // TODO support changing IsDefaultContent + [Fact] + public void IsDefaultContentChanges_DoesNotOverrideAnotherSectionContent() + { + _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _appElement.FindElement(By.Id("text_render_section_content")).Click(); + + _appElement.FindElement(By.Id("text_section_content_make_default")).Click(); + + // TextComponent's SectionContent IsDefaultContent=true does not override Counter's SectionContent + Browser.DoesNotExist(By.Id("text")); + Browser.Exists(By.Id("counter")); + } } diff --git a/src/Components/test/testassets/BasicTestApp/Index.razor b/src/Components/test/testassets/BasicTestApp/Index.razor index efbf78276cca..387c478de4b1 100644 --- a/src/Components/test/testassets/BasicTestApp/Index.razor +++ b/src/Components/test/testassets/BasicTestApp/Index.razor @@ -104,7 +104,7 @@ - + @System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor index e3632c872f98..41be2fd26d75 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor @@ -1,16 +1,36 @@ @using Microsoft.AspNetCore.Components.Sections -Counter +
+ Counter -

Counter

+

Counter

- -

@currentCount

-
+

Counter SectionContent was rendered = @SectionContentExists.ToString()

- +

Counter SectionContent Name = @SectionName

+ +

Counter IsDefaultContent = @IsDefaultContent.ToString()

+ + @if (SectionContentExists) + { + +

@currentCount

+
+ } + + +
@code { + [Parameter] + public bool SectionContentExists { get; set; } + + [Parameter] + public bool IsDefaultContent { get; set; } + + [Parameter] + public string SectionName { get; set; } + int currentCount = 0; void IncrementCount() diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor deleted file mode 100644 index 93be12d39833..000000000000 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponent.razor +++ /dev/null @@ -1,24 +0,0 @@ -@using Microsoft.AspNetCore.Components.Sections - -

Parent Component

- - - - - -@if (SelectedComponent == "counter") -{ - -} -else if (SelectedComponent == "simple-component") -{ - -} - -@code { - string SelectedComponent { get; set; } = "none"; -} diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor new file mode 100644 index 000000000000..38d6901c9db3 --- /dev/null +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor @@ -0,0 +1,97 @@ +@using Microsoft.AspNetCore.Components.Sections + +

Parent Component

+ +@if (SectionOutletExists) +{ + +} + +

Text between two section outlets

+ + + + + + + + +
+ +
+ +
+ +
+ + +
+ + + + + + +
+ +
+ + + + + +@code { + private const string firstSectionName = "test1"; + private const string secondSectionName = "test2"; + + [Parameter] + public bool CounterSectionContentExists { get; set; } = false; + [Parameter] + public bool TextComponentSectionContentExists { get; set; } = false; + + [Parameter] + public string CounterSectionContentName { get; set; } = firstSectionName; + [Parameter] + public string TextComponentSectionContentName { get; set; } = firstSectionName; + + [Parameter] + public bool CounterIsDefaultContent { get; set; } = false; + [Parameter] + public bool TextComponentIsDefaultContent { get; set; } = false; + + [Parameter] + public bool SectionOutletExists { get; set; } = true; +} diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/SimpleComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/SimpleComponent.razor deleted file mode 100644 index 41f5d418d1ea..000000000000 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/SimpleComponent.razor +++ /dev/null @@ -1,6 +0,0 @@ -@using Microsoft.AspNetCore.Components.Sections -

SimpleComponent

- - -

Hello!

-
diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor new file mode 100644 index 000000000000..86b3c8d57bd2 --- /dev/null +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor @@ -0,0 +1,29 @@ +@using Microsoft.AspNetCore.Components.Sections + +
+

Text Component

+ +

TextComponent SectionContent was rendered = @SectionContentExists.ToString()

+ +

TextComponent SectionContent Name @SectionName

+ +

TextComponent SectionContent IsDefaultContent = @IsDefaultContent.ToString()

+ + @if (SectionContentExists) + { + +

Hello!

+
+ } +
+ +@code { + [Parameter] + public bool SectionContentExists { get; set; } + + [Parameter] + public bool IsDefaultContent { get; set; } + + [Parameter] + public string SectionName { get; set; } +} From 269b8d4b1592510b37080a84d39e410d750824d8 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Tue, 21 Feb 2023 21:36:46 +0100 Subject: [PATCH 06/28] use private fields for buttons in tests; use "-" instead of "_" fir ids --- .../test/E2ETest/Tests/SectionsTest.cs | 87 ++++++++++++------- .../BasicTestApp/SectionsTest/Counter.razor | 2 +- .../ParentComponentWithTwoChildren.razor | 18 ++-- 3 files changed, 67 insertions(+), 40 deletions(-) diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index 8b84886b7fdf..27dd36156b15 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -10,10 +10,24 @@ using OpenQA.Selenium; namespace Microsoft.AspNetCore.Components.E2ETests.Tests; + public class SectionsTest : ServerTestBase> { private IWebElement _appElement; + private IWebElement _renderCounterSectionContent; + private IWebElement _renderTextSectionContent; + + private IWebElement _disposeCounterSectionContent; + private IWebElement _disposeTextSectionContent; + + private IWebElement _changeCounterSectionContentName; + private IWebElement _changeCounterSectionContentNameToNonExisting; + + private IWebElement _makeTextSectionContentDefault; + + private IWebElement _disposeSectionOutlet; + public SectionsTest (BrowserFixture browserFixture, ToggleExecutionModeServerFixture serverFixture, @@ -26,6 +40,19 @@ protected override void InitializeAsyncCore() { Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Client); _appElement = Browser.MountTestComponent(); + + _renderCounterSectionContent = _appElement.FindElement(By.Id("counter-render-section-content")); + _renderTextSectionContent = _appElement.FindElement(By.Id("text-render-section-content")); + + _disposeCounterSectionContent = _appElement.FindElement(By.Id("counter-dispose-section-content")); + _disposeTextSectionContent = _appElement.FindElement(By.Id("text-dispose-section-content")); + + _changeCounterSectionContentName = _appElement.FindElement(By.Id("counter-change-section-content-name")); + _changeCounterSectionContentNameToNonExisting = _appElement.FindElement(By.Id("counter-change-section-content-name-nonexisting")); + + _makeTextSectionContentDefault = _appElement.FindElement(By.Id("text-section-content-make-default")); + + _disposeSectionOutlet = _appElement.FindElement(By.Id("section-outlet-dispose")); } [Fact] @@ -39,12 +66,12 @@ public void NoExistingSectionContents_SectionOutletsRenderNothing() [Fact] public void RenderOneSectionContent_MatchingSectionOutletRendersContentSuccessfully() { - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _renderCounterSectionContent.Click(); var counter = Browser.Exists(By.Id("counter")); Assert.Equal("0", counter.Text); - _appElement.FindElement(By.Id("increment_button")).Click(); + _appElement.FindElement(By.Id("increment-button")).Click(); Assert.Equal("1", counter.Text); } @@ -52,11 +79,11 @@ public void RenderOneSectionContent_MatchingSectionOutletRendersContentSuccessfu [Fact] public void RenderTwoSectionContentsWithSameName_LastRenderedOverridesSectionOutletContent() { - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); + _renderCounterSectionContent.Click(); Browser.Exists(By.Id("counter")); - _appElement.FindElement(By.Id("text_render_section_content")).Click(); + _renderTextSectionContent.Click(); Browser.Exists(By.Id("text")); Browser.DoesNotExist(By.Id("counter")); @@ -67,10 +94,10 @@ public void SecondSectionContentGetsDisposed_SectionOutletRendersFirstSectionCon { //Render Counter and TextComponent SectionContents with same Name // TextComponent's SectionContent overrides Counter SectionContent - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); - _appElement.FindElement(By.Id("text_render_section_content")).Click(); + _renderCounterSectionContent.Click(); + _renderTextSectionContent.Click(); - _appElement.FindElement(By.Id("text_dispose_section_content")).Click(); + _disposeTextSectionContent.Click(); Browser.Exists(By.Id("counter")); } @@ -78,11 +105,11 @@ public void SecondSectionContentGetsDisposed_SectionOutletRendersFirstSectionCon [Fact] public void BothSectionContentsGetDisposed_SectionOutletsRenderNothing() { - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); - _appElement.FindElement(By.Id("text_render_section_content")).Click(); + _renderCounterSectionContent.Click(); + _renderTextSectionContent.Click(); - _appElement.FindElement(By.Id("counter_dispose_section_content")).Click(); - _appElement.FindElement(By.Id("text_dispose_section_content")).Click(); + _disposeCounterSectionContent.Click(); + _disposeTextSectionContent.Click(); Browser.DoesNotExist(By.Id("counter")); Browser.DoesNotExist(By.Id("text")); @@ -93,10 +120,10 @@ public void SectionContentNameChanges_MatchingSectionOutletRendersContent() { // Render Counter and TextComponent SectionContents with same Name // TextComponent's SectionContent overrides Counter SectionContent - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); - _appElement.FindElement(By.Id("text_render_section_content")).Click(); + _renderCounterSectionContent.Click(); + _renderTextSectionContent.Click(); - _appElement.FindElement(By.Id("counter_change_section_content_name")).Click(); + _changeCounterSectionContentName.Click(); Browser.Exists(By.Id("counter")); } @@ -106,10 +133,10 @@ public void SectionContentNameChangesToNonExisting_NoMatchingSectionOutletResult { // Render Counter and TextComponent SectionContents with same Name // TextComponent's SectionContent overrides Counter SectionContent - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); - _appElement.FindElement(By.Id("text_render_section_content")).Click(); + _renderCounterSectionContent.Click(); + _renderTextSectionContent.Click(); - _appElement.FindElement(By.Id("counter_change_section_content_name_nonexisting")).Click(); + _changeCounterSectionContentNameToNonExisting.Click(); Browser.DoesNotExist(By.Id("counter")); } @@ -118,13 +145,13 @@ public void SectionContentNameChangesToNonExisting_NoMatchingSectionOutletResult public void SectionOutletGetsDisposed_NoContentsRendered() { // Render Counter and TextComponent SectionContents with same Name - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); - _appElement.FindElement(By.Id("text_render_section_content")).Click(); + _renderCounterSectionContent.Click(); + _renderTextSectionContent.Click(); // TextComponent's SectionContent overrides Counter SectionContent Browser.Exists(By.Id("text")); - _appElement.FindElement(By.Id("section_outlet_dispose")).Click(); + _disposeSectionOutlet.Click(); Browser.DoesNotExist(By.Id("counter")); Browser.DoesNotExist(By.Id("text")); @@ -133,10 +160,10 @@ public void SectionOutletGetsDisposed_NoContentsRendered() [Fact] public void DefaultSectionContent_DoesNotOverrideAnotherSectionContent() { - _appElement.FindElement(By.Id("text_section_content_make_default")).Click(); + _makeTextSectionContentDefault.Click(); - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); - _appElement.FindElement(By.Id("text_render_section_content")).Click(); + _renderCounterSectionContent.Click(); + _renderTextSectionContent.Click(); // TextComponent's SectionContent IsDefaultContent=true does not override Counter's SectionContent Browser.DoesNotExist(By.Id("text")); @@ -152,12 +179,12 @@ public void BothDefaultSectionContents_() [Fact] public void DefaultSectionContent_RendersWhenAnotherSectionContentGetsDisposed() { - _appElement.FindElement(By.Id("text_section_content_make_default")).Click(); + _makeTextSectionContentDefault.Click(); - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); - _appElement.FindElement(By.Id("text_render_section_content")).Click(); + _renderCounterSectionContent.Click(); + _renderTextSectionContent.Click(); - _appElement.FindElement(By.Id("counter_dispose_section_content")).Click(); + _disposeCounterSectionContent.Click(); Browser.DoesNotExist(By.Id("counter")); Browser.Exists(By.Id("text")); @@ -167,10 +194,10 @@ public void DefaultSectionContent_RendersWhenAnotherSectionContentGetsDisposed() [Fact] public void IsDefaultContentChanges_DoesNotOverrideAnotherSectionContent() { - _appElement.FindElement(By.Id("counter_render_section_content")).Click(); - _appElement.FindElement(By.Id("text_render_section_content")).Click(); + _renderCounterSectionContent.Click(); + _renderTextSectionContent.Click(); - _appElement.FindElement(By.Id("text_section_content_make_default")).Click(); + _makeTextSectionContentDefault.Click(); // TextComponent's SectionContent IsDefaultContent=true does not override Counter's SectionContent Browser.DoesNotExist(By.Id("text")); diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor index 41be2fd26d75..c71f686e8588 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor @@ -18,7 +18,7 @@ } - + @code { diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor index 38d6901c9db3..a6e6cffc576e 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor @@ -11,7 +11,7 @@ - @@ -24,28 +24,28 @@
-
-
-
-
- @@ -56,17 +56,17 @@ IsDefaultContent="@TextComponentIsDefaultContent"> -
-
- From 85f3a7c3993c408bb2e2a584c72b515b116fdd4c Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Wed, 22 Feb 2023 15:07:40 +0100 Subject: [PATCH 07/28] support changing IsDefaultContent; fix tests --- .../Components/src/Sections/SectionContent.cs | 4 +- .../test/E2ETest/Tests/SectionsTest.cs | 57 ++++++++++++++----- .../ParentComponentWithTwoChildren.razor | 19 +++---- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/Components/Components/src/Sections/SectionContent.cs b/src/Components/Components/src/Sections/SectionContent.cs index 6f4385d240d6..4ef7eb904f13 100644 --- a/src/Components/Components/src/Sections/SectionContent.cs +++ b/src/Components/Components/src/Sections/SectionContent.cs @@ -9,6 +9,7 @@ namespace Microsoft.AspNetCore.Components.Sections; public sealed class SectionContent : ISectionContentProvider, IComponent, IDisposable { private string? _registeredName; + private bool? _registeredIsDefaultContent; private SectionRegistry _registry = default!; /// @@ -44,7 +45,7 @@ Task IComponent.SetParametersAsync(ParameterView parameters) throw new InvalidOperationException($"{GetType()} requires a non-empty string parameter '{nameof(Name)}'."); } - if (Name != _registeredName) + if (Name != _registeredName || IsDefaultContent != _registeredIsDefaultContent) { if (_registeredName is not null) { @@ -53,6 +54,7 @@ Task IComponent.SetParametersAsync(ParameterView parameters) _registry.AddProvider(Name, this, IsDefaultContent); _registeredName = Name; + _registeredIsDefaultContent = IsDefaultContent; } _registry.NotifyContentChanged(Name, this); diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index 27dd36156b15..fca268fdd06f 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -24,7 +24,9 @@ public class SectionsTest : ServerTestBase Change Counter SectionContent Name to non sexisting - -
+

+ + Make TextComponent's SectionContent Default - - - +
@code { private const string firstSectionName = "test1"; private const string secondSectionName = "test2"; - [Parameter] public bool CounterSectionContentExists { get; set; } = false; - [Parameter] public bool TextComponentSectionContentExists { get; set; } = false; - [Parameter] public string CounterSectionContentName { get; set; } = firstSectionName; - [Parameter] public string TextComponentSectionContentName { get; set; } = firstSectionName; - [Parameter] public bool CounterIsDefaultContent { get; set; } = false; - [Parameter] public bool TextComponentIsDefaultContent { get; set; } = false; - [Parameter] public bool SectionOutletExists { get; set; } = true; } From ebca8978818d4222fb38900526b8cceb9700dfd7 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 23 Feb 2023 10:12:34 +0100 Subject: [PATCH 08/28] fix --- .../test/E2ETest/Tests/SectionsTest.cs | 106 +++++++----------- .../ParentComponentWithTwoChildren.razor | 24 ++-- 2 files changed, 50 insertions(+), 80 deletions(-) diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index fca268fdd06f..ffe5061c8b40 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -15,21 +15,6 @@ public class SectionsTest : ServerTestBase serverFixture, @@ -42,21 +27,6 @@ protected override void InitializeAsyncCore() { Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Client); _appElement = Browser.MountTestComponent(); - - _renderCounterSectionContent = _appElement.FindElement(By.Id("counter-render-section-content")); - _renderTextSectionContent = _appElement.FindElement(By.Id("text-render-section-content")); - - _disposeCounterSectionContent = _appElement.FindElement(By.Id("counter-dispose-section-content")); - _disposeTextSectionContent = _appElement.FindElement(By.Id("text-dispose-section-content")); - - _changeCounterSectionContentName = _appElement.FindElement(By.Id("counter-change-section-content-name")); - _changeCounterSectionContentNameToNonExisting = _appElement.FindElement(By.Id("counter-change-section-content-name-nonexisting")); - - _makeCounterSectionContentDefault = _appElement.FindElement(By.Id("counter-section-content-make-default")); - _makeTextSectionContentDefault = _appElement.FindElement(By.Id("text-section-content-make-default")); - _makeCounterSectionContentNonDefault = _appElement.FindElement(By.Id("counter-section-content-make-non-default")); - - _disposeSectionOutlet = _appElement.FindElement(By.Id("section-outlet-dispose")); } [Fact] @@ -70,7 +40,7 @@ public void NoExistingSectionContents_SectionOutletsRenderNothing() [Fact] public void RenderOneSectionContent_MatchingSectionOutletRendersContentSuccessfully() { - _renderCounterSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); var counter = Browser.Exists(By.Id("counter")); Assert.Equal("0", counter.Text); @@ -83,11 +53,11 @@ public void RenderOneSectionContent_MatchingSectionOutletRendersContentSuccessfu [Fact] public void RenderTwoSectionContentsWithSameName_LastRenderedOverridesSectionOutletContent() { - _renderCounterSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); Browser.Exists(By.Id("counter")); - _renderTextSectionContent.Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); Browser.Exists(By.Id("text")); Browser.DoesNotExist(By.Id("counter")); @@ -98,10 +68,10 @@ public void SecondSectionContentGetsDisposed_SectionOutletRendersFirstSectionCon { //Render Counter and TextComponent SectionContents with same Name // TextComponent SectionContent overrides Counter SectionContent - _renderCounterSectionContent.Click(); - _renderTextSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _disposeTextSectionContent.Click(); + _appElement.FindElement(By.Id("text-dispose-section-content")).Click(); Browser.Exists(By.Id("counter")); } @@ -109,11 +79,11 @@ public void SecondSectionContentGetsDisposed_SectionOutletRendersFirstSectionCon [Fact] public void BothSectionContentsGetDisposed_SectionOutletsRenderNothing() { - _renderCounterSectionContent.Click(); - _renderTextSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _disposeCounterSectionContent.Click(); - _disposeTextSectionContent.Click(); + _appElement.FindElement(By.Id("counter-dispose-section-content")).Click(); + _appElement.FindElement(By.Id("text-dispose-section-content")).Click(); Browser.DoesNotExist(By.Id("counter")); Browser.DoesNotExist(By.Id("text")); @@ -124,10 +94,10 @@ public void SectionContentNameChanges_MatchingSectionOutletRendersContent() { // Render Counter and TextComponent SectionContents with same Name // TextComponent SectionContent overrides Counter SectionContent - _renderCounterSectionContent.Click(); - _renderTextSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _changeCounterSectionContentName.Click(); + _appElement.FindElement(By.Id("counter-change-section-content-name")).Click(); Browser.Exists(By.Id("counter")); } @@ -137,10 +107,10 @@ public void SectionContentNameChangesToNonExisting_NoMatchingSectionOutletResult { // Render Counter and TextComponent SectionContents with same Name // TextComponent SectionContent overrides Counter SectionContent - _renderCounterSectionContent.Click(); - _renderTextSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _changeCounterSectionContentNameToNonExisting.Click(); + _appElement.FindElement(By.Id("counter-change-section-content-name-nonexisting")).Click(); Browser.DoesNotExist(By.Id("counter")); } @@ -149,13 +119,13 @@ public void SectionContentNameChangesToNonExisting_NoMatchingSectionOutletResult public void SectionOutletGetsDisposed_NoContentsRendered() { // Render Counter and TextComponent SectionContents with same Name - _renderCounterSectionContent.Click(); - _renderTextSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); // TextComponent SectionContent overrides Counter SectionContent Browser.Exists(By.Id("text")); - _disposeSectionOutlet.Click(); + _appElement.FindElement(By.Id("section-outlet-dispose")).Click(); Browser.DoesNotExist(By.Id("counter")); Browser.DoesNotExist(By.Id("text")); @@ -164,10 +134,10 @@ public void SectionOutletGetsDisposed_NoContentsRendered() [Fact] public void DefaultSectionContent_DoesNotOverrideAnotherSectionContent() { - _makeTextSectionContentDefault.Click(); + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - _renderCounterSectionContent.Click(); - _renderTextSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); // TextComponent SectionContent IsDefaultContent=true does not override Counter SectionContent Browser.DoesNotExist(By.Id("text")); @@ -177,12 +147,12 @@ public void DefaultSectionContent_DoesNotOverrideAnotherSectionContent() [Fact] public void DefaultSectionContent_RendersWhenAnotherSectionContentGetsDisposed() { - _makeTextSectionContentDefault.Click(); + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - _renderCounterSectionContent.Click(); - _renderTextSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _disposeCounterSectionContent.Click(); + _appElement.FindElement(By.Id("counter-dispose-section-content")).Click(); Browser.DoesNotExist(By.Id("counter")); Browser.Exists(By.Id("text")); @@ -191,10 +161,10 @@ public void DefaultSectionContent_RendersWhenAnotherSectionContentGetsDisposed() [Fact] public void IsDefaultContentChanges_DoesNotOverrideAnotherSectionContent() { - _renderCounterSectionContent.Click(); - _renderTextSectionContent.Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _makeTextSectionContentDefault.Click(); + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); // TextComponent SectionContent IsDefaultContent=true does not override Counter SectionContent Browser.DoesNotExist(By.Id("text")); @@ -205,12 +175,12 @@ public void IsDefaultContentChanges_DoesNotOverrideAnotherSectionContent() public void BothDefaultSectionContents_LastRenderedIsMoreDefault() { // Order of default doesn't matter before rendering - _makeTextSectionContentDefault.Click(); - _makeCounterSectionContentDefault.Click(); + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); + _appElement.FindElement(By.Id("counter-section-content-make-default")).Click(); // Counter SectionContent rendered last so it is more "default" than TextComponent - _renderTextSectionContent.Click(); - _renderCounterSectionContent.Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); Browser.Exists(By.Id("text")); Browser.DoesNotExist(By.Id("counter")); @@ -220,15 +190,15 @@ public void BothDefaultSectionContents_LastRenderedIsMoreDefault() public void BothDefaultSectionContents_LastRenderedChanges_FirstRenderedIsNowDefault() { // Order of default doesn't matter before rendering - _makeTextSectionContentDefault.Click(); - _makeCounterSectionContentDefault.Click(); + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); + _appElement.FindElement(By.Id("counter-section-content-make-default")).Click(); // Counter SectionContent rendered last so it is more "default" than TextComponent - _renderTextSectionContent.Click(); - _renderCounterSectionContent.Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); // Change Counter SectionContent to non default - _makeCounterSectionContentNonDefault.Click(); + _appElement.FindElement(By.Id("counter-section-content-make-non-default")).Click(); // TextComponent SectionContent is default Browser.DoesNotExist(By.Id("text")); diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor index d7207f70a5ca..c72860791d9c 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor @@ -4,12 +4,12 @@ @if (SectionOutletExists) { - + }

Text between two section outlets

- +

@@ -76,17 +76,17 @@
@code { - private const string firstSectionName = "test1"; - private const string secondSectionName = "test2"; + private const string FirstSectionName = "test1"; + private const string SecondSectionName = "test2"; - public bool CounterSectionContentExists { get; set; } = false; - public bool TextComponentSectionContentExists { get; set; } = false; + private bool CounterSectionContentExists = false; + private bool TextComponentSectionContentExists = false; - public string CounterSectionContentName { get; set; } = firstSectionName; - public string TextComponentSectionContentName { get; set; } = firstSectionName; + private string CounterSectionContentName = FirstSectionName; + private string TextComponentSectionContentName = FirstSectionName; - public bool CounterIsDefaultContent { get; set; } = false; - public bool TextComponentIsDefaultContent { get; set; } = false; + private bool CounterIsDefaultContent = false; + private bool TextComponentIsDefaultContent = false; - public bool SectionOutletExists { get; set; } = true; + private bool SectionOutletExists = true; } From 85b8ec64d9a6ec3d77392a3bce9fe760ecab3c37 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 23 Feb 2023 16:40:24 +0100 Subject: [PATCH 09/28] use object SectionId instead of string Name --- .../Components/src/PublicAPI.Unshipped.txt | 8 +-- .../Components/src/Sections/SectionContent.cs | 28 +++++----- .../Components/src/Sections/SectionOutlet.cs | 26 +++++----- .../src/Sections/SectionRegistry.cs | 52 +++++++++---------- src/Components/Web/src/Head/HeadContent.cs | 2 +- src/Components/Web/src/Head/HeadOutlet.cs | 10 ++-- src/Components/Web/src/Head/PageTitle.cs | 2 +- .../BasicTestApp/SectionsTest/Counter.razor | 6 +-- .../ParentComponentWithTwoChildren.razor | 20 +++---- .../SectionsTest/TextComponent.razor | 6 +-- 10 files changed, 79 insertions(+), 81 deletions(-) diff --git a/src/Components/Components/src/PublicAPI.Unshipped.txt b/src/Components/Components/src/PublicAPI.Unshipped.txt index 73866f65c485..64719ebaa963 100644 --- a/src/Components/Components/src/PublicAPI.Unshipped.txt +++ b/src/Components/Components/src/PublicAPI.Unshipped.txt @@ -9,12 +9,12 @@ Microsoft.AspNetCore.Components.Sections.SectionContent.ChildContent.set -> void Microsoft.AspNetCore.Components.Sections.SectionContent.Dispose() -> void Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.get -> bool Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.set -> void -Microsoft.AspNetCore.Components.Sections.SectionContent.Name.get -> string! -Microsoft.AspNetCore.Components.Sections.SectionContent.Name.set -> void Microsoft.AspNetCore.Components.Sections.SectionContent.SectionContent() -> void +Microsoft.AspNetCore.Components.Sections.SectionContent.SectionId.get -> object! +Microsoft.AspNetCore.Components.Sections.SectionContent.SectionId.set -> void Microsoft.AspNetCore.Components.Sections.SectionOutlet Microsoft.AspNetCore.Components.Sections.SectionOutlet.Dispose() -> void -Microsoft.AspNetCore.Components.Sections.SectionOutlet.Name.get -> string! -Microsoft.AspNetCore.Components.Sections.SectionOutlet.Name.set -> void +Microsoft.AspNetCore.Components.Sections.SectionOutlet.SectionId.get -> object! +Microsoft.AspNetCore.Components.Sections.SectionOutlet.SectionId.set -> void Microsoft.AspNetCore.Components.Sections.SectionOutlet.SectionOutlet() -> void Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddComponentParameter(int sequence, string! name, object? value) -> void diff --git a/src/Components/Components/src/Sections/SectionContent.cs b/src/Components/Components/src/Sections/SectionContent.cs index 4ef7eb904f13..69ea1fd9f678 100644 --- a/src/Components/Components/src/Sections/SectionContent.cs +++ b/src/Components/Components/src/Sections/SectionContent.cs @@ -4,19 +4,19 @@ namespace Microsoft.AspNetCore.Components.Sections; /// -/// Provides content to components with matching s. +/// Provides content to components with matching s. /// public sealed class SectionContent : ISectionContentProvider, IComponent, IDisposable { - private string? _registeredName; + private object? _registeredSectionId; private bool? _registeredIsDefaultContent; private SectionRegistry _registry = default!; /// - /// Gets or sets the name that determines which instance will render + /// Gets or sets the Id that determines which instance will render /// the content of this instance. /// - [Parameter] public string Name { get; set; } = default!; + [Parameter] public object SectionId { get; set; } = default!; /// /// Gets or sets whether this component should provide the default content for the target @@ -40,24 +40,24 @@ Task IComponent.SetParametersAsync(ParameterView parameters) { parameters.SetParameterProperties(this); - if (string.IsNullOrEmpty(Name)) + if (SectionId is null) { - throw new InvalidOperationException($"{GetType()} requires a non-empty string parameter '{nameof(Name)}'."); + throw new InvalidOperationException($"{GetType()} requires a non-empty string parameter '{nameof(SectionId)}'."); } - if (Name != _registeredName || IsDefaultContent != _registeredIsDefaultContent) + if (SectionId != _registeredSectionId! || IsDefaultContent != _registeredIsDefaultContent) { - if (_registeredName is not null) + if (_registeredSectionId is not null) { - _registry.RemoveProvider(_registeredName, this); + _registry.RemoveProvider(_registeredSectionId, this); } - _registry.AddProvider(Name, this, IsDefaultContent); - _registeredName = Name; + _registry.AddProvider(SectionId, this, IsDefaultContent); + _registeredSectionId = SectionId; _registeredIsDefaultContent = IsDefaultContent; } - _registry.NotifyContentChanged(Name, this); + _registry.NotifyContentChanged(SectionId, this); return Task.CompletedTask; } @@ -65,9 +65,9 @@ Task IComponent.SetParametersAsync(ParameterView parameters) /// public void Dispose() { - if (_registeredName is not null) + if (_registeredSectionId is not null) { - _registry.RemoveProvider(_registeredName, this); + _registry.RemoveProvider(_registeredSectionId, this); } } } diff --git a/src/Components/Components/src/Sections/SectionOutlet.cs b/src/Components/Components/src/Sections/SectionOutlet.cs index 43b451f48c7e..ecac0d3fe593 100644 --- a/src/Components/Components/src/Sections/SectionOutlet.cs +++ b/src/Components/Components/src/Sections/SectionOutlet.cs @@ -4,22 +4,22 @@ namespace Microsoft.AspNetCore.Components.Sections; /// -/// Renders content provided by components with matching s. +/// Renders content provided by components with matching s. /// public sealed class SectionOutlet : ISectionContentSubscriber, IComponent, IDisposable { private static readonly RenderFragment _emptyRenderFragment = _ => { }; - private string? _subscribedName; + private object? _subscribedSectionId; private RenderHandle _renderHandle; private SectionRegistry _registry = default!; private RenderFragment? _content; /// - /// Gets or sets the name that determines which instances will provide + /// Gets or sets the Id that determines which instances will provide /// content to this instance. /// - [Parameter] public string Name { get; set; } = default!; + [Parameter] public object SectionId { get; set; } = default!; void IComponent.Attach(RenderHandle renderHandle) { @@ -31,20 +31,20 @@ Task IComponent.SetParametersAsync(ParameterView parameters) { parameters.SetParameterProperties(this); - if (string.IsNullOrEmpty(Name)) + if (SectionId is null) { - throw new InvalidOperationException($"{GetType()} requires a non-empty string parameter '{nameof(Name)}'."); + throw new InvalidOperationException($"{GetType()} requires a non-empty string parameter '{nameof(SectionId)}'."); } - if (Name != _subscribedName) + if (SectionId != _subscribedSectionId) { - if (_subscribedName is not null) + if (_subscribedSectionId is not null) { - _registry.Unsubscribe(_subscribedName); + _registry.Unsubscribe(_subscribedSectionId); } - _registry.Subscribe(Name, this); - _subscribedName = Name; + _registry.Subscribe(SectionId, this); + _subscribedSectionId = SectionId; } RenderContent(); @@ -74,9 +74,9 @@ private void RenderContent() /// public void Dispose() { - if (_subscribedName is not null) + if (_subscribedSectionId is not null) { - _registry.Unsubscribe(_subscribedName); + _registry.Unsubscribe(_subscribedSectionId); } } } diff --git a/src/Components/Components/src/Sections/SectionRegistry.cs b/src/Components/Components/src/Sections/SectionRegistry.cs index 7e30f0b3d92f..38f2793e99d6 100644 --- a/src/Components/Components/src/Sections/SectionRegistry.cs +++ b/src/Components/Components/src/Sections/SectionRegistry.cs @@ -5,15 +5,15 @@ namespace Microsoft.AspNetCore.Components.Sections; internal sealed class SectionRegistry { - private readonly Dictionary _subscribersByName = new(); - private readonly Dictionary> _providersByName = new(); + private readonly Dictionary _subscribersBySectionId = new(); + private readonly Dictionary> _providersBySectionId = new(); - public void AddProvider(string name, ISectionContentProvider provider, bool isDefaultProvider) + public void AddProvider(object sectionId, ISectionContentProvider provider, bool isDefaultProvider) { - if (!_providersByName.TryGetValue(name, out var providers)) + if (!_providersBySectionId.TryGetValue(sectionId, out var providers)) { providers = new(); - _providersByName.Add(name, providers); + _providersBySectionId.Add(sectionId, providers); } if (isDefaultProvider) @@ -26,18 +26,18 @@ public void AddProvider(string name, ISectionContentProvider provider, bool isDe } } - public void RemoveProvider(string name, ISectionContentProvider provider) + public void RemoveProvider(object sectionId, ISectionContentProvider provider) { - if (!_providersByName.TryGetValue(name, out var providers)) + if (!_providersBySectionId.TryGetValue(sectionId, out var providers)) { - throw new InvalidOperationException($"There are no content providers with the name '{name}'."); + throw new InvalidOperationException($"There are no content providers with the given SectionId."); } var index = providers.LastIndexOf(provider); if (index < 0) { - throw new InvalidOperationException($"The provider was not found in the providers list of name '{name}'."); + throw new InvalidOperationException($"The provider was not found in the providers list of the given SectionId."); } providers.RemoveAt(index); @@ -47,44 +47,44 @@ public void RemoveProvider(string name, ISectionContentProvider provider) // We just removed the most recently added provider, meaning we need to change // the current content to that of second most recently added provider. var content = GetCurrentProviderContentOrDefault(providers); - NotifyContentChangedForSubscriber(name, content); + NotifyContentChangedForSubscriber(sectionId, content); } } - public void Subscribe(string name, ISectionContentSubscriber subscriber) + public void Subscribe(object sectionId, ISectionContentSubscriber subscriber) { - if (_subscribersByName.ContainsKey(name)) + if (_subscribersBySectionId.ContainsKey(sectionId)) { - throw new InvalidOperationException($"There is already a subscriber to the content '{name}'."); + throw new InvalidOperationException($"There is already a subscriber to the content with the given SectionId."); } // Notify the new subscriber with any existing content. - var content = GetCurrentProviderContentOrDefault(name); + var content = GetCurrentProviderContentOrDefault(sectionId); subscriber.ContentChanged(content); - _subscribersByName.Add(name, subscriber); + _subscribersBySectionId.Add(sectionId, subscriber); } - public void Unsubscribe(string name) + public void Unsubscribe(object sectionId) { - if (!_subscribersByName.Remove(name)) + if (!_subscribersBySectionId.Remove(sectionId)) { - throw new InvalidOperationException($"The subscriber with name '{name}' is already unsubscribed."); + throw new InvalidOperationException($"The subscriber with the given SectionId is already unsubscribed."); } } - public void NotifyContentChanged(string name, ISectionContentProvider provider) + public void NotifyContentChanged(object sectionId, ISectionContentProvider provider) { - if (!_providersByName.TryGetValue(name, out var providers)) + if (!_providersBySectionId.TryGetValue(sectionId, out var providers)) { - throw new InvalidOperationException($"There are no content providers with the name '{name}'."); + throw new InvalidOperationException($"There are no content providers with the given SectionId."); } // We only notify content changed for subscribers when the content of the // most recently added provider changes. if (providers.Count != 0 && providers[^1] == provider) { - NotifyContentChangedForSubscriber(name, provider.Content); + NotifyContentChangedForSubscriber(sectionId, provider.Content); } } @@ -93,14 +93,14 @@ public void NotifyContentChanged(string name, ISectionContentProvider provider) ? providers[^1].Content : null; - private RenderFragment? GetCurrentProviderContentOrDefault(string name) - => _providersByName.TryGetValue(name, out var existingList) + private RenderFragment? GetCurrentProviderContentOrDefault(object sectionId) + => _providersBySectionId.TryGetValue(sectionId, out var existingList) ? GetCurrentProviderContentOrDefault(existingList) : null; - private void NotifyContentChangedForSubscriber(string name, RenderFragment? content) + private void NotifyContentChangedForSubscriber(object sectionId, RenderFragment? content) { - if (_subscribersByName.TryGetValue(name, out var subscriber)) + if (_subscribersBySectionId.TryGetValue(sectionId, out var subscriber)) { subscriber.ContentChanged(content); } diff --git a/src/Components/Web/src/Head/HeadContent.cs b/src/Components/Web/src/Head/HeadContent.cs index 19aeb5bd94c0..88aca506d1d4 100644 --- a/src/Components/Web/src/Head/HeadContent.cs +++ b/src/Components/Web/src/Head/HeadContent.cs @@ -21,7 +21,7 @@ public sealed class HeadContent : ComponentBase protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenComponent(0); - builder.AddComponentParameter(1, nameof(SectionContent.Name), HeadOutlet.HeadSectionOutletName); + builder.AddComponentParameter(1, nameof(SectionContent.SectionId), HeadOutlet.HeadSectionOutletName); builder.AddComponentParameter(2, nameof(SectionContent.ChildContent), ChildContent); builder.CloseComponent(); } diff --git a/src/Components/Web/src/Head/HeadOutlet.cs b/src/Components/Web/src/Head/HeadOutlet.cs index a25d4c46b617..6474f9032371 100644 --- a/src/Components/Web/src/Head/HeadOutlet.cs +++ b/src/Components/Web/src/Head/HeadOutlet.cs @@ -14,8 +14,8 @@ public sealed class HeadOutlet : ComponentBase { private const string GetAndRemoveExistingTitle = "Blazor._internal.PageTitle.getAndRemoveExistingTitle"; - internal const string HeadSectionOutletName = "blazor_head"; - internal const string TitleSectionOutletName = "blazor_title"; + internal static readonly object HeadSectionOutletName = new(); + internal static readonly object TitleSectionOutletName = new(); private string? _defaultTitle; @@ -37,14 +37,14 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) { // Render the title content builder.OpenComponent(0); - builder.AddComponentParameter(1, nameof(SectionOutlet.Name), TitleSectionOutletName); + builder.AddComponentParameter(1, nameof(SectionOutlet.SectionId), TitleSectionOutletName); builder.CloseComponent(); // Render the default title if it exists if (!string.IsNullOrEmpty(_defaultTitle)) { builder.OpenComponent(2); - builder.AddComponentParameter(3, nameof(SectionContent.Name), TitleSectionOutletName); + builder.AddComponentParameter(3, nameof(SectionContent.SectionId), TitleSectionOutletName); builder.AddComponentParameter(4, nameof(SectionContent.IsDefaultContent), true); builder.AddComponentParameter(5, nameof(SectionContent.ChildContent), (RenderFragment)BuildDefaultTitleRenderTree); builder.CloseComponent(); @@ -52,7 +52,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) // Render the rest of the head metadata builder.OpenComponent(6); - builder.AddComponentParameter(7, nameof(SectionOutlet.Name), HeadSectionOutletName); + builder.AddComponentParameter(7, nameof(SectionOutlet.SectionId), HeadSectionOutletName); builder.CloseComponent(); } diff --git a/src/Components/Web/src/Head/PageTitle.cs b/src/Components/Web/src/Head/PageTitle.cs index 7a745973e5e7..a98fb6a7e289 100644 --- a/src/Components/Web/src/Head/PageTitle.cs +++ b/src/Components/Web/src/Head/PageTitle.cs @@ -21,7 +21,7 @@ public sealed class PageTitle : ComponentBase protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenComponent(0); - builder.AddComponentParameter(1, nameof(SectionContent.Name), HeadOutlet.TitleSectionOutletName); + builder.AddComponentParameter(1, nameof(SectionContent.SectionId), HeadOutlet.TitleSectionOutletName); builder.AddComponentParameter(2, nameof(SectionContent.ChildContent), (RenderFragment)BuildTitleRenderTree); builder.CloseComponent(); } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor index c71f686e8588..c9d7cca62867 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor @@ -7,13 +7,11 @@

Counter SectionContent was rendered = @SectionContentExists.ToString()

-

Counter SectionContent Name = @SectionName

-

Counter IsDefaultContent = @IsDefaultContent.ToString()

@if (SectionContentExists) { - +

@currentCount

} @@ -29,7 +27,7 @@ public bool IsDefaultContent { get; set; } [Parameter] - public string SectionName { get; set; } + public object SectionId { get; set; } int currentCount = 0; diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor index c72860791d9c..84513b4cc637 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor @@ -4,12 +4,14 @@ @if (SectionOutletExists) { - + } + +

Text between two section outlets

- +

@@ -56,7 +58,7 @@ @@ -76,14 +78,14 @@
@code { - private const string FirstSectionName = "test1"; - private const string SecondSectionName = "test2"; + private static object FirstSectionId = new(); + private static object SecondSectionId = new(); private bool CounterSectionContentExists = false; private bool TextComponentSectionContentExists = false; - private string CounterSectionContentName = FirstSectionName; - private string TextComponentSectionContentName = FirstSectionName; + private object CounterSectionContentName = FirstSectionId; + private object TextComponentSectionContentName = FirstSectionId; private bool CounterIsDefaultContent = false; private bool TextComponentIsDefaultContent = false; diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor index 86b3c8d57bd2..fc6ef8c1914b 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor @@ -5,13 +5,11 @@

TextComponent SectionContent was rendered = @SectionContentExists.ToString()

-

TextComponent SectionContent Name @SectionName

-

TextComponent SectionContent IsDefaultContent = @IsDefaultContent.ToString()

@if (SectionContentExists) { - +

Hello!

} @@ -25,5 +23,5 @@ public bool IsDefaultContent { get; set; } [Parameter] - public string SectionName { get; set; } + public object SectionId { get; set; } } From 435716a9408d18ea8c8a68bd31fe5e7f0b965f50 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 23 Feb 2023 16:46:22 +0100 Subject: [PATCH 10/28] fix --- .../SectionsTest/ParentComponentWithTwoChildren.razor | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor index 84513b4cc637..403f00b24b69 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor @@ -7,8 +7,6 @@ } - -

Text between two section outlets

From c42520ac7ebcd8d062b89fb6fcbdb2c7c2b54405 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada <114938397+surayya-MS@users.noreply.github.com> Date: Fri, 24 Feb 2023 11:59:33 +0100 Subject: [PATCH 11/28] Update src/Components/Components/src/Sections/SectionContent.cs Co-authored-by: Mackinnon Buck --- src/Components/Components/src/Sections/SectionContent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Components/src/Sections/SectionContent.cs b/src/Components/Components/src/Sections/SectionContent.cs index 69ea1fd9f678..f39ecca74383 100644 --- a/src/Components/Components/src/Sections/SectionContent.cs +++ b/src/Components/Components/src/Sections/SectionContent.cs @@ -13,7 +13,7 @@ public sealed class SectionContent : ISectionContentProvider, IComponent, IDispo private SectionRegistry _registry = default!; /// - /// Gets or sets the Id that determines which instance will render + /// Gets or sets the ID that determines which instance will render /// the content of this instance. /// [Parameter] public object SectionId { get; set; } = default!; From d3352bebea13fa062f325b69e0c03a58bdf45820 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Tue, 28 Feb 2023 16:01:41 +0100 Subject: [PATCH 12/28] fix --- .../Components/src/Sections/SectionContent.cs | 4 ++-- .../Components/src/Sections/SectionOutlet.cs | 4 ++-- src/Components/Web/src/Head/HeadContent.cs | 2 +- src/Components/Web/src/Head/HeadOutlet.cs | 10 +++++----- src/Components/Web/src/Head/PageTitle.cs | 2 +- .../test/E2ETest/Tests/SectionsTest.cs | 6 +++--- .../BasicTestApp/SectionsTest/Counter.razor | 2 +- .../ParentComponentWithTwoChildren.razor | 18 +++++++++--------- .../SectionsTest/TextComponent.razor | 2 +- 9 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/Components/Components/src/Sections/SectionContent.cs b/src/Components/Components/src/Sections/SectionContent.cs index f39ecca74383..82f654f6a2f7 100644 --- a/src/Components/Components/src/Sections/SectionContent.cs +++ b/src/Components/Components/src/Sections/SectionContent.cs @@ -16,7 +16,7 @@ public sealed class SectionContent : ISectionContentProvider, IComponent, IDispo /// Gets or sets the ID that determines which instance will render /// the content of this instance. ///
- [Parameter] public object SectionId { get; set; } = default!; + [Parameter, EditorRequired] public object SectionId { get; set; } = default!; /// /// Gets or sets whether this component should provide the default content for the target @@ -45,7 +45,7 @@ Task IComponent.SetParametersAsync(ParameterView parameters) throw new InvalidOperationException($"{GetType()} requires a non-empty string parameter '{nameof(SectionId)}'."); } - if (SectionId != _registeredSectionId! || IsDefaultContent != _registeredIsDefaultContent) + if (!object.Equals(SectionId, _registeredSectionId) || IsDefaultContent != _registeredIsDefaultContent) { if (_registeredSectionId is not null) { diff --git a/src/Components/Components/src/Sections/SectionOutlet.cs b/src/Components/Components/src/Sections/SectionOutlet.cs index ecac0d3fe593..d37830a31ca9 100644 --- a/src/Components/Components/src/Sections/SectionOutlet.cs +++ b/src/Components/Components/src/Sections/SectionOutlet.cs @@ -19,7 +19,7 @@ public sealed class SectionOutlet : ISectionContentSubscriber, IComponent, IDisp /// Gets or sets the Id that determines which instances will provide /// content to this instance. /// - [Parameter] public object SectionId { get; set; } = default!; + [Parameter, EditorRequired] public object SectionId { get; set; } = default!; void IComponent.Attach(RenderHandle renderHandle) { @@ -36,7 +36,7 @@ Task IComponent.SetParametersAsync(ParameterView parameters) throw new InvalidOperationException($"{GetType()} requires a non-empty string parameter '{nameof(SectionId)}'."); } - if (SectionId != _subscribedSectionId) + if (!object.Equals(SectionId, _subscribedSectionId)) { if (_subscribedSectionId is not null) { diff --git a/src/Components/Web/src/Head/HeadContent.cs b/src/Components/Web/src/Head/HeadContent.cs index 88aca506d1d4..89924a555fde 100644 --- a/src/Components/Web/src/Head/HeadContent.cs +++ b/src/Components/Web/src/Head/HeadContent.cs @@ -21,7 +21,7 @@ public sealed class HeadContent : ComponentBase protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenComponent(0); - builder.AddComponentParameter(1, nameof(SectionContent.SectionId), HeadOutlet.HeadSectionOutletName); + builder.AddComponentParameter(1, nameof(SectionContent.SectionId), HeadOutlet.HeadSectionOutletId); builder.AddComponentParameter(2, nameof(SectionContent.ChildContent), ChildContent); builder.CloseComponent(); } diff --git a/src/Components/Web/src/Head/HeadOutlet.cs b/src/Components/Web/src/Head/HeadOutlet.cs index 6474f9032371..f53bd1c0688e 100644 --- a/src/Components/Web/src/Head/HeadOutlet.cs +++ b/src/Components/Web/src/Head/HeadOutlet.cs @@ -14,8 +14,8 @@ public sealed class HeadOutlet : ComponentBase { private const string GetAndRemoveExistingTitle = "Blazor._internal.PageTitle.getAndRemoveExistingTitle"; - internal static readonly object HeadSectionOutletName = new(); - internal static readonly object TitleSectionOutletName = new(); + internal static readonly object HeadSectionOutletId = new(); + internal static readonly object TitleSectionOutletId = new(); private string? _defaultTitle; @@ -37,14 +37,14 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) { // Render the title content builder.OpenComponent(0); - builder.AddComponentParameter(1, nameof(SectionOutlet.SectionId), TitleSectionOutletName); + builder.AddComponentParameter(1, nameof(SectionOutlet.SectionId), TitleSectionOutletId); builder.CloseComponent(); // Render the default title if it exists if (!string.IsNullOrEmpty(_defaultTitle)) { builder.OpenComponent(2); - builder.AddComponentParameter(3, nameof(SectionContent.SectionId), TitleSectionOutletName); + builder.AddComponentParameter(3, nameof(SectionContent.SectionId), TitleSectionOutletId); builder.AddComponentParameter(4, nameof(SectionContent.IsDefaultContent), true); builder.AddComponentParameter(5, nameof(SectionContent.ChildContent), (RenderFragment)BuildDefaultTitleRenderTree); builder.CloseComponent(); @@ -52,7 +52,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) // Render the rest of the head metadata builder.OpenComponent(6); - builder.AddComponentParameter(7, nameof(SectionOutlet.SectionId), HeadSectionOutletName); + builder.AddComponentParameter(7, nameof(SectionOutlet.SectionId), HeadSectionOutletId); builder.CloseComponent(); } diff --git a/src/Components/Web/src/Head/PageTitle.cs b/src/Components/Web/src/Head/PageTitle.cs index a98fb6a7e289..d96c86fa5292 100644 --- a/src/Components/Web/src/Head/PageTitle.cs +++ b/src/Components/Web/src/Head/PageTitle.cs @@ -21,7 +21,7 @@ public sealed class PageTitle : ComponentBase protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenComponent(0); - builder.AddComponentParameter(1, nameof(SectionContent.SectionId), HeadOutlet.TitleSectionOutletName); + builder.AddComponentParameter(1, nameof(SectionContent.SectionId), HeadOutlet.TitleSectionOutletId); builder.AddComponentParameter(2, nameof(SectionContent.ChildContent), (RenderFragment)BuildTitleRenderTree); builder.CloseComponent(); } diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index ffe5061c8b40..97ac9e9bcae2 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -51,7 +51,7 @@ public void RenderOneSectionContent_MatchingSectionOutletRendersContentSuccessfu } [Fact] - public void RenderTwoSectionContentsWithSameName_LastRenderedOverridesSectionOutletContent() + public void RenderTwoSectionContentsWithSameId_LastRenderedOverridesSectionOutletContent() { _appElement.FindElement(By.Id("counter-render-section-content")).Click(); @@ -90,7 +90,7 @@ public void BothSectionContentsGetDisposed_SectionOutletsRenderNothing() } [Fact] - public void SectionContentNameChanges_MatchingSectionOutletRendersContent() + public void SectionContentIdChanges_MatchingSectionOutletRendersContent() { // Render Counter and TextComponent SectionContents with same Name // TextComponent SectionContent overrides Counter SectionContent @@ -103,7 +103,7 @@ public void SectionContentNameChanges_MatchingSectionOutletRendersContent() } [Fact] - public void SectionContentNameChangesToNonExisting_NoMatchingSectionOutletResultingNoRendering() + public void SectionContentIdChangesToNonExisting_NoMatchingSectionOutletResultingNoRendering() { // Render Counter and TextComponent SectionContents with same Name // TextComponent SectionContent overrides Counter SectionContent diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor index c9d7cca62867..ff508d9ec1ba 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor @@ -11,7 +11,7 @@ @if (SectionContentExists) { - +

@currentCount

} diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor index 403f00b24b69..0ff2bc6e3e8d 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor @@ -4,12 +4,12 @@ @if (SectionOutletExists) { - + }

Text between two section outlets

- + + SectionContentExists="CounterSectionContentExists" + SectionId="CounterSectionContentName" + IsDefaultContent="CounterIsDefaultContent">
@@ -55,9 +55,9 @@ + SectionContentExists="TextComponentSectionContentExists" + SectionId="TextComponentSectionContentName" + IsDefaultContent="TextComponentIsDefaultContent">


@@ -56,7 +56,7 @@ @@ -82,8 +82,8 @@ private bool CounterSectionContentExists = false; private bool TextComponentSectionContentExists = false; - private object CounterSectionContentName = FirstSectionId; - private object TextComponentSectionContentName = FirstSectionId; + private object CounterSectionContentId = FirstSectionId; + private object TextComponentSectionContentId = FirstSectionId; private bool CounterIsDefaultContent = false; private bool TextComponentIsDefaultContent = false; From ae3171efdcad47c32e2e273d815040d0b4eae5f7 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Thu, 2 Mar 2023 10:25:42 +0100 Subject: [PATCH 19/28] fix --- .../Components/src/Sections/SectionRegistry.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Components/Components/src/Sections/SectionRegistry.cs b/src/Components/Components/src/Sections/SectionRegistry.cs index 38f2793e99d6..55240e67f6a1 100644 --- a/src/Components/Components/src/Sections/SectionRegistry.cs +++ b/src/Components/Components/src/Sections/SectionRegistry.cs @@ -30,14 +30,14 @@ public void RemoveProvider(object sectionId, ISectionContentProvider provider) { if (!_providersBySectionId.TryGetValue(sectionId, out var providers)) { - throw new InvalidOperationException($"There are no content providers with the given SectionId."); + throw new InvalidOperationException($"There are no content providers with the given section ID."); } var index = providers.LastIndexOf(provider); if (index < 0) { - throw new InvalidOperationException($"The provider was not found in the providers list of the given SectionId."); + throw new InvalidOperationException($"The provider was not found in the providers list of the given section ID."); } providers.RemoveAt(index); @@ -55,7 +55,7 @@ public void Subscribe(object sectionId, ISectionContentSubscriber subscriber) { if (_subscribersBySectionId.ContainsKey(sectionId)) { - throw new InvalidOperationException($"There is already a subscriber to the content with the given SectionId."); + throw new InvalidOperationException($"There is already a subscriber to the content with the given section ID."); } // Notify the new subscriber with any existing content. @@ -69,7 +69,7 @@ public void Unsubscribe(object sectionId) { if (!_subscribersBySectionId.Remove(sectionId)) { - throw new InvalidOperationException($"The subscriber with the given SectionId is already unsubscribed."); + throw new InvalidOperationException($"The subscriber with the given section ID is already unsubscribed."); } } @@ -77,7 +77,7 @@ public void NotifyContentChanged(object sectionId, ISectionContentProvider provi { if (!_providersBySectionId.TryGetValue(sectionId, out var providers)) { - throw new InvalidOperationException($"There are no content providers with the given SectionId."); + throw new InvalidOperationException($"There are no content providers with the given section ID."); } // We only notify content changed for subscribers when the content of the From 35d13b906ea49c6392337b2dafdee6b8904ae9d7 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Mon, 6 Mar 2023 11:17:47 +0100 Subject: [PATCH 20/28] fix --- src/Components/Web/src/Head/HeadOutlet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Web/src/Head/HeadOutlet.cs b/src/Components/Web/src/Head/HeadOutlet.cs index 420fb2d69822..313ec37b550e 100644 --- a/src/Components/Web/src/Head/HeadOutlet.cs +++ b/src/Components/Web/src/Head/HeadOutlet.cs @@ -52,7 +52,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) // Render the rest of the head metadata builder.OpenComponent(6); - builder.AddComponentParameter(7, nameof(SectionOutlet.SectionId), TitleSectionId); + builder.AddComponentParameter(7, nameof(SectionOutlet.SectionId), HeadSectionId); builder.CloseComponent(); } From 275100b466d3d15cb87fc0635d4d98f69fc9bfef Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Mon, 6 Mar 2023 11:49:05 +0100 Subject: [PATCH 21/28] change exceptions messages in SectionRegistry --- .../Components/src/Sections/SectionRegistry.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Components/Components/src/Sections/SectionRegistry.cs b/src/Components/Components/src/Sections/SectionRegistry.cs index 55240e67f6a1..0976deccc466 100644 --- a/src/Components/Components/src/Sections/SectionRegistry.cs +++ b/src/Components/Components/src/Sections/SectionRegistry.cs @@ -30,14 +30,14 @@ public void RemoveProvider(object sectionId, ISectionContentProvider provider) { if (!_providersBySectionId.TryGetValue(sectionId, out var providers)) { - throw new InvalidOperationException($"There are no content providers with the given section ID."); + throw new InvalidOperationException($"There are no content providers with the given section ID '{sectionId}'."); } var index = providers.LastIndexOf(provider); if (index < 0) { - throw new InvalidOperationException($"The provider was not found in the providers list of the given section ID."); + throw new InvalidOperationException($"The provider was not found in the providers list of the given section ID '{sectionId}'."); } providers.RemoveAt(index); @@ -55,7 +55,7 @@ public void Subscribe(object sectionId, ISectionContentSubscriber subscriber) { if (_subscribersBySectionId.ContainsKey(sectionId)) { - throw new InvalidOperationException($"There is already a subscriber to the content with the given section ID."); + throw new InvalidOperationException($"There is already a subscriber to the content with the given section ID '{sectionId}'."); } // Notify the new subscriber with any existing content. @@ -69,7 +69,7 @@ public void Unsubscribe(object sectionId) { if (!_subscribersBySectionId.Remove(sectionId)) { - throw new InvalidOperationException($"The subscriber with the given section ID is already unsubscribed."); + throw new InvalidOperationException($"The subscriber with the given section ID '{sectionId}' is already unsubscribed."); } } @@ -77,7 +77,7 @@ public void NotifyContentChanged(object sectionId, ISectionContentProvider provi { if (!_providersBySectionId.TryGetValue(sectionId, out var providers)) { - throw new InvalidOperationException($"There are no content providers with the given section ID."); + throw new InvalidOperationException($"There are no content providers with the given section ID '{sectionId}'."); } // We only notify content changed for subscribers when the content of the From 4b92f0c43d005985e727cf0e01c9d5026cb4df28 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Mon, 6 Mar 2023 17:02:29 +0100 Subject: [PATCH 22/28] remove IsDefaultContent from SectionContent --- .../Components/src/Sections/SectionContent.cs | 12 +-- .../src/Sections/SectionRegistry.cs | 11 +-- src/Components/Web/src/Head/HeadOutlet.cs | 7 +- .../test/E2ETest/Tests/SectionsTest.cs | 74 ------------------- .../BasicTestApp/SectionsTest/Counter.razor | 7 +- .../ParentComponentWithTwoChildren.razor | 24 +----- .../SectionsTest/TextComponent.razor | 7 +- 7 files changed, 11 insertions(+), 131 deletions(-) diff --git a/src/Components/Components/src/Sections/SectionContent.cs b/src/Components/Components/src/Sections/SectionContent.cs index 6bcea804e350..1f432d59ca8a 100644 --- a/src/Components/Components/src/Sections/SectionContent.cs +++ b/src/Components/Components/src/Sections/SectionContent.cs @@ -9,7 +9,6 @@ namespace Microsoft.AspNetCore.Components.Sections; public sealed class SectionContent : ISectionContentProvider, IComponent, IDisposable { private object? _registeredSectionId; - private bool? _registeredIsDefaultContent; private SectionRegistry _registry = default!; /// @@ -18,12 +17,6 @@ public sealed class SectionContent : ISectionContentProvider, IComponent, IDispo /// [Parameter, EditorRequired] public object SectionId { get; set; } = default!; - /// - /// Gets or sets whether this component should provide the default content for the target - /// . - /// - [Parameter] public bool IsDefaultContent { get; set; } - /// /// Gets or sets the content to be rendered in corresponding instances. /// @@ -45,16 +38,15 @@ Task IComponent.SetParametersAsync(ParameterView parameters) throw new InvalidOperationException($"{nameof(SectionContent)} requires a non-null value for the parameter '{nameof(SectionId)}'."); } - if (!object.Equals(SectionId, _registeredSectionId) || IsDefaultContent != _registeredIsDefaultContent) + if (!object.Equals(SectionId, _registeredSectionId)) { if (_registeredSectionId is not null) { _registry.RemoveProvider(_registeredSectionId, this); } - _registry.AddProvider(SectionId, this, IsDefaultContent); + _registry.AddProvider(SectionId, this); _registeredSectionId = SectionId; - _registeredIsDefaultContent = IsDefaultContent; } _registry.NotifyContentChanged(SectionId, this); diff --git a/src/Components/Components/src/Sections/SectionRegistry.cs b/src/Components/Components/src/Sections/SectionRegistry.cs index 0976deccc466..67f6bcf40ef7 100644 --- a/src/Components/Components/src/Sections/SectionRegistry.cs +++ b/src/Components/Components/src/Sections/SectionRegistry.cs @@ -8,7 +8,7 @@ internal sealed class SectionRegistry private readonly Dictionary _subscribersBySectionId = new(); private readonly Dictionary> _providersBySectionId = new(); - public void AddProvider(object sectionId, ISectionContentProvider provider, bool isDefaultProvider) + public void AddProvider(object sectionId, ISectionContentProvider provider) { if (!_providersBySectionId.TryGetValue(sectionId, out var providers)) { @@ -16,14 +16,7 @@ public void AddProvider(object sectionId, ISectionContentProvider provider, bool _providersBySectionId.Add(sectionId, providers); } - if (isDefaultProvider) - { - providers.Insert(0, provider); - } - else - { - providers.Add(provider); - } + providers.Add(provider); } public void RemoveProvider(object sectionId, ISectionContentProvider provider) diff --git a/src/Components/Web/src/Head/HeadOutlet.cs b/src/Components/Web/src/Head/HeadOutlet.cs index 313ec37b550e..fdf9012529f3 100644 --- a/src/Components/Web/src/Head/HeadOutlet.cs +++ b/src/Components/Web/src/Head/HeadOutlet.cs @@ -45,14 +45,13 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenComponent(2); builder.AddComponentParameter(3, nameof(SectionContent.SectionId), TitleSectionId); - builder.AddComponentParameter(4, nameof(SectionContent.IsDefaultContent), true); - builder.AddComponentParameter(5, nameof(SectionContent.ChildContent), (RenderFragment)BuildDefaultTitleRenderTree); + builder.AddComponentParameter(4, nameof(SectionContent.ChildContent), (RenderFragment)BuildDefaultTitleRenderTree); builder.CloseComponent(); } // Render the rest of the head metadata - builder.OpenComponent(6); - builder.AddComponentParameter(7, nameof(SectionOutlet.SectionId), HeadSectionId); + builder.OpenComponent(5); + builder.AddComponentParameter(6, nameof(SectionOutlet.SectionId), HeadSectionId); builder.CloseComponent(); } diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index e878df841803..4e798996daa2 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -130,78 +130,4 @@ public void SectionOutletGetsDisposed_NoContentsRendered() Browser.DoesNotExist(By.Id("counter")); Browser.DoesNotExist(By.Id("text")); } - - [Fact] - public void DefaultSectionContent_DoesNotOverrideAnotherSectionContent() - { - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - - // TextComponent SectionContent IsDefaultContent=true does not override Counter SectionContent - Browser.DoesNotExist(By.Id("text")); - Browser.Exists(By.Id("counter")); - } - - [Fact] - public void DefaultSectionContent_RendersWhenAnotherSectionContentGetsDisposed() - { - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - - _appElement.FindElement(By.Id("counter-dispose-section-content")).Click(); - - Browser.DoesNotExist(By.Id("counter")); - Browser.Exists(By.Id("text")); - } - - [Fact] - public void IsDefaultContentChanges_DoesNotOverrideAnotherSectionContent() - { - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - - // TextComponent SectionContent IsDefaultContent=true does not override Counter SectionContent - Browser.DoesNotExist(By.Id("text")); - Browser.Exists(By.Id("counter")); - } - - [Fact] - public void BothDefaultSectionContents_LastRenderedIsMoreDefault() - { - // Order of default doesn't matter before rendering - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - _appElement.FindElement(By.Id("counter-section-content-make-default")).Click(); - - // Counter SectionContent rendered last so it is more "default" than TextComponent - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - - Browser.Exists(By.Id("text")); - Browser.DoesNotExist(By.Id("counter")); - } - - [Fact] - public void BothDefaultSectionContents_LastRenderedChanges_FirstRenderedIsNowDefault() - { - // Order of default doesn't matter before rendering - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - _appElement.FindElement(By.Id("counter-section-content-make-default")).Click(); - - // Counter SectionContent rendered last so it is more "default" than TextComponent - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - - // Change Counter SectionContent to non default - _appElement.FindElement(By.Id("counter-section-content-make-non-default")).Click(); - - // TextComponent SectionContent is default - Browser.DoesNotExist(By.Id("text")); - Browser.Exists(By.Id("counter")); - } } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor index ff508d9ec1ba..15f1d2444b0f 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor @@ -7,11 +7,9 @@

Counter SectionContent was rendered = @SectionContentExists.ToString()

-

Counter IsDefaultContent = @IsDefaultContent.ToString()

- @if (SectionContentExists) { - +

@currentCount

} @@ -23,9 +21,6 @@ [Parameter] public bool SectionContentExists { get; set; } - [Parameter] - public bool IsDefaultContent { get; set; } - [Parameter] public object SectionId { get; set; } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor index f3ae3dd8f16e..4834b62abcd2 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor @@ -18,8 +18,7 @@ + SectionId="CounterSectionContentId">
@@ -44,20 +43,9 @@ Change Counter SectionContent Name to non sexisting
-
- - - + SectionId="TextComponentSectionContentId">
- @code { private static object FirstSectionId = new(); private static int SecondSectionId = 123; @@ -85,8 +68,5 @@ private object CounterSectionContentId = FirstSectionId; private object TextComponentSectionContentId = FirstSectionId; - private bool CounterIsDefaultContent = false; - private bool TextComponentIsDefaultContent = false; - private bool SectionOutletExists = true; } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor index 676db6864868..c8d0ebf2997a 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor @@ -5,11 +5,9 @@

TextComponent SectionContent was rendered = @SectionContentExists.ToString()

-

TextComponent SectionContent IsDefaultContent = @IsDefaultContent.ToString()

- @if (SectionContentExists) { - +

Hello!

} @@ -19,9 +17,6 @@ [Parameter] public bool SectionContentExists { get; set; } - [Parameter] - public bool IsDefaultContent { get; set; } - [Parameter] public object SectionId { get; set; } } From 48538d176e9d4272206aad4773eaee937e145722 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Mon, 6 Mar 2023 17:49:22 +0100 Subject: [PATCH 23/28] remove IsDefaultContent from PublicAPI --- src/Components/Components/src/PublicAPI.Unshipped.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Components/Components/src/PublicAPI.Unshipped.txt b/src/Components/Components/src/PublicAPI.Unshipped.txt index 64719ebaa963..988ad9fea27e 100644 --- a/src/Components/Components/src/PublicAPI.Unshipped.txt +++ b/src/Components/Components/src/PublicAPI.Unshipped.txt @@ -7,8 +7,6 @@ Microsoft.AspNetCore.Components.Sections.SectionContent Microsoft.AspNetCore.Components.Sections.SectionContent.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment? Microsoft.AspNetCore.Components.Sections.SectionContent.ChildContent.set -> void Microsoft.AspNetCore.Components.Sections.SectionContent.Dispose() -> void -Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.get -> bool -Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.set -> void Microsoft.AspNetCore.Components.Sections.SectionContent.SectionContent() -> void Microsoft.AspNetCore.Components.Sections.SectionContent.SectionId.get -> object! Microsoft.AspNetCore.Components.Sections.SectionContent.SectionId.set -> void From e418aa6e0b41e526c39527cd4f96c2ee0192e3fa Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Mon, 6 Mar 2023 20:15:04 +0100 Subject: [PATCH 24/28] fix --- src/Components/Web/test/HtmlRendering/HtmlRendererTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Components/Web/test/HtmlRendering/HtmlRendererTest.cs b/src/Components/Web/test/HtmlRendering/HtmlRendererTest.cs index a458475cfa10..6a842fa1d25e 100644 --- a/src/Components/Web/test/HtmlRendering/HtmlRendererTest.cs +++ b/src/Components/Web/test/HtmlRendering/HtmlRendererTest.cs @@ -819,7 +819,7 @@ await htmlRenderer.Dispatcher.InvokeAsync(async () => // Arrange/Act/Assert 1: initially get some empty output var first = await htmlRenderer.RenderComponentAsync(ParameterView.FromDictionary(new Dictionary { - { nameof(SectionOutlet.Name), "testsection" } + { nameof(SectionOutlet.SectionId), "testsection" } })); Assert.Empty(first.ToHtmlString()); @@ -827,7 +827,7 @@ await htmlRenderer.Dispatcher.InvokeAsync(async () => // Act/Assert 2: cause it to be updated var second = await htmlRenderer.RenderComponentAsync(ParameterView.FromDictionary(new Dictionary { - { nameof(SectionContent.Name), "testsection" }, + { nameof(SectionContent.SectionId), "testsection" }, { nameof(SectionContent.ChildContent), (RenderFragment)(builder => { builder.AddContent(0, "Hello from the section content provider"); From 157bdaff8f4443368b02a73ec3dcb5aff077b821 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Tue, 7 Mar 2023 16:14:05 +0100 Subject: [PATCH 25/28] Revert "remove IsDefaultContent from PublicAPI" This reverts commit 48538d176e9d4272206aad4773eaee937e145722. --- src/Components/Components/src/PublicAPI.Unshipped.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Components/Components/src/PublicAPI.Unshipped.txt b/src/Components/Components/src/PublicAPI.Unshipped.txt index 2709f48969f1..5be63d6b2c14 100644 --- a/src/Components/Components/src/PublicAPI.Unshipped.txt +++ b/src/Components/Components/src/PublicAPI.Unshipped.txt @@ -8,6 +8,8 @@ Microsoft.AspNetCore.Components.Sections.SectionContent Microsoft.AspNetCore.Components.Sections.SectionContent.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment? Microsoft.AspNetCore.Components.Sections.SectionContent.ChildContent.set -> void Microsoft.AspNetCore.Components.Sections.SectionContent.Dispose() -> void +Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.get -> bool +Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.set -> void Microsoft.AspNetCore.Components.Sections.SectionContent.SectionContent() -> void Microsoft.AspNetCore.Components.Sections.SectionContent.SectionId.get -> object! Microsoft.AspNetCore.Components.Sections.SectionContent.SectionId.set -> void From 036dc2d9660767ac992e773fd73e861878869cb4 Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Tue, 7 Mar 2023 16:14:14 +0100 Subject: [PATCH 26/28] Revert "remove IsDefaultContent from SectionContent" This reverts commit 4b92f0c43d005985e727cf0e01c9d5026cb4df28. --- .../Components/src/Sections/SectionContent.cs | 12 ++- .../src/Sections/SectionRegistry.cs | 11 ++- src/Components/Web/src/Head/HeadOutlet.cs | 7 +- .../test/E2ETest/Tests/SectionsTest.cs | 74 +++++++++++++++++++ .../BasicTestApp/SectionsTest/Counter.razor | 7 +- .../ParentComponentWithTwoChildren.razor | 24 +++++- .../SectionsTest/TextComponent.razor | 7 +- 7 files changed, 131 insertions(+), 11 deletions(-) diff --git a/src/Components/Components/src/Sections/SectionContent.cs b/src/Components/Components/src/Sections/SectionContent.cs index 1f432d59ca8a..6bcea804e350 100644 --- a/src/Components/Components/src/Sections/SectionContent.cs +++ b/src/Components/Components/src/Sections/SectionContent.cs @@ -9,6 +9,7 @@ namespace Microsoft.AspNetCore.Components.Sections; public sealed class SectionContent : ISectionContentProvider, IComponent, IDisposable { private object? _registeredSectionId; + private bool? _registeredIsDefaultContent; private SectionRegistry _registry = default!; /// @@ -17,6 +18,12 @@ public sealed class SectionContent : ISectionContentProvider, IComponent, IDispo /// [Parameter, EditorRequired] public object SectionId { get; set; } = default!; + /// + /// Gets or sets whether this component should provide the default content for the target + /// . + /// + [Parameter] public bool IsDefaultContent { get; set; } + /// /// Gets or sets the content to be rendered in corresponding instances. /// @@ -38,15 +45,16 @@ Task IComponent.SetParametersAsync(ParameterView parameters) throw new InvalidOperationException($"{nameof(SectionContent)} requires a non-null value for the parameter '{nameof(SectionId)}'."); } - if (!object.Equals(SectionId, _registeredSectionId)) + if (!object.Equals(SectionId, _registeredSectionId) || IsDefaultContent != _registeredIsDefaultContent) { if (_registeredSectionId is not null) { _registry.RemoveProvider(_registeredSectionId, this); } - _registry.AddProvider(SectionId, this); + _registry.AddProvider(SectionId, this, IsDefaultContent); _registeredSectionId = SectionId; + _registeredIsDefaultContent = IsDefaultContent; } _registry.NotifyContentChanged(SectionId, this); diff --git a/src/Components/Components/src/Sections/SectionRegistry.cs b/src/Components/Components/src/Sections/SectionRegistry.cs index 67f6bcf40ef7..0976deccc466 100644 --- a/src/Components/Components/src/Sections/SectionRegistry.cs +++ b/src/Components/Components/src/Sections/SectionRegistry.cs @@ -8,7 +8,7 @@ internal sealed class SectionRegistry private readonly Dictionary _subscribersBySectionId = new(); private readonly Dictionary> _providersBySectionId = new(); - public void AddProvider(object sectionId, ISectionContentProvider provider) + public void AddProvider(object sectionId, ISectionContentProvider provider, bool isDefaultProvider) { if (!_providersBySectionId.TryGetValue(sectionId, out var providers)) { @@ -16,7 +16,14 @@ public void AddProvider(object sectionId, ISectionContentProvider provider) _providersBySectionId.Add(sectionId, providers); } - providers.Add(provider); + if (isDefaultProvider) + { + providers.Insert(0, provider); + } + else + { + providers.Add(provider); + } } public void RemoveProvider(object sectionId, ISectionContentProvider provider) diff --git a/src/Components/Web/src/Head/HeadOutlet.cs b/src/Components/Web/src/Head/HeadOutlet.cs index fdf9012529f3..313ec37b550e 100644 --- a/src/Components/Web/src/Head/HeadOutlet.cs +++ b/src/Components/Web/src/Head/HeadOutlet.cs @@ -45,13 +45,14 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) { builder.OpenComponent(2); builder.AddComponentParameter(3, nameof(SectionContent.SectionId), TitleSectionId); - builder.AddComponentParameter(4, nameof(SectionContent.ChildContent), (RenderFragment)BuildDefaultTitleRenderTree); + builder.AddComponentParameter(4, nameof(SectionContent.IsDefaultContent), true); + builder.AddComponentParameter(5, nameof(SectionContent.ChildContent), (RenderFragment)BuildDefaultTitleRenderTree); builder.CloseComponent(); } // Render the rest of the head metadata - builder.OpenComponent(5); - builder.AddComponentParameter(6, nameof(SectionOutlet.SectionId), HeadSectionId); + builder.OpenComponent(6); + builder.AddComponentParameter(7, nameof(SectionOutlet.SectionId), HeadSectionId); builder.CloseComponent(); } diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index 4e798996daa2..e878df841803 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -130,4 +130,78 @@ public void SectionOutletGetsDisposed_NoContentsRendered() Browser.DoesNotExist(By.Id("counter")); Browser.DoesNotExist(By.Id("text")); } + + [Fact] + public void DefaultSectionContent_DoesNotOverrideAnotherSectionContent() + { + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); + + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); + + // TextComponent SectionContent IsDefaultContent=true does not override Counter SectionContent + Browser.DoesNotExist(By.Id("text")); + Browser.Exists(By.Id("counter")); + } + + [Fact] + public void DefaultSectionContent_RendersWhenAnotherSectionContentGetsDisposed() + { + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); + + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); + + _appElement.FindElement(By.Id("counter-dispose-section-content")).Click(); + + Browser.DoesNotExist(By.Id("counter")); + Browser.Exists(By.Id("text")); + } + + [Fact] + public void IsDefaultContentChanges_DoesNotOverrideAnotherSectionContent() + { + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + _appElement.FindElement(By.Id("text-render-section-content")).Click(); + + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); + + // TextComponent SectionContent IsDefaultContent=true does not override Counter SectionContent + Browser.DoesNotExist(By.Id("text")); + Browser.Exists(By.Id("counter")); + } + + [Fact] + public void BothDefaultSectionContents_LastRenderedIsMoreDefault() + { + // Order of default doesn't matter before rendering + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); + _appElement.FindElement(By.Id("counter-section-content-make-default")).Click(); + + // Counter SectionContent rendered last so it is more "default" than TextComponent + _appElement.FindElement(By.Id("text-render-section-content")).Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + + Browser.Exists(By.Id("text")); + Browser.DoesNotExist(By.Id("counter")); + } + + [Fact] + public void BothDefaultSectionContents_LastRenderedChanges_FirstRenderedIsNowDefault() + { + // Order of default doesn't matter before rendering + _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); + _appElement.FindElement(By.Id("counter-section-content-make-default")).Click(); + + // Counter SectionContent rendered last so it is more "default" than TextComponent + _appElement.FindElement(By.Id("text-render-section-content")).Click(); + _appElement.FindElement(By.Id("counter-render-section-content")).Click(); + + // Change Counter SectionContent to non default + _appElement.FindElement(By.Id("counter-section-content-make-non-default")).Click(); + + // TextComponent SectionContent is default + Browser.DoesNotExist(By.Id("text")); + Browser.Exists(By.Id("counter")); + } } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor index 15f1d2444b0f..ff508d9ec1ba 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor @@ -7,9 +7,11 @@

Counter SectionContent was rendered = @SectionContentExists.ToString()

+

Counter IsDefaultContent = @IsDefaultContent.ToString()

+ @if (SectionContentExists) { - +

@currentCount

} @@ -21,6 +23,9 @@ [Parameter] public bool SectionContentExists { get; set; } + [Parameter] + public bool IsDefaultContent { get; set; } + [Parameter] public object SectionId { get; set; } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor index 4834b62abcd2..f3ae3dd8f16e 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor @@ -18,7 +18,8 @@ + SectionId="CounterSectionContentId" + IsDefaultContent="CounterIsDefaultContent">
@@ -43,9 +44,20 @@ Change Counter SectionContent Name to non sexisting
+
+ + + + SectionId="TextComponentSectionContentId" + IsDefaultContent="TextComponentIsDefaultContent">
+ @code { private static object FirstSectionId = new(); private static int SecondSectionId = 123; @@ -68,5 +85,8 @@ private object CounterSectionContentId = FirstSectionId; private object TextComponentSectionContentId = FirstSectionId; + private bool CounterIsDefaultContent = false; + private bool TextComponentIsDefaultContent = false; + private bool SectionOutletExists = true; } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor index c8d0ebf2997a..676db6864868 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor @@ -5,9 +5,11 @@

TextComponent SectionContent was rendered = @SectionContentExists.ToString()

+

TextComponent SectionContent IsDefaultContent = @IsDefaultContent.ToString()

+ @if (SectionContentExists) { - +

Hello!

} @@ -17,6 +19,9 @@ [Parameter] public bool SectionContentExists { get; set; } + [Parameter] + public bool IsDefaultContent { get; set; } + [Parameter] public object SectionId { get; set; } } From a6ef0910b0af164bba0d8920560c262672c84fdc Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Tue, 7 Mar 2023 18:06:12 +0100 Subject: [PATCH 27/28] made IsDefaultContent internal --- .../Components/src/PublicAPI.Unshipped.txt | 2 - .../Components/src/Sections/SectionContent.cs | 2 +- .../test/E2ETest/Tests/SectionsTest.cs | 74 ------------------- .../BasicTestApp/SectionsTest/Counter.razor | 7 +- .../ParentComponentWithTwoChildren.razor | 24 +----- .../SectionsTest/TextComponent.razor | 7 +- 6 files changed, 5 insertions(+), 111 deletions(-) diff --git a/src/Components/Components/src/PublicAPI.Unshipped.txt b/src/Components/Components/src/PublicAPI.Unshipped.txt index 5be63d6b2c14..2709f48969f1 100644 --- a/src/Components/Components/src/PublicAPI.Unshipped.txt +++ b/src/Components/Components/src/PublicAPI.Unshipped.txt @@ -8,8 +8,6 @@ Microsoft.AspNetCore.Components.Sections.SectionContent Microsoft.AspNetCore.Components.Sections.SectionContent.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment? Microsoft.AspNetCore.Components.Sections.SectionContent.ChildContent.set -> void Microsoft.AspNetCore.Components.Sections.SectionContent.Dispose() -> void -Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.get -> bool -Microsoft.AspNetCore.Components.Sections.SectionContent.IsDefaultContent.set -> void Microsoft.AspNetCore.Components.Sections.SectionContent.SectionContent() -> void Microsoft.AspNetCore.Components.Sections.SectionContent.SectionId.get -> object! Microsoft.AspNetCore.Components.Sections.SectionContent.SectionId.set -> void diff --git a/src/Components/Components/src/Sections/SectionContent.cs b/src/Components/Components/src/Sections/SectionContent.cs index 6bcea804e350..30b7e609b0ab 100644 --- a/src/Components/Components/src/Sections/SectionContent.cs +++ b/src/Components/Components/src/Sections/SectionContent.cs @@ -22,7 +22,7 @@ public sealed class SectionContent : ISectionContentProvider, IComponent, IDispo /// Gets or sets whether this component should provide the default content for the target /// . ///
- [Parameter] public bool IsDefaultContent { get; set; } + internal bool IsDefaultContent { get; set; } /// /// Gets or sets the content to be rendered in corresponding instances. diff --git a/src/Components/test/E2ETest/Tests/SectionsTest.cs b/src/Components/test/E2ETest/Tests/SectionsTest.cs index e878df841803..4e798996daa2 100644 --- a/src/Components/test/E2ETest/Tests/SectionsTest.cs +++ b/src/Components/test/E2ETest/Tests/SectionsTest.cs @@ -130,78 +130,4 @@ public void SectionOutletGetsDisposed_NoContentsRendered() Browser.DoesNotExist(By.Id("counter")); Browser.DoesNotExist(By.Id("text")); } - - [Fact] - public void DefaultSectionContent_DoesNotOverrideAnotherSectionContent() - { - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - - // TextComponent SectionContent IsDefaultContent=true does not override Counter SectionContent - Browser.DoesNotExist(By.Id("text")); - Browser.Exists(By.Id("counter")); - } - - [Fact] - public void DefaultSectionContent_RendersWhenAnotherSectionContentGetsDisposed() - { - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - - _appElement.FindElement(By.Id("counter-dispose-section-content")).Click(); - - Browser.DoesNotExist(By.Id("counter")); - Browser.Exists(By.Id("text")); - } - - [Fact] - public void IsDefaultContentChanges_DoesNotOverrideAnotherSectionContent() - { - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - - // TextComponent SectionContent IsDefaultContent=true does not override Counter SectionContent - Browser.DoesNotExist(By.Id("text")); - Browser.Exists(By.Id("counter")); - } - - [Fact] - public void BothDefaultSectionContents_LastRenderedIsMoreDefault() - { - // Order of default doesn't matter before rendering - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - _appElement.FindElement(By.Id("counter-section-content-make-default")).Click(); - - // Counter SectionContent rendered last so it is more "default" than TextComponent - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - - Browser.Exists(By.Id("text")); - Browser.DoesNotExist(By.Id("counter")); - } - - [Fact] - public void BothDefaultSectionContents_LastRenderedChanges_FirstRenderedIsNowDefault() - { - // Order of default doesn't matter before rendering - _appElement.FindElement(By.Id("text-section-content-make-default")).Click(); - _appElement.FindElement(By.Id("counter-section-content-make-default")).Click(); - - // Counter SectionContent rendered last so it is more "default" than TextComponent - _appElement.FindElement(By.Id("text-render-section-content")).Click(); - _appElement.FindElement(By.Id("counter-render-section-content")).Click(); - - // Change Counter SectionContent to non default - _appElement.FindElement(By.Id("counter-section-content-make-non-default")).Click(); - - // TextComponent SectionContent is default - Browser.DoesNotExist(By.Id("text")); - Browser.Exists(By.Id("counter")); - } } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor index ff508d9ec1ba..15f1d2444b0f 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor @@ -7,11 +7,9 @@

Counter SectionContent was rendered = @SectionContentExists.ToString()

-

Counter IsDefaultContent = @IsDefaultContent.ToString()

- @if (SectionContentExists) { - +

@currentCount

} @@ -23,9 +21,6 @@ [Parameter] public bool SectionContentExists { get; set; } - [Parameter] - public bool IsDefaultContent { get; set; } - [Parameter] public object SectionId { get; set; } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor index f3ae3dd8f16e..4834b62abcd2 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/ParentComponentWithTwoChildren.razor @@ -18,8 +18,7 @@ + SectionId="CounterSectionContentId">
@@ -44,20 +43,9 @@ Change Counter SectionContent Name to non sexisting
-
- - - + SectionId="TextComponentSectionContentId">
- @code { private static object FirstSectionId = new(); private static int SecondSectionId = 123; @@ -85,8 +68,5 @@ private object CounterSectionContentId = FirstSectionId; private object TextComponentSectionContentId = FirstSectionId; - private bool CounterIsDefaultContent = false; - private bool TextComponentIsDefaultContent = false; - private bool SectionOutletExists = true; } diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor index 676db6864868..c8d0ebf2997a 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/TextComponent.razor @@ -5,11 +5,9 @@

TextComponent SectionContent was rendered = @SectionContentExists.ToString()

-

TextComponent SectionContent IsDefaultContent = @IsDefaultContent.ToString()

- @if (SectionContentExists) { - +

Hello!

} @@ -19,9 +17,6 @@ [Parameter] public bool SectionContentExists { get; set; } - [Parameter] - public bool IsDefaultContent { get; set; } - [Parameter] public object SectionId { get; set; } } From b1b8be8b17ba3da4c1588cd3125ae07badec75bd Mon Sep 17 00:00:00 2001 From: Surayya Huseyn Zada Date: Wed, 8 Mar 2023 17:19:17 +0100 Subject: [PATCH 28/28] fix --- .../Components/src/Sections/SectionContent.cs | 18 +++++++++++++++++- .../BasicTestApp/SectionsTest/Counter.razor | 2 -- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Components/Components/src/Sections/SectionContent.cs b/src/Components/Components/src/Sections/SectionContent.cs index 30b7e609b0ab..b19cf3525a5f 100644 --- a/src/Components/Components/src/Sections/SectionContent.cs +++ b/src/Components/Components/src/Sections/SectionContent.cs @@ -38,7 +38,23 @@ void IComponent.Attach(RenderHandle renderHandle) Task IComponent.SetParametersAsync(ParameterView parameters) { - parameters.SetParameterProperties(this); + foreach (var param in parameters) + { + switch (param.Name) + { + case nameof(SectionContent.SectionId): + SectionId = param.Value; + break; + case nameof(SectionContent.IsDefaultContent): + IsDefaultContent = (bool)param.Value; + break; + case nameof(SectionContent.ChildContent): + ChildContent = (RenderFragment)param.Value; + break; + default: + throw new ArgumentException($"Unknown parameter '{param.Name}'"); + } + } if (SectionId is null) { diff --git a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor index 15f1d2444b0f..fca31ddcef75 100644 --- a/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor +++ b/src/Components/test/testassets/BasicTestApp/SectionsTest/Counter.razor @@ -1,8 +1,6 @@ @using Microsoft.AspNetCore.Components.Sections
- Counter -

Counter

Counter SectionContent was rendered = @SectionContentExists.ToString()