Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(895)

Side by Side Diff: test/mjsunit/harmony/object-observe.js

Issue 11369154: Implement Object.getNotifier() and remove Object.notify() (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: More restrictive, more tests Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/cctest/test-object-observe.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 }) 94 })
95 95
96 // Object.observe 96 // Object.observe
97 assertThrows(function() { Object.observe("non-object", observer.callback); }, Ty peError); 97 assertThrows(function() { Object.observe("non-object", observer.callback); }, Ty peError);
98 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); 98 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError);
99 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); 99 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError);
100 100
101 // Object.unobserve 101 // Object.unobserve
102 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError); 102 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError);
103 103
104 // Object.notify 104 // Object.getNotifier
105 assertThrows(function() { Object.notify(obj, {}); }, TypeError); 105 var notifier = Object.getNotifier(obj);
106 assertThrows(function() { Object.notify(obj, { type: 4 }); }, TypeError); 106 assertSame(notifier, Object.getNotifier(obj));
107 assertEquals(null, Object.getNotifier(Object.freeze({})));
108 assertFalse(notifier.hasOwnProperty('notify'));
109 assertEquals([], Object.keys(notifier));
110 var notifyDesc = Object.getOwnPropertyDescriptor(notifier.__proto__, 'notify');
111 assertTrue(notifyDesc.configurable);
112 assertTrue(notifyDesc.writable);
113 assertFalse(notifyDesc.enumerable);
114 assertThrows(function() { notifier.notify({}); }, TypeError);
115 assertThrows(function() { notifier.notify({ type: 4 }); }, TypeError);
116 var notify = notifier.notify;
117 assertThrows(function() { notify.call(undefined, { type: 'a' }); }, TypeError);
118 assertThrows(function() { notify.call(null, { type: 'a' }); }, TypeError);
119 assertThrows(function() { notify.call(5, { type: 'a' }); }, TypeError);
120 assertThrows(function() { notify.call('hello', { type: 'a' }); }, TypeError);
121 assertThrows(function() { notify.call(false, { type: 'a' }); }, TypeError);
122 assertThrows(function() { notify.call({}, { type: 'a' }); }, TypeError);
107 assertFalse(recordCreated); 123 assertFalse(recordCreated);
108 Object.notify(obj, changeRecordWithAccessor); 124 notifier.notify(changeRecordWithAccessor);
109 assertFalse(recordCreated); 125 assertFalse(recordCreated); // not observed yet
110 126
111 // Object.deliverChangeRecords 127 // Object.deliverChangeRecords
112 assertThrows(function() { Object.deliverChangeRecords(nonFunction); }, TypeError ); 128 assertThrows(function() { Object.deliverChangeRecords(nonFunction); }, TypeError );
113 129
114 // Multiple records are delivered. 130 // Multiple records are delivered.
115 Object.observe(obj, observer.callback); 131 Object.observe(obj, observer.callback);
116 Object.notify(obj, { 132 notifier.notify({
117 object: obj,
118 type: 'updated', 133 type: 'updated',
119 name: 'foo', 134 name: 'foo',
120 expando: 1 135 expando: 1
121 }); 136 });
122 137
123 Object.notify(obj, { 138 notifier.notify({
124 object: obj, 139 object: notifier, // object property is ignored
125 type: 'deleted', 140 type: 'deleted',
126 name: 'bar', 141 name: 'bar',
127 expando2: 'str' 142 expando2: 'str'
128 }); 143 });
129 Object.deliverChangeRecords(observer.callback); 144 Object.deliverChangeRecords(observer.callback);
130 observer.assertCallbackRecords([ 145 observer.assertCallbackRecords([
131 { object: obj, name: 'foo', type: 'updated', expando: 1 }, 146 { object: obj, name: 'foo', type: 'updated', expando: 1 },
132 { object: obj, name: 'bar', type: 'deleted', expando2: 'str' } 147 { object: obj, name: 'bar', type: 'deleted', expando2: 'str' }
133 ]); 148 ]);
134 149
135 // No delivery takes place if no records are pending 150 // No delivery takes place if no records are pending
136 reset(); 151 reset();
137 Object.deliverChangeRecords(observer.callback); 152 Object.deliverChangeRecords(observer.callback);
138 observer.assertNotCalled(); 153 observer.assertNotCalled();
139 154
140 // Multiple observation has no effect. 155 // Multiple observation has no effect.
141 reset(); 156 reset();
142 Object.observe(obj, observer.callback); 157 Object.observe(obj, observer.callback);
143 Object.observe(obj, observer.callback); 158 Object.observe(obj, observer.callback);
144 Object.notify(obj, { 159 Object.getNotifier(obj).notify({
145 type: 'foo', 160 type: 'foo',
146 }); 161 });
147 Object.deliverChangeRecords(observer.callback); 162 Object.deliverChangeRecords(observer.callback);
148 observer.assertCalled(); 163 observer.assertCalled();
149 164
150 // Observation can be stopped. 165 // Observation can be stopped.
151 reset(); 166 reset();
152 Object.unobserve(obj, observer.callback); 167 Object.unobserve(obj, observer.callback);
153 Object.notify(obj, { 168 Object.getNotifier(obj).notify({
154 type: 'foo', 169 type: 'foo',
155 }); 170 });
156 Object.deliverChangeRecords(observer.callback); 171 Object.deliverChangeRecords(observer.callback);
157 observer.assertNotCalled(); 172 observer.assertNotCalled();
158 173
159 // Multiple unobservation has no effect 174 // Multiple unobservation has no effect
160 reset(); 175 reset();
161 Object.unobserve(obj, observer.callback); 176 Object.unobserve(obj, observer.callback);
162 Object.unobserve(obj, observer.callback); 177 Object.unobserve(obj, observer.callback);
163 Object.notify(obj, { 178 Object.getNotifier(obj).notify({
164 type: 'foo', 179 type: 'foo',
165 }); 180 });
166 Object.deliverChangeRecords(observer.callback); 181 Object.deliverChangeRecords(observer.callback);
167 observer.assertNotCalled(); 182 observer.assertNotCalled();
168 183
169 // Re-observation works and only includes changeRecords after of call. 184 // Re-observation works and only includes changeRecords after of call.
170 reset(); 185 reset();
171 Object.notify(obj, { 186 Object.getNotifier(obj).notify({
172 type: 'foo', 187 type: 'foo',
173 }); 188 });
174 Object.observe(obj, observer.callback); 189 Object.observe(obj, observer.callback);
175 Object.notify(obj, { 190 Object.getNotifier(obj).notify({
176 type: 'foo', 191 type: 'foo',
177 }); 192 });
178 records = undefined; 193 records = undefined;
179 Object.deliverChangeRecords(observer.callback); 194 Object.deliverChangeRecords(observer.callback);
180 observer.assertRecordCount(1); 195 observer.assertRecordCount(1);
181 196
182 // Observing a continuous stream of changes, while itermittantly unobserving. 197 // Observing a continuous stream of changes, while itermittantly unobserving.
183 reset(); 198 reset();
184 Object.observe(obj, observer.callback); 199 Object.observe(obj, observer.callback);
185 Object.notify(obj, { 200 Object.getNotifier(obj).notify({
186 type: 'foo', 201 type: 'foo',
187 val: 1 202 val: 1
188 }); 203 });
189 204
190 Object.unobserve(obj, observer.callback); 205 Object.unobserve(obj, observer.callback);
191 Object.notify(obj, { 206 Object.getNotifier(obj).notify({
192 type: 'foo', 207 type: 'foo',
193 val: 2 208 val: 2
194 }); 209 });
195 210
196 Object.observe(obj, observer.callback); 211 Object.observe(obj, observer.callback);
197 Object.notify(obj, { 212 Object.getNotifier(obj).notify({
198 type: 'foo', 213 type: 'foo',
199 val: 3 214 val: 3
200 }); 215 });
201 216
202 Object.unobserve(obj, observer.callback); 217 Object.unobserve(obj, observer.callback);
203 Object.notify(obj, { 218 Object.getNotifier(obj).notify({
204 type: 'foo', 219 type: 'foo',
205 val: 4 220 val: 4
206 }); 221 });
207 222
208 Object.observe(obj, observer.callback); 223 Object.observe(obj, observer.callback);
209 Object.notify(obj, { 224 Object.getNotifier(obj).notify({
210 type: 'foo', 225 type: 'foo',
211 val: 5 226 val: 5
212 }); 227 });
213 228
214 Object.unobserve(obj, observer.callback); 229 Object.unobserve(obj, observer.callback);
215 Object.deliverChangeRecords(observer.callback); 230 Object.deliverChangeRecords(observer.callback);
216 observer.assertCallbackRecords([ 231 observer.assertCallbackRecords([
217 { object: obj, type: 'foo', val: 1 }, 232 { object: obj, type: 'foo', val: 1 },
218 { object: obj, type: 'foo', val: 3 }, 233 { object: obj, type: 'foo', val: 3 },
219 { object: obj, type: 'foo', val: 5 } 234 { object: obj, type: 'foo', val: 5 }
220 ]); 235 ]);
221 236
222 // Observing multiple objects; records appear in order. 237 // Observing multiple objects; records appear in order.
223 reset(); 238 reset();
224 var obj2 = {}; 239 var obj2 = {};
225 var obj3 = {} 240 var obj3 = {}
226 Object.observe(obj, observer.callback); 241 Object.observe(obj, observer.callback);
227 Object.observe(obj3, observer.callback); 242 Object.observe(obj3, observer.callback);
228 Object.observe(obj2, observer.callback); 243 Object.observe(obj2, observer.callback);
229 Object.notify(obj, { 244 Object.getNotifier(obj).notify({
230 type: 'foo1', 245 type: 'foo1',
231 }); 246 });
232 Object.notify(obj2, { 247 Object.getNotifier(obj2).notify({
233 type: 'foo2', 248 type: 'foo2',
234 }); 249 });
235 Object.notify(obj3, { 250 Object.getNotifier(obj3).notify({
236 type: 'foo3', 251 type: 'foo3',
237 }); 252 });
238 Object.observe(obj3, observer.callback); 253 Object.observe(obj3, observer.callback);
239 Object.deliverChangeRecords(observer.callback); 254 Object.deliverChangeRecords(observer.callback);
240 observer.assertCallbackRecords([ 255 observer.assertCallbackRecords([
241 { object: obj, type: 'foo1' }, 256 { object: obj, type: 'foo1' },
242 { object: obj2, type: 'foo2' }, 257 { object: obj2, type: 'foo2' },
243 { object: obj3, type: 'foo3' } 258 { object: obj3, type: 'foo3' }
244 ]); 259 ]);
245 260
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 { object: arr, name: '3', type: 'new' }, 439 { object: arr, name: '3', type: 'new' },
425 { object: arr, name: 'length', type: 'updated', oldValue: 3 }, 440 { object: arr, name: 'length', type: 'updated', oldValue: 3 },
426 { object: arr, name: '100', type: 'new' }, 441 { object: arr, name: '100', type: 'new' },
427 { object: arr, name: 'length', type: 'updated', oldValue: 4 }, 442 { object: arr, name: 'length', type: 'updated', oldValue: 4 },
428 { object: arr, name: '200', type: 'new' }, 443 { object: arr, name: '200', type: 'new' },
429 { object: arr, name: 'length', type: 'updated', oldValue: 101 }, 444 { object: arr, name: 'length', type: 'updated', oldValue: 101 },
430 { object: arr, name: '400', type: 'new' }, 445 { object: arr, name: '400', type: 'new' },
431 { object: arr, name: 'length', type: 'updated', oldValue: 201 }, 446 { object: arr, name: 'length', type: 'updated', oldValue: 201 },
432 { object: arr, name: '50', type: 'new' }, 447 { object: arr, name: '50', type: 'new' },
433 ]); 448 ]);
OLDNEW
« no previous file with comments | « test/cctest/test-object-observe.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698