1 | // TODO: FIXME: Refactor this to use D.O.H. instead of its own assertions |
---|
2 | |
---|
3 | dojo.require("dojox.flash"); |
---|
4 | |
---|
5 | var flashLoaded = false; |
---|
6 | var pageLoaded = false; |
---|
7 | var testXML = testBook = null; |
---|
8 | |
---|
9 | function flashReady(){ |
---|
10 | console.debug("flashReady"); |
---|
11 | if (flashLoaded) { |
---|
12 | return; // prevent double loads |
---|
13 | } |
---|
14 | |
---|
15 | flashLoaded = true; |
---|
16 | |
---|
17 | if(isReady()){ |
---|
18 | run(); |
---|
19 | } |
---|
20 | } |
---|
21 | |
---|
22 | function pageReady(){ |
---|
23 | console.debug("pageReady"); |
---|
24 | if (pageLoaded) { |
---|
25 | return; // prevent double loads |
---|
26 | } |
---|
27 | |
---|
28 | pageLoaded = true; |
---|
29 | |
---|
30 | loadResources(); |
---|
31 | |
---|
32 | if(isReady()){ |
---|
33 | run(); |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | function isReady(){ |
---|
38 | return testXML && testBook && pageLoaded && flashLoaded; |
---|
39 | } |
---|
40 | |
---|
41 | function loadResources(){ |
---|
42 | console.debug("Trying to load resources"); |
---|
43 | |
---|
44 | var d = dojo.xhrGet({ |
---|
45 | url: "../../storage/tests/resources/testXML.xml", |
---|
46 | handleAs: "text" |
---|
47 | }); |
---|
48 | |
---|
49 | d.addCallback(function(results){ |
---|
50 | console.debug("testXML loaded"); |
---|
51 | testXML = results; |
---|
52 | if(isReady()){ |
---|
53 | run(); |
---|
54 | } |
---|
55 | }); |
---|
56 | |
---|
57 | d.addErrback(function(error){ |
---|
58 | console.debug("Unable to load testXML.xml: " + error); |
---|
59 | }); |
---|
60 | |
---|
61 | d = dojo.xhrGet({ |
---|
62 | url: "../../storage/tests/resources/testBook.txt", |
---|
63 | handleAs: "text" |
---|
64 | }); |
---|
65 | |
---|
66 | d.addCallback(function(results){ |
---|
67 | console.debug("testBook loaded"); |
---|
68 | testBook = results; |
---|
69 | if(isReady()){ |
---|
70 | run(); |
---|
71 | } |
---|
72 | }); |
---|
73 | |
---|
74 | d.addErrback(function(error){ |
---|
75 | console.debug("Unable to load testXML.xml: " + error); |
---|
76 | }); |
---|
77 | } |
---|
78 | |
---|
79 | function run(){ |
---|
80 | console.debug("run"); |
---|
81 | try{ |
---|
82 | var correct, actual; |
---|
83 | |
---|
84 | console.debug("Setting simple message..."); |
---|
85 | correct = "hello world"; |
---|
86 | dojox.flash.comm.setMessage(correct); |
---|
87 | actual = dojox.flash.comm.getMessage(); |
---|
88 | assert(correct, actual, "Setting/getting simple message did not work"); |
---|
89 | chop(correct); |
---|
90 | |
---|
91 | console.debug("Setting message with evil characters..."); |
---|
92 | // our correct and actual values get tricky when we have double back |
---|
93 | // slashes; do a trick so that they can be compared easier |
---|
94 | var doubleSlash = "\\"; |
---|
95 | doubleSlash = doubleSlash.charAt(0); |
---|
96 | correct = "hello world\n\n\nasdfasdf!@#$@#%^[]{}&<xml>" + doubleSlash |
---|
97 | + "<div>$%^&%^&*^&()<><><>,./;\0\r\f\'][`~=\"+-]\\0MORE!\n\rLESS"; |
---|
98 | var putSize = dojox.flash.comm.setMessage(correct); |
---|
99 | assert(putSize, correct.length, "Failed putting. Correct length = " |
---|
100 | + correct.length + ", Flash length = " + putSize); |
---|
101 | dojox.flash.comm.setMessage(correct); |
---|
102 | actual = dojox.flash.comm.getMessage(); |
---|
103 | assert(correct, actual, "Setting/getting message with evil characters did not work"); |
---|
104 | chop(correct); |
---|
105 | |
---|
106 | console.debug("Setting testXML..."); |
---|
107 | correct = testXML; |
---|
108 | dojox.flash.comm.setMessage(correct); |
---|
109 | actual = dojox.flash.comm.getMessage(); |
---|
110 | assert(correct, actual, "Setting/getting testXML did not work"); |
---|
111 | chop(correct); |
---|
112 | |
---|
113 | console.debug("Setting testBook(~300K)..."); |
---|
114 | correct = testBook; |
---|
115 | dojox.flash.comm.setMessage(correct); |
---|
116 | actual = dojox.flash.comm.getMessage(); |
---|
117 | assert(correct, actual, "Setting/getting testBook did not work"); |
---|
118 | |
---|
119 | console.debug("Setting testBook 3 times (~900K)..."); |
---|
120 | correct = testBook + testBook + testBook; |
---|
121 | dojox.flash.comm.setMessage(correct); |
---|
122 | actual = dojox.flash.comm.getMessage(); |
---|
123 | assert(correct, actual, "Setting/getting testBook X 3 did not work"); |
---|
124 | |
---|
125 | console.debug("Setting JSON..."); |
---|
126 | var obj = {type: "car", color: "red", model: "Ford", year: "2008", |
---|
127 | features: ["A/C", "automatic", "4-wheel drive"]}; |
---|
128 | correct = dojo.toJson(obj, true); |
---|
129 | dojox.flash.comm.setMessage(correct); |
---|
130 | actual = dojox.flash.comm.getMessage(); |
---|
131 | assert(correct, actual, "Setting/getting JSON did not work"); |
---|
132 | chop(correct); |
---|
133 | |
---|
134 | console.debug("Calling method that takes multiple values..."); |
---|
135 | actual = dojox.flash.comm.multipleValues("key", "value", "namespace"); |
---|
136 | assert("namespacekeyvalue", actual, "Setting/getting multiple values did not work"); |
---|
137 | |
---|
138 | var allPassed = document.createElement("p"); |
---|
139 | allPassed.style.backgroundColor = "green"; |
---|
140 | allPassed.style.color = "white"; |
---|
141 | allPassed.style.fontSize = "24pt"; |
---|
142 | allPassed.appendChild(document.createTextNode("All tests passed")); |
---|
143 | var body = document.getElementsByTagName("body")[0]; |
---|
144 | body.appendChild(allPassed); |
---|
145 | }catch(e){ |
---|
146 | console.debug(e.message || e); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | function chop(testString){ |
---|
151 | console.debug("chopping '" + testString + "'"); |
---|
152 | for(var i = 0; i < Math.min(testString.length, 100); i++){ |
---|
153 | //console.debug("index = " + i); |
---|
154 | testSlice(testString, 0, i+1); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | function testSlice( testString, from, to){ |
---|
159 | var putSize = dojox.flash.comm.setMessageSlice( testString, from, to); |
---|
160 | var actual = dojox.flash.comm.getMessage(); |
---|
161 | var correct = testString.slice(from, to); |
---|
162 | |
---|
163 | var msg = "Put a string with " + testString.length |
---|
164 | + " chars, but it got with " + putSize + " chars in the Flash layer"; |
---|
165 | assert(putSize, testString.length, msg); |
---|
166 | |
---|
167 | msg = "I got '" + actual + "' instead of '" + correct +"'"; |
---|
168 | assert(correct, actual, msg); |
---|
169 | } |
---|
170 | |
---|
171 | function assert(correct, actual, msg){ |
---|
172 | //alert("correct="+correct+",\n\nactual="+actual); |
---|
173 | if(correct != actual){ |
---|
174 | var failed = document.createElement("p"); |
---|
175 | failed.style.backgroundColor = "red"; |
---|
176 | failed.style.color = "white"; |
---|
177 | failed.style.fontSize = "24pt"; |
---|
178 | failed.appendChild(document.createTextNode("Test failed: " + msg)); |
---|
179 | var body = document.getElementsByTagName("body")[0]; |
---|
180 | body.appendChild(failed); |
---|
181 | |
---|
182 | throw new Error("ASSERTION FAILED: " + msg); |
---|
183 | }else{ |
---|
184 | //console.debug("Assertion passed"); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | console.debug("adding listeners..."); |
---|
189 | dojox.flash.addLoadedListener(flashReady); |
---|
190 | dojox.flash.setSwf("TestFlash.swf", false); |
---|
191 | dojo.connect(dojo, "loaded", pageReady); |
---|