Skip to content

Commit 4e54ec5

Browse files
author
msftbot[bot]
authored
Fix ToastContentBuilder audio ms-winsoundevent and ms-appx (#3755)
<!-- 🚨 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 #3753 <!-- 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. --> Add support for using ms-windsoundevent and ms-appx in the toast content audio builder (previously we were incorrectly throwing an exception when devs provided those). Also added unit tests to ensure this works correctly and ensure we correctly throw on https audio urls and other non-supported ones. ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> - Bugfix <!-- - 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. --> Throws when you try to use `ms-winsoundevent:` or `ms-appx:`, both of which are supported by Windows. Today it only supports absolute file paths (`C:\audio.mp3`), which are only supported by desktop apps. ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> Doesn't throw for those. ## PR Checklist Please check if your PR fulfills the following requirements: - [x] Tested code with current [supported SDKs](../readme.md#supported) - [x] Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link --> - [x] Sample in sample app has been added / updated (for bug fixes / features) - [x] Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets) - [x] 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 f966319 + aa83b21 commit 4e54ec5

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

Microsoft.Toolkit.Uwp.Notifications/Toasts/Builder/ToastContentBuilder.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,25 +355,20 @@ public ToastContentBuilder AddAudio(
355355
bool? silent = default)
356356
#endif
357357
{
358-
if (!src.IsFile)
359-
{
360-
throw new ArgumentException(nameof(src), "Audio Source has to be a file.");
361-
}
362-
363-
Content.Audio = new ToastAudio();
364-
Content.Audio.Src = src;
358+
var audio = new ToastAudio();
359+
audio.Src = src;
365360

366361
if (loop != default)
367362
{
368-
Content.Audio.Loop = loop.Value;
363+
audio.Loop = loop.Value;
369364
}
370365

371366
if (silent != default)
372367
{
373-
Content.Audio.Silent = silent.Value;
368+
audio.Silent = silent.Value;
374369
}
375370

376-
return this;
371+
return AddAudio(audio);
377372
}
378373

379374
/// <summary>
@@ -383,6 +378,11 @@ public ToastContentBuilder AddAudio(
383378
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
384379
public ToastContentBuilder AddAudio(ToastAudio audio)
385380
{
381+
if (audio.Src != null && !audio.Src.IsFile && audio.Src.Scheme != "ms-appx" && audio.Src.Scheme != "ms-winsoundevent")
382+
{
383+
throw new InvalidOperationException("Audio Source must either be a ms-appx file, absolute file, or ms-winsoundevent.");
384+
}
385+
386386
Content.Audio = audio;
387387
return this;
388388
}

UnitTests/UnitTests.Notifications.Shared/TestToastContentBuilder.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,72 @@ public void AddAudioTest_WithFullArgs_ReturnSelfWithCustomAudioAddedWithAllOptio
548548
Assert.AreEqual(testToastAudioSilent, builder.Content.Audio.Silent);
549549
}
550550

551+
[TestMethod]
552+
public void AddAudioTest_WithMsWinSoundEvent_ReturnSelfWithCustomAudioAdded()
553+
{
554+
// Arrange
555+
Uri testAudioUriSrc = new Uri("ms-winsoundevent:Notification.Reminder");
556+
557+
// Act
558+
ToastContentBuilder builder = new ToastContentBuilder();
559+
ToastContentBuilder anotherReference = builder.AddAudio(testAudioUriSrc);
560+
561+
// Assert
562+
Assert.AreSame(builder, anotherReference);
563+
Assert.AreEqual(testAudioUriSrc.OriginalString, builder.Content.Audio.Src.OriginalString);
564+
}
565+
566+
[TestMethod]
567+
public void AddAudioTest_WithMsAppx_ReturnSelfWithCustomAudioAdded()
568+
{
569+
// Arrange
570+
Uri testAudioUriSrc = new Uri("ms-appx:///Assets/Audio.mp3");
571+
572+
// Act
573+
ToastContentBuilder builder = new ToastContentBuilder();
574+
ToastContentBuilder anotherReference = builder.AddAudio(testAudioUriSrc);
575+
576+
// Assert
577+
Assert.AreSame(builder, anotherReference);
578+
Assert.AreEqual(testAudioUriSrc.OriginalString, builder.Content.Audio.Src.OriginalString);
579+
}
580+
581+
[TestMethod]
582+
[ExpectedException(typeof(InvalidOperationException))]
583+
public void AddAudioTest_WithInvalidMsUri_ThrowException()
584+
{
585+
// Arrange
586+
Uri testAudioUriSrc = new Uri("ms-doesntexist:Notification.Reminder");
587+
588+
// Act
589+
ToastContentBuilder builder = new ToastContentBuilder();
590+
builder.AddAudio(testAudioUriSrc);
591+
}
592+
593+
[TestMethod]
594+
[ExpectedException(typeof(InvalidOperationException))]
595+
public void AddAudioTest_WithInvalidAppDataUri_ThrowException()
596+
{
597+
// Arrange (ms-appdata isn't currently supported)
598+
Uri testAudioUriSrc = new Uri("ms-appdata:///local/Sound.mp3");
599+
600+
// Act
601+
ToastContentBuilder builder = new ToastContentBuilder();
602+
builder.AddAudio(testAudioUriSrc);
603+
}
604+
605+
[TestMethod]
606+
[ExpectedException(typeof(InvalidOperationException))]
607+
public void AddAudioTest_WithInvalidHttpUri_ThrowException()
608+
{
609+
// Arrange
610+
Uri testAudioUriSrc = new Uri("https://myaudio.com/song.mp3");
611+
612+
// Act
613+
ToastContentBuilder builder = new ToastContentBuilder();
614+
builder.AddAudio(testAudioUriSrc);
615+
}
616+
551617
[TestMethod]
552618
public void AddAudioTest_WithAudioObject_ReturnSelfWithCustomAudioAdded()
553619
{

0 commit comments

Comments
 (0)