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 | Cu.import("resource://services-common/storageservice.js"); |
michael@0 | 5 | |
michael@0 | 6 | function run_test() { |
michael@0 | 7 | initTestLogging("Trace"); |
michael@0 | 8 | |
michael@0 | 9 | run_next_test(); |
michael@0 | 10 | } |
michael@0 | 11 | |
michael@0 | 12 | add_test(function test_bso_constructor() { |
michael@0 | 13 | _("Ensure created BSO instances are initialized properly."); |
michael@0 | 14 | |
michael@0 | 15 | let bso = new BasicStorageObject(); |
michael@0 | 16 | do_check_eq(bso.id, null); |
michael@0 | 17 | do_check_eq(bso.collection, null); |
michael@0 | 18 | do_check_attribute_count(bso.data, 0); |
michael@0 | 19 | do_check_eq(bso.payload, null); |
michael@0 | 20 | do_check_eq(bso.modified, null); |
michael@0 | 21 | do_check_eq(bso.sortindex, 0); |
michael@0 | 22 | do_check_eq(bso.ttl, null); |
michael@0 | 23 | |
michael@0 | 24 | let bso = new BasicStorageObject("foobar"); |
michael@0 | 25 | do_check_eq(bso.id, "foobar"); |
michael@0 | 26 | do_check_eq(bso.collection, null); |
michael@0 | 27 | do_check_attribute_count(bso.data, 0); |
michael@0 | 28 | |
michael@0 | 29 | let bso = new BasicStorageObject("foo", "coll"); |
michael@0 | 30 | do_check_eq(bso.id, "foo"); |
michael@0 | 31 | do_check_eq(bso.collection, "coll"); |
michael@0 | 32 | do_check_attribute_count(bso.data, 0); |
michael@0 | 33 | |
michael@0 | 34 | run_next_test(); |
michael@0 | 35 | }); |
michael@0 | 36 | |
michael@0 | 37 | add_test(function test_bso_attributes() { |
michael@0 | 38 | _("Ensure attribute getters and setters work."); |
michael@0 | 39 | |
michael@0 | 40 | let bso = new BasicStorageObject("foobar"); |
michael@0 | 41 | bso.payload = "pay"; |
michael@0 | 42 | do_check_eq(bso.payload, "pay"); |
michael@0 | 43 | |
michael@0 | 44 | bso.modified = 35423; |
michael@0 | 45 | do_check_eq(bso.modified, 35423); |
michael@0 | 46 | |
michael@0 | 47 | bso.sortindex = 10; |
michael@0 | 48 | do_check_eq(bso.sortindex, 10); |
michael@0 | 49 | |
michael@0 | 50 | bso.ttl = 60; |
michael@0 | 51 | do_check_eq(bso.ttl, 60); |
michael@0 | 52 | |
michael@0 | 53 | run_next_test(); |
michael@0 | 54 | }); |
michael@0 | 55 | |
michael@0 | 56 | add_test(function test_bso_deserialize() { |
michael@0 | 57 | _("Ensure that deserialize() works."); |
michael@0 | 58 | |
michael@0 | 59 | _("A simple working test."); |
michael@0 | 60 | let json = '{"id": "foobar", "payload": "pay", "modified": 1223145532}'; |
michael@0 | 61 | let bso = new BasicStorageObject(); |
michael@0 | 62 | bso.deserialize(json); |
michael@0 | 63 | do_check_neq(bso, null); |
michael@0 | 64 | do_check_eq(bso.id, "foobar"); |
michael@0 | 65 | do_check_eq(bso.payload, "pay"); |
michael@0 | 66 | do_check_eq(bso.modified, 1223145532); |
michael@0 | 67 | |
michael@0 | 68 | _("Invalid JSON."); |
michael@0 | 69 | let json = '{id: "foobar}'; |
michael@0 | 70 | let bso = new BasicStorageObject(); |
michael@0 | 71 | try { |
michael@0 | 72 | bso.deserialize(json); |
michael@0 | 73 | do_check_true(false); |
michael@0 | 74 | } catch (ex) { |
michael@0 | 75 | do_check_eq(ex.name, "SyntaxError"); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | _("Invalid key in JSON."); |
michael@0 | 79 | let json = '{"id": "foo", "payload": "pay", "BADKEY": "irrelevant"}'; |
michael@0 | 80 | let bso = new BasicStorageObject(); |
michael@0 | 81 | try { |
michael@0 | 82 | bso.deserialize(json); |
michael@0 | 83 | do_check_true(false); |
michael@0 | 84 | } catch (ex) { |
michael@0 | 85 | do_check_eq(ex.name, "Error"); |
michael@0 | 86 | do_check_eq(ex.message.indexOf("Invalid key"), 0); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | _("Loading native JS objects works."); |
michael@0 | 90 | let bso = new BasicStorageObject(); |
michael@0 | 91 | bso.deserialize({id: "foo", payload: "pay"}); |
michael@0 | 92 | do_check_neq(bso, null); |
michael@0 | 93 | do_check_eq(bso.id, "foo"); |
michael@0 | 94 | do_check_eq(bso.payload, "pay"); |
michael@0 | 95 | |
michael@0 | 96 | _("Passing invalid type is caught."); |
michael@0 | 97 | let bso = new BasicStorageObject(); |
michael@0 | 98 | try { |
michael@0 | 99 | bso.deserialize(["foo", "bar"]); |
michael@0 | 100 | do_check_true(false); |
michael@0 | 101 | } catch (ex) { |
michael@0 | 102 | do_check_eq(ex.name, "Error"); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | run_next_test(); |
michael@0 | 106 | }); |
michael@0 | 107 | |
michael@0 | 108 | add_test(function test_bso_toJSON() { |
michael@0 | 109 | _("Ensure JSON serialization works."); |
michael@0 | 110 | |
michael@0 | 111 | let bso = new BasicStorageObject(); |
michael@0 | 112 | do_check_attribute_count(bso.toJSON(), 0); |
michael@0 | 113 | |
michael@0 | 114 | bso.id = "foo"; |
michael@0 | 115 | bso.payload = "pay"; |
michael@0 | 116 | let json = bso.toJSON(); |
michael@0 | 117 | let original = json; |
michael@0 | 118 | |
michael@0 | 119 | do_check_attribute_count(original, 2); |
michael@0 | 120 | do_check_eq(original.id, "foo"); |
michael@0 | 121 | do_check_eq(original.payload, "pay"); |
michael@0 | 122 | |
michael@0 | 123 | run_next_test(); |
michael@0 | 124 | }); |