Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Tools / GardeningServer / scripts / base_unittests.js
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 (function () {
27
28 module("base");
29
30 test("joinPath", 1, function() {
31     var value = base.joinPath("path/to", "test.html");
32     equals(value, "path/to/test.html");
33 });
34
35 test("endsWith", 9, function() {
36     ok(base.endsWith("xyz", ""));
37     ok(base.endsWith("xyz", "z"));
38     ok(base.endsWith("xyz", "yz"));
39     ok(base.endsWith("xyz", "xyz"));
40     ok(!base.endsWith("xyz", "wxyz"));
41     ok(!base.endsWith("xyz", "gwxyz"));
42     ok(base.endsWith("", ""));
43     ok(!base.endsWith("", "z"));
44     ok(!base.endsWith("xyxy", "yx"));
45 });
46
47 test("trimExtension", 6, function() {
48     equals(base.trimExtension("xyz"), "xyz");
49     equals(base.trimExtension("xy.z"), "xy");
50     equals(base.trimExtension("x.yz"), "x");
51     equals(base.trimExtension("x.y.z"), "x.y");
52     equals(base.trimExtension(".xyz"), "");
53     equals(base.trimExtension(""), "");
54 });
55
56 test("joinPath with empty parent", 1, function() {
57     var value = base.joinPath("", "test.html");
58     equals(value, "test.html");
59 });
60
61 test("dirName", 3, function() {
62     equals(base.dirName("foo.html"), "foo.html");
63     equals(base.dirName("foo/bar.html"), "foo");
64     equals(base.dirName("foo/bar/baz.html"), "foo/bar");
65 });
66
67 test("uniquifyArray", 5, function() {
68     deepEqual(base.uniquifyArray([]), []);
69     deepEqual(base.uniquifyArray(["a"]), ["a"]);
70     deepEqual(base.uniquifyArray(["a", "b"]), ["a", "b"]);
71     deepEqual(base.uniquifyArray(["a", "b", "b"]), ["a", "b"]);
72     deepEqual(base.uniquifyArray(["a", "b", "b", "a"]), ["a", "b"]);
73 });
74
75 test("flattenArray", 5, function() {
76     deepEqual(base.flattenArray([]), []);
77     deepEqual(base.flattenArray([["a"]]), ["a"]);
78     deepEqual(base.flattenArray([["a"], ["b"]]), ["a", "b"]);
79     deepEqual(base.flattenArray([["a"], ["b", "c"]]), ["a", "b", "c"]);
80     deepEqual(base.flattenArray([["a"], [], ["b"]]), ["a", "b"]);
81 });
82
83 test("filterDictionary", 3, function() {
84     var dictionary = {
85         'foo': 43,
86         'bar': 11
87     };
88     deepEqual(base.filterDictionary(dictionary, function() { return true; }), {
89         "foo": 43,
90         "bar": 11
91     });
92     deepEqual(base.filterDictionary(dictionary, function() { return false; }), { });
93     deepEqual(base.filterDictionary(dictionary, function(key) { return key == 'foo'; }), {
94         "foo": 43
95     });
96 });
97
98 test("mapDictionary", 3, function() {
99     deepEqual(base.mapDictionary({}, function(value) { return value - 10; }), {});
100     var dictionary = {
101         'foo': 43,
102         'bar': 11
103     };
104     deepEqual(base.mapDictionary(dictionary, function(value) { return value - 10; }), {
105         "foo": 33,
106         "bar": 1
107     });
108     deepEqual(base.mapDictionary(dictionary, function(value) {
109         if (value > 20)
110             return value - 20;
111     }), {
112         "foo": 23,
113     });
114 });
115
116 test("filterTree", 2, function() {
117     var tree = {
118         'path': {
119             'to': {
120                 'test.html': {
121                     'actual': 'PASS',
122                     'expected': 'FAIL'
123                 }
124             },
125             'another.html': {
126                 'actual': 'TEXT',
127                 'expected': 'PASS'
128             }
129         }
130     }
131
132     function isLeaf(node)
133     {
134         return !!node.actual;
135     }
136
137     function actualIsText(node)
138     {
139         return node.actual == 'TEXT';
140     }
141
142     var all = base.filterTree(tree, isLeaf, function() { return true });
143     deepEqual(all, {
144         'path/to/test.html': {
145             'actual': 'PASS',
146             'expected': 'FAIL'
147         },
148         'path/another.html': {
149             'actual': 'TEXT',
150             'expected': 'PASS'
151         }
152     });
153
154     var text = base.filterTree(tree, isLeaf, actualIsText);
155     deepEqual(text, {
156         'path/another.html': {
157             'actual': 'TEXT',
158             'expected': 'PASS'
159         }
160     });
161 });
162
163 test("UpdateTracker", 20, function() {
164     var dict;
165
166     function dumpKeys()
167     {
168         var updates = []
169         dict.forEach(function(item, key, updated) {
170             updates.push(key);
171         });
172         return updates;
173     }
174
175     function dumpUpdatedKeys()
176     {
177         var updates = []
178         dict.forEach(function(item, key, updated) {
179             updated && updates.push(key);
180         });
181         return updates;
182     }
183
184
185     dict = new base.UpdateTracker();
186     dict.update("5", {});
187     deepEqual(dumpUpdatedKeys(), ["5"]);
188     dict.update("6", {});
189     dict.update("7", {});
190     deepEqual(dumpUpdatedKeys(), ["5", "6", "7"]);
191     deepEqual(dict.get("6"), {});
192     ok(dict.exists("7"));
193     dict.purge();
194     deepEqual(dumpUpdatedKeys(), []);
195     deepEqual(dumpKeys(), ["5", "6", "7"]);
196     dict.update("5", {});
197     deepEqual(dumpUpdatedKeys(), ["5"]);
198     dict.update("4", {});
199     deepEqual(dumpUpdatedKeys(), ["4", "5"]);
200     deepEqual(dumpKeys(), ["4", "5", "6", "7"]);
201     dict.purge();
202     deepEqual(dumpKeys(), ["4", "5"]);
203     deepEqual(dumpUpdatedKeys(), []);
204     dict.purge();
205     deepEqual(dumpKeys(), []);
206
207     var removeCount = 0;
208     dict.update("one");
209     deepEqual(dumpUpdatedKeys(), ["one"]);
210     dict.update("two");
211     deepEqual(dumpUpdatedKeys(), ["one", "two"]);
212     dict.update("three");
213     dict.purge();
214     deepEqual(dumpKeys(), ["one", "three", "two"]);
215     dict.update("two");
216     dict.purge(function() {
217         removeCount++;
218     });
219     deepEqual(dumpKeys(), ["two"]);
220     equal(removeCount, 2);
221     dict.update("four");
222     var removeCounter = { count: 0 };
223     dict.purge(function() {
224         this.count++;
225     }, removeCounter);
226     equal(removeCounter.count, 1);
227     dict.purge(function() {
228         equal(String(this), "four");
229     });
230
231     dict = new base.UpdateTracker();
232     dict.update("one");
233     var thisObject = {}
234     dict.forEach(function(item) {
235         equal(this, thisObject);
236     }, thisObject);
237
238 });
239
240 test("extends", 14, function() {
241
242     var LikeDiv = base.extends("div", {
243         init: function() {
244             this.textContent = "awesome";
245         },
246         method: function(msg) {
247             return 42;
248         }
249     });
250
251     var LikeLikeDiv = base.extends(LikeDiv, {
252         init: function() {
253             this.className = "like";
254         }
255     });
256
257     var LikeP = base.extends("p", {
258         init: function(content) {
259             this.textContent = content
260         }
261     });
262
263     var LikeProgress = base.extends("progress", {
264         init: function() {
265             this.max = 100;
266             this.value = 10;
267         }
268     });
269
270     var LikeLikeProgress = base.extends(LikeProgress, {
271         completed: function() {
272             this.value = 100;
273         }
274     });
275
276     document.body.appendChild(new LikeDiv());
277     equals(document.body.lastChild.tagName, "DIV");
278     equals(document.body.lastChild.innerHTML, "awesome");
279     equals(document.body.lastChild.method(), 42);
280     document.body.removeChild(document.body.lastChild);
281
282     document.body.appendChild(new LikeLikeDiv());
283     equals(document.body.lastChild.tagName, "DIV");
284     equals(document.body.lastChild.innerHTML, "awesome");
285     equals(document.body.lastChild.method(), 42);
286     equals(document.body.lastChild.className, "like");
287     document.body.removeChild(document.body.lastChild);
288
289     document.body.appendChild(new LikeP("super"));
290     equals(document.body.lastChild.tagName, "P");
291     equals(document.body.lastChild.innerHTML, "super");
292     raises(function() {
293         document.body.lastChild.method();
294     });
295     document.body.removeChild(document.body.lastChild);
296
297     document.body.appendChild(new LikeProgress());
298     equals(document.body.lastChild.tagName, "PROGRESS");
299     // Safari 5.1 lacks the <progress> element.
300     // equals(document.body.lastChild.position, 0.1);
301     equals(document.body.lastChild.innerHTML, "");
302     raises(function() {
303         document.body.lastChild.method();
304     });
305     document.body.removeChild(document.body.lastChild);
306
307     document.body.appendChild(new LikeLikeProgress());
308     equals(document.body.lastChild.tagName, "PROGRESS");
309     // Safari 5.1 lacks the <progress> element.
310     // equals(document.body.lastChild.position, 0.1);
311     document.body.lastChild.completed();
312     // Safari 5.1 lacks the <progress> element.
313     // equals(document.body.lastChild.position, 1);
314     document.body.removeChild(document.body.lastChild);
315 });
316
317 test("relativizeTime", 14, function() {
318     var time = new Date();
319     equals(base.relativizeTime(time), "Just now");
320     time.setMinutes(time.getMinutes() - 1);
321     equals(base.relativizeTime(time), "1 minute ago");
322     time.setMinutes(time.getMinutes() - 1);
323     equals(base.relativizeTime(time), "2 minutes ago");
324     time.setMinutes(time.getMinutes() - 1);
325     equals(base.relativizeTime(time), "3 minutes ago");
326     time.setMinutes(time.getMinutes() - 56);
327     equals(base.relativizeTime(time), "59 minutes ago");
328     time.setMinutes(time.getMinutes() - 1);
329     equals(base.relativizeTime(time), "1 hour ago");
330     time.setMinutes(time.getMinutes() - 29);
331     equals(base.relativizeTime(time), "1 hour ago");
332     time.setMinutes(time.getMinutes() - 2);
333     equals(base.relativizeTime(time), "2 hours ago");
334     time.setMinutes(time.getMinutes() - 29);
335     equals(base.relativizeTime(time), "2 hours ago");
336     time.setHours(time.getHours() - 1);
337     equals(base.relativizeTime(time), "3 hours ago");
338     time.setHours(time.getHours() - 20);
339     equals(base.relativizeTime(time), "23 hours ago");
340     time.setHours(time.getHours() - 1);
341     equals(base.relativizeTime(time), "1 day ago");
342     time.setDate(time.getDate() - 1);
343     equals(base.relativizeTime(time), "2 days ago");
344     time.setDate(time.getDate() - 998);
345     equals(base.relativizeTime(time), "1000 days ago");
346 });
347
348 test("getURLParameter", 1, function() {
349     ok(!base.getURLParameter('non-existant'));
350 });
351
352 test("parseJSONP", 6, function() {
353     deepEqual(base.parseJSONP(""), {});
354     deepEqual(base.parseJSONP('p({"key": "value"})'), {"key": "value"});
355     deepEqual(base.parseJSONP('ADD_RESULTS({"dummy":"data"});'), {"dummy":"data"});
356     deepEqual(base.parseJSONP('{"dummy":"data"}'), {"dummy":"data"});
357     deepEqual(base.parseJSONP('ADD_RESULTS({"builder(1)":"data"});'), {"builder(1)":"data"});
358     deepEqual(base.parseJSONP('{"builder(1)":"data"}'), {"builder(1)":"data"});
359 });
360
361 })();