From 6448b30e9cda5e39e0d02cf0654dd5f544d404e0 Mon Sep 17 00:00:00 2001 From: Alexandre Poirot Date: Tue, 11 Dec 2018 15:19:43 +0000 Subject: [PATCH] Bug 1512220 - Refactor debugger test using testGlobal to threadClientTest helper. r=yulia This isn't removing listTabs calls as these tests were using attachTestTab, but all these tests are using testGlobal helper whereas they don't have to if they were using threadClientTest helper. MozReview-Commit-ID: FPtYlDvYUeD Depends on D13900 Differential Revision: https://phabricator.services.mozilla.com/D13901 --HG-- extra : moz-landing-system : lando --- devtools/server/tests/unit/head_dbg.js | 7 + devtools/server/tests/unit/test_attach.js | 44 ++---- .../server/tests/unit/test_client_close.js | 50 +++---- devtools/server/tests/unit/test_dbgactor.js | 125 ++++-------------- .../unit/test_dbgclient_debuggerstatement.js | 76 +++-------- devtools/server/tests/unit/test_interrupt.js | 50 ++----- .../server/tests/unit/test_reattach-thread.js | 56 ++------ 7 files changed, 99 insertions(+), 309 deletions(-) diff --git a/devtools/server/tests/unit/head_dbg.js b/devtools/server/tests/unit/head_dbg.js index 8e8d128b6f92..c7b1da87e5b9 100644 --- a/devtools/server/tests/unit/head_dbg.js +++ b/devtools/server/tests/unit/head_dbg.js @@ -392,6 +392,8 @@ function getTestTab(client, title, callback) { function attachTestTab(client, title, callback) { getTestTab(client, title, function(tab) { client.attachTarget(tab).then(([response, targetFront]) => { + Assert.equal(response.type, "tabAttached"); + Assert.ok(typeof response.threadActor === "string"); callback(response, targetFront); }); }); @@ -404,6 +406,10 @@ function attachTestTab(client, title, callback) { function attachTestThread(client, title, callback) { attachTestTab(client, title, function(tabResponse, targetFront) { function onAttach([response, threadClient]) { + Assert.equal(threadClient.state, "paused", "Thread client is paused"); + Assert.equal(response.type, "paused"); + Assert.ok("why" in response); + Assert.equal(response.why.type, "attached"); callback(response, targetFront, threadClient, tabResponse); } targetFront.attachThread({ @@ -421,6 +427,7 @@ function attachTestTabAndResume(client, title, callback = () => {}) { return new Promise((resolve) => { attachTestThread(client, title, function(response, targetFront, threadClient) { threadClient.resume(function(response) { + Assert.equal(response.type, "resumed"); callback(response, targetFront, threadClient); resolve([response, targetFront, threadClient]); }); diff --git a/devtools/server/tests/unit/test_attach.js b/devtools/server/tests/unit/test_attach.js index 373badd4aca5..24c5d89b9961 100644 --- a/devtools/server/tests/unit/test_attach.js +++ b/devtools/server/tests/unit/test_attach.js @@ -3,34 +3,18 @@ "use strict"; -var gClient; -var gDebuggee; +const ThreadClient = require("devtools/shared/client/thread-client"); +const { BrowsingContextTargetFront } = require("devtools/shared/fronts/targets/browsing-context"); -function run_test() { - initTestDebuggerServer(); - gDebuggee = testGlobal("test-1"); - DebuggerServer.addTestGlobal(gDebuggee); - - const transport = DebuggerServer.connectPipe(); - gClient = new DebuggerClient(transport); - gClient.connect().then(function([type, traits]) { - attachTestTab(gClient, "test-1", function(reply, targetFront) { - test_attach(targetFront); - }); - }); - do_test_pending(); -} - -function test_attach(targetFront) { - targetFront.attachThread({}).then(function([response, threadClient]) { - Assert.equal(threadClient.state, "paused"); - threadClient.resume(cleanup); - }); -} - -function cleanup() { - gClient.addListener("closed", function(event) { - do_test_finished(); - }); - gClient.close(); -} +/** + * Very naive test that checks threadClearTest helper. + * It ensures that the thread client is correctly attached. + */ +add_task(threadClientTest(({ threadClient, debuggee, client, targetFront }) => { + ok(true, "Thread actor was able to attach"); + ok(threadClient instanceof ThreadClient, "Thread client is valid"); + Assert.equal(threadClient.state, "attached", "Thread client is paused"); + Assert.equal(String(debuggee), "[object Sandbox]", "Debuggee client is valid"); + ok(client instanceof DebuggerClient, "Client is valid"); + ok(targetFront instanceof BrowsingContextTargetFront, "TargetFront is valid"); +})); diff --git a/devtools/server/tests/unit/test_client_close.js b/devtools/server/tests/unit/test_client_close.js index 4ff56e4000ac..8e1757a5c79d 100644 --- a/devtools/server/tests/unit/test_client_close.js +++ b/devtools/server/tests/unit/test_client_close.js @@ -3,37 +3,21 @@ "use strict"; -var gClient; -var gDebuggee; - -function run_test() { - initTestDebuggerServer(); - gDebuggee = testGlobal("test-1"); - DebuggerServer.addTestGlobal(gDebuggee); - - const transport = DebuggerServer.connectPipe(); - gClient = new DebuggerClient(transport); - gClient.connect().then(function(type, traits) { - attachTestTab(gClient, "test-1", function(reply, targetFront) { - test_close(transport); - }); +add_task(threadClientTest(({ client }) => { + return new Promise(resolve => { + // Check that, if we fake a transport shutdown + // (like if a device is unplugged) + // the client is automatically closed, + // and we can still call client.close. + const onClosed = function() { + client.removeListener("closed", onClosed); + ok(true, "Client emitted 'closed' event"); + client.close().then(function() { + ok(true, "client.close() successfully called its callback"); + resolve(); + }); + }; + client.addListener("closed", onClosed); + client.transport.close(); }); - do_test_pending(); -} - -function test_close(transport) { - // Check that, if we fake a transport shutdown - // (like if a device is unplugged) - // the client is automatically closed, - // and we can still call client.close. - const onClosed = function() { - gClient.removeListener("closed", onClosed); - ok(true, "Client emitted 'closed' event"); - gClient.close().then(function() { - ok(true, "client.close() successfully called its callback"); - do_test_finished(); - }); - }; - gClient.addListener("closed", onClosed); - transport.close(); -} +})); diff --git a/devtools/server/tests/unit/test_dbgactor.js b/devtools/server/tests/unit/test_dbgactor.js index 3c4deeaaaa10..54c91b3c22bb 100644 --- a/devtools/server/tests/unit/test_dbgactor.js +++ b/devtools/server/tests/unit/test_dbgactor.js @@ -3,112 +3,37 @@ "use strict"; -var gClient; -var gDebuggee; - const xpcInspector = Cc["@mozilla.org/jsinspector;1"].getService(Ci.nsIJSInspector); -function run_test() { - initTestDebuggerServer(); - gDebuggee = testGlobal("test-1"); - DebuggerServer.addTestGlobal(gDebuggee); +add_task(threadClientTest(async ({ threadClient, debuggee, client, targetFront }) => { + Assert.equal(xpcInspector.eventLoopNestLevel, 0); - const transport = DebuggerServer.connectPipe(); - gClient = new DebuggerClient(transport); - gClient.addListener("connected", function(event, type, traits) { - gClient.listTabs().then((response) => { - Assert.ok("tabs" in response); - for (const tab of response.tabs) { - if (tab.title == "test-1") { - test_attach_tab(tab.actor); - return false; - } - } - // We should have found our tab in the list. - Assert.ok(false); - return undefined; + await new Promise(resolve => { + client.addListener("paused", function(name, packet) { + Assert.equal(name, "paused"); + Assert.equal(false, "error" in packet); + Assert.equal(packet.from, threadClient.actor); + Assert.equal(packet.type, "paused"); + Assert.ok("actor" in packet); + Assert.ok("why" in packet); + Assert.equal(packet.why.type, "debuggerStatement"); + + // Reach around the protocol to check that the debuggee is in the state + // we expect. + Assert.ok(debuggee.a); + Assert.ok(!debuggee.b); + + Assert.equal(xpcInspector.eventLoopNestLevel, 1); + + // Let the debuggee continue execution. + threadClient.resume().then(resolve); }); - }); - - gClient.connect(); - - do_test_pending(); -} - -// Attach to |targetActor|, and check the response. -function test_attach_tab(targetActor) { - gClient.request({ to: targetActor, type: "attach" }, function(response) { - Assert.equal(false, "error" in response); - Assert.equal(response.from, targetActor); - Assert.equal(response.type, "tabAttached"); - Assert.ok(typeof response.threadActor === "string"); - - test_attach_thread(response.threadActor); - }); -} - -// Attach to |threadActor|, check the response, and resume it. -function test_attach_thread(threadActor) { - gClient.request({ to: threadActor, type: "attach" }, function(response) { - Assert.equal(false, "error" in response); - Assert.equal(response.from, threadActor); - Assert.equal(response.type, "paused"); - Assert.ok("why" in response); - Assert.equal(response.why.type, "attached"); - - test_resume_thread(threadActor); - }); -} - -// Resume |threadActor|, and see that it stops at the 'debugger' -// statement. -function test_resume_thread(threadActor) { - // Allow the client to resume execution. - gClient.request({ to: threadActor, type: "resume" }, function(response) { - Assert.equal(false, "error" in response); - Assert.equal(response.from, threadActor); - Assert.equal(response.type, "resumed"); - - Assert.equal(xpcInspector.eventLoopNestLevel, 0); // Now that we know we're resumed, we can make the debuggee do something. - Cu.evalInSandbox("var a = true; var b = false; debugger; var b = true;", gDebuggee); + Cu.evalInSandbox("var a = true; var b = false; debugger; var b = true;", debuggee); // Now make sure that we've run the code after the debugger statement... - Assert.ok(gDebuggee.b); + Assert.ok(debuggee.b); }); - gClient.addListener("paused", function(name, packet) { - Assert.equal(name, "paused"); - Assert.equal(false, "error" in packet); - Assert.equal(packet.from, threadActor); - Assert.equal(packet.type, "paused"); - Assert.ok("actor" in packet); - Assert.ok("why" in packet); - Assert.equal(packet.why.type, "debuggerStatement"); - - // Reach around the protocol to check that the debuggee is in the state - // we expect. - Assert.ok(gDebuggee.a); - Assert.ok(!gDebuggee.b); - - Assert.equal(xpcInspector.eventLoopNestLevel, 1); - - // Let the debuggee continue execution. - gClient.request({ to: threadActor, type: "resume" }, cleanup); - }); -} - -function cleanup() { - gClient.addListener("closed", function(event, result) { - do_test_finished(); - }); - - try { - const inspector = Cc["@mozilla.org/jsinspector;1"].getService(Ci.nsIJSInspector); - Assert.equal(inspector.eventLoopNestLevel, 0); - } catch (e) { - dump(e); - } - - gClient.close(); -} + Assert.equal(xpcInspector.eventLoopNestLevel, 0); +})); diff --git a/devtools/server/tests/unit/test_dbgclient_debuggerstatement.js b/devtools/server/tests/unit/test_dbgclient_debuggerstatement.js index be6824dd2a55..08e3895738a1 100644 --- a/devtools/server/tests/unit/test_dbgclient_debuggerstatement.js +++ b/devtools/server/tests/unit/test_dbgclient_debuggerstatement.js @@ -3,69 +3,27 @@ "use strict"; -var gClient; -var gTargetFront; -var gDebuggee; +const xpcInspector = Cc["@mozilla.org/jsinspector;1"].getService(Ci.nsIJSInspector); -function run_test() { - initTestDebuggerServer(); - gDebuggee = testGlobal("test-1"); - DebuggerServer.addTestGlobal(gDebuggee); +add_task(threadClientTest(async ({ threadClient, debuggee, client, targetFront }) => { + await new Promise(resolve => { + threadClient.addListener("paused", function(event, packet) { + Assert.equal(threadClient.state, "paused"); + // Reach around the protocol to check that the debuggee is in the state + // we expect. + Assert.ok(debuggee.a); + Assert.ok(!debuggee.b); - const transport = DebuggerServer.connectPipe(); - gClient = new DebuggerClient(transport); - gClient.connect().then(function([type, traits]) { - attachTestTab(gClient, "test-1", function(reply, targetFront) { - gTargetFront = targetFront; - test_threadAttach(reply.threadActor); + Assert.equal(xpcInspector.eventLoopNestLevel, 1); + + threadClient.resume().then(resolve); }); - }); - do_test_pending(); -} -function test_threadAttach(threadActorID) { - info("Trying to attach to thread " + threadActorID); - gTargetFront.attachThread({}).then(function([response, threadClient]) { - Assert.equal(threadClient.state, "paused"); - Assert.equal(threadClient.actor, threadActorID); - threadClient.resume(function() { - Assert.equal(threadClient.state, "attached"); - test_debugger_statement(threadClient); - }); - }); -} + Cu.evalInSandbox("var a = true; var b = false; debugger; var b = true;", debuggee); -function test_debugger_statement(threadClient) { - threadClient.addListener("paused", function(event, packet) { - Assert.equal(threadClient.state, "paused"); - // Reach around the protocol to check that the debuggee is in the state - // we expect. - Assert.ok(gDebuggee.a); - Assert.ok(!gDebuggee.b); - - const xpcInspector = Cc["@mozilla.org/jsinspector;1"].getService(Ci.nsIJSInspector); - Assert.equal(xpcInspector.eventLoopNestLevel, 1); - - threadClient.resume(cleanup); + // Now make sure that we've run the code after the debugger statement... + Assert.ok(debuggee.b); }); - Cu.evalInSandbox("var a = true; var b = false; debugger; var b = true;", gDebuggee); - - // Now make sure that we've run the code after the debugger statement... - Assert.ok(gDebuggee.b); -} - -function cleanup() { - gClient.addListener("closed", function(event) { - do_test_finished(); - }); - - try { - const xpcInspector = Cc["@mozilla.org/jsinspector;1"].getService(Ci.nsIJSInspector); - Assert.equal(xpcInspector.eventLoopNestLevel, 0); - } catch (e) { - dump(e); - } - - gClient.close(); -} + Assert.equal(xpcInspector.eventLoopNestLevel, 0); +})); diff --git a/devtools/server/tests/unit/test_interrupt.js b/devtools/server/tests/unit/test_interrupt.js index b9b6ff0cb3e4..840c08890812 100644 --- a/devtools/server/tests/unit/test_interrupt.js +++ b/devtools/server/tests/unit/test_interrupt.js @@ -4,46 +4,14 @@ "use strict"; -var gClient; -var gDebuggee; - -function run_test() { - initTestDebuggerServer(); - gDebuggee = testGlobal("test-1"); - DebuggerServer.addTestGlobal(gDebuggee); - - const transport = DebuggerServer.connectPipe(); - gClient = new DebuggerClient(transport); - gClient.connect().then(function(type, traits) { - attachTestTab(gClient, "test-1", test_attach); - }); - do_test_pending(); -} - -function test_attach(response, targetFront) { - targetFront.attachThread({}).then(function([response, threadClient]) { - Assert.equal(threadClient.paused, true); - threadClient.resume(function() { - test_interrupt(threadClient); +add_task(threadClientTest(async ({ threadClient, debuggee, client, targetFront }) => { + return new Promise(resolve => { + threadClient.interrupt(function(response) { + Assert.equal(threadClient.paused, true); + threadClient.resume(function() { + Assert.equal(threadClient.paused, false); + resolve(); + }); }); }); -} - -function test_interrupt(threadClient) { - Assert.equal(threadClient.paused, false); - threadClient.interrupt(function(response) { - Assert.equal(threadClient.paused, true); - threadClient.resume(function() { - Assert.equal(threadClient.paused, false); - cleanup(); - }); - }); -} - -function cleanup() { - gClient.addListener("closed", function(event) { - do_test_finished(); - }); - gClient.close(); -} - +})); diff --git a/devtools/server/tests/unit/test_reattach-thread.js b/devtools/server/tests/unit/test_reattach-thread.js index 28601b0c15a8..4c081e47a395 100644 --- a/devtools/server/tests/unit/test_reattach-thread.js +++ b/devtools/server/tests/unit/test_reattach-thread.js @@ -7,49 +7,13 @@ * Test that reattaching to a previously detached thread works. */ -var gClient, gDebuggee, gThreadClient, gTargetFront; - -function run_test() { - initTestDebuggerServer(); - gDebuggee = testGlobal("test-reattach"); - DebuggerServer.addTestGlobal(gDebuggee); - - const transport = DebuggerServer.connectPipe(); - gClient = new DebuggerClient(transport); - gClient.connect().then(() => { - attachTestTab(gClient, "test-reattach", (reply, targetFront) => { - gTargetFront = targetFront; - test_attach(); - }); - }); - do_test_pending(); -} - -function test_attach() { - gTargetFront.attachThread({}).then(([response, threadClient]) => { - Assert.equal(threadClient.state, "paused"); - gThreadClient = threadClient; - threadClient.resume(test_detach); - }); -} - -function test_detach() { - gThreadClient.detach(() => { - Assert.equal(gThreadClient.state, "detached"); - Assert.equal(gTargetFront.thread, null); - test_reattach(); - }); -} - -function test_reattach() { - gTargetFront.attachThread({}).then(([response, threadClient]) => { - Assert.notEqual(gThreadClient, threadClient); - Assert.equal(threadClient.state, "paused"); - Assert.equal(gTargetFront.thread, threadClient); - threadClient.resume(cleanup); - }); -} - -function cleanup() { - gClient.close().then(do_test_finished); -} +add_task(threadClientTest(async ({ threadClient, debuggee, client, targetFront }) => { + await threadClient.detach(); + Assert.equal(threadClient.state, "detached"); + Assert.equal(targetFront.thread, null); + const [, newThreadClient] = await targetFront.attachThread({}); + Assert.notEqual(threadClient, newThreadClient); + Assert.equal(newThreadClient.state, "paused"); + Assert.equal(targetFront.thread, newThreadClient); + await newThreadClient.resume(); +}));