OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 // Flags: --harmony-object-observe | |
29 | |
30 (function() { | |
rossberg
2012/10/24 09:57:24
Nit: We don't usually turn test cases into iffies,
rafaelw
2012/10/24 11:18:04
Done.
| |
31 var obj = {}; | |
32 var records; | |
33 var callbackCount = 0; | |
34 function obs(r) { | |
35 callbackCount++; | |
36 records = r; | |
37 } | |
38 | |
39 function frozenFunction() {} | |
40 Object.freeze(frozenFunction); | |
41 var nonFunction = {}; | |
42 var changeRecordWithAccessor = { type: 'foo' }; | |
43 var recordCreated = false; | |
44 Object.defineProperty(changeRecordWithAccessor, 'name', { | |
45 get: function() { | |
46 recordCreated = true; | |
47 }, | |
48 enumerable: true | |
49 }) | |
50 | |
51 // Object.observe | |
52 assertThrows(function() { Object.observe("non-object", obs); }, TypeError); | |
53 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); | |
54 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); | |
55 | |
56 // Object.unobserve | |
57 assertThrows(function() { Object.unobserve(4, obs); }, TypeError); | |
58 | |
59 // Object.notify | |
60 assertThrows(function() { Object.notify(obs, {}); }, TypeError); | |
61 assertThrows(function() { Object.notify(obs, { type: 4 }); }, TypeError); | |
62 Object.notify(obs, changeRecordWithAccessor); | |
63 assertFalse(recordCreated); | |
64 | |
65 // Object.deliverChangeRecords | |
66 assertThrows(function() { Object.deliverChangeRecords(nonFunction); }, TypeErr or); | |
67 | |
68 // Multiple records are delivered. | |
69 Object.observe(obj, obs); | |
70 Object.notify(obj, { | |
71 object: obj, | |
72 type: 'updated', | |
73 name: 'foo', | |
74 expando: 1 | |
75 }); | |
76 | |
77 Object.notify(obj, { | |
78 object: obj, | |
79 type: 'deleted', | |
80 name: 'bar', | |
81 expando2: 'str' | |
82 }); | |
83 Object.deliverChangeRecords(obs); | |
84 assertEquals(1, callbackCount); | |
85 assertEquals(2, records.length); | |
86 assertEquals(obj, records[0].object); | |
87 assertEquals('foo', records[0].name); | |
88 assertEquals('updated', records[0].type); | |
89 assertEquals(1, records[0].expando); | |
90 assertEquals(obj, records[1].object); | |
91 assertEquals('bar', records[1].name); | |
92 assertEquals('deleted', records[1].type); | |
93 assertEquals('str', records[1].expando2); | |
94 assertEquals(undefined, records[1].expando); | |
95 | |
96 // No delivery takes place if no records are pending | |
97 records = undefined; | |
98 Object.deliverChangeRecords(obs); | |
99 assertEquals(1, callbackCount); | |
rossberg
2012/10/24 09:57:24
Better reset callback count and test for 0. Otherw
rafaelw
2012/10/24 11:18:04
Done.
| |
100 assertEquals(undefined, records); | |
101 | |
102 // Multiple observation has no effect. | |
103 Object.observe(obj, obs); | |
104 Object.observe(obj, obs); | |
105 Object.notify(obj, { | |
106 type: 'foo', | |
107 }); | |
108 Object.deliverChangeRecords(obs); | |
109 assertEquals(2, callbackCount); | |
110 | |
111 // Observation can be stopped. | |
112 Object.unobserve(obj, obs); | |
113 Object.notify(obj, { | |
114 type: 'foo', | |
115 }); | |
116 Object.deliverChangeRecords(obs); | |
117 assertEquals(2, callbackCount); | |
118 | |
119 // Multiple unobservation has no effect | |
120 Object.unobserve(obj, obs); | |
121 Object.unobserve(obj, obs); | |
122 Object.notify(obj, { | |
123 type: 'foo', | |
124 }); | |
125 Object.deliverChangeRecords(obs); | |
126 assertEquals(2, callbackCount); | |
127 | |
128 // Re-observation works and only includes changeRecords after of call. | |
129 Object.notify(obj, { | |
130 type: 'foo', | |
131 }); | |
132 Object.observe(obj, obs); | |
133 Object.notify(obj, { | |
134 type: 'foo', | |
135 }); | |
136 records = undefined; | |
137 Object.deliverChangeRecords(obs); | |
138 assertEquals(3, callbackCount); | |
139 assertEquals(1, records.length); | |
140 })() | |
rossberg
2012/10/24 09:57:24
Other scenarios to test:
- One function observing
rossberg
2012/10/24 10:06:52
- Objects observed by multiple observers (perhaps
rafaelw
2012/10/24 11:18:04
Done.
rafaelw
2012/10/24 11:18:04
Yeah. I think I'd like to add that in the next pat
rossberg
2012/10/24 12:03:13
Sounds good.
| |
OLD | NEW |