toolkit/devtools/server/tests/unit/test_nesting-02.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/server/tests/unit/test_nesting-02.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/* -*- Mode: javascript; js-indent-level: 2; -*- */
     1.5 +/* Any copyright is dedicated to the Public Domain.
     1.6 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.7 +
     1.8 +// Test that we can nest event loops and then automatically exit nested event
     1.9 +// loops when requested.
    1.10 +
    1.11 +var gClient;
    1.12 +var gThreadActor;
    1.13 +
    1.14 +function run_test() {
    1.15 +  initTestDebuggerServer();
    1.16 +  let gDebuggee = addTestGlobal("test-nesting");
    1.17 +  gClient = new DebuggerClient(DebuggerServer.connectPipe());
    1.18 +  gClient.connect(function () {
    1.19 +    attachTestTabAndResume(gClient, "test-nesting", function (aResponse, aTabClient, aThreadClient) {
    1.20 +      // Reach over the protocol connection and get a reference to the thread actor.
    1.21 +      gThreadActor = aThreadClient._transport._serverConnection.getActor(aThreadClient._actor);
    1.22 +
    1.23 +      test_nesting();
    1.24 +    });
    1.25 +  });
    1.26 +  do_test_pending();
    1.27 +}
    1.28 +
    1.29 +function test_nesting() {
    1.30 +  const thread = gThreadActor;
    1.31 +  const { resolve, reject, promise: p } = promise.defer();
    1.32 +
    1.33 +  // The following things should happen (in order):
    1.34 +  // 1. In the new event loop (created by synchronize)
    1.35 +  // 2. Resolve the promise (shouldn't exit any event loops)
    1.36 +  // 3. Exit the event loop (should also then exit synchronize's event loop)
    1.37 +  // 4. Be after the synchronize call
    1.38 +  let currentStep = 0;
    1.39 +
    1.40 +  executeSoon(function () {
    1.41 +    let eventLoop;
    1.42 +
    1.43 +    executeSoon(function () {
    1.44 +      // Should be at step 2
    1.45 +      do_check_eq(++currentStep, 2);
    1.46 +      // Before resolving, should have the synchronize event loop and the one just created.
    1.47 +      do_check_eq(thread._nestedEventLoops.size, 2);
    1.48 +
    1.49 +      executeSoon(function () {
    1.50 +        // Should be at step 3
    1.51 +        do_check_eq(++currentStep, 3);
    1.52 +        // Before exiting the manually created event loop, should have the
    1.53 +        // synchronize event loop and the manual event loop.
    1.54 +        do_check_eq(thread._nestedEventLoops.size, 2);
    1.55 +        // Should have the event loop
    1.56 +        do_check_true(!!eventLoop);
    1.57 +        eventLoop.resolve();
    1.58 +      });
    1.59 +
    1.60 +      resolve(true);
    1.61 +      // Shouldn't exit any event loops because a new one started since the call to synchronize
    1.62 +      do_check_eq(thread._nestedEventLoops.size, 2);
    1.63 +    });
    1.64 +
    1.65 +    // Should be at step 1
    1.66 +    do_check_eq(++currentStep, 1);
    1.67 +    // Should have only the synchronize event loop
    1.68 +    do_check_eq(thread._nestedEventLoops.size, 1);
    1.69 +    eventLoop = thread._nestedEventLoops.push();
    1.70 +    eventLoop.enter();
    1.71 +  });
    1.72 +
    1.73 +  do_check_eq(thread.synchronize(p), true);
    1.74 +
    1.75 +  // Should be on the fourth step
    1.76 +  do_check_eq(++currentStep, 4);
    1.77 +  // There shouldn't be any nested event loops anymore
    1.78 +  do_check_eq(thread._nestedEventLoops.size, 0);
    1.79 +
    1.80 +  finishClient(gClient);
    1.81 +}

mercurial