Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | Components.utils.import("resource://gre/modules/OrderedBroadcast.jsm"); |
michael@0 | 7 | Components.utils.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 8 | |
michael@0 | 9 | let _observerId = 0; |
michael@0 | 10 | |
michael@0 | 11 | function makeObserver() { |
michael@0 | 12 | let deferred = Promise.defer(); |
michael@0 | 13 | |
michael@0 | 14 | let ret = { |
michael@0 | 15 | id: _observerId++, |
michael@0 | 16 | count: 0, |
michael@0 | 17 | promise: deferred.promise, |
michael@0 | 18 | callback: function(data, token, action) { |
michael@0 | 19 | ret.count += 1; |
michael@0 | 20 | deferred.resolve({ data: data, |
michael@0 | 21 | token: token, |
michael@0 | 22 | action: action }); |
michael@0 | 23 | }, |
michael@0 | 24 | }; |
michael@0 | 25 | |
michael@0 | 26 | return ret; |
michael@0 | 27 | }; |
michael@0 | 28 | |
michael@0 | 29 | add_task(function test_send() { |
michael@0 | 30 | let deferred = Promise.defer(); |
michael@0 | 31 | |
michael@0 | 32 | let observer = makeObserver(); |
michael@0 | 33 | |
michael@0 | 34 | let token = { a: "bcde", b: 1234 }; |
michael@0 | 35 | sendOrderedBroadcast("org.mozilla.gecko.test.receiver", |
michael@0 | 36 | token, observer.callback); |
michael@0 | 37 | |
michael@0 | 38 | let value = yield observer.promise; |
michael@0 | 39 | |
michael@0 | 40 | do_check_eq(observer.count, 1); |
michael@0 | 41 | |
michael@0 | 42 | // We get back the correct action and token. |
michael@0 | 43 | do_check_neq(value, null); |
michael@0 | 44 | do_check_matches(value.token, token); |
michael@0 | 45 | do_check_eq(value.action, "org.mozilla.gecko.test.receiver"); |
michael@0 | 46 | |
michael@0 | 47 | // Data is provided by testOrderedBroadcast.java.in. |
michael@0 | 48 | do_check_neq(value.data, null); |
michael@0 | 49 | do_check_eq(value.data.c, "efg"); |
michael@0 | 50 | do_check_eq(value.data.d, 456); |
michael@0 | 51 | |
michael@0 | 52 | // And the provided token is returned to us (as a string) by |
michael@0 | 53 | // testOrderedBroadcast.java.in. |
michael@0 | 54 | do_check_neq(value.data.token, null); |
michael@0 | 55 | do_check_eq(typeof(value.data.token), "string"); |
michael@0 | 56 | do_check_matches(JSON.parse(value.data.token), token); |
michael@0 | 57 | }); |
michael@0 | 58 | |
michael@0 | 59 | add_task(function test_null_token() { |
michael@0 | 60 | let deferred = Promise.defer(); |
michael@0 | 61 | |
michael@0 | 62 | let observer = makeObserver(); |
michael@0 | 63 | |
michael@0 | 64 | sendOrderedBroadcast("org.mozilla.gecko.test.receiver", |
michael@0 | 65 | null, observer.callback); |
michael@0 | 66 | |
michael@0 | 67 | let value = yield observer.promise; |
michael@0 | 68 | |
michael@0 | 69 | do_check_eq(observer.count, 1); |
michael@0 | 70 | |
michael@0 | 71 | // We get back the correct action and token. |
michael@0 | 72 | do_check_neq(value, null); |
michael@0 | 73 | do_check_eq(value.token, null); |
michael@0 | 74 | do_check_eq(value.action, "org.mozilla.gecko.test.receiver"); |
michael@0 | 75 | |
michael@0 | 76 | // Data is provided by testOrderedBroadcast.java.in. |
michael@0 | 77 | do_check_neq(value.data, null); |
michael@0 | 78 | do_check_eq(value.data.c, "efg"); |
michael@0 | 79 | do_check_eq(value.data.d, 456); |
michael@0 | 80 | }); |
michael@0 | 81 | |
michael@0 | 82 | add_task(function test_string_token() { |
michael@0 | 83 | let deferred = Promise.defer(); |
michael@0 | 84 | |
michael@0 | 85 | let observer = makeObserver(); |
michael@0 | 86 | |
michael@0 | 87 | let token = "string_token"; |
michael@0 | 88 | let permission = null; // means any receiver can listen for our intent |
michael@0 | 89 | sendOrderedBroadcast("org.mozilla.gecko.test.receiver", |
michael@0 | 90 | token, observer.callback, permission); |
michael@0 | 91 | |
michael@0 | 92 | let value = yield observer.promise; |
michael@0 | 93 | |
michael@0 | 94 | do_check_eq(observer.count, 1); |
michael@0 | 95 | |
michael@0 | 96 | // We get back the correct action and token. |
michael@0 | 97 | do_check_neq(value, null); |
michael@0 | 98 | do_check_eq(value.token, token); |
michael@0 | 99 | do_check_eq(typeof(value.token), "string"); |
michael@0 | 100 | do_check_eq(value.action, "org.mozilla.gecko.test.receiver"); |
michael@0 | 101 | |
michael@0 | 102 | // Data is provided by testOrderedBroadcast.java.in. |
michael@0 | 103 | do_check_neq(value.data, null); |
michael@0 | 104 | do_check_eq(value.data.c, "efg"); |
michael@0 | 105 | do_check_eq(value.data.d, 456); |
michael@0 | 106 | do_check_eq(value.data.token, token); |
michael@0 | 107 | }); |
michael@0 | 108 | |
michael@0 | 109 | add_task(function test_permission() { |
michael@0 | 110 | let deferred = Promise.defer(); |
michael@0 | 111 | |
michael@0 | 112 | let observer = makeObserver(); |
michael@0 | 113 | |
michael@0 | 114 | sendOrderedBroadcast("org.mozilla.gecko.test.receiver", |
michael@0 | 115 | null, observer.callback, |
michael@0 | 116 | "org.mozilla.gecko.fake.permission"); |
michael@0 | 117 | |
michael@0 | 118 | let value = yield observer.promise; |
michael@0 | 119 | |
michael@0 | 120 | do_check_eq(observer.count, 1); |
michael@0 | 121 | |
michael@0 | 122 | // We get back the correct action and token. |
michael@0 | 123 | do_check_neq(value, null); |
michael@0 | 124 | do_check_eq(value.token, null); |
michael@0 | 125 | do_check_eq(value.action, "org.mozilla.gecko.test.receiver"); |
michael@0 | 126 | |
michael@0 | 127 | // Data would be provided by testOrderedBroadcast.java.in, except |
michael@0 | 128 | // the no package has the permission, so no responder exists. |
michael@0 | 129 | do_check_eq(value.data, null); |
michael@0 | 130 | }); |
michael@0 | 131 | |
michael@0 | 132 | add_task(function test_send_no_receiver() { |
michael@0 | 133 | let deferred = Promise.defer(); |
michael@0 | 134 | |
michael@0 | 135 | let observer = makeObserver(); |
michael@0 | 136 | |
michael@0 | 137 | sendOrderedBroadcast("org.mozilla.gecko.test.no.receiver", |
michael@0 | 138 | { a: "bcd", b: 123 }, observer.callback); |
michael@0 | 139 | |
michael@0 | 140 | let value = yield observer.promise; |
michael@0 | 141 | |
michael@0 | 142 | do_check_eq(observer.count, 1); |
michael@0 | 143 | |
michael@0 | 144 | // We get back the correct action and token, but the default is to |
michael@0 | 145 | // return no data. |
michael@0 | 146 | do_check_neq(value, null); |
michael@0 | 147 | do_check_neq(value.token, null); |
michael@0 | 148 | do_check_eq(value.token.a, "bcd"); |
michael@0 | 149 | do_check_eq(value.token.b, 123); |
michael@0 | 150 | do_check_eq(value.action, "org.mozilla.gecko.test.no.receiver"); |
michael@0 | 151 | do_check_eq(value.data, null); |
michael@0 | 152 | }); |
michael@0 | 153 | |
michael@0 | 154 | run_next_test(); |