summaryrefslogtreecommitdiffstats
path: root/chrome_frame/test/data/chrome_frame_tester_helpers.js
blob: d0394efcc90f47f10eb99f25344ec50a37731d6f (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// This script provides some mechanics for testing ChromeFrame
//
function onSuccess(name, id) {
  appendStatus("Success reported!");
  onFinished(name, id, "OK");
}

function onFailure(name, id, status) {
  appendStatus("Failure reported: " + status);
  onFinished(name, id, status);
}

function byId(id) {
  return document.getElementById(id);
}

function getXHRObject(){
  var XMLHTTP_PROGIDS = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP',
                         'Msxml2.XMLHTTP.4.0'];
  var http = null;
  try {
    http = new XMLHttpRequest();
  } catch(e) {
  }

  if (http)
    return http;

  for (var i = 0; i < 3; ++i) {
    var progid = XMLHTTP_PROGIDS[i];
    try {
      http = new ActiveXObject(progid);
    } catch(e) {
  }

  if (http)
    break;
  }
  return http;
}

var reportURL = "/writefile/";

function shutdownServer() {
  var xhr = getXHRObject();
  if(!xhr)
    return;

  xhr.open("POST", "/kill", false);
  try {
    xhr.send(null);
  } catch(e) {
    appendStatus("XHR send failed. Error: " + e.description);
  }
}

// Optionally send the server a notification that onload was fired.
// To be called from within window.onload.
function sendOnLoadEvent() {
  writeToServer("OnLoadEvent", "loaded");
}

function writeToServer(name, result) {
  var xhr = getXHRObject();
  if(!xhr)
    return;

  // synchronously POST the results
  xhr.open("POST", reportURL + name, false);
  xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  try {
    xhr.send(result);
  } catch(e) {
    appendStatus("XHR send failed. Error: " + e.description);
  }
}

function postResult(name, result) {
  writeToServer(name, result);
  // NOTE:
  //  not watching for failure or return status issues. What should we do here?
  shutdownServer();
}

// Finish running a test by setting the status
// and the cookie.
function onFinished(name, id, result) {
  // set a cookie to report the results...
  var cookie = name + "." + id + ".status=" + result + "; path=/";
  document.cookie = cookie;

  // ...and POST the status back to the server
  postResult(name, result);
}

function appendStatus(message) {
  var statusPanel = byId("statusPanel");
  if (statusPanel) {
    statusPanel.innerHTML += '<BR>' + message;
  }
}

function readCookie(name) {
  var cookie_name = name + "=";
  var ca = document.cookie.split(';');

  for(var i = 0 ; i < ca.length ; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1,c.length);
    }
    if (c.indexOf(cookie_name) == 0) {
      return c.substring(cookie_name.length, c.length);
    }
  }
  return null;
}

function createCookie(name,value,days) {
  var expires = "";
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  }
  document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}

function isRunningInMSIE() {
  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
      return true;

  return false;
}

function reloadUsingCFProtocol() {
  var redirect_location = "gcf:";
  redirect_location += window.location;
  window.location = redirect_location;
}

function isRunningInChrome() {
  var is_chrome_frame = /chromeframe/.test(navigator.userAgent.toLowerCase());
  if (is_chrome_frame)
    return 0;
  var is_chrome = /chrome/.test(navigator.userAgent.toLowerCase());
  return is_chrome;
}

function TestIfRunningInChrome() {
  var is_chrome = isRunningInChrome();
  if (!is_chrome) {
    onFailure("ChromeFrameWindowOpen", "Window Open failed :-(",
              "User agent = " + navigator.userAgent.toLowerCase());
  }
  return is_chrome;
}

// Returns the base document url.
function GetBaseUrlPath() {
 var url = window.location.protocol + "//" + window.location.host + "/";
 return url;
}

// Appends arguments passed in to the base document url and returns the same.
function AppendArgumentsToBaseUrl() {
  var url = GetBaseUrlPath();
  for (arg_index = 0; arg_index < arguments.length; arg_index++) {
    url += arguments[arg_index];
  }
  return url;
}