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

Side by Side Diff: chrome/browser/resources/shared/js/cr_test.html

Issue 6246078: Add an optional set_hook argument to defineProperty. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome/browser/resources
Patch Set: Style fixes. Created 9 years, 10 months 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
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <html> 2 <html>
3 <head> 3 <head>
4 <title></title> 4 <title></title>
5 <style> 5 <style>
6 6
7 </style> 7 </style>
8 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.j s"></script> 8 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.j s"></script>
9 <script src="cr.js"></script> 9 <script src="cr.js"></script>
10 <script src="cr/event_target.js"></script> 10 <script src="cr/event_target.js"></script>
(...skipping 21 matching lines...) Expand all
32 32
33 function testDefinePropertyOnClass() { 33 function testDefinePropertyOnClass() {
34 function C() {} 34 function C() {}
35 C.prototype = { 35 C.prototype = {
36 __proto__: EventTarget.prototype 36 __proto__: EventTarget.prototype
37 }; 37 };
38 38
39 cr.defineProperty(C, 'test'); 39 cr.defineProperty(C, 'test');
40 40
41 var obj = new C; 41 var obj = new C;
42 assertUndefined(obj.test);
43
42 obj.test = 1; 44 obj.test = 1;
43 assertEquals(1, obj.test); 45 assertEquals(1, obj.test);
44 assertEquals(1, obj.test_); 46 assertEquals(1, obj.test_);
45 } 47 }
46 48
47 function testDefinePropertyWithDefault() { 49 function testDefinePropertyWithSetter() {
48 var obj = new EventTarget; 50 var obj = new EventTarget;
49 51
50 cr.defineProperty(obj, 'test', null, 1); 52 var hit = false;
51 53 function onTestSet(value, oldValue) {
52 assertEquals(1, obj.test); 54 assertEquals(obj, this);
53 assertEquals(1, obj.test_); 55 assertEquals(2, this.test);
54 56 assertUndefined(oldValue);
57 assertEquals(2, value);
58 hit = true;
59 }
60 cr.defineProperty(obj, 'test', cr.PropertyKind.JS, onTestSet);
55 obj.test = 2; 61 obj.test = 2;
56 assertEquals(2, obj.test); 62 assertTrue(hit);
57 assertEquals(2, obj.test_);
58 } 63 }
59 64
60 function testDefinePropertyEvent() { 65 function testDefinePropertyEvent() {
61 var obj = new EventTarget; 66 var obj = new EventTarget;
62 cr.defineProperty(obj, 'test'); 67 cr.defineProperty(obj, 'test');
63 obj.test = 1; 68 obj.test = 1;
64 69
65 var count = 0; 70 var count = 0;
66 function f(e) { 71 function f(e) {
67 assertEquals('testChange', e.type); 72 assertEquals('testChange', e.type);
68 assertEquals('test', e.propertyName); 73 assertEquals('test', e.propertyName);
69 assertEquals(1, e.oldValue); 74 assertEquals(1, e.oldValue);
70 assertEquals(2, e.newValue); 75 assertEquals(2, e.newValue);
71 count++; 76 count++;
72 } 77 }
73 78
74 obj.addEventListener('testChange', f); 79 obj.addEventListener('testChange', f);
75 obj.test = 2; 80 obj.test = 2;
76 assertEquals(2, obj.test); 81 assertEquals(2, obj.test);
77 assertEquals('Should have called the property change listener', 1, count); 82 assertEquals('Should have called the property change listener', 1, count);
78 83
79 obj.test = 2; 84 obj.test = 2;
80 assertEquals(1, count); 85 assertEquals(1, count);
81 } 86 }
82 87
83 function testDefinePropertyEventWithDefault() { 88 function testDefinePropertyEventWithDefault() {
84 var obj = new EventTarget; 89 var obj = new EventTarget;
85 cr.defineProperty(obj, 'test', cr.PropertyKind.JS, 1); 90 cr.defineProperty(obj, 'test', cr.PropertyKind.JS);
86 91
87 var count = 0; 92 var count = 0;
88 function f(e) { 93 function f(e) {
89 assertEquals('testChange', e.type); 94 assertEquals('testChange', e.type);
90 assertEquals('test', e.propertyName); 95 assertEquals('test', e.propertyName);
91 assertEquals(1, e.oldValue); 96 assertUndefined(e.oldValue);
92 assertEquals(2, e.newValue); 97 assertEquals(2, e.newValue);
93 count++; 98 count++;
94 } 99 }
95 100
96 obj.addEventListener('testChange', f); 101 obj.addEventListener('testChange', f);
97 102
98 obj.test = 1; 103 obj.test = undefined;
99 assertEquals('Should not have called the property change listener', 0, count); 104 assertEquals('Should not have called the property change listener', 0, count);
100 105
101 obj.test = 2; 106 obj.test = 2;
102 assertEquals(2, obj.test); 107 assertEquals(2, obj.test);
103 assertEquals('Should have called the property change listener', 1, count); 108 assertEquals('Should have called the property change listener', 1, count);
104 109
105 obj.test = 2; 110 obj.test = 2;
106 assertEquals(1, count); 111 assertEquals(1, count);
107 } 112 }
108 113
109 function testDefinePropertyAttr() { 114 function testDefinePropertyAttr() {
110 var obj = document.createElement('div'); 115 var obj = document.createElement('div');
111 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR); 116 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR);
112 117
113 obj.test = 'a'; 118 obj.test = 'a';
114 assertEquals('a', obj.test); 119 assertEquals('a', obj.test);
115 assertEquals('a', obj.getAttribute('test')); 120 assertEquals('a', obj.getAttribute('test'));
116 } 121 }
117 122
118 function testDefinePropertyAttrOnClass() { 123 function testDefinePropertyAttrOnClass() {
119 var obj = document.createElement('button'); 124 var obj = document.createElement('button');
120 cr.defineProperty(HTMLButtonElement, 'test', cr.PropertyKind.ATTR); 125 cr.defineProperty(HTMLButtonElement, 'test', cr.PropertyKind.ATTR);
121 126
127 assertEquals(null, obj.test);
128
122 obj.test = 'a'; 129 obj.test = 'a';
123 assertEquals('a', obj.test); 130 assertEquals('a', obj.test);
124 assertEquals('a', obj.getAttribute('test')); 131 assertEquals('a', obj.getAttribute('test'));
125 } 132 }
126 133
127 function testDefinePropertyAttrWithDefault() { 134 function testDefinePropertyAttrWithSetter() {
128 var obj = document.createElement('div'); 135 var obj = document.createElement('div');
129 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR, 'a');
130 136
131 assertEquals('a', obj.test); 137 var hit = false;
132 assertFalse(obj.hasAttribute('test')); 138 function onTestSet(value, oldValue) {
133 139 assertEquals(obj, this);
140 assertEquals(null, oldValue);
141 assertEquals('b', value);
142 assertEquals('b', this.test);
143 hit = true;
144 }
145 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR, onTestSet);
134 obj.test = 'b'; 146 obj.test = 'b';
135 assertEquals('b', obj.test); 147 assertTrue(hit);
136 assertEquals('b', obj.getAttribute('test'));
137 } 148 }
138 149
139 function testDefinePropertyAttrEvent() { 150 function testDefinePropertyAttrEvent() {
140 var obj = document.createElement('div'); 151 var obj = document.createElement('div');
141 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR); 152 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR);
142 obj.test = 'a';
143 153
144 var count = 0; 154 var count = 0;
145 function f(e) { 155 function f(e) {
146 assertEquals('testChange', e.type); 156 assertEquals('testChange', e.type);
147 assertEquals('test', e.propertyName); 157 assertEquals('test', e.propertyName);
148 assertEquals('a', e.oldValue); 158 assertEquals(null, e.oldValue);
149 assertEquals('b', e.newValue);
150 count++;
151 }
152
153 obj.addEventListener('testChange', f);
154 obj.test = 'b';
155 assertEquals('b', obj.test);
156 assertEquals('Should have called the property change listener', 1, count);
157
158 obj.test = 'b';
159 assertEquals(1, count);
160 }
161
162 function testDefinePropertyAttrEventWithDefault() {
163 var obj = document.createElement('div');
164 cr.defineProperty(obj, 'test', cr.PropertyKind.ATTR, 'a');
165
166 var count = 0;
167 function f(e) {
168 assertEquals('testChange', e.type);
169 assertEquals('test', e.propertyName);
170 assertEquals('a', e.oldValue);
171 assertEquals('b', e.newValue); 159 assertEquals('b', e.newValue);
172 count++; 160 count++;
173 } 161 }
174 162
175 obj.addEventListener('testChange', f); 163 obj.addEventListener('testChange', f);
176 164
177 obj.test = 'a'; 165 obj.test = null;
178 assertEquals('Should not have called the property change listener', 0, count); 166 assertEquals('Should not have called the property change listener', 0, count);
179 167
180 obj.test = 'b'; 168 obj.test = 'b';
181 assertEquals('b', obj.test); 169 assertEquals('b', obj.test);
182 assertEquals('Should have called the property change listener', 1, count); 170 assertEquals('Should have called the property change listener', 1, count);
183 171
184 obj.test = 'b'; 172 obj.test = 'b';
185 assertEquals(1, count); 173 assertEquals(1, count);
186 } 174 }
187 175
(...skipping 28 matching lines...) Expand all
216 204
217 obj.addEventListener('testChange', f); 205 obj.addEventListener('testChange', f);
218 obj.test = true; 206 obj.test = true;
219 assertTrue(obj.test); 207 assertTrue(obj.test);
220 assertEquals('Should have called the property change listener', 1, count); 208 assertEquals('Should have called the property change listener', 1, count);
221 209
222 obj.test = true; 210 obj.test = true;
223 assertEquals(1, count); 211 assertEquals(1, count);
224 } 212 }
225 213
214 function testDefinePropertyBoolAttrEvent() {
215 var obj = document.createElement('div');
216 var hit = false;
217 function onTestSet(value, oldValue) {
218 assertEquals(obj, this);
219 assertTrue(this.test);
220 assertFalse(oldValue);
221 assertTrue(value);
222 hit = true;
223 }
224 cr.defineProperty(obj, 'test', cr.PropertyKind.BOOL_ATTR, onTestSet);
225 obj.test = true;
226 assertTrue(hit);
227 }
228
226 function testAddSingletonGetter() { 229 function testAddSingletonGetter() {
227 function Foo() {}; 230 function Foo() {};
228 cr.addSingletonGetter(Foo); 231 cr.addSingletonGetter(Foo);
229 232
230 assertNotNull('Should add get instance function', Foo.getInstance); 233 assertNotNull('Should add get instance function', Foo.getInstance);
231 234
232 var x = Foo.getInstance(); 235 var x = Foo.getInstance();
233 assertNotNull('Should successfully create an object', x); 236 assertNotNull('Should successfully create an object', x);
234 237
235 var y = Foo.getInstance(); 238 var y = Foo.getInstance();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 assertEquals(0, foo.v); 272 assertEquals(0, foo.v);
270 273
271 v = 1; 274 v = 1;
272 assertEquals(1, foo.v); 275 assertEquals(1, foo.v);
273 } 276 }
274 277
275 </script> 278 </script>
276 279
277 </body> 280 </body>
278 </html> 281 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698