internal Dictionary <string, string> SubscribeToScripts(Inspector insp) { dicScriptsIdToUrl = new Dictionary <string, string>(); dicFileToUrl = new Dictionary <string, string>(); insp.On("Debugger.scriptParsed", async(args, c) => { var script_id = args?["scriptId"]?.Value <string>(); var url = args["url"]?.Value <string>(); if (script_id.StartsWith("dotnet://")) { var dbgUrl = args["dotNetUrl"]?.Value <string>(); var arrStr = dbgUrl.Split("/"); dbgUrl = arrStr[0] + "/" + arrStr[1] + "/" + arrStr[2] + "/" + arrStr[arrStr.Length - 1]; dicScriptsIdToUrl[script_id] = dbgUrl; dicFileToUrl[dbgUrl] = args["url"]?.Value <string>(); } else if (!String.IsNullOrEmpty(url)) { var dbgUrl = args["url"]?.Value <string>(); var arrStr = dbgUrl.Split("/"); dicScriptsIdToUrl[script_id] = arrStr[arrStr.Length - 1]; dicFileToUrl[new Uri(url).AbsolutePath] = url; } await Task.FromResult(0); }); return(dicScriptsIdToUrl); }
public async Task RaiseDebugEventTraceTest(bool?trace) { var insp = new Inspector(); var scripts = SubscribeToScripts(insp); await Ready(); await insp.Ready(async (cli, token) => { ctx = new DebugTestContext(cli, insp, token, scripts); var tcs = new TaskCompletionSource <bool>(); insp.On("Runtime.consoleAPICalled", async(args, token) => { if (args?["type"]?.Value <string>() == "debug" && args?["args"]?.Type == JTokenType.Array && args?["args"]?[0]?["value"]?.Value <string>()?.StartsWith("mono_wasm_debug_event_raised:") == true) { tcs.SetResult(true); } await Task.CompletedTask; }); var trace_str = trace.HasValue ? $"trace: {trace.ToString().ToLower()}" : String.Empty; var expression = $"MONO.mono_wasm_raise_debug_event({{ eventName:'qwe' }}, {{ {trace_str} }})"; var res = await ctx.cli.SendCommand($"Runtime.evaluate", JObject.FromObject(new { expression }), ctx.token); Assert.True(res.IsOk, $"Expected to pass for {expression}"); var t = await Task.WhenAny(tcs.Task, Task.Delay(2000)); if (trace == true) { Assert.True(tcs.Task == t, "Timed out waiting for the event to be logged"); } else { Assert.False(tcs.Task == t, "Event should not have been logged"); } }); }
async Task AssemblyLoadedEventTest(string asm_name, string asm_path, string pdb_path, string source_file, int expected_count) { var insp = new Inspector(); var scripts = SubscribeToScripts(insp); int event_count = 0; var tcs = new TaskCompletionSource <bool>(); insp.On("Debugger.scriptParsed", async(args, c) => { try { var url = args["url"]?.Value <string>(); if (url?.EndsWith(source_file) == true) { event_count++; if (event_count > expected_count) { tcs.SetResult(false); } } } catch (Exception ex) { tcs.SetException(ex); } await Task.CompletedTask; }); await Ready(); await insp.Ready(async (cli, token) => { ctx = new DebugTestContext(cli, insp, token, scripts); byte[] bytes = File.ReadAllBytes(asm_path); string asm_base64 = Convert.ToBase64String(bytes); string pdb_base64 = String.Empty; if (pdb_path != null) { bytes = File.ReadAllBytes(pdb_path); pdb_base64 = Convert.ToBase64String(bytes); } var expression = $@"MONO.mono_wasm_raise_debug_event({{ eventName: 'AssemblyLoaded', assembly_name: '{asm_name}', assembly_b64: '{asm_base64}', pdb_b64: '{pdb_base64}' }});"; var res = await ctx.cli.SendCommand($"Runtime.evaluate", JObject.FromObject(new { expression }), ctx.token); Assert.True(res.IsOk, $"Expected to pass for {expression}"); res = await ctx.cli.SendCommand($"Runtime.evaluate", JObject.FromObject(new { expression }), ctx.token); Assert.True(res.IsOk, $"Expected to pass for {expression}"); var t = await Task.WhenAny(tcs.Task, Task.Delay(2000)); if (t.IsFaulted) { throw t.Exception; } Assert.True(event_count <= expected_count, $"number of scriptParsed events received. Expected: {expected_count}, Actual: {event_count}"); }); }