Skip to content

Commit 017153e

Browse files
authored
Merge branch 'master' into deprecate-radial
2 parents fb55c8d + 4e54ec5 commit 017153e

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)