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

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

Issue 11225058: Initial JS stub implementation of Object.observe. Adds support for .object/.unobserve/.notify/.deli… (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: install functions on Object, not Object.prototype 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
OLDNEW
(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 "use strict"; // So that |this| in callback can be undefined.
37
38 var observer = {
39 records: undefined,
40 callbackCount: 0,
41 reset: function() {
42 this.records = undefined;
43 this.callbackCount = 0;
44 },
45 assertNotCalled: function() {
46 assertEquals(undefined, this.records);
47 assertEquals(0, this.callbackCount);
48 },
49 assertCalled: function() {
50 assertEquals(1, this.callbackCount);
51 },
52 assertRecordCount: function(count) {
53 this.assertCalled();
54 assertEquals(count, this.records.length);
55 },
56 assertCallbackRecords: function(recs) {
57 this.assertRecordCount(recs.length);
58 for (var i = 0; i < recs.length; i++) {
59 assertSame(this.records[i].object, recs[i].object);
60 assertEquals('string', typeof recs[i].type);
61 assertPropertiesEqual(this.records[i], recs[i]);
62 }
63 }
64 };
65
66 observer.callback = function(r) {
67 assertEquals(undefined, this);
68 assertEquals('object', typeof r);
69 assertTrue(r instanceof Array)
70 observer.records = r;
71 observer.callbackCount++;
72 };
73
74 observer.reset();
75 allObservers.push(observer);
76 return observer;
77 }
78
79 var observer = createObserver();
80 assertEquals("function", typeof observer.callback);
81 var obj = {};
82
83 function frozenFunction() {}
84 Object.freeze(frozenFunction);
85 var nonFunction = {};
86 var changeRecordWithAccessor = { type: 'foo' };
87 var recordCreated = false;
88 Object.defineProperty(changeRecordWithAccessor, 'name', {
89 get: function() {
90 recordCreated = true;
91 },
92 enumerable: true
93 })
94
95 // Object.observe
96 assertThrows(function() { Object.observe("non-object", observer.callback); }, Ty peError);
97 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError);
98 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError);
99
100 // Object.unobserve
101 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError);
102
103 // Object.notify
104 assertThrows(function() { Object.notify(obj, {}); }, TypeError);
105 assertThrows(function() { Object.notify(obj, { type: 4 }); }, TypeError);
106 Object.notify(obj, changeRecordWithAccessor);
107 assertFalse(recordCreated);
108
109 // Object.deliverChangeRecords
110 assertThrows(function() { Object.deliverChangeRecords(nonFunction); }, TypeError );
111
112 // Multiple records are delivered.
113 Object.observe(obj, observer.callback);
114 Object.notify(obj, {
115 object: obj,
116 type: 'updated',
117 name: 'foo',
118 expando: 1
119 });
120
121 Object.notify(obj, {
122 object: obj,
123 type: 'deleted',
124 name: 'bar',
125 expando2: 'str'
126 });
127 Object.deliverChangeRecords(observer.callback);
128 observer.assertCallbackRecords([
129 { object: obj, name: 'foo', type: 'updated', expando: 1 },
130 { object: obj, name: 'bar', type: 'deleted', expando2: 'str' }
131 ]);
132
133 // No delivery takes place if no records are pending
134 reset();
135 Object.deliverChangeRecords(observer.callback);
136 observer.assertNotCalled();
137
138 // Multiple observation has no effect.
139 reset();
140 Object.observe(obj, observer.callback);
141 Object.observe(obj, observer.callback);
142 Object.notify(obj, {
143 type: 'foo',
144 });
145 Object.deliverChangeRecords(observer.callback);
146 observer.assertCalled();
147
148 // Observation can be stopped.
149 reset();
150 Object.unobserve(obj, observer.callback);
151 Object.notify(obj, {
152 type: 'foo',
153 });
154 Object.deliverChangeRecords(observer.callback);
155 observer.assertNotCalled();
156
157 // Multiple unobservation has no effect
158 reset();
159 Object.unobserve(obj, observer.callback);
160 Object.unobserve(obj, observer.callback);
161 Object.notify(obj, {
162 type: 'foo',
163 });
164 Object.deliverChangeRecords(observer.callback);
165 observer.assertNotCalled();
166
167 // Re-observation works and only includes changeRecords after of call.
168 reset();
169 Object.notify(obj, {
170 type: 'foo',
171 });
172 Object.observe(obj, observer.callback);
173 Object.notify(obj, {
174 type: 'foo',
175 });
176 records = undefined;
177 Object.deliverChangeRecords(observer.callback);
178 observer.assertRecordCount(1);
179
180 // Observing a continuous stream of changes, while itermittantly unobserving.
181 reset();
182 Object.observe(obj, observer.callback);
183 Object.notify(obj, {
184 type: 'foo',
185 val: 1
186 });
187
188 Object.unobserve(obj, observer.callback);
189 Object.notify(obj, {
190 type: 'foo',
191 val: 2
192 });
193
194 Object.observe(obj, observer.callback);
195 Object.notify(obj, {
196 type: 'foo',
197 val: 3
198 });
199
200 Object.unobserve(obj, observer.callback);
201 Object.notify(obj, {
202 type: 'foo',
203 val: 4
204 });
205
206 Object.observe(obj, observer.callback);
207 Object.notify(obj, {
208 type: 'foo',
209 val: 5
210 });
211
212 Object.unobserve(obj, observer.callback);
213 Object.deliverChangeRecords(observer.callback);
214 observer.assertCallbackRecords([
215 { object: obj, type: 'foo', val: 1 },
216 { object: obj, type: 'foo', val: 3 },
217 { object: obj, type: 'foo', val: 5 }
218 ]);
219
220 // Observing multiple objects; records appear in order;.
221 reset();
222 var obj2 = {};
223 var obj3 = {}
224 Object.observe(obj, observer.callback);
225 Object.observe(obj3, observer.callback);
226 Object.observe(obj2, observer.callback);
227 Object.notify(obj, {
228 type: 'foo',
229 });
230 Object.notify(obj2, {
231 type: 'foo',
232 });
233 Object.notify(obj3, {
234 type: 'foo',
235 });
236 Object.deliverChangeRecords(observer.callback);
237 observer.assertCallbackRecords([
238 { object: obj, type: 'foo' },
239 { object: obj2, type: 'foo' },
240 { object: obj3, type: 'foo' }
241 ]);
OLDNEW
« src/object-observe.js ('K') | « src/object-observe.js ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698