-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
ReactWindows/ReactNative.Tests/Modules/Toast/ToastNotificationTests.cs
This file contains 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,62 @@ | ||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; | ||
using ReactNative.Bridge; | ||
using ReactNative.Modules.Toast; | ||
using System; | ||
|
||
namespace ReactNative.Tests.Modules.Toast | ||
{ | ||
[TestClass] | ||
public class ToastNotificationTests | ||
{ | ||
const string TEST_CATEGORY = "Modules"; | ||
|
||
[TestMethod] | ||
[TestCategory(TEST_CATEGORY)] | ||
public void ToastModule_Null_ArgumentsTest() | ||
{ | ||
AssertEx.Throws<ArgumentNullException>( | ||
() => new ToastModule(null), | ||
ex => Assert.AreEqual("reactContext", ex.ParamName)); | ||
|
||
|
||
var context = new ReactApplicationContext(); | ||
var module = new ToastModule(context); | ||
Assert.AreSame(context, module.Context); | ||
|
||
} | ||
|
||
[TestMethod] | ||
[TestCategory(TEST_CATEGORY)] | ||
public void Send_Toast_Invalid_Duration() | ||
{ | ||
var context = new ReactApplicationContext(); | ||
var module = new ToastModule(context); | ||
|
||
AssertEx.Throws<ArgumentException>( | ||
() => module.show("Invalid Toast", -1), | ||
ex => Assert.AreEqual("duration", ex.ParamName)); | ||
} | ||
|
||
[TestMethod] | ||
[TestCategory(TEST_CATEGORY)] | ||
public void Send_Basic_Toast() | ||
{ | ||
var context = new ReactApplicationContext(); | ||
var module = new ToastModule(context); | ||
|
||
module.show("SHORT TOAST", 0); | ||
} | ||
|
||
[TestMethod] | ||
[TestCategory(TEST_CATEGORY)] | ||
public void Send_Long_Toast() | ||
{ | ||
var context = new ReactApplicationContext(); | ||
var module = new ToastModule(context); | ||
|
||
module.show("LONG TOAST container", 1); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains 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 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,60 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Windows.Data.Xml.Dom; | ||
using Windows.UI.Notifications; | ||
|
||
namespace ReactNative.Modules.Toast | ||
{ | ||
class ToastHelper | ||
{ | ||
internal static void SendToast(string message, string duration = "short") | ||
{ | ||
var doc = MakeDoc(message).Result; | ||
var notification = new ToastNotification(doc); | ||
|
||
// Get the toast notification manager for the current app. | ||
ToastNotificationManager.CreateToastNotifier().Show(notification); | ||
} | ||
|
||
static async Task<XmlDocument> MakeDoc(string content, Durations duration = Durations.@short) | ||
{ | ||
/// TODO: alt templates: https://msdn.microsoft.com/en-us/library/windows/apps/hh761494.aspx | ||
using (var stream = new MemoryStream()) | ||
using (var reader = new StreamReader(stream)) | ||
{ | ||
await BuildNotificationXml(stream, content, duration); | ||
stream.Position = 0; | ||
var xml = await reader.ReadToEndAsync(); | ||
var doc = new XmlDocument(); | ||
doc.LoadXml(xml); | ||
return doc; | ||
} | ||
} | ||
|
||
static async Task BuildNotificationXml(Stream stream, string content, Durations duration = Durations.@short) | ||
{ | ||
System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings(); | ||
settings.OmitXmlDeclaration = true; | ||
settings.Async = true; | ||
|
||
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stream, settings)) | ||
{ | ||
await writer.WriteStartElementAsync(null, "toast", null); | ||
await writer.WriteAttributeStringAsync(null, "activationType", null, "foreground"); | ||
await writer.WriteAttributeStringAsync(null, "duration", null, duration.ToString()); | ||
|
||
await writer.WriteStartElementAsync(null, "visual", null); | ||
await writer.WriteStartElementAsync(null, "binding", null); | ||
await writer.WriteAttributeStringAsync(null, "template", null, "ToastText01"); | ||
|
||
await writer.WriteStartElementAsync(null, "text", null); | ||
await writer.WriteAttributeStringAsync(null, "id", null, "1"); | ||
await writer.WriteStringAsync(content); | ||
await writer.WriteEndElementAsync(); | ||
|
||
await writer.WriteEndElementAsync(); | ||
await writer.FlushAsync(); | ||
} | ||
} | ||
} | ||
} |
This file contains 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,52 @@ | ||
using ReactNative.Bridge; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace ReactNative.Modules.Toast | ||
{ | ||
enum Durations : int { @short, @long } | ||
|
||
public sealed class ToastModule : ReactContextNativeModuleBase | ||
{ | ||
const string DURATION_SHORT_KEY = "SHORT"; | ||
const string DURATION_LONG_KEY = "LONG"; | ||
|
||
public ToastModule(ReactApplicationContext reactContext) | ||
: base(reactContext) | ||
{ } | ||
|
||
public override string Name | ||
{ | ||
get | ||
{ | ||
return "ToastModule"; | ||
} | ||
} | ||
|
||
public override IReadOnlyDictionary<string, object> Constants | ||
{ | ||
get | ||
{ | ||
return new Dictionary<string, object> | ||
{ | ||
{ DURATION_SHORT_KEY, DURATION_SHORT_KEY }, | ||
{ DURATION_LONG_KEY, DURATION_LONG_KEY }, | ||
}; | ||
} | ||
} | ||
|
||
[ReactMethod] | ||
public void show(string message, int duration) | ||
{ | ||
if (Enum.IsDefined(typeof(Durations), duration)) | ||
{ | ||
var durationToUse = (Durations)duration; | ||
ToastHelper.SendToast(message, durationToUse.ToString()); | ||
} | ||
else | ||
{ | ||
throw new ArgumentException("invalid duration for Toast", "duration"); | ||
} | ||
} | ||
} | ||
} |
This file contains 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