// BEGIN FLOCK GPL
//
// Copyright Flock Inc. 2005-2009
// http://flock.com
//
// This file may be used under the terms of the
// GNU General Public License Version 2 or later (the "GPL"),
// http://www.gnu.org/licenses/gpl.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// END FLOCK GPL

// This file has a few various basic coop tests: add objects to a list, iterate...

const NAME = "http://home.netscape.com/NC-rdf#Name";

const testData = [
  "apple",
  "pear",
  "peach",
  "blueberry",
  "strawberry",
  "pineapple",
  "coconut"
];

function testEnumerators() {
  var newFolder = new coop.Folder();
  do_check_false(newFolder.children.enumerate().hasMoreElements());

  // Test enumerate()
  for (let i = 0; i < testData.length; i++) {
    var element = new coop.Object();
    element.name = testData[i];
    newFolder.children.add(element);
  }
  var fruitEnum = newFolder.children.enumerate();
  for (let i = 0; i < testData.length; i++) {
    do_check_true(fruitEnum.hasMoreElements());
    var fruit = fruitEnum.getNext();
    do_check_eq(newFolder.children.indexOf(fruit), i + 1);
    do_check_eq(fruit.name, testData[i]);
  }
  do_check_false(fruitEnum.hasMoreElements());

  // Test enumerateBackwards()
  var backEnum = newFolder.children.enumerateBackwards();
  for (let i = (testData.length - 1); i >= 0; i--) {
    do_check_true(backEnum.hasMoreElements());
    var fruit = backEnum.getNext();
    do_check_eq(newFolder.children.indexOf(fruit), i + 1);
    do_check_eq(fruit.name, testData[i]);
  }
  do_check_false(backEnum.hasMoreElements());
}

function testInsertSorted() {
  var sortedData = testData.concat().sort();

  // With a cap bigger than the total data length
  var sortedFolder = new coop.Folder();
  for (let i = 0; i < testData.length; i++) {
    var element = new coop.Object();
    element.name = testData[i];
    sortedFolder.children.insertSortedOn(NAME, element, 50);
  }

  var fruitEnum = sortedFolder.children.enumerate();
  for (let i = 0; i < sortedData.length; i++) {
    do_check_true(fruitEnum.hasMoreElements());
    var fruit = fruitEnum.getNext();
    do_check_eq(fruit.name, sortedData[i]);
  }
  do_check_false(fruitEnum.hasMoreElements());

  // With a cap smaller than the total data length
  const MAX = 4;
  var smallFolder = new coop.Folder();
  for (let i = 0; i < testData.length; i++) {
    var element = new coop.Object();
    element.name = testData[i];
    smallFolder.children.insertSortedOn(NAME, element, MAX);
  }

  var smallEnum = smallFolder.children.enumerate();
  var smallData = sortedData.slice(sortedData.length - MAX)
  for (let i = 0; i < 4; i++) {
    do_check_true(smallEnum.hasMoreElements());
    var fruit = smallEnum.getNext();
    do_check_eq(fruit.name, smallData[i]);
  }
  do_check_false(fruitEnum.hasMoreElements());

}

function run_test() {
  // Root present?
  var coopRoot = coop.favorites_root;
  do_check_neq(coopRoot, null);

  testEnumerators();
  testInsertSorted();

  do_test_finished();
}
