Adds unit test for native module registry configuration writer.
This commit is contained in:
Родитель
7c04c27865
Коммит
0935062f79
|
@ -1,7 +1,10 @@
|
|||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using ReactNative.Bridge;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace ReactNative.Tests.Bridge
|
||||
{
|
||||
|
@ -28,9 +31,12 @@ namespace ReactNative.Tests.Bridge
|
|||
[TestMethod]
|
||||
public void NativeModuleRegistry_Override_Allowed()
|
||||
{
|
||||
var builder = new NativeModuleRegistry.Builder();
|
||||
builder.Add(new OverrideAllowedModule());
|
||||
builder.Add(new OverrideAllowedModule());
|
||||
var registry = new NativeModuleRegistry.Builder()
|
||||
.Add(new OverrideAllowedModule())
|
||||
.Add(new OverrideAllowedModule())
|
||||
.Build();
|
||||
|
||||
Assert.AreEqual(1, registry.Modules.Count());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
@ -42,6 +48,47 @@ namespace ReactNative.Tests.Bridge
|
|||
ex => Assert.AreEqual("module", ex.ParamName));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NativeModuleRegistry_WriteModuleDefinitions()
|
||||
{
|
||||
var registry = new NativeModuleRegistry.Builder()
|
||||
.Add(new TestNativeModule())
|
||||
.Build();
|
||||
|
||||
using (var stringWriter = new StringWriter())
|
||||
{
|
||||
using (var writer = new JsonTextWriter(stringWriter))
|
||||
{
|
||||
registry.WriteModuleDescriptions(writer);
|
||||
}
|
||||
|
||||
var actual = JObject.Parse(stringWriter.ToString());
|
||||
Assert.AreEqual(1, actual.Properties().Count());
|
||||
|
||||
var moduleDef = actual.GetValue("Test") as JObject;
|
||||
Assert.IsNotNull(moduleDef);
|
||||
|
||||
var moduleId = moduleDef.GetValue("moduleID");
|
||||
Assert.IsNotNull(moduleId);
|
||||
Assert.AreEqual("0", moduleId.ToString());
|
||||
|
||||
var methods = moduleDef.GetValue("methods") as JObject;
|
||||
Assert.IsNotNull(methods);
|
||||
|
||||
var fooMethod = methods.GetValue("Foo") as JObject;
|
||||
Assert.IsNotNull(fooMethod);
|
||||
|
||||
var barMethod = methods.GetValue("Bar") as JObject;
|
||||
Assert.IsNotNull(barMethod);
|
||||
|
||||
var fooMethodId = fooMethod.GetValue("methodID");
|
||||
var barMethodId = barMethod.GetValue("methodID");
|
||||
Assert.AreNotEqual(fooMethodId.ToString(), barMethodId.ToString());
|
||||
Assert.IsTrue(fooMethodId.ToString() == "0" || fooMethodId.ToString() == "1");
|
||||
Assert.IsTrue(barMethodId.ToString() == "0" || barMethodId.ToString() == "1");
|
||||
}
|
||||
}
|
||||
|
||||
class OverrideDisallowedModule : NativeModuleBase
|
||||
{
|
||||
public override string Name
|
||||
|
@ -82,5 +129,22 @@ namespace ReactNative.Tests.Bridge
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestNativeModule : NativeModuleBase
|
||||
{
|
||||
public override string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Test";
|
||||
}
|
||||
}
|
||||
|
||||
[ReactMethod]
|
||||
public void Foo(int x) { }
|
||||
|
||||
[ReactMethod]
|
||||
public void Bar(string x) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ namespace ReactNative.Bridge
|
|||
});
|
||||
}
|
||||
|
||||
public void InvokeFunction(int moduleId, int methodId, JArray arguments, string tracingName)
|
||||
public /* TODO: internal? */ void InvokeFunction(int moduleId, int methodId, JArray arguments, string tracingName)
|
||||
{
|
||||
_catalystQueueConfiguration.JSQueueThread.RunOnQueue(() =>
|
||||
{
|
||||
|
|
|
@ -56,6 +56,24 @@ namespace ReactNative.Bridge
|
|||
throw new InvalidOperationException("No module instance for type '{0}'.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write the module descriptions to the given <see cref="JsonWriter"/>.
|
||||
/// </summary>
|
||||
/// <param name="writer">The JSON writer.</param>
|
||||
public /* TODO: internal? */ void WriteModuleDescriptions(JsonWriter writer)
|
||||
{
|
||||
using (Tracer.Trace(Tracer.TRACE_TAG_REACT_BRIDGE, "CreateJSON"))
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
foreach (var moduleDef in _moduleTable)
|
||||
{
|
||||
writer.WritePropertyName(moduleDef.Name);
|
||||
moduleDef.WriteModuleDescription(writer);
|
||||
}
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invoke a method on a native module.
|
||||
/// </summary>
|
||||
|
@ -109,24 +127,6 @@ namespace ReactNative.Bridge
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write the module descriptions to the given <see cref="JsonWriter"/>.
|
||||
/// </summary>
|
||||
/// <param name="writer">The JSON writer.</param>
|
||||
internal /* TODO: public? */ void WriteModuleDescriptions(JsonWriter writer)
|
||||
{
|
||||
using (Tracer.Trace(Tracer.TRACE_TAG_REACT_BRIDGE, "CreateJSON"))
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
foreach (var moduleDef in _moduleTable)
|
||||
{
|
||||
writer.WritePropertyName(moduleDef.Name);
|
||||
moduleDef.WriteModuleDescription(writer);
|
||||
}
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
|
||||
class ModuleDefinition
|
||||
{
|
||||
private readonly int _id;
|
||||
|
|
Загрузка…
Ссылка в новой задаче