B2G/gecko/toolkit/components/places/tests/queries/test_redirects.js
/**
 * Takes a sequence of query options, and compare query results obtained through
 * them with a custom filtered array of visits, based on the values we are
 * expecting from the query.
 *
 * @param  aSequence
 *         an array that contains query options in the form:
 *         [includeHidden, maxResults, sortingMode]
 */
Takes a sequence of query options, and compare query results obtained through
them with a custom filtered array of visits, based on the values we are
expecting from the query.

@param  aSequence
an array that contains query options in the form:
[includeHidden, maxResults, sortingMode]
Takes a sequence of query options and compare query results obtained through them with a custom filtered array of visits based on the values we are expecting from the query . @param aSequence an array that contains query options in the form : [ includeHidden maxResults sortingMode ]
function check_results_callback(aSequence) {
  do_check_eq(aSequence.length, 3);
  let includeHidden = aSequence[0];
  let maxResults = aSequence[1];
  let sortingMode = aSequence[2];
  print("\nTESTING: includeHidden(" + includeHidden + ")," +
                  " maxResults("    + maxResults    + ")," +
                  " sortingMode("   + sortingMode   + ").");
  function isHidden(aVisit) {
    return aVisit.transType == Ci.nsINavHistoryService.TRANSITION_FRAMED_LINK ||
           aVisit.isRedirect;
  }
  let expectedData = visits.filter(function (aVisit, aIndex, aArray) {
    if (aVisit.transType == Ci.nsINavHistoryService.TRANSITION_EMBED)
      return false;
    if (!includeHidden && isHidden(aVisit)) {
      if (visits.filter(function (refVisit) {
        return refVisit.uri == aVisit.uri && !isHidden(refVisit);
          }).length == 0)
        return false;
    }
    return true;
  });
  let seen = [];
  expectedData = expectedData.filter(function (aData) {
    if (seen.indexOf(aData.uri) != -1)
      return false;
    else
      seen.push(aData.uri);
    return true;
  });
  function getFirstIndexFor(aEntry) {
    for (let i = 0; i < visits.length; i++) {
      if (visits[i].uri == aEntry.uri)
        return i;
    }
  }
  function comparator(a, b) {
    if (sortingMode == Ci.nsINavHistoryQueryOptions.SORT_BY_DATE_DESCENDING)
      return b.lastVisit - a.lastVisit;
    else if (sortingMode == Ci.nsINavHistoryQueryOptions.SORT_BY_VISITCOUNT_DESCENDING)
      return b.visitCount - a.visitCount;
    else
      return getFirstIndexFor(a) - getFirstIndexFor(b);
  }
  expectedData.sort(comparator);
  if (maxResults) {
    expectedData = expectedData.slice(0, maxResults);
  }
  let query = PlacesUtils.history.getNewQuery();
  let options = PlacesUtils.history.getNewQueryOptions();
  options.includeHidden = includeHidden;
  options.sortingMode = sortingMode;
  if (maxResults)
    options.maxResults = maxResults;
  let result = PlacesUtils.history.executeQuery(query, options);
  let root = result.root;
  root.containerOpen = true;
  compareArrayToResult(expectedData, root);
  root.containerOpen = false;
}
function check_results_callback ( aSequence ) { do_check_eq ( aSequence . length , 3 ) ; let includeHidden = aSequence [ 0 ] ; let maxResults = aSequence [ 1 ] ; let sortingMode = aSequence [ 2 ] ; print ( "\nTESTING: includeHidden(" + includeHidden + ")," + " maxResults(" + maxResults + ")," + " sortingMode(" + sortingMode + ")." ) ; function isHidden ( aVisit ) { return aVisit . transType == Ci . nsINavHistoryService . TRANSITION_FRAMED_LINK || aVisit . isRedirect ; } let expectedData = visits . filter ( function ( aVisit , aIndex , aArray ) { if ( aVisit . transType == Ci . nsINavHistoryService . TRANSITION_EMBED ) return false ; if ( ! includeHidden && isHidden ( aVisit ) ) { if ( visits . filter ( function ( refVisit ) { return refVisit . uri == aVisit . uri && ! isHidden ( refVisit ) ; } ) . length == 0 ) return false ; } return true ; } ) ; let seen = [ ] ; expectedData = expectedData . filter ( function ( aData ) { if ( seen . indexOf ( aData . uri ) != - 1 ) return false ; else seen . push ( aData . uri ) ; return true ; } ) ; function getFirstIndexFor ( aEntry ) { for ( let i = 0 ; i < visits . length ; i ++ ) { if ( visits [ i ] . uri == aEntry . uri ) return i ; } } function comparator ( a , b ) { if ( sortingMode == Ci . nsINavHistoryQueryOptions . SORT_BY_DATE_DESCENDING ) return b . lastVisit - a . lastVisit ; else if ( sortingMode == Ci . nsINavHistoryQueryOptions . SORT_BY_VISITCOUNT_DESCENDING ) return b . visitCount - a . visitCount ; else return getFirstIndexFor ( a ) - getFirstIndexFor ( b ) ; } expectedData . sort ( comparator ) ; if ( maxResults ) { expectedData = expectedData . slice ( 0 , maxResults ) ; } let query = PlacesUtils . history . getNewQuery ( ) ; let options = PlacesUtils . history . getNewQueryOptions ( ) ; options . includeHidden = includeHidden ; options . sortingMode = sortingMode ; if ( maxResults ) options . maxResults = maxResults ; let result = PlacesUtils . history . executeQuery ( query , options ) ; let root = result . root ; root . containerOpen = true ; compareArrayToResult ( expectedData , root ) ; root . containerOpen = false ; }
Takes a sequence of query options, and compare query results obtained through
 them with a custom filtered array of visits, based on the values we are
 expecting from the query.
Takes a sequence of query options and compare query results obtained through them with a custom filtered array of visits based on the values we are expecting from the query .
// Sanity check: we should receive 3 parameters. // Build expectedData array. // Embed visits never appear in results. // If the page has any non-hidden visit, then it's visible. // Remove duplicates, since queries are RESULTS_AS_URI (unique pages). // Sort expectedData. // Crop results to maxResults if it's defined. // Create a new query with required options. // Compare resultset with expectedData.