public void Value_from_Boolean(bool input) { var actual = SciterValue.Create(input); Assert.IsTrue(actual.IsBool); Assert.AreEqual(input, actual.AsBoolean(!input)); }
public void Value_from_Double(double input) { var actual = SciterValue.Create(input); Assert.IsTrue(actual.IsFloat); Assert.AreEqual(input, actual.AsDouble()); }
public void Value_is_not_a_Boolean() { var actual = SciterValue.Create("string"); Assert.IsFalse(actual.IsBool); Assert.AreEqual(false, actual.AsBoolean()); }
public void Value_MapTo_v1() { var obj = new { key1 = (object)null, key2 = true, key3 = (byte)1, key4 = 1, key5 = 1d, key6 = 1f, key7 = 1L }; var actual = SciterValue.Create(obj); Assert.IsTrue(actual.IsMap); var mapToClass = actual.MapTo <TestClass>(); Assert.NotNull(mapToClass); Assert.AreEqual(mapToClass.key1, null); Assert.AreEqual(mapToClass.key2, true); Assert.AreEqual(mapToClass.key3, (byte)1); Assert.AreEqual(mapToClass.key4, 1); Assert.AreEqual(mapToClass.DoubleProperty, 1d); Assert.AreEqual(mapToClass.SingleProperty, 1f); Assert.AreEqual(mapToClass.LongProperty, 1L); }
public void Value_from_ByteArray(byte[] input) { var actual = SciterValue.Create(input); Assert.IsTrue(actual.IsBytes); Assert.AreEqual(input ?? new byte[0], actual.AsBytes()); }
public void Value_from_Float(float input) { var actual = SciterValue.Create(input); Assert.IsTrue(actual.IsFloat); Assert.AreEqual(input, actual.AsFloat()); }
public void Value_from_ValueArray() { var input = new List <SciterValue> { SciterValue.Create(1), SciterValue.Create(1u), SciterValue.Create(1d), SciterValue.Create("string"), SciterValue.Create(DateTime.Now), SciterValue.Create(true), SciterValue.Create(1f), }; var actual = SciterValue.Create(input.ToArray()); Assert.IsTrue(actual.IsArray); Assert.AreEqual(input.Count, actual.Length); Assert.AreEqual(input[0].AsInt32(-1), actual.GetItem(0).AsInt32()); Assert.AreEqual(input[1].AsDouble(-1u), actual.GetItem(1).AsUInt32()); Assert.AreEqual(input[2].AsDouble(-1d), actual.GetItem(2).AsDouble()); Assert.AreEqual(input[3].AsString("exp"), actual.GetItem(3).AsString("actual")); Assert.AreEqual(input[4].AsDateTime(), actual.GetItem(4).AsDateTime()); Assert.AreEqual(input[5].AsBoolean(), actual.GetItem(5).AsBoolean(true)); Assert.AreEqual(input[6].AsFloat(-1f), actual.GetItem(6).AsFloat()); }
public void Value_from_String(string input) { var actual = SciterValue.Create(input); Assert.IsTrue(input == null ? actual.IsUndefined : actual.IsString); Assert.AreEqual(input, actual.AsString()); }
public void Value_from_UInt32(uint input) { var actual = SciterValue.Create(input); Assert.IsTrue(actual.IsInt); Assert.AreEqual(input, actual.AsUInt32()); }
public void Value_is_not_an_IEnumerable() { var actual = SciterValue.Create("Not a IEnumerable<>"); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed Assert.Throws <InvalidOperationException>(() => actual.AsEnumerable().ToList()); }
public void Value_Clear() { var actual = SciterValue .Create("some value to discard") .Clear(); Assert.IsTrue(actual.IsUndefined); }
public void Value_is_not_an_Angle() { var actual = SciterValue.Create("string"); Assert.IsFalse(actual.IsAngle); Assert.Throws <InvalidOperationException>(() => actual.AsAngle()); }
public void Value_from_SciterColor_RGBA(byte r, byte g, byte b, byte a) { var color = SciterColor.Create(r, g, b, a); var actual = SciterValue.Create(color); Assert.IsTrue(actual.IsColor); Assert.AreEqual(color, actual.AsColor()); }
public void Value_from_Object() { var list = new List <int> { 1, 2, 3 }; var obj = new { list = list }; var actual = SciterValue.Create(obj); Assert.IsTrue(actual.IsMap); Assert.IsTrue(actual["list"].IsArray); Assert.AreEqual(list, actual["list"].AsEnumerable().Select(s => s.AsInt32())); }
public void Value_from_Dictionary() { var dictionary = new Dictionary <string, SciterValue> { { "int", SciterValue.Create(1) }, { "string", SciterValue.Create("string") }, { "double", SciterValue.Create(3d) }, { "color", SciterValue.Create(SciterColor.Khaki) } }; var actual = SciterValue.Create(dictionary); Assert.IsTrue(actual.IsMap); }
public Task ThrowException(SciterValue numerator, SciterValue denominator) { try { //This will purposely throw an exception! var value = numerator.AsInt32() / denominator.AsInt32(); return(Task.FromResult(SciterValue.Create(value))); } catch (Exception ex) { _logger.LogError(exception: ex, message: "{Message}", ex.Message); throw; } }
public void Value_get_Keys() { var dictionary = new Dictionary <string, IConvertible> { { "key1", null }, { "key2", true }, { "key3", (byte)1 }, { "key4", 1 } }; var actual = SciterValue.Create(dictionary); Assert.IsTrue(actual.IsMap); Assert.AreEqual(dictionary.Keys, actual.Keys.Select(s => s.AsString())); }
public async Task CallMeBack(SciterElement element, SciterValue value, SciterValue onProgress, SciterValue onCompleted) { _callMeBackResetEvent = new ManualResetEventSlim(false); for (var i = 0; i < 201; i++) { if (_callMeBackResetEvent.IsSet) { break; } //Simulates a delay await Task.Delay(10); onProgress.Invoke(SciterValue.Create(i), SciterValue.Create(i / 200d * 100)); } onCompleted.Invoke(SciterValue.Create($"You have {(!_callMeBackResetEvent.IsSet ? "successfully completed" : "cancelled")} your task!"), SciterValue.Create(!_callMeBackResetEvent.IsSet)); }
public ApplicationHost(ILogger <ApplicationHost> logger, ApplicationWindow wnd, HostEventHandler hostEventHandler) : base(logger, wnd) { var host = this; host .AttachEventHandler(hostEventHandler) //.RegisterBehaviorHandler<VirtualTreeBehavior>() .RegisterBehaviorHandler <DragDropBehavior>() .RegisterBehaviorHandler <CustomWindowEventHandler>(); host.LoadPage("vtree.html", onFailed: (sciterHost, window) => throw new InvalidOperationException("Unable to load the requested page.")); host.Window.OnShow += (sender, args) => { var treeElement = wnd.RootElement.SelectFirst("widget#tree"); treeElement.AttachEventHandler <VirtualTreeBehavior>(); var button = wnd.RootElement.SelectFirst("#new-dialog"); button.FireEvent(new SciterBehaviorArgs() { Command = BehaviorEvents.ButtonClick, Target = button, Source = button, Name = "Hello", Data = SciterValue.Create("World!"), }); //var window2 = DevZH.UI.Window.AttachWindow(host.Window.WindowHandle); //var window3 = DevZH.UI.Window.AttachWindow(host.Window.WindowHandle); //window2.Title = window2.Title += "::libui"; //var t = window2.Title; //var t3 = window3.Title; #if DEBUG host.ConnectToInspector(); #endif }; }
static void Main(string[] args) { var list = new List <int> { 123 }; var ss = SciterValue.Create(new { aa = list }); Console.WriteLine($@"Sciter: {Sciter.SciterApi.SciterVersion()}"); Console.WriteLine("Bitness: " + IntPtr.Size); // Platform specific (required for GTK) SciterPlatform.Initialize(); // SciterCore needs this for drag 'n drop support (on Windows) SciterPlatform.EnableDragAndDrop(); // Create the window AppWindow = new SciterWindow() .CreateMainWindow(800, 600) .CenterWindow() .SetTitle("SciterCore.Windows::Core") .SetIcon(SciterTest.Core.Properties.Resources.IconMain); // Prepares SciterHost and then load the page AppHost = new AppHost(AppWindow); AppHost .SetupWindow(AppWindow) .AttachEventHandler(new AppEventHandler()); AppHost.SetupPage("index.html"); //AppHost.ConnectToInspector(); //byte[] css_bytes = File.ReadAllBytes(@"D:\ProjetosSciter\AssetsDrop\AssetsDrop\res\css\global.css"); //SciterX.API.SciterAppendMasterCSS(css_bytes, (uint) css_bytes.Length); Debug.Assert(!AppHost.EvalScript("Utils").IsUndefined); // Show window and Run message loop AppWindow.Show(); SciterPlatform.RunMessageLoop(); }
public Task StackTrace(SciterElement element, SciterValue onCompleted) { var stackTrace = new StackTrace(true); var stackFrame = stackTrace.GetFrame(0); var value = SciterValue.Create( new { MethodName = stackFrame?.GetMethod()?.Name, Parameters = stackFrame?.GetMethod()?.GetParameters().Select(s => new { s.Name, s.Position, Type = s.ParameterType.Name }), FileUri = new Uri(stackFrame?.GetFileName() ?? string.Empty).AbsoluteUri, FileName = Path.GetFileName(stackFrame?.GetFileName()), LineNumber = stackFrame?.GetFileLineNumber(), ColumnNumber = stackFrame?.GetFileColumnNumber() }); onCompleted.Invoke(value); return(Task.CompletedTask); }
public Task GetRuntimeInfo(SciterElement element, SciterValue onCompleted, SciterValue onError) { try { var value = SciterValue.Create( new { FrameworkDescription = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription, ProcessArchitecture = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString(), OSArchitecture = System.Runtime.InteropServices.RuntimeInformation.OSArchitecture.ToString(), OSDescription = System.Runtime.InteropServices.RuntimeInformation.OSDescription, SystemVersion = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion() }); onCompleted.Invoke(value); } catch (Exception e) { onError.Invoke(SciterValue.MakeError(e.Message)); } return(Task.CompletedTask); }
private ScriptEventResult ExceptionCallbackResult(Exception e) { if (!_isWrappedCallback || !_isAwaitable) { return(ScriptEventResult.Successful(SciterValue.MakeError(e?.Message))); } //TODO: Clean this up, maybe change the Dictionary<> implementation? var properties = (e) .GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(w => typeof(IConvertible).IsAssignableFrom(w.PropertyType)) .ToDictionary(key => key.Name, value => value.GetValue(e) as IConvertible); //.ToDictionary(key => key.Name, value => SciterValue.Create(value.GetValue(e.InnerException))); properties.Add(nameof(Type), e?.GetType().FullName); _callbackValue?.Invoke(SciterValue.Null, SciterValue.Create(properties)); return(ScriptEventResult.Successful()); }
public void TestSciterValue() { //string[] arr = new string[] { "A", "B", "C" }; int[] arr = new int[] { 1, 2, 3 }; //SciterValue res = SciterValue.FromList(arr); SciterCore.SciterValue res = new SciterCore.SciterValue(); res.Append(SciterValue.Create(1)); res.Append(SciterValue.Create(1)); res.Append(SciterValue.Create(1)); res.Append(SciterValue.MakeSymbol("symbol").Append(SciterValue.Create(1))); string r = res.ToString(); string r2 = res.ToString(); string r3 = res.AsJsonString(StringConversionType.JsonLiteral); { // http://sciter.com/forums/topic/erasing-sequence-elements-with-scitervalue/ SciterCore.SciterValue sv = SciterCore.SciterValue.FromJsonString("[1,2,3,4,5])"); sv[0] = SciterCore.SciterValue.Undefined; sv[2] = SciterCore.SciterValue.Undefined; SciterCore.SciterValue sv2 = SciterCore.SciterValue.FromJsonString("{one: 1, two: 2, three: 3}"); sv2["one"] = SciterCore.SciterValue.Undefined; Assert.IsTrue(sv2["two"].AsInt32(0) == 2); } // Datetime { var now = DateTime.Now; SciterValue sv = SciterValue.Create(now); Assert.IsTrue(sv.AsDateTime(false) == now); } // SciterValue.AsDictionary { SciterCore.SciterValue sv = SciterCore.SciterValue.FromJsonString("{ a: 1, b: true }"); var dic = sv.AsDictionary(); Assert.IsTrue(dic.Count == 2); } }
public void Value_from_IConvertible_Dictionary() { var dictionary = new Dictionary <string, IConvertible> { { "null", null }, { "bool", true }, { "byte", (byte)1 }, { "int", 1 }, { "string", "string" }, { "double", 0d }, { "uint", 0u }, { "decimal", 0m }, { "dateTime", DateTime.UtcNow.ToFileTime() }, { "color", SciterColor.Khaki } }; var actual = SciterValue.Create(dictionary); Assert.IsTrue(actual.IsMap); Assert.AreEqual(dictionary, actual.AsDictionary()); }
/// A dynamic script call handler. Any call in TIScript to function 'view.Host_HelloSciter()' with invoke this method /// Notice that signature of these handlers is always the same /// (Hint: install OmniCode snippets which adds the 'ssh' snippet to C# editor so you can easily declare 'Siter Handler' methods) /// (see: https://github.com/MISoftware/OmniCode-Snippets) public Task HelloSciterCore(SciterElement element, SciterValue onCompleted) { var stackTrace = new StackTrace(true); var stackFrame = stackTrace.GetFrame(0); var value = SciterValue.Create(System.Text.Json.JsonSerializer.Serialize(new { MethodName = stackFrame?.GetMethod()?.Name, FileUri = new Uri(stackFrame?.GetFileName() ?? "file://<unknown>")?.AbsoluteUri, FileName = Path.GetFileName(stackFrame?.GetFileName()), LineNumber = stackFrame?.GetFileLineNumber(), ColumnNumber = stackFrame?.GetFileColumnNumber() }, options: new JsonSerializerOptions() { WriteIndented = true })); //value = SciterValue.Create($"<h2>Hello Sciter from C# in .Net Core!</h2><code>Method: {stackFrame?.GetMethod()?.Name}<br/>File: <a href=\"{new Uri(stackFrame?.GetFileName())?.AbsoluteUri}\">{Path.GetFileName(stackFrame?.GetFileName())}</a><br/>Line: {stackFrame?.GetFileLineNumber()}<br/>Column: {stackFrame?.GetFileColumnNumber()}</code>"); onCompleted.Invoke(value); return(Task.CompletedTask); }
public void GetDotNetVersion(SciterElement element, SciterValue onCompleted) { var value = SciterValue.Create(RuntimeInformation.FrameworkDescription); onCompleted.Invoke(value); }
public void Value_is_not_an_IConvertible_Dictionary() { var actual = SciterValue.Create("Not a Dictionary<>"); Assert.Throws <InvalidOperationException>(() => actual.AsDictionary()); }
public void Value_from_Object_recursion_too_deep() { var obj = new { a = new { b = new { c = new { d = new { e = new { f = new { g = new { h = new { i = new { j = new { k = new { } } } } } } } } } } } }; Assert.Throws <InvalidOperationException>(() => SciterValue.Create(obj)); }
public void Value_MapTo_v2() { var obj = new { key1 = (object)null, key2 = true, key3 = (byte)1, key4 = 1, key5 = 1d, key6 = 1f, key7 = 1L, key8 = new { key1 = (object)null, key2 = true, key3 = (byte)2, key4 = 2, key5 = 2d, SingleProperty = 2f, LongProperty = 2L, }, key9 = new { key1 = (object)null, key2 = true, key3 = (byte)3, key4 = 3, DoubleProperty = 3d, key6 = 3f, key7 = 3L, key8 = new { key1 = (object)null, key2 = true, key3 = (byte)4, key4 = 4, DoubleProperty = 4d, SingleProperty = 4f, LongProperty = 4L, }, key9 = new { key1 = (object)null, key2 = true, key3 = (byte)5, key4 = 5, DoubleProperty = 5d, SingleProperty = 5f, LongProperty = 5L, key8 = new { key1 = (object)null, key2 = true, key3 = (byte)6, key4 = 6, key5 = 6d, SingleProperty = 6f, key7 = 6L, }, } } }; var actual = SciterValue.Create(obj); Assert.IsTrue(actual.IsMap); var mapToClass = actual.MapTo <TestNestedClass>(); Assert.NotNull(mapToClass); Assert.AreEqual(mapToClass.key8.SingleProperty, 2L); Assert.AreEqual(mapToClass.key9.DoubleProperty, 3d); Assert.AreEqual(mapToClass.key9.key8.SingleProperty, 4d); Assert.AreEqual(mapToClass.key9.key9.DoubleProperty, 5d); Assert.AreEqual(mapToClass.key9.key9.key8.LongProperty, 6L); }