-
Notifications
You must be signed in to change notification settings - Fork 79
More markdowntextblock improvements #771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
niels9001
wants to merge
24
commits into
main
Choose a base branch
from
niels9001/mdtb-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d70af24
Adding cornerradius to image
niels9001 86ce5b6
Making images scale accordingly
niels9001 0de08e8
Merge branch 'main' into niels9001/mdtb-improvements
niels9001 0811436
Apply suggestion from @Copilot
Arlodotexe 4cc9798
Add ImageProviderConstraintTest to verify theme constraint behavior
Arlodotexe db7cf00
Remove references about image corner radius
niels9001 acb4755
Update MyImage.cs
niels9001 bcfe6c6
XAML styling
niels9001 c7fd09a
Fix image scaling
niels9001 ffcd8ff
Add visual sample for image rescaling
niels9001 563f4b4
Update components/MarkdownTextBlock/tests/ImageProviderConstraintTest.cs
niels9001 9e35244
Update MarkdownTextBlockImageProviderSample.xaml
niels9001 ce2ce57
Merge branch 'niels9001/mdtb-improvements' of https://github.com/Comm…
niels9001 a2bfa7e
Update MarkdownTextBlockImageProviderSample.xaml.cs
niels9001 4501564
Update MarkdownTextBlockImageProviderSample.xaml.cs
niels9001 ad46d46
Change image tag in tests
niels9001 f93803e
Update MarkdownTextBlock.md
niels9001 b4fab55
Update MarkdownTextBlockImageProviderSample.xaml.cs
niels9001 4492759
Update MarkdownTextBlockImageProviderSample.xaml.cs
niels9001 079b375
Update MarkdownTextBlockImageProviderSample.xaml.cs
niels9001 cabc532
Revert "Change image tag in tests"
Arlodotexe 44e39a4
Use html syntax for specifying custom image size inline where appropr…
Arlodotexe 5a1d4f7
Fix async void issues
Arlodotexe b5c2514
Removing sample
niels9001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
components/MarkdownTextBlock/tests/ImageProviderConstraintTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using CommunityToolkit.Tooling.TestGen; | ||
| using CommunityToolkit.Tests; | ||
| using CommunityToolkit.WinUI.Controls; | ||
|
|
||
| namespace MarkdownTextBlockTests; | ||
|
|
||
| /// <summary> | ||
| /// Test class to verify image constraint behavior when using IImageProvider | ||
| /// Regression test for: https://github.com/CommunityToolkit/Labs-Windows/pull/771 | ||
| /// </summary> | ||
| [TestClass] | ||
| public partial class ImageProviderConstraintTest : VisualUITestBase | ||
| { | ||
| /// <summary> | ||
| /// Mock image provider that returns an image with specific dimensions | ||
| /// </summary> | ||
| private class TestImageProvider : IImageProvider | ||
| { | ||
| private readonly double _width; | ||
| private readonly double _height; | ||
|
|
||
| public TestImageProvider(double width = 100, double height = 100) | ||
| { | ||
| _width = width; | ||
| _height = height; | ||
| } | ||
|
|
||
| public bool ShouldUseThisProvider(string url) => url.StartsWith("test://"); | ||
|
|
||
| public Task<Image> GetImage(string url) | ||
| { | ||
| var image = new Image | ||
| { | ||
| // Simulate a 100x100 image with natural dimensions | ||
| Width = _width, | ||
| Height = _height, | ||
| Source = new Microsoft.UI.Xaml.Media.Imaging.BitmapImage() | ||
Arlodotexe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| return Task.FromResult(image); | ||
| } | ||
| } | ||
|
|
||
| [UIThreadTestMethod] | ||
| public async Task ImageProvider_WithThemeConstraints_ShouldNotApplyConstraintsToSmallerImages() | ||
| { | ||
| // Arrange: Image provider returns a 100x100 image | ||
| var provider = new TestImageProvider(width: 100, height: 100); | ||
|
|
||
| var config = new MarkdownConfig | ||
| { | ||
| ImageProvider = provider, | ||
| Themes = new MarkdownThemes | ||
| { | ||
| // Theme allows images up to 500px wide | ||
| ImageMaxWidth = 500, | ||
| ImageMaxHeight = 500 | ||
| } | ||
| }; | ||
|
|
||
| var markdown = new MarkdownTextBlock | ||
| { | ||
| Config = config, | ||
| // Image URL with no precedent dimensions specified | ||
| Text = "" | ||
| }; | ||
|
|
||
| // Act | ||
| await LoadTestContentAsync(markdown); | ||
|
|
||
| // Give the async image loading time to complete | ||
| await Task.Delay(500); | ||
| await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); | ||
|
|
||
| // Find the image element | ||
| var image = markdown.FindDescendant<Image>(); | ||
| Assert.IsNotNull(image, "Image element should be rendered"); | ||
|
|
||
| // Assert: The image should maintain its natural size (100x100) | ||
| // and NOT be constrained to the theme's larger MaxWidth (500). | ||
| // | ||
| // This is a regression test for a previous bug where, when IImageProvider was used, | ||
| // natural dimensions weren't set on the image, so MaxWidth defaulted to Infinity. | ||
| // In that case the theme constraint (500 < Infinity) incorrectly applied, forcing MaxWidth=500. | ||
| // | ||
| // The purpose of this test is to ensure that the image's MaxWidth is set to its natural size (100) | ||
| // before theme constraints are evaluated, so the theme constraint is not incorrectly applied. | ||
|
|
||
| Assert.AreEqual(100.0, image.MaxWidth, 0.1, | ||
| "Image MaxWidth should be its natural size (100), not the theme constraint (500). " + | ||
| "When using IImageProvider, natural dimensions must be set before applying theme constraints."); | ||
| } | ||
|
|
||
| [UIThreadTestMethod] | ||
| public async Task ImageProvider_WithPrecedentDimensions_ShouldUsePrecedent() | ||
| { | ||
| // Arrange: Image provider returns a 100x100 image | ||
| var provider = new TestImageProvider(width: 100, height: 100); | ||
|
|
||
| var config = new MarkdownConfig | ||
| { | ||
| ImageProvider = provider, | ||
| Themes = new MarkdownThemes | ||
| { | ||
| ImageMaxWidth = 500, | ||
| ImageMaxHeight = 500 | ||
| } | ||
| }; | ||
|
|
||
| var markdown = new MarkdownTextBlock | ||
| { | ||
| Config = config, | ||
| // Image with explicit width specified in markdown | ||
| Text = "<img src=\"test://example.png\" width=\"200\">" | ||
| }; | ||
|
|
||
| // Act | ||
| await LoadTestContentAsync(markdown); | ||
| await Task.Delay(500); | ||
| await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); | ||
|
|
||
| var image = markdown.FindDescendant<Image>(); | ||
| Assert.IsNotNull(image, "Image element should be rendered"); | ||
|
|
||
| // Assert: Precedent dimensions should be respected | ||
| Assert.AreEqual(200.0, image.MaxWidth, 0.1, | ||
| "Image MaxWidth should respect precedent dimension (200) from markdown"); | ||
| } | ||
|
|
||
| [UIThreadTestMethod] | ||
| public async Task ImageProvider_WithSmallerThemeConstraint_ShouldApplyThemeConstraint() | ||
| { | ||
| // Arrange: Image provider returns a 800x600 image | ||
| var provider = new TestImageProvider(width: 800, height: 600); | ||
|
|
||
| var config = new MarkdownConfig | ||
| { | ||
| ImageProvider = provider, | ||
| Themes = new MarkdownThemes | ||
| { | ||
| // Theme constrains images to max 400px | ||
| ImageMaxWidth = 400, | ||
| ImageMaxHeight = 400 | ||
| } | ||
| }; | ||
|
|
||
| var markdown = new MarkdownTextBlock | ||
| { | ||
| Config = config, | ||
| Text = "" | ||
| }; | ||
|
|
||
| // Act | ||
| await LoadTestContentAsync(markdown); | ||
| await Task.Delay(500); | ||
| await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { }); | ||
|
|
||
| var image = markdown.FindDescendant<Image>(); | ||
| Assert.IsNotNull(image, "Image element should be rendered"); | ||
|
|
||
| // Assert: When natural size (800) is larger than theme constraint (400), | ||
| // the theme constraint should apply | ||
| Assert.AreEqual(400.0, image.MaxWidth, 0.1, | ||
| "Image MaxWidth should be constrained to theme max (400) when natural size (800) is larger"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.