Chromium Code Reviews| 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 var allObservers = []; | |
| 31 function reset() { | |
| 32 allObservers.forEach(function(observer) { observer.reset(); }); | |
| 33 } | |
| 34 | |
| 35 function createObserver() { | |
| 36 var observer = { | |
| 37 records: undefined, | |
| 38 callbackCount: 0, | |
| 39 reset: function() { | |
| 40 this.records = undefined; | |
| 41 this.callbackCount = 0; | |
| 42 }, | |
| 43 assertNotCalled: function() { | |
| 44 assertEquals(undefined, this.records); | |
| 45 assertEquals(0, this.callbackCount); | |
| 46 }, | |
| 47 assertCalled: function() { | |
| 48 assertEquals(1, this.callbackCount); | |
|
rossberg
2012/10/25 13:22:32
Suggestion: make the count an argument.
rafaelw
2012/10/25 14:02:19
I don't think we'll ever want to have been called
| |
| 49 }, | |
| 50 assertRecordCount: function(count) { | |
| 51 this.assertCalled(); | |
| 52 assertEquals(count, this.records.length); | |
| 53 }, | |
| 54 assertCallbackRecords: function(recs) { | |
| 55 this.assertRecordCount(recs.length); | |
| 56 for (var i = 0; i < recs.length; i++) { | |
| 57 assertSame(this.records[i].object, recs[i].object); | |
| 58 assertEquals('string', typeof recs[i].type); | |
| 59 assertPropertiesEqual(this.records[i], recs[i]); | |
| 60 } | |
| 61 } | |
| 62 }; | |
| 63 | |
| 64 observer.callback = function(r) { | |
| 65 assertEquals('object', typeof r); | |
| 66 assertTrue(r instanceof Array) | |
| 67 this.records = r; | |
| 68 this.callbackCount++; | |
| 69 }.bind(observer); | |
|
rossberg
2012/10/25 13:22:32
Perhaps not bind to the observer, but rather asser
rafaelw
2012/10/25 14:02:19
Done.
| |
| 70 | |
| 71 observer.reset(); | |
| 72 allObservers.push(observer); | |
| 73 return observer; | |
| 74 } | |
| 75 | |
| 76 var observer = createObserver(); | |
| 77 assertEquals("function", typeof observer.callback); | |
|
rossberg
2012/10/25 13:22:32
Nit: this seems a bit redundant.
rafaelw
2012/10/25 14:02:19
Good catch. That was throw away code I used to tra
| |
| 78 var obj = {}; | |
| 79 | |
| 80 function frozenFunction() {} | |
| 81 Object.freeze(frozenFunction); | |
| 82 var nonFunction = {}; | |
| 83 var changeRecordWithAccessor = { type: 'foo' }; | |
| 84 var recordCreated = false; | |
| 85 Object.defineProperty(changeRecordWithAccessor, 'name', { | |
| 86 get: function() { | |
| 87 recordCreated = true; | |
| 88 }, | |
| 89 enumerable: true | |
| 90 }) | |
| 91 | |
| 92 // Object.observe | |
| 93 assertThrows(function() { Object.observe("non-object", observer.callback); }, Ty peError); | |
| 94 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); | |
| 95 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); | |
| 96 | |
| 97 // Object.unobserve | |
| 98 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError); | |
| 99 | |
| 100 // Object.notify | |
| 101 assertThrows(function() { Object.notify(obj, {}); }, TypeError); | |
| 102 assertThrows(function() { Object.notify(obj, { type: 4 }); }, TypeError); | |
| 103 Object.notify(obj, changeRecordWithAccessor); | |
| 104 assertFalse(recordCreated); | |
| 105 | |
| 106 // Object.deliverChangeRecords | |
| 107 assertThrows(function() { Object.deliverChangeRecords(nonFunction); }, TypeError ); | |
| 108 | |
| 109 // Multiple records are delivered. | |
| 110 Object.observe(obj, observer.callback); | |
| 111 Object.notify(obj, { | |
| 112 object: obj, | |
| 113 type: 'updated', | |
| 114 name: 'foo', | |
| 115 expando: 1 | |
| 116 }); | |
| 117 | |
| 118 Object.notify(obj, { | |
| 119 object: obj, | |
| 120 type: 'deleted', | |
| 121 name: 'bar', | |
| 122 expando2: 'str' | |
| 123 }); | |
| 124 Object.deliverChangeRecords(observer.callback); | |
| 125 observer.assertCallbackRecords([ | |
| 126 { object: obj, name: 'foo', type: 'updated', expando: 1 }, | |
| 127 { object: obj, name: 'bar', type: 'deleted', expando2: 'str' } | |
| 128 ]); | |
| 129 | |
| 130 // No delivery takes place if no records are pending | |
| 131 reset(); | |
| 132 Object.deliverChangeRecords(observer.callback); | |
| 133 observer.assertNotCalled(); | |
| 134 | |
| 135 // Multiple observation has no effect. | |
| 136 reset(); | |
| 137 Object.observe(obj, observer.callback); | |
| 138 Object.observe(obj, observer.callback); | |
| 139 Object.notify(obj, { | |
| 140 type: 'foo', | |
| 141 }); | |
| 142 Object.deliverChangeRecords(observer.callback); | |
| 143 observer.assertCalled(); | |
| 144 | |
| 145 // Observation can be stopped. | |
| 146 reset(); | |
| 147 Object.unobserve(obj, observer.callback); | |
| 148 Object.notify(obj, { | |
| 149 type: 'foo', | |
| 150 }); | |
| 151 Object.deliverChangeRecords(observer.callback); | |
| 152 observer.assertNotCalled(); | |
| 153 | |
| 154 // Multiple unobservation has no effect | |
| 155 reset(); | |
| 156 Object.unobserve(obj, observer.callback); | |
| 157 Object.unobserve(obj, observer.callback); | |
| 158 Object.notify(obj, { | |
| 159 type: 'foo', | |
| 160 }); | |
| 161 Object.deliverChangeRecords(observer.callback); | |
| 162 observer.assertNotCalled(); | |
| 163 | |
| 164 // Re-observation works and only includes changeRecords after of call. | |
| 165 reset(); | |
| 166 Object.notify(obj, { | |
| 167 type: 'foo', | |
| 168 }); | |
| 169 Object.observe(obj, observer.callback); | |
| 170 Object.notify(obj, { | |
| 171 type: 'foo', | |
| 172 }); | |
| 173 records = undefined; | |
| 174 Object.deliverChangeRecords(observer.callback); | |
| 175 observer.assertRecordCount(1); | |
| 176 | |
| 177 // Observing a continuous stream of changes, while itermittantly unobserving. | |
| 178 reset(); | |
| 179 Object.observe(obj, observer.callback); | |
| 180 Object.notify(obj, { | |
| 181 type: 'foo', | |
| 182 val: 1 | |
| 183 }); | |
| 184 | |
| 185 Object.unobserve(obj, observer.callback); | |
| 186 Object.notify(obj, { | |
| 187 type: 'foo', | |
| 188 val: 2 | |
| 189 }); | |
| 190 | |
| 191 Object.observe(obj, observer.callback); | |
| 192 Object.notify(obj, { | |
| 193 type: 'foo', | |
| 194 val: 3 | |
| 195 }); | |
| 196 | |
| 197 Object.unobserve(obj, observer.callback); | |
| 198 Object.notify(obj, { | |
| 199 type: 'foo', | |
| 200 val: 4 | |
| 201 }); | |
| 202 | |
| 203 Object.observe(obj, observer.callback); | |
| 204 Object.notify(obj, { | |
| 205 type: 'foo', | |
| 206 val: 5 | |
| 207 }); | |
| 208 | |
| 209 Object.unobserve(obj, observer.callback); | |
| 210 Object.deliverChangeRecords(observer.callback); | |
| 211 observer.assertCallbackRecords([ | |
| 212 { object: obj, type: 'foo', val: 1 }, | |
| 213 { object: obj, type: 'foo', val: 3 }, | |
| 214 { object: obj, type: 'foo', val: 5 } | |
| 215 ]); | |
| 216 | |
| 217 // Observing multiple objects; records appear in order;. | |
| 218 reset(); | |
| 219 var obj2 = {}; | |
| 220 var obj3 = {} | |
| 221 Object.observe(obj, observer.callback); | |
| 222 Object.observe(obj3, observer.callback); | |
| 223 Object.observe(obj2, observer.callback); | |
| 224 Object.notify(obj, { | |
| 225 type: 'foo', | |
| 226 }); | |
| 227 Object.notify(obj2, { | |
| 228 type: 'foo', | |
| 229 }); | |
| 230 Object.notify(obj3, { | |
| 231 type: 'foo', | |
| 232 }); | |
| 233 Object.deliverChangeRecords(observer.callback); | |
| 234 observer.assertCallbackRecords([ | |
| 235 { object: obj, type: 'foo' }, | |
| 236 { object: obj2, type: 'foo' }, | |
| 237 { object: obj3, type: 'foo' } | |
| 238 ]); | |
| OLD | NEW |