Skip to content

Commit 8fb123f

Browse files
author
msftbot[bot]
authored
Fixed NRE when setting text on Wide tile only via TileContentBuilder (#3994)
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 --> <!-- 📝 It is preferred if you keep the "☑️ Allow edits by maintainers" checked in the Pull Request Template as it increases collaboration with the Toolkit maintainers by permitting commits to your PR branch (only) created from your fork. This can let us quickly make fixes for minor typos or forgotten StyleCop issues during review without needing to wait on you doing extra work. Let us help you help us! 🎉 --> ## Fixes #3955 <!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - Bugfix - Unit test added <!-- - Feature --> <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. --> Issue #3955 ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> Text on **Wide tile** is correctly set without throwing `NullReferenceException`. ## PR Checklist Please check if your PR fulfills the following requirements: - [x] Tested code with current [supported SDKs](../readme.md#supported) - [ ] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link --> - [ ] Sample in sample app has been added / updated (for bug fixes / features) - [ ] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets) - [ ] New major technical changes in the toolkit have or will be added to the [Wiki](https://github.com/windows-toolkit/WindowsCommunityToolkit/wiki) e.g. build changes, source generators, testing infrastructure, sample creation changes, etc... - [x] Tests for the changes have been added (for bug fixes / features) (if applicable) - [x] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [x] Contains **NO** breaking changes <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. Please note that breaking changes are likely to be rejected within minor release cycles or held until major versions. --> ## Other information
2 parents 0bb6eb8 + d8a0794 commit 8fb123f

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

Microsoft.Toolkit.Uwp.Notifications/Tiles/Builder/TileContentBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ public TileContentBuilder AddAdaptiveTileVisualChild(ITileBindingContentAdaptive
499499
GetAdaptiveTileContent(MediumTile).Children.Add(child);
500500
}
501501

502-
if (size.HasFlag(TileSize.Wide) && WideTile != null && GetAdaptiveTileContent(MediumTile) != null)
502+
if (size.HasFlag(TileSize.Wide) && WideTile != null && GetAdaptiveTileContent(WideTile) != null)
503503
{
504504
GetAdaptiveTileContent(WideTile).Children.Add(child);
505505
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Linq;
6+
using Microsoft.Toolkit.Uwp.Notifications;
7+
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
9+
namespace UnitTests.Notifications
10+
{
11+
#if !WINRT
12+
[TestClass]
13+
public class TestTileContentBuilder
14+
{
15+
[TestMethod]
16+
public void AddTextTest_OnSmallTileOnly()
17+
{
18+
// Arrange
19+
string text = "text on small tile";
20+
TileContentBuilder builder = new TileContentBuilder();
21+
builder.AddTile(TileSize.Small);
22+
23+
// Act
24+
builder.AddText(text);
25+
26+
// Assert
27+
var tileText = GetTileAdaptiveText(builder, TileSize.Small);
28+
Assert.IsNotNull(tileText);
29+
Assert.AreSame("text on small tile", (string)tileText.Text);
30+
}
31+
32+
[TestMethod]
33+
public void AddTextTest_OnMediumTileOnly()
34+
{
35+
// Arrange
36+
string text = "text on medium tile";
37+
TileContentBuilder builder = new TileContentBuilder();
38+
builder.AddTile(TileSize.Medium);
39+
40+
// Act
41+
builder.AddText(text);
42+
43+
// Assert
44+
var tileText = GetTileAdaptiveText(builder, TileSize.Medium);
45+
Assert.IsNotNull(tileText);
46+
Assert.AreSame("text on medium tile", (string)tileText.Text);
47+
}
48+
49+
[TestMethod]
50+
public void AddTextTest_OnWideTileOnly()
51+
{
52+
// Arrange
53+
string text = "text on wide tile";
54+
TileContentBuilder builder = new TileContentBuilder();
55+
builder.AddTile(TileSize.Wide);
56+
57+
// Act
58+
builder.AddText(text);
59+
60+
// Assert
61+
var tileText = GetTileAdaptiveText(builder, TileSize.Wide);
62+
Assert.IsNotNull(tileText);
63+
Assert.AreSame("text on wide tile", (string)tileText.Text);
64+
}
65+
66+
[TestMethod]
67+
public void AddTextTest_OnLargeTileOnly()
68+
{
69+
// Arrange
70+
string text = "text on large tile";
71+
TileContentBuilder builder = new TileContentBuilder();
72+
builder.AddTile(TileSize.Large);
73+
74+
// Act
75+
builder.AddText(text);
76+
77+
// Assert
78+
var tileText = GetTileAdaptiveText(builder, TileSize.Large);
79+
Assert.IsNotNull(tileText);
80+
Assert.AreSame("text on large tile", (string)tileText.Text);
81+
}
82+
83+
private static AdaptiveText GetTileAdaptiveText(TileContentBuilder builder, TileSize size)
84+
{
85+
TileBinding tileBinding;
86+
switch (size)
87+
{
88+
case TileSize.Small:
89+
tileBinding = builder.Content.Visual.TileSmall;
90+
break;
91+
92+
case TileSize.Medium:
93+
tileBinding = builder.Content.Visual.TileMedium;
94+
break;
95+
96+
case TileSize.Wide:
97+
tileBinding = builder.Content.Visual.TileWide;
98+
break;
99+
100+
case TileSize.Large:
101+
tileBinding = builder.Content.Visual.TileLarge;
102+
break;
103+
104+
default:
105+
return null;
106+
}
107+
108+
var content = (TileBindingContentAdaptive)tileBinding.Content;
109+
return content.Children.FirstOrDefault() as AdaptiveText;
110+
}
111+
}
112+
#endif
113+
}

UnitTests/UnitTests.Notifications.Shared/UnitTests.Notifications.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)TestAssertHelper.cs" />
1313
<Compile Include="$(MSBuildThisFileDirectory)TestMail.cs" />
14+
<Compile Include="$(MSBuildThisFileDirectory)TestTileContentBuilder.cs" />
1415
<Compile Include="$(MSBuildThisFileDirectory)TestToastArguments.cs" />
1516
<Compile Include="$(MSBuildThisFileDirectory)TestToastContentBuilder.cs" />
1617
<Compile Include="$(MSBuildThisFileDirectory)TestWeather.cs" />

0 commit comments

Comments
 (0)