aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/flash/tests/test_flash.js
blob: 1b26eb2615eebb5a2193a4cc82f17ca7c4f6ac38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// TODO: FIXME: Refactor this to use D.O.H. instead of its own assertions

dojo.require("dojox.flash");

var flashLoaded = false;
var pageLoaded = false;
var testXML = testBook = null;

function flashReady(){
	console.debug("flashReady");
	flashLoaded = true;
	
	if(isReady()){
		run();
	}
}

function pageReady(){
	console.debug("pageReady");
	pageLoaded = true;
	
	loadResources();
	
	if(isReady()){
		run();
	}
}

function isReady(){
	return testXML && testBook && pageLoaded && flashLoaded;
}

function loadResources(){
	console.debug("Trying to load resources");
	
	var d = dojo.xhrGet({
		url: "../../storage/tests/resources/testXML.xml",
		handleAs: "text"
	});

	d.addCallback(function(results){
		console.debug("testXML loaded");
		testXML = results;
		if(isReady()){
			run();
		}
	});

	d.addErrback(function(error){ 
		console.debug("Unable to load testXML.xml: " + error);
	});
	
	d = dojo.xhrGet({
		url: "../../storage/tests/resources/testBook.txt",
		handleAs: "text"
	});

	d.addCallback(function(results){
		console.debug("testBook loaded");
		testBook = results;
		if(isReady()){
			run();
		}
	});

	d.addErrback(function(error){ 
		console.debug("Unable to load testXML.xml: " + error);
	});
}

function run(){
	console.debug("run");
	try{
		var correct, actual;
		
		console.debug("Setting simple message...");
		correct = "hello world";
		dojox.flash.comm.setMessage(correct);
		actual = dojox.flash.comm.getMessage();
		assert(correct, actual, "Setting/getting simple message did not work");
	
		console.debug("Setting message with evil characters...");
		// our correct and actual values get tricky when we have double back
		// slashes; do a trick so that they can be compared easier
		var doubleSlash = "\\";
		doubleSlash = doubleSlash.charAt(0);
		correct = "hello world\n\n\nasdfasdf!@#$@#%^[]{}&amp;<xml>" + doubleSlash 
					+ "<div>$%^&%^&*^&()<><><>,./;\0\r\f\'][`~=\"+-]MORE!\n\rLESS";
		dojox.flash.comm.setMessage(correct);
		actual = dojox.flash.comm.getMessage();
		assert(correct, actual, "Setting/getting message with evil characters did not work");
	
		console.debug("Setting testXML...");
		correct = testXML;
		dojox.flash.comm.setMessage(correct);
		actual = dojox.flash.comm.getMessage();
		assert(correct, actual, "Setting/getting testXML did not work");
		
		console.debug("Setting testBook(~300K)...");
		correct = testBook;
		dojox.flash.comm.setMessage(correct);
		actual = dojox.flash.comm.getMessage();
		assert(correct, actual, "Setting/getting testBook did not work");
	
		console.debug("Setting testBook 3 times (~900K)...");
		correct = testBook + testBook + testBook;
		dojox.flash.comm.setMessage(correct);
		actual = dojox.flash.comm.getMessage();
		assert(correct, actual, "Setting/getting testBook X 3 did not work");
		
		console.debug("Setting JSON...");
		var obj = {type: "car", color: "red", model: "Ford", year: "2008",
					features: ["A/C", "automatic", "4-wheel drive"]};
		correct = dojo.toJson(obj, true);
		dojox.flash.comm.setMessage(correct);
		actual = dojox.flash.comm.getMessage();
		assert(correct, actual, "Setting/getting JSON did not work");
		
		console.debug("Calling method that takes multiple values...");
		actual = dojox.flash.comm.multipleValues("key", "value", "namespace");
		assert("namespacekeyvalue", actual, "Setting/getting multiple values did not work");
		
		var allPassed = document.createElement("p");
		allPassed.style.backgroundColor = "green";
		allPassed.style.color = "white";
		allPassed.style.fontSize = "24pt";
		allPassed.appendChild(document.createTextNode("All tests passed"));
		var body = document.getElementsByTagName("body")[0];
		body.appendChild(allPassed);
	}catch(e){
		console.debug(e.message || e);
	}
}

function assert(correct, actual, msg){
	//alert("correct="+correct+",\n\nactual="+actual);
	if(correct != actual){
	  var failed = document.createElement("p");
		failed.style.backgroundColor = "red";
		failed.style.color = "white";
		failed.style.fontSize = "24pt";
		failed.appendChild(document.createTextNode("Test failed: " + msg));
		var body = document.getElementsByTagName("body")[0];
		body.appendChild(failed);
		
		throw new Error("ASSERTION FAILED: " + msg);
	}else{
		console.debug("Assertion passed");
	}
}

console.debug("adding listeners...");
dojox.flash.addLoadedListener(flashReady);
dojox.flash.setSwf("TestFlash.swf", true);
dojo.connect(dojo, "loaded", pageReady);