toolkit/devtools/server/tests/unit/test_protocol_simple.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_simple.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,280 @@
     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 +
    1.11 +let protocol = devtools.require("devtools/server/protocol");
    1.12 +let {method, Arg, Option, RetVal} = protocol;
    1.13 +let events = devtools.require("sdk/event/core");
    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 +let RootActor = protocol.ActorClass({
    1.24 +  typeName: "root",
    1.25 +  initialize: function(conn) {
    1.26 +    protocol.Actor.prototype.initialize.call(this, conn);
    1.27 +    // Root actor owns itself.
    1.28 +    this.manage(this);
    1.29 +    this.actorID = "root";
    1.30 +  },
    1.31 +
    1.32 +  sayHello: simpleHello,
    1.33 +
    1.34 +  simpleReturn: method(function() {
    1.35 +    return 1;
    1.36 +  }, {
    1.37 +    response: { value: RetVal() },
    1.38 +  }),
    1.39 +
    1.40 +  promiseReturn: method(function() {
    1.41 +    return promise.resolve(1);
    1.42 +  }, {
    1.43 +    response: { value: RetVal("number") },
    1.44 +  }),
    1.45 +
    1.46 +  simpleArgs: method(function(a, b) {
    1.47 +    return { firstResponse: a + 1, secondResponse: b + 1 };
    1.48 +  }, {
    1.49 +    request: {
    1.50 +      firstArg: Arg(0),
    1.51 +      secondArg: Arg(1),
    1.52 +    },
    1.53 +    response: RetVal()
    1.54 +  }),
    1.55 +
    1.56 +  nestedArgs: method(function(a, b, c) {
    1.57 +    return { a: a, b: b, c: c };
    1.58 +  }, {
    1.59 +    request: {
    1.60 +      firstArg: Arg(0),
    1.61 +      nest: {
    1.62 +        secondArg: Arg(1),
    1.63 +        nest: {
    1.64 +          thirdArg: Arg(2)
    1.65 +        }
    1.66 +      }
    1.67 +    },
    1.68 +    response: RetVal()
    1.69 +  }),
    1.70 +
    1.71 +  optionArgs: method(function(options) {
    1.72 +    return { option1: options.option1, option2: options.option2 };
    1.73 +  }, {
    1.74 +    request: {
    1.75 +      option1: Option(0),
    1.76 +      option2: Option(0)
    1.77 +    },
    1.78 +    response: RetVal()
    1.79 +  }),
    1.80 +
    1.81 +  optionalArgs: method(function(a, b=200) {
    1.82 +    return b;
    1.83 +  }, {
    1.84 +    request: {
    1.85 +      a: Arg(0),
    1.86 +      b: Arg(1, "nullable:number")
    1.87 +    },
    1.88 +    response: {
    1.89 +      value: RetVal("number")
    1.90 +    },
    1.91 +  }),
    1.92 +
    1.93 +  arrayArgs: method(function(a) {
    1.94 +    return a;
    1.95 +  }, {
    1.96 +    request: {
    1.97 +      a: Arg(0, "array:number")
    1.98 +    },
    1.99 +    response: {
   1.100 +      arrayReturn: RetVal("array:number")
   1.101 +    },
   1.102 +  }),
   1.103 +
   1.104 +  nestedArrayArgs: method(function(a) {
   1.105 +    return a;
   1.106 +  }, {
   1.107 +    request: { a: Arg(0, "array:array:number") },
   1.108 +    response: { value: RetVal("array:array:number") },
   1.109 +  }),
   1.110 +
   1.111 +  /**
   1.112 +   * Test that the 'type' part of the request packet works
   1.113 +   * correctly when the type isn't the same as the method name
   1.114 +   */
   1.115 +  renamedEcho: method(function(a) {
   1.116 +    if (this.conn.currentPacket.type != "echo") {
   1.117 +      return "goodbye";
   1.118 +    }
   1.119 +    return a;
   1.120 +  }, {
   1.121 +    request: {
   1.122 +      type: "echo",
   1.123 +      a: Arg(0),
   1.124 +    },
   1.125 +    response: {
   1.126 +      value: RetVal("string")
   1.127 +    },
   1.128 +  }),
   1.129 +
   1.130 +  testOneWay: method(function(a) {
   1.131 +    // Emit to show that we got this message, because there won't be a response.
   1.132 +    events.emit(this, "oneway", a);
   1.133 +  }, {
   1.134 +    request: { a: Arg(0) },
   1.135 +    oneway: true
   1.136 +  }),
   1.137 +
   1.138 +  events: {
   1.139 +    "oneway": { a: Arg(0) }
   1.140 +  }
   1.141 +});
   1.142 +
   1.143 +let RootFront = protocol.FrontClass(RootActor, {
   1.144 +  initialize: function(client) {
   1.145 +    this.actorID = "root";
   1.146 +    protocol.Front.prototype.initialize.call(this, client);
   1.147 +    // Root owns itself.
   1.148 +    this.manage(this);
   1.149 +  }
   1.150 +});
   1.151 +
   1.152 +function run_test()
   1.153 +{
   1.154 +  DebuggerServer.createRootActor = (conn => {
   1.155 +    return RootActor(conn);
   1.156 +  });
   1.157 +  DebuggerServer.init(() => true);
   1.158 +
   1.159 +  check_except(() => {
   1.160 +    let badActor = ActorClass({
   1.161 +      missing: preEvent("missing-event", function() {
   1.162 +      })
   1.163 +    })
   1.164 +  });
   1.165 +
   1.166 +  protocol.types.getType("array:array:array:number");
   1.167 +  protocol.types.getType("array:array:array:number");
   1.168 +
   1.169 +  check_except(() => protocol.types.getType("unknown"));
   1.170 +  check_except(() => protocol.types.getType("array:unknown"));
   1.171 +  check_except(() => protocol.types.getType("unknown:number"));
   1.172 +  let trace = connectPipeTracing();
   1.173 +  let client = new DebuggerClient(trace);
   1.174 +  let rootClient;
   1.175 +
   1.176 +  client.connect((applicationType, traits) => {
   1.177 +    trace.expectReceive({"from":"<actorid>","applicationType":"xpcshell-tests","traits":[]});
   1.178 +    do_check_eq(applicationType, "xpcshell-tests");
   1.179 +
   1.180 +    rootClient = RootFront(client);
   1.181 +
   1.182 +    rootClient.simpleReturn().then(ret => {
   1.183 +      trace.expectSend({"type":"simpleReturn","to":"<actorid>"});
   1.184 +      trace.expectReceive({"value":1,"from":"<actorid>"});
   1.185 +      do_check_eq(ret, 1);
   1.186 +    }).then(() => {
   1.187 +      return rootClient.promiseReturn();
   1.188 +    }).then(ret => {
   1.189 +      trace.expectSend({"type":"promiseReturn","to":"<actorid>"});
   1.190 +      trace.expectReceive({"value":1,"from":"<actorid>"});
   1.191 +      do_check_eq(ret, 1);
   1.192 +    }).then(() => {
   1.193 +      // Missing argument should throw an exception
   1.194 +      check_except(() => {
   1.195 +        rootClient.simpleArgs(5);
   1.196 +      });
   1.197 +
   1.198 +      return rootClient.simpleArgs(5, 10)
   1.199 +    }).then(ret => {
   1.200 +      trace.expectSend({"type":"simpleArgs","firstArg":5,"secondArg":10,"to":"<actorid>"});
   1.201 +      trace.expectReceive({"firstResponse":6,"secondResponse":11,"from":"<actorid>"});
   1.202 +      do_check_eq(ret.firstResponse, 6);
   1.203 +      do_check_eq(ret.secondResponse, 11);
   1.204 +    }).then(() => {
   1.205 +      return rootClient.nestedArgs(1, 2, 3);
   1.206 +    }).then(ret => {
   1.207 +      trace.expectSend({"type":"nestedArgs","firstArg":1,"nest":{"secondArg":2,"nest":{"thirdArg":3}},"to":"<actorid>"});
   1.208 +      trace.expectReceive({"a":1,"b":2,"c":3,"from":"<actorid>"});
   1.209 +      do_check_eq(ret.a, 1);
   1.210 +      do_check_eq(ret.b, 2);
   1.211 +      do_check_eq(ret.c, 3);
   1.212 +    }).then(() => {
   1.213 +      return rootClient.optionArgs({
   1.214 +        "option1": 5,
   1.215 +        "option2": 10
   1.216 +      });
   1.217 +    }).then(ret => {
   1.218 +      trace.expectSend({"type":"optionArgs","option1":5,"option2":10,"to":"<actorid>"});
   1.219 +      trace.expectReceive({"option1":5,"option2":10,"from":"<actorid>"});
   1.220 +      do_check_eq(ret.option1, 5);
   1.221 +      do_check_eq(ret.option2, 10);
   1.222 +    }).then(() => {
   1.223 +      return rootClient.optionArgs({});
   1.224 +    }).then(ret => {
   1.225 +      trace.expectSend({"type":"optionArgs","to":"<actorid>"});
   1.226 +      trace.expectReceive({"from":"<actorid>"});
   1.227 +      do_check_true(typeof(ret.option1) === "undefined");
   1.228 +      do_check_true(typeof(ret.option2) === "undefined");
   1.229 +    }).then(ret => {
   1.230 +      // Explicitly call an optional argument...
   1.231 +      return rootClient.optionalArgs(5, 10);
   1.232 +    }).then(ret => {
   1.233 +      trace.expectSend({"type":"optionalArgs","a":5,"b":10,"to":"<actorid>"});
   1.234 +      trace.expectReceive({"value":10,"from":"<actorid>"});
   1.235 +      do_check_eq(ret, 10);
   1.236 +    }).then(() => {
   1.237 +      // Now don't pass the optional argument, expect the default.
   1.238 +      return rootClient.optionalArgs(5);
   1.239 +    }).then(ret => {
   1.240 +      trace.expectSend({"type":"optionalArgs","a":5,"to":"<actorid>"});
   1.241 +      trace.expectReceive({"value":200,"from":"<actorid>"});
   1.242 +      do_check_eq(ret, 200);
   1.243 +    }).then(ret => {
   1.244 +      return rootClient.arrayArgs([0, 1, 2, 3, 4, 5]);
   1.245 +    }).then(ret => {
   1.246 +      trace.expectSend({"type":"arrayArgs","a":[0,1,2,3,4,5],"to":"<actorid>"});
   1.247 +      trace.expectReceive({"arrayReturn":[0,1,2,3,4,5],"from":"<actorid>"});
   1.248 +      do_check_eq(ret[0], 0);
   1.249 +      do_check_eq(ret[5], 5);
   1.250 +    }).then(() => {
   1.251 +      return rootClient.arrayArgs([[5]]);
   1.252 +    }).then(ret => {
   1.253 +      trace.expectSend({"type":"arrayArgs","a":[[5]],"to":"<actorid>"});
   1.254 +      trace.expectReceive({"arrayReturn":[[5]],"from":"<actorid>"});
   1.255 +      do_check_eq(ret[0][0], 5);
   1.256 +    }).then(() => {
   1.257 +      return rootClient.renamedEcho("hello");
   1.258 +    }).then(str => {
   1.259 +      trace.expectSend({"type":"echo","a":"hello","to":"<actorid>"});
   1.260 +      trace.expectReceive({"value":"hello","from":"<actorid>"});
   1.261 +
   1.262 +      do_check_eq(str, "hello");
   1.263 +
   1.264 +      let deferred = promise.defer();
   1.265 +      rootClient.on("oneway", (response) => {
   1.266 +        trace.expectSend({"type":"testOneWay","a":"hello","to":"<actorid>"});
   1.267 +        trace.expectReceive({"type":"oneway","a":"hello","from":"<actorid>"});
   1.268 +
   1.269 +        do_check_eq(response, "hello");
   1.270 +        deferred.resolve();
   1.271 +      });
   1.272 +      do_check_true(typeof(rootClient.testOneWay("hello")) === "undefined");
   1.273 +      return deferred.promise;
   1.274 +    }).then(() => {
   1.275 +      client.close(() => {
   1.276 +        do_test_finished();
   1.277 +      });
   1.278 +    }).then(null, err => {
   1.279 +      do_report_unexpected_exception(err, "Failure executing test");
   1.280 +    });
   1.281 +  });
   1.282 +  do_test_pending();
   1.283 +}

mercurial