B2G/gecko/toolkit/components/places/tests/queries/test_containersQueries_sorting.js
/**
 * Test if children of aRootNode are correctly sorted.
 * @param aRootNode
 *        already opened root node from our query's result.
 * @param aExpectedSortingMode
 *        The sortingMode we expect results to be.
 */
Test if children of aRootNode are correctly sorted.
@param aRootNode
already opened root node from our query's result.
@param aExpectedSortingMode
The sortingMode we expect results to be.
Test if children of aRootNode are correctly sorted . @param aRootNode already opened root node from our query ' s result . @param aExpectedSortingMode The sortingMode we expect results to be .
function check_children_sorting(aRootNode, aExpectedSortingMode) {
  var results = [];
  print("Found children:");
  for (var i = 0; i < aRootNode.childCount; i++) {
    results[i] = aRootNode.getChild(i);
    print(i + " " + results[i].title);
  }
  function caseInsensitiveStringComparator(a, b) {
    var aLC = a.toLowerCase();
    var bLC = b.toLowerCase();
    if (aLC < bLC)
      return -1;
    if (aLC > bLC)
      return 1;
    return 0;
  }
  var comparator;
  switch(aExpectedSortingMode) {
    case Ci.nsINavHistoryQueryOptions.SORT_BY_NONE:
      comparator = function (a, b) {
        return 0;
      }
      break;
    case Ci.nsINavHistoryQueryOptions.SORT_BY_TITLE_ASCENDING:
      comparator = function (a, b) {
        return caseInsensitiveStringComparator(a.title, b.title);
      }
      break;
    case Ci.nsINavHistoryQueryOptions.SORT_BY_TITLE_DESCENDING:
      comparator = function (a, b) {
        return -caseInsensitiveStringComparator(a.title, b.title);
      }
      break;
    case Ci.nsINavHistoryQueryOptions.SORT_BY_DATE_ASCENDING:
      comparator = function (a, b) {
        return a.time - b.time;
      }
      break;
    case Ci.nsINavHistoryQueryOptions.SORT_BY_DATE_DESCENDING:
      comparator = function (a, b) {
        return b.time - a.time;
      }
    case Ci.nsINavHistoryQueryOptions.SORT_BY_DATEADDED_ASCENDING:
      comparator = function (a, b) {
        return a.dateAdded - b.dateAdded;
      }
      break;
    case Ci.nsINavHistoryQueryOptions.SORT_BY_DATEADDED_DESCENDING:
      comparator = function (a, b) {
        return b.dateAdded - a.dateAdded;
      }
      break;
    default:
      do_throw("Unknown sorting type: " + aExpectedSortingMode);
  }
  var sortedResults = results.slice();
  sortedResults.sort(comparator);
  for (var i = 0; i < sortedResults.length; i++) {
    if (sortedResults[i].title != results[i].title)
      print(i + " index wrong, expected " + sortedResults[i].title +
            " found " + results[i].title);
    do_check_eq(sortedResults[i].title, results[i].title);
  }
}
function check_children_sorting ( aRootNode , aExpectedSortingMode ) { var results = [ ] ; print ( "Found children:" ) ; for ( var i = 0 ; i < aRootNode . childCount ; i ++ ) { results [ i ] = aRootNode . getChild ( i ) ; print ( i + " " + results [ i ] . title ) ; } function caseInsensitiveStringComparator ( a , b ) { var aLC = a . toLowerCase ( ) ; var bLC = b . toLowerCase ( ) ; if ( aLC < bLC ) return - 1 ; if ( aLC > bLC ) return 1 ; return 0 ; } var comparator ; switch ( aExpectedSortingMode ) { case Ci . nsINavHistoryQueryOptions . SORT_BY_NONE : comparator = function ( a , b ) { return 0 ; } break ; case Ci . nsINavHistoryQueryOptions . SORT_BY_TITLE_ASCENDING : comparator = function ( a , b ) { return caseInsensitiveStringComparator ( a . title , b . title ) ; } break ; case Ci . nsINavHistoryQueryOptions . SORT_BY_TITLE_DESCENDING : comparator = function ( a , b ) { return - caseInsensitiveStringComparator ( a . title , b . title ) ; } break ; case Ci . nsINavHistoryQueryOptions . SORT_BY_DATE_ASCENDING : comparator = function ( a , b ) { return a . time - b . time ; } break ; case Ci . nsINavHistoryQueryOptions . SORT_BY_DATE_DESCENDING : comparator = function ( a , b ) { return b . time - a . time ; } case Ci . nsINavHistoryQueryOptions . SORT_BY_DATEADDED_ASCENDING : comparator = function ( a , b ) { return a . dateAdded - b . dateAdded ; } break ; case Ci . nsINavHistoryQueryOptions . SORT_BY_DATEADDED_DESCENDING : comparator = function ( a , b ) { return b . dateAdded - a . dateAdded ; } break ; default : do_throw ( "Unknown sorting type: " + aExpectedSortingMode ) ; } var sortedResults = results . slice ( ) ; sortedResults . sort ( comparator ) ; for ( var i = 0 ; i < sortedResults . length ; i ++ ) { if ( sortedResults [ i ] . title != results [ i ] . title ) print ( i + " index wrong, expected " + sortedResults [ i ] . title + " found " + results [ i ] . title ) ; do_check_eq ( sortedResults [ i ] . title , results [ i ] . title ) ; } }
Test if children of aRootNode are correctly sorted.
Test if children of aRootNode are correctly sorted .
// Helper for case insensitive string comparison. // Get a comparator based on expected sortingMode. // Make an independent copy of the results array and sort it. // Actually compare returned children with our sorted array.