toolkit/devtools/server/tests/unit/test_protocol_longstring.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_protocol_longstring.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,206 @@
     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 simple requests using the protocol helpers.
     1.9 + */
    1.10 +let protocol = devtools.require("devtools/server/protocol");
    1.11 +let {method, RetVal, Arg, Option} = protocol;
    1.12 +let events = devtools.require("sdk/event/core");
    1.13 +let {LongStringActor} = devtools.require("devtools/server/actors/string");
    1.14 +
    1.15 +function simpleHello() {
    1.16 +  return {
    1.17 +    from: "root",
    1.18 +    applicationType: "xpcshell-tests",
    1.19 +    traits: [],
    1.20 +  }
    1.21 +}
    1.22 +
    1.23 +DebuggerServer.LONG_STRING_LENGTH = DebuggerServer.LONG_STRING_INITIAL_LENGTH = DebuggerServer.LONG_STRING_READ_LENGTH = 5;
    1.24 +
    1.25 +let SHORT_STR = "abc";
    1.26 +let LONG_STR = "abcdefghijklmnop";
    1.27 +
    1.28 +let rootActor = null;
    1.29 +
    1.30 +let RootActor = protocol.ActorClass({
    1.31 +  typeName: "root",
    1.32 +
    1.33 +  initialize: function(conn) {
    1.34 +    rootActor = this;
    1.35 +    protocol.Actor.prototype.initialize.call(this, conn);
    1.36 +    // Root actor owns itself.
    1.37 +    this.manage(this);
    1.38 +    this.actorID = "root";
    1.39 +  },
    1.40 +
    1.41 +  sayHello: simpleHello,
    1.42 +
    1.43 +  shortString: method(function() {
    1.44 +    return new LongStringActor(this.conn, SHORT_STR);
    1.45 +  }, {
    1.46 +    response: { value: RetVal("longstring") },
    1.47 +  }),
    1.48 +
    1.49 +  longString: method(function() {
    1.50 +    return new LongStringActor(this.conn, LONG_STR);
    1.51 +  }, {
    1.52 +    response: { value: RetVal("longstring") },
    1.53 +  }),
    1.54 +
    1.55 +  emitShortString: method(function() {
    1.56 +    events.emit(this, "string-event", new LongStringActor(this.conn, SHORT_STR));
    1.57 +  }, {
    1.58 +    oneway: true,
    1.59 +  }),
    1.60 +
    1.61 +  emitLongString: method(function() {
    1.62 +    events.emit(this, "string-event", new LongStringActor(this.conn, LONG_STR));
    1.63 +  }, {
    1.64 +    oneway: true,
    1.65 +  }),
    1.66 +
    1.67 +  events: {
    1.68 +    "string-event": {
    1.69 +      str: Arg(0, "longstring")
    1.70 +    }
    1.71 +  }
    1.72 +});
    1.73 +
    1.74 +let RootFront = protocol.FrontClass(RootActor, {
    1.75 +  initialize: function(client) {
    1.76 +    this.actorID = "root";
    1.77 +    protocol.Front.prototype.initialize.call(this, client);
    1.78 +    // Root owns itself.
    1.79 +    this.manage(this);
    1.80 +  }
    1.81 +});
    1.82 +
    1.83 +function run_test()
    1.84 +{
    1.85 +  DebuggerServer.createRootActor = (conn => {
    1.86 +    return RootActor(conn);
    1.87 +  });
    1.88 +
    1.89 +  DebuggerServer.init(() => true);
    1.90 +  let trace = connectPipeTracing();
    1.91 +  let client = new DebuggerClient(trace);
    1.92 +  let rootClient;
    1.93 +
    1.94 +  let strfront = null;
    1.95 +
    1.96 +  let expectRootChildren = function(size) {
    1.97 +    do_check_eq(rootActor.__poolMap.size, size + 1);
    1.98 +    do_check_eq(rootClient.__poolMap.size, size + 1);
    1.99 +  }
   1.100 +
   1.101 +
   1.102 +  client.connect((applicationType, traits) => {
   1.103 +    rootClient = RootFront(client);
   1.104 +
   1.105 +    // Root actor has no children yet.
   1.106 +    expectRootChildren(0);
   1.107 +
   1.108 +    trace.expectReceive({"from":"<actorid>","applicationType":"xpcshell-tests","traits":[]});
   1.109 +    do_check_eq(applicationType, "xpcshell-tests");
   1.110 +    rootClient.shortString().then(ret => {
   1.111 +      trace.expectSend({"type":"shortString","to":"<actorid>"});
   1.112 +      trace.expectReceive({"value":"abc","from":"<actorid>"});
   1.113 +
   1.114 +      // Should only own the one reference (itself) at this point.
   1.115 +      expectRootChildren(0);
   1.116 +      strfront = ret;
   1.117 +    }).then(() => {
   1.118 +      return strfront.string();
   1.119 +    }).then(ret => {
   1.120 +      do_check_eq(ret, SHORT_STR);
   1.121 +    }).then(() => {
   1.122 +      return rootClient.longString();
   1.123 +    }).then(ret => {
   1.124 +      trace.expectSend({"type":"longString","to":"<actorid>"});
   1.125 +      trace.expectReceive({"value":{"type":"longString","actor":"<actorid>","length":16,"initial":"abcde"},"from":"<actorid>"});
   1.126 +
   1.127 +      strfront = ret;
   1.128 +      // Should own a reference to itself and an extra string now.
   1.129 +      expectRootChildren(1);
   1.130 +    }).then(() => {
   1.131 +      return strfront.string();
   1.132 +    }).then(ret => {
   1.133 +      trace.expectSend({"type":"substring","start":5,"end":10,"to":"<actorid>"});
   1.134 +      trace.expectReceive({"substring":"fghij","from":"<actorid>"});
   1.135 +      trace.expectSend({"type":"substring","start":10,"end":15,"to":"<actorid>"});
   1.136 +      trace.expectReceive({"substring":"klmno","from":"<actorid>"});
   1.137 +      trace.expectSend({"type":"substring","start":15,"end":20,"to":"<actorid>"});
   1.138 +      trace.expectReceive({"substring":"p","from":"<actorid>"});
   1.139 +
   1.140 +      do_check_eq(ret, LONG_STR);
   1.141 +    }).then(() => {
   1.142 +      return strfront.release();
   1.143 +    }).then(() => {
   1.144 +      trace.expectSend({"type":"release","to":"<actorid>"});
   1.145 +      trace.expectReceive({"from":"<actorid>"});
   1.146 +
   1.147 +      // That reference should be removed now.
   1.148 +      expectRootChildren(0);
   1.149 +    }).then(() => {
   1.150 +      let deferred = promise.defer();
   1.151 +      rootClient.once("string-event", (str) => {
   1.152 +        trace.expectSend({"type":"emitShortString","to":"<actorid>"});
   1.153 +        trace.expectReceive({"type":"string-event","str":"abc","from":"<actorid>"});
   1.154 +
   1.155 +        do_check_true(!!str);
   1.156 +        strfront = str;
   1.157 +        // Shouldn't generate any new references
   1.158 +        expectRootChildren(0);
   1.159 +        // will generate no packets.
   1.160 +        strfront.string().then((value) => { deferred.resolve(value) });
   1.161 +      });
   1.162 +      rootClient.emitShortString();
   1.163 +      return deferred.promise;
   1.164 +    }).then(value => {
   1.165 +      do_check_eq(value, SHORT_STR);
   1.166 +    }).then(() => {
   1.167 +      // Will generate no packets
   1.168 +      return strfront.release();
   1.169 +    }).then(() => {
   1.170 +      let deferred = promise.defer();
   1.171 +      rootClient.once("string-event", (str) => {
   1.172 +        trace.expectSend({"type":"emitLongString","to":"<actorid>"});
   1.173 +        trace.expectReceive({"type":"string-event","str":{"type":"longString","actor":"<actorid>","length":16,"initial":"abcde"},"from":"<actorid>"});
   1.174 +
   1.175 +        do_check_true(!!str);
   1.176 +        // Should generate one new reference
   1.177 +        expectRootChildren(1);
   1.178 +        strfront = str;
   1.179 +        strfront.string().then((value) => {
   1.180 +          trace.expectSend({"type":"substring","start":5,"end":10,"to":"<actorid>"});
   1.181 +          trace.expectReceive({"substring":"fghij","from":"<actorid>"});
   1.182 +          trace.expectSend({"type":"substring","start":10,"end":15,"to":"<actorid>"});
   1.183 +          trace.expectReceive({"substring":"klmno","from":"<actorid>"});
   1.184 +          trace.expectSend({"type":"substring","start":15,"end":20,"to":"<actorid>"});
   1.185 +          trace.expectReceive({"substring":"p","from":"<actorid>"});
   1.186 +
   1.187 +          deferred.resolve(value);
   1.188 +        });
   1.189 +      });
   1.190 +      rootClient.emitLongString();
   1.191 +      return deferred.promise;
   1.192 +    }).then(value => {
   1.193 +      do_check_eq(value, LONG_STR);
   1.194 +    }).then(() => {
   1.195 +      return strfront.release();
   1.196 +    }).then(() => {
   1.197 +      trace.expectSend({"type":"release","to":"<actorid>"});
   1.198 +      trace.expectReceive({"from":"<actorid>"});
   1.199 +      expectRootChildren(0);
   1.200 +    }).then(() => {
   1.201 +      client.close(() => {
   1.202 +        do_test_finished();
   1.203 +      });
   1.204 +    }).then(null, err => {
   1.205 +      do_report_unexpected_exception(err, "Failure executing test");
   1.206 +    });
   1.207 +  });
   1.208 +  do_test_pending();
   1.209 +}

mercurial