- add sources.
[platform/framework/web/crosswalk.git] / src / chrome_frame / test / data / chrome_frame_tester_helpers.js
1 //
2 // This script provides some mechanics for testing ChromeFrame
3 //
4 function onSuccess(name, id) {
5   appendStatus("Success reported!");
6   onFinished(name, id, "OK");
7 }
8
9 function onFailure(name, id, status) {
10   appendStatus("Failure reported: " + status);
11   onFinished(name, id, status);
12 }
13
14 function byId(id) {
15   return document.getElementById(id);
16 }
17
18 function getXHRObject(){
19   var XMLHTTP_PROGIDS = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP',
20                          'Msxml2.XMLHTTP.4.0'];
21   var http = null;
22   try {
23     http = new XMLHttpRequest();
24   } catch(e) {
25   }
26
27   if (http)
28     return http;
29
30   for (var i = 0; i < 3; ++i) {
31     var progid = XMLHTTP_PROGIDS[i];
32     try {
33       http = new ActiveXObject(progid);
34     } catch(e) {
35   }
36
37   if (http)
38     break;
39   }
40   return http;
41 }
42
43 var reportURL = "/writefile/";
44
45 // Optionally send the server a notification that onload was fired.
46 // To be called from within window.onload.
47 function sendOnLoadEvent() {
48   writeToServer("OnLoadEvent", "loaded");
49 }
50
51 function writeToServer(name, result) {
52   var xhr = getXHRObject();
53   if(!xhr)
54     return;
55
56   // asynchronously POST the results
57   xhr.open("POST", reportURL + name, true);
58   xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
59   try {
60     xhr.send(result);
61   } catch(e) {
62     appendStatus("XHR send failed. Error: " + e.description);
63   }
64 }
65
66 function postResult(name, result) {
67   writeToServer(name, result);
68 }
69
70 // Finish running a test by setting the status
71 // and the cookie.
72 function onFinished(name, id, result) {
73   // set a cookie to report the results...
74   var cookie = name + "." + id + ".status=" + result + "; path=/";
75   document.cookie = cookie;
76
77   // ...and POST the status back to the server
78   postResult(name, result);
79 }
80
81 function appendStatus(message) {
82   var statusPanel = byId("statusPanel");
83   if (statusPanel) {
84     statusPanel.innerHTML += '<BR>' + message;
85   }
86 }
87
88 function readCookie(name) {
89   var cookie_name = name + "=";
90   var ca = document.cookie.split(';');
91
92   for(var i = 0 ; i < ca.length ; i++) {
93     var c = ca[i];
94     while (c.charAt(0) == ' ') {
95       c = c.substring(1,c.length);
96     }
97     if (c.indexOf(cookie_name) == 0) {
98       return c.substring(cookie_name.length, c.length);
99     }
100   }
101   return null;
102 }
103
104 function createCookie(name,value,days) {
105   var expires = "";
106   if (days) {
107     var date = new Date();
108     date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
109     expires = "; expires=" + date.toGMTString();
110   }
111   document.cookie = name+"="+value+expires+"; path=/";
112 }
113
114 function eraseCookie(name) {
115   createCookie(name, "", -1);
116 }
117
118 function isRunningInMSIE() {
119   if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
120       return true;
121
122   return false;
123 }
124
125 function reloadUsingCFProtocol() {
126   var redirect_location = "gcf:";
127   redirect_location += window.location;
128   window.location = redirect_location;
129 }
130
131 function isRunningInChrome() {
132   var is_chrome_frame = /chromeframe/.test(navigator.userAgent.toLowerCase());
133   if (is_chrome_frame)
134     return 0;
135   var is_chrome = /chrome/.test(navigator.userAgent.toLowerCase());
136   return is_chrome;
137 }
138
139 function TestIfRunningInChrome() {
140   var is_chrome = isRunningInChrome();
141   if (!is_chrome) {
142     onFailure("ChromeFrameWindowOpen", "Window Open failed :-(",
143               "User agent = " + navigator.userAgent.toLowerCase());
144   }
145   return is_chrome;
146 }
147
148 // Returns the base document url.
149 function GetBaseUrlPath() {
150  var url = window.location.protocol + "//" + window.location.host + "/";
151  return url;
152 }
153
154 // Appends arguments passed in to the base document url and returns the same.
155 function AppendArgumentsToBaseUrl() {
156   var url = GetBaseUrlPath();
157   for (arg_index = 0; arg_index < arguments.length; arg_index++) {
158     url += arguments[arg_index];
159   }
160   return url;
161 }
162
163 // Get the value of the first parameter from the query string which matches the
164 // given name.
165 function getURLParameter(name) {
166   name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
167   var regexString = "[\\?&]" + name + "=([^&#]*)";
168   var regex = new RegExp(regexString);
169   var results = regex.exec(window.location.href);
170   if (results == null) {
171     return "";
172   } else {
173     return results[1];
174   }
175 }
176
177 // Create new URL by given html page name and querystring, based on current URL
178 // path.
179 function buildURL(pageName, queryString) {
180   var path = window.location.pathname;
181   var url = "";
182
183   url += window.location.protocol + "//" + window.location.host;
184   if (path.lastIndexOf("/") > 0) {
185     url += path.substring(0, path.lastIndexOf("/")) + "/" + pageName;
186   } else {
187     url += "/" + pageName;
188   }
189   url += ((queryString == "") ? "" : "?" + queryString);
190   return url;
191 }
192
193 // Helper function for insertControl.
194 function generateControlHtml(configuration) {
195   var objectAttributes = new Object();
196   var params = new Array();
197   var embedAttributes = new Object();
198   var param;
199   var html;
200
201   function stringifyAttributes(attributeCollection) {
202     var result = new String();
203     for (var attributeName in attributeCollection) {
204       result += ' ' + attributeName + '="' +
205            attributeCollection[attributeName] + '"';
206     }
207     return result;
208   }
209
210   function applyAttribute(attributeCollection, name, value, defaultValue) {
211     if (value === undefined)
212       value = defaultValue;
213     if (value !== null)
214       attributeCollection[name] = value;
215   }
216
217   objectAttributes.classid="CLSID:E0A900DF-9611-4446-86BD-4B1D47E7DB2A";
218   objectAttributes.codebase="http://www.google.com";
219   applyAttribute(objectAttributes, "id", configuration.id, "ChromeFrame");
220   applyAttribute(objectAttributes, "width", configuration.width, "500");
221   applyAttribute(objectAttributes, "height", configuration.height, "500");
222
223   // Attributes provided by the caller override any defaults.
224   for (var attribute in configuration.objectAttributes) {
225     if (configuration.objectAttributes[attribute] === null)
226       delete objectAttributes[attribute];
227     else
228       objectAttributes[attribute] = configuration.objectAttributes[attribute];
229   }
230
231   embedAttributes.type = "application/chromeframe";
232
233   // By default, embed id = object.id + "Plugin".  null id means omit id.
234   if (embedAttributes.id === null)
235     delete embedAttributes.id;
236   else if (embedAttributes.id === undefined && objectAttributes.id !== null)
237     embedAttributes.id = objectAttributes.id + "Plugin";
238
239   // By default, embed name = object.id.  null name means omit name.
240   if (embedAttributes.name === null)
241     delete embedAttributes.name;
242   else if (embedAttributes.name === undefined && objectAttributes.id !== null)
243     embedAttributes.name = objectAttributes.id;
244
245   applyAttribute(embedAttributes, "width", configuration.width, "500");
246   applyAttribute(embedAttributes, "height", configuration.height, "500");
247   applyAttribute(embedAttributes, "src", configuration.src, null);
248
249   for (var attribute in configuration.embedAttributes) {
250     if (configuration.embedAttributes[attribute] === null)
251       delete embedAttributes[attribute];
252     else
253       embedAttributes[attribute] = configuration.embedAttributes[attribute];
254   }
255
256   if (embedAttributes.src !== undefined) {
257     param = new Object();
258     param.name = "src";
259     param.value = embedAttributes.src;
260     params.push(param);
261   }
262
263   // All event handlers are params and attributes of the embed object.
264   for (var eventName in configuration.eventHandlers) {
265     param = new Object();
266     param.name = eventName;
267     param.value = configuration.eventHandlers[eventName];
268     params.push(param);
269     embedAttributes[eventName] = configuration.eventHandlers[eventName];
270   }
271
272   html = "<object" + stringifyAttributes(objectAttributes) + ">\r\n";
273   for (var i = 0; i < params.length; ++i) {
274     html += "  <param" + stringifyAttributes(params[i]) + ">\r\n";
275   }
276   html += "  <embed" + stringifyAttributes(embedAttributes) + "></embed>\r\n"
277
278   html += "</object>";
279
280   return html;
281 }
282
283 // Write the text for the Chrome Frame ActiveX control into an element.
284 // This works around a "feature" of IE versions released between April, 2006
285 // and April, 2008 that required the user to click on an ActiveX control to
286 // "activate" it.  See http://msdn.microsoft.com/en-us/library/ms537508.aspx.
287 //
288 // |elementId| identifies the element in the current document into which the
289 // control markup will be inserted.  |configuration| is an Object used to
290 // configure the control as below.  Values shown are defaults, which may be
291 // overridden by supplying null values.
292 // {
293 //   "id": "ChromeFrame", // id of object tag, name of the embed tag, and
294 //                        // basis of id of the embed tag.
295 //   "width": "500", // width of both object and embed tags.
296 //   "height": "500", // height of both object and embed tags.
297 //   "src": "url", // src of embed tag and of param to object tag.
298 //   "eventHandlers": { // Applied to embed tag and params to object tag.
299 //     "onclose": "...",
300 //     "onload": "...",
301 //     "onloaderror": "..."
302 //   }
303 //   "objectAttributes": { // Custom attributes for the object tag. Any
304 //     "tabindex": "...",  // properties explicitly set to null will override
305 //     "border": "...",    // defaults.
306 //     "style": "..."
307 //   },
308 //   "embedAttributes": {        // Custom attributes for the embed tag;
309 //     "privileged_mode": "...", // similar to above.
310 //     "style": "..."
311 //   }
312 // }
313 function insertControl(elementId, configuration) {
314   var e = document.getElementById(elementId);
315   e.innerHTML = generateControlHtml(configuration);
316 }