190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)var QueryString = function() {
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Allows access to query parameters on the URL; e.g., given a URL like:
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //    http://<server>/my.html?test=123&bob=123
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Parameters can then be accessed via QueryString.test or QueryString.bob.
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  var params = {};
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // RegEx to split out values by &.
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  var r = /([^&=]+)=?([^&]*)/g;
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Lambda function for decoding extracted match values. Replaces '+' with
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // space so decodeURIComponent functions properly.
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  function d(s) { return decodeURIComponent(s.replace(/\+/g, ' ')); }
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  var match;
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  while (match = r.exec(window.location.search.substring(1)))
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    params[d(match[1])] = d(match[2]);
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return params;
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}();
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function failTest(msg) {
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  var failMessage = msg;
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (msg instanceof Event)
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    failMessage = msg.target + '.' + msg.type;
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  console.log("FAILED TEST: " + msg);
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  setResultInTitle('FAILED');
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)var titleChanged = false;
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function setResultInTitle(title) {
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // If document title is 'ENDED', then update it with new title to possibly
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // mark a test as failure.  Otherwise, keep the first title change in place.
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (!titleChanged || document.title.toUpperCase() == 'ENDED')
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    document.title = title.toUpperCase();
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  console.log('Set document title to: ' + title + ', updated title: ' +
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)              document.title);
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  titleChanged = true;
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function installTitleEventHandler(element, event) {
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  element.addEventListener(event, function(e) {
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    setResultInTitle(event.toString());
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }, false);
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function convertToArray(input) {
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (Array.isArray(input))
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return input;
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return [input];
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
51