|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Globalization; |
| 5 | +using BasicTestApp; |
| 6 | +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; |
| 7 | +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; |
| 8 | +using Microsoft.AspNetCore.E2ETesting; |
| 9 | +using OpenQA.Selenium; |
| 10 | +using OpenQA.Selenium.Interactions; |
| 11 | +using Xunit.Abstractions; |
| 12 | + |
| 13 | +namespace Microsoft.AspNetCore.Components.E2ETest.Tests; |
| 14 | + |
| 15 | +public class EventFlagsTest : ServerTestBase<ToggleExecutionModeServerFixture<Program>> |
| 16 | +{ |
| 17 | + public EventFlagsTest( |
| 18 | + BrowserFixture browserFixture, |
| 19 | + ToggleExecutionModeServerFixture<Program> serverFixture, |
| 20 | + ITestOutputHelper output) |
| 21 | + : base(browserFixture, serverFixture, output) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + protected override void InitializeAsyncCore() |
| 26 | + { |
| 27 | + Navigate(ServerPathBase); |
| 28 | + Browser.MountTestComponent<EventFlagsComponent>(); |
| 29 | + } |
| 30 | + |
| 31 | + [Theory] |
| 32 | + [InlineData(true)] |
| 33 | + [InlineData(false)] |
| 34 | + public void OnMouseDown_WithPreventDefaultEnabled_DoesNotFocusButton(bool handlersEnabled) |
| 35 | + { |
| 36 | + if (!handlersEnabled) |
| 37 | + { |
| 38 | + // Disable onmousedown handlers |
| 39 | + var toggleHandlers = Browser.Exists(By.Id("toggle-handlers")); |
| 40 | + toggleHandlers.Click(); |
| 41 | + } |
| 42 | + |
| 43 | + var button = Browser.Exists(By.Id("mousedown-test-button")); |
| 44 | + button.Click(); |
| 45 | + |
| 46 | + // Check that the button has not gained focus (should not be yellow) |
| 47 | + var afterClickBackgroundColor = button.GetCssValue("background-color"); |
| 48 | + Assert.DoesNotContain("255, 255, 0", afterClickBackgroundColor); |
| 49 | + } |
| 50 | + |
| 51 | + [Theory] |
| 52 | + [InlineData(true)] |
| 53 | + [InlineData(false)] |
| 54 | + public void OnMouseDown_WithPreventDefaultDisabled_DoesFocusButton(bool handlersEnabled) |
| 55 | + { |
| 56 | + if (!handlersEnabled) |
| 57 | + { |
| 58 | + // Disable onmousedown handlers |
| 59 | + var toggleHandlers = Browser.Exists(By.Id("toggle-handlers")); |
| 60 | + toggleHandlers.Click(); |
| 61 | + } |
| 62 | + |
| 63 | + // Disable preventDefault |
| 64 | + var togglePreventDefault = Browser.Exists(By.Id("toggle-prevent-default")); |
| 65 | + togglePreventDefault.Click(); |
| 66 | + |
| 67 | + var button = Browser.Exists(By.Id("mousedown-test-button")); |
| 68 | + |
| 69 | + // Get the initial background color and check that it is no yellow |
| 70 | + var initialBackgroundColor = button.GetCssValue("background-color"); |
| 71 | + Assert.DoesNotContain("255, 255, 0", initialBackgroundColor); |
| 72 | + |
| 73 | + button.Click(); |
| 74 | + |
| 75 | + // Check that the button has gained focus (yellow background) |
| 76 | + var afterClickBackgroundColor = button.GetCssValue("background-color"); |
| 77 | + Assert.Contains("255, 255, 0", afterClickBackgroundColor); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void OnClick_WithStopPropagationEnabled_DoesNotPropagateToParent() |
| 82 | + { |
| 83 | + var button = Browser.Exists(By.Id("stop-propagation-test-button")); |
| 84 | + button.Click(); |
| 85 | + |
| 86 | + var eventLog = Browser.Exists(By.Id("event-log")); |
| 87 | + Assert.Contains("mousedown handler called on child", eventLog.Text); |
| 88 | + Assert.DoesNotContain("mousedown handler called on parent", eventLog.Text); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void OnClick_WithStopPropagationDisabled_PropagatesToParent() |
| 93 | + { |
| 94 | + // Disable stopPropagation |
| 95 | + var toggleStopPropagation = Browser.Exists(By.Id("toggle-stop-propagation")); |
| 96 | + toggleStopPropagation.Click(); |
| 97 | + |
| 98 | + var button = Browser.Exists(By.Id("stop-propagation-test-button")); |
| 99 | + button.Click(); |
| 100 | + |
| 101 | + var eventLog = Browser.Exists(By.Id("event-log")); |
| 102 | + Assert.Contains("mousedown handler called on child", eventLog.Text); |
| 103 | + Assert.Contains("mousedown handler called on parent", eventLog.Text); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void OnWheel_WithPreventDefaultEnabled_DoesNotScrollDiv() |
| 108 | + { |
| 109 | + var scrollableDiv = Browser.Exists(By.Id("wheel-test-area")); |
| 110 | + |
| 111 | + // Simulate a wheel scroll action |
| 112 | + var scrollOrigin = new WheelInputDevice.ScrollOrigin |
| 113 | + { |
| 114 | + Element = scrollableDiv, |
| 115 | + }; |
| 116 | + new Actions(Browser) |
| 117 | + .ScrollFromOrigin(scrollOrigin, 0, 200) |
| 118 | + .Perform(); |
| 119 | + |
| 120 | + // The Selenium scrolling action always changes the scrollTop property even when the event is prevented. |
| 121 | + // For this reason, we do not check for equality with zero. |
| 122 | + var newScrollTop = int.Parse(scrollableDiv.GetDomProperty("scrollTop"), CultureInfo.InvariantCulture); |
| 123 | + Assert.True(newScrollTop < 3); |
| 124 | + } |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void OnWheel_WithPreventDefaultDisabled_DoesScrollDiv() |
| 128 | + { |
| 129 | + // Disable preventDefault |
| 130 | + var togglePreventDefault = Browser.Exists(By.Id("toggle-prevent-default")); |
| 131 | + togglePreventDefault.Click(); |
| 132 | + |
| 133 | + var scrollableDiv = Browser.Exists(By.Id("wheel-test-area")); |
| 134 | + |
| 135 | + // Simulate a wheel scroll action |
| 136 | + var scrollOrigin = new WheelInputDevice.ScrollOrigin |
| 137 | + { |
| 138 | + Element = scrollableDiv, |
| 139 | + }; |
| 140 | + new Actions(Browser) |
| 141 | + .ScrollFromOrigin(scrollOrigin, 0, 200) |
| 142 | + .Perform(); |
| 143 | + |
| 144 | + // The Selenium scrolling action is not precise and changes the scrollTop property to e.g. 202 instead of 200. |
| 145 | + // For this reason, we do not check for equality with specific value. |
| 146 | + var newScrollTop = int.Parse(scrollableDiv.GetDomProperty("scrollTop"), CultureInfo.InvariantCulture); |
| 147 | + Assert.True(newScrollTop >= 200); |
| 148 | + } |
| 149 | +} |
0 commit comments