toolkit/devtools/server/tests/unit/test_blackboxing-01.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_blackboxing-01.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,174 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +/**
     1.8 + * Test basic black boxing.
     1.9 + */
    1.10 +
    1.11 +var gDebuggee;
    1.12 +var gClient;
    1.13 +var gThreadClient;
    1.14 +
    1.15 +function run_test()
    1.16 +{
    1.17 +  initTestDebuggerServer();
    1.18 +  gDebuggee = addTestGlobal("test-black-box");
    1.19 +  gClient = new DebuggerClient(DebuggerServer.connectPipe());
    1.20 +  gClient.connect(function() {
    1.21 +    attachTestTabAndResume(gClient, "test-black-box", function(aResponse, aTabClient, aThreadClient) {
    1.22 +      gThreadClient = aThreadClient;
    1.23 +      test_black_box();
    1.24 +    });
    1.25 +  });
    1.26 +  do_test_pending();
    1.27 +}
    1.28 +
    1.29 +const BLACK_BOXED_URL = "http://example.com/blackboxme.js";
    1.30 +const SOURCE_URL = "http://example.com/source.js";
    1.31 +
    1.32 +function test_black_box()
    1.33 +{
    1.34 +  gClient.addOneTimeListener("paused", function () {
    1.35 +    gThreadClient.setBreakpoint({
    1.36 +      url: SOURCE_URL,
    1.37 +      line: 2
    1.38 +    }, function (aResponse) {
    1.39 +      do_check_true(!aResponse.error, "Should be able to set breakpoint.");
    1.40 +      gThreadClient.resume(test_black_box_default);
    1.41 +    });
    1.42 +  });
    1.43 +
    1.44 +  Components.utils.evalInSandbox(
    1.45 +    "" + function doStuff(k) { // line 1
    1.46 +      let arg = 15;            // line 2 - Step in here
    1.47 +      k(arg);                  // line 3
    1.48 +    },                         // line 4
    1.49 +    gDebuggee,
    1.50 +    "1.8",
    1.51 +    BLACK_BOXED_URL,
    1.52 +    1
    1.53 +  );
    1.54 +
    1.55 +  Components.utils.evalInSandbox(
    1.56 +    "" + function runTest() { // line 1
    1.57 +      doStuff(                // line 2 - Break here
    1.58 +        function (n) {        // line 3 - Step through `doStuff` to here
    1.59 +          debugger;           // line 5
    1.60 +        }                     // line 6
    1.61 +      );                      // line 7
    1.62 +    }                         // line 8
    1.63 +    + "\n debugger;",         // line 9
    1.64 +    gDebuggee,
    1.65 +    "1.8",
    1.66 +    SOURCE_URL,
    1.67 +    1
    1.68 +  );
    1.69 +}
    1.70 +
    1.71 +function test_black_box_default() {
    1.72 +  gThreadClient.getSources(function (aResponse) {
    1.73 +    do_check_true(!aResponse.error, "Should be able to get sources.");
    1.74 +
    1.75 +    let sourceClient = gThreadClient.source(
    1.76 +      aResponse.sources.filter(s => s.url == BLACK_BOXED_URL)[0]);
    1.77 +    do_check_true(!sourceClient.isBlackBoxed,
    1.78 +                  "By default the source is not black boxed.");
    1.79 +
    1.80 +    // Test that we can step into `doStuff` when we are not black boxed.
    1.81 +    runTest(
    1.82 +      function onSteppedLocation(aLocation) {
    1.83 +        do_check_eq(aLocation.url, BLACK_BOXED_URL,
    1.84 +                    "Should step into `doStuff`.");
    1.85 +        do_check_eq(aLocation.line, 2,
    1.86 +                    "Should step into `doStuff`.");
    1.87 +      },
    1.88 +      function onDebuggerStatementFrames(aFrames) {
    1.89 +        do_check_true(!aFrames.some(f => f.source.isBlackBoxed));
    1.90 +      },
    1.91 +      test_black_boxing.bind(null, sourceClient)
    1.92 +    );
    1.93 +  });
    1.94 +}
    1.95 +
    1.96 +function test_black_boxing(aSourceClient) {
    1.97 +  aSourceClient.blackBox(function (aResponse) {
    1.98 +    do_check_true(!aResponse.error, "Should not get an error black boxing.");
    1.99 +    do_check_true(aSourceClient.isBlackBoxed,
   1.100 +       "The source client should report itself as black boxed correctly.");
   1.101 +
   1.102 +    // Test that we step through `doStuff` when we are black boxed and its frame
   1.103 +    // doesn't show up.
   1.104 +    runTest(
   1.105 +      function onSteppedLocation(aLocation) {
   1.106 +        do_check_eq(aLocation.url, SOURCE_URL,
   1.107 +                    "Should step through `doStuff`.");
   1.108 +        do_check_eq(aLocation.line, 3,
   1.109 +                    "Should step through `doStuff`.");
   1.110 +      },
   1.111 +      function onDebuggerStatementFrames(aFrames) {
   1.112 +        for (let f of aFrames) {
   1.113 +          if (f.where.url == BLACK_BOXED_URL) {
   1.114 +            do_check_true(f.source.isBlackBoxed, "Should be black boxed");
   1.115 +          } else {
   1.116 +            do_check_true(!f.source.isBlackBoxed, "Should not be black boxed")
   1.117 +          }
   1.118 +        }
   1.119 +      },
   1.120 +      test_unblack_boxing.bind(null, aSourceClient)
   1.121 +    );
   1.122 +  });
   1.123 +}
   1.124 +
   1.125 +function test_unblack_boxing(aSourceClient) {
   1.126 +  aSourceClient.unblackBox(function (aResponse) {
   1.127 +    do_check_true(!aResponse.error, "Should not get an error un-black boxing");
   1.128 +    do_check_true(!aSourceClient.isBlackBoxed, "The source is not black boxed.");
   1.129 +
   1.130 +    // Test that we can step into `doStuff` again.
   1.131 +    runTest(
   1.132 +      function onSteppedLocation(aLocation) {
   1.133 +        do_check_eq(aLocation.url, BLACK_BOXED_URL,
   1.134 +                    "Should step into `doStuff`.");
   1.135 +        do_check_eq(aLocation.line, 2,
   1.136 +                    "Should step into `doStuff`.");
   1.137 +      },
   1.138 +      function onDebuggerStatementFrames(aFrames) {
   1.139 +        do_check_true(!aFrames.some(f => f.source.isBlackBoxed));
   1.140 +      },
   1.141 +      finishClient.bind(null, gClient)
   1.142 +    );
   1.143 +  });
   1.144 +}
   1.145 +
   1.146 +function runTest(aOnSteppedLocation, aOnDebuggerStatementFrames, aFinishedCallback) {
   1.147 +  gClient.addOneTimeListener("paused", function (aEvent, aPacket) {
   1.148 +    do_check_eq(aPacket.why.type, "breakpoint");
   1.149 +    gClient.addOneTimeListener("paused", function () {
   1.150 +      gClient.addOneTimeListener("paused", function () {
   1.151 +        getCurrentLocation(function (aLocation) {
   1.152 +          aOnSteppedLocation(aLocation);
   1.153 +          gClient.addOneTimeListener("paused", function (aEvent, aPacket) {
   1.154 +            do_check_eq(aPacket.why.type, "debuggerStatement");
   1.155 +            gThreadClient.getFrames(0, 100, function ({frames}) {
   1.156 +              aOnDebuggerStatementFrames(frames);
   1.157 +              gThreadClient.resume(aFinishedCallback);
   1.158 +            });
   1.159 +          });
   1.160 +          gThreadClient.resume();
   1.161 +        });
   1.162 +      });
   1.163 +      gThreadClient.stepIn();
   1.164 +    });
   1.165 +    gThreadClient.stepIn();
   1.166 +  });
   1.167 +
   1.168 +  gDebuggee.runTest();
   1.169 +}
   1.170 +
   1.171 +function getCurrentLocation(aCallback) {
   1.172 +  gThreadClient.getFrames(0, 1, function ({frames, error}) {
   1.173 +    do_check_true(!error, "Should not get an error: " + error);
   1.174 +    let [{where}] = frames;
   1.175 +    aCallback(where);
   1.176 +  });
   1.177 +}

mercurial