Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Components.utils.import("resource://services-common/observers.js"); |
michael@0 | 5 | |
michael@0 | 6 | let gSubject = {}; |
michael@0 | 7 | |
michael@0 | 8 | function run_test() { |
michael@0 | 9 | run_next_test(); |
michael@0 | 10 | } |
michael@0 | 11 | |
michael@0 | 12 | add_test(function test_function_observer() { |
michael@0 | 13 | let foo = false; |
michael@0 | 14 | |
michael@0 | 15 | let onFoo = function(subject, data) { |
michael@0 | 16 | foo = !foo; |
michael@0 | 17 | do_check_eq(subject, gSubject); |
michael@0 | 18 | do_check_eq(data, "some data"); |
michael@0 | 19 | }; |
michael@0 | 20 | |
michael@0 | 21 | Observers.add("foo", onFoo); |
michael@0 | 22 | Observers.notify("foo", gSubject, "some data"); |
michael@0 | 23 | |
michael@0 | 24 | // The observer was notified after being added. |
michael@0 | 25 | do_check_true(foo); |
michael@0 | 26 | |
michael@0 | 27 | Observers.remove("foo", onFoo); |
michael@0 | 28 | Observers.notify("foo"); |
michael@0 | 29 | |
michael@0 | 30 | // The observer was not notified after being removed. |
michael@0 | 31 | do_check_true(foo); |
michael@0 | 32 | |
michael@0 | 33 | run_next_test(); |
michael@0 | 34 | }); |
michael@0 | 35 | |
michael@0 | 36 | add_test(function test_method_observer() { |
michael@0 | 37 | let obj = { |
michael@0 | 38 | foo: false, |
michael@0 | 39 | onFoo: function(subject, data) { |
michael@0 | 40 | this.foo = !this.foo; |
michael@0 | 41 | do_check_eq(subject, gSubject); |
michael@0 | 42 | do_check_eq(data, "some data"); |
michael@0 | 43 | } |
michael@0 | 44 | }; |
michael@0 | 45 | |
michael@0 | 46 | // The observer is notified after being added. |
michael@0 | 47 | Observers.add("foo", obj.onFoo, obj); |
michael@0 | 48 | Observers.notify("foo", gSubject, "some data"); |
michael@0 | 49 | do_check_true(obj.foo); |
michael@0 | 50 | |
michael@0 | 51 | // The observer is not notified after being removed. |
michael@0 | 52 | Observers.remove("foo", obj.onFoo, obj); |
michael@0 | 53 | Observers.notify("foo"); |
michael@0 | 54 | do_check_true(obj.foo); |
michael@0 | 55 | |
michael@0 | 56 | run_next_test(); |
michael@0 | 57 | }); |
michael@0 | 58 | |
michael@0 | 59 | add_test(function test_object_observer() { |
michael@0 | 60 | let obj = { |
michael@0 | 61 | foo: false, |
michael@0 | 62 | observe: function(subject, topic, data) { |
michael@0 | 63 | this.foo = !this.foo; |
michael@0 | 64 | |
michael@0 | 65 | do_check_eq(subject, gSubject); |
michael@0 | 66 | do_check_eq(topic, "foo"); |
michael@0 | 67 | do_check_eq(data, "some data"); |
michael@0 | 68 | } |
michael@0 | 69 | }; |
michael@0 | 70 | |
michael@0 | 71 | Observers.add("foo", obj); |
michael@0 | 72 | Observers.notify("foo", gSubject, "some data"); |
michael@0 | 73 | |
michael@0 | 74 | // The observer is notified after being added. |
michael@0 | 75 | do_check_true(obj.foo); |
michael@0 | 76 | |
michael@0 | 77 | Observers.remove("foo", obj); |
michael@0 | 78 | Observers.notify("foo"); |
michael@0 | 79 | |
michael@0 | 80 | // The observer is not notified after being removed. |
michael@0 | 81 | do_check_true(obj.foo); |
michael@0 | 82 | |
michael@0 | 83 | run_next_test(); |
michael@0 | 84 | }); |