Skip to content

Commit

Permalink
adding ToastModule with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cicorias committed Jan 5, 2016
1 parent 0a7455a commit 1ebfc89
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
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);
}


}
}
1 change: 1 addition & 0 deletions ReactWindows/ReactNative.Tests/ReactNative.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Compile Include="Internal\MockEvent.cs" />
<Compile Include="Modules\Core\RCTDeviceEventEmitterTests.cs" />
<Compile Include="Modules\Core\RCTNativeAppEventEmitterTests.cs" />
<Compile Include="Modules\Toast\ToastNotificationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReactInstanceManagerTests.cs" />
<Compile Include="UIManager\AppRegistryTests.cs" />
Expand Down
60 changes: 60 additions & 0 deletions ReactWindows/ReactNative/Modules/Toast/ToastHelper.cs
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();
}
}
}
}
52 changes: 52 additions & 0 deletions ReactWindows/ReactNative/Modules/Toast/ToastModule.cs
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");
}
}
}
}
2 changes: 2 additions & 0 deletions ReactWindows/ReactNative/ReactNative.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@
<Compile Include="Modules\Core\ExceptionsManagerModule.cs" />
<Compile Include="Modules\Core\Timing.cs" />
<Compile Include="Modules\Storage\AsyncStorageModule.cs" />
<Compile Include="Modules\Toast\ToastHelper.cs" />
<Compile Include="Modules\Toast\ToastModule.cs" />
<Compile Include="Modules\WebSocket\WebSocketModule.cs" />
<Compile Include="ReactInstanceManagerImpl.cs" />
<Compile Include="Hosting\JavaScriptBackgroundWorkItemCallback.cs" />
Expand Down

0 comments on commit 1ebfc89

Please sign in to comment.