<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>script.aculo.us Unit test file</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="../../lib/prototype.js" type="text/javascript"></script>
  <script src="../../src/scriptaculous.js" type="text/javascript"></script>
  <script src="../../src/unittest.js" type="text/javascript"></script>
  <link rel="stylesheet" href="../test.css" type="text/css" />
</head>
<body>
<h1>script.aculo.us Unit test file</h1>
<p>
  Test of sortable functions in dragdrop.js
</p>

<!-- Log output -->
<div id="testlog"> </div>


<ul id="sortable1">
  <li id="item_1" class="a">item 1</li>
  <li id="item_2" class="c b">item 1<ul><li id="item_99">!!!</li></ul></li>
  <li id="item_3" class="b">item 1</li>
  <li id="item_xy" class="x y">item 1</li>
  <!-- a comment -->
</ul>

<div id="sortable2">
  <div id="item_4">item 4</div>  <div id="item_5">item 5</div>
  <img src="icon.png" alt="img"/>
  <!-- a comment -->
</div>

<div id="sortable3">
  
</div>

<!-- 
  By default, _ is the only valid seperator
  for the DOM ids. Complex element ids as in
  the form of "some_element_id_1" should also
  be parsed correctly (only the last part should
  be serialized)
-->
<ul id="sortable_complex">
  <li id="a_b_item_1" class="a">item 1</li>
  <li id="ab_item_2" class="c b">item 1
    <ul>
      <li id="item_99">!!!</li>
    </ul>
  </li>
  <li id="a-b-item_3z_33" class="b">item 1</li>
  <li id="a-item-xy" class="x y">item 1</li>
  <!-- a comment -->
</ul>


<ul id="sortable_specialcreate">
  <li id="y1item">item 1</li>
  <li id="y2item">item 1<ul><li id="yyyy9928282hjhd">!!!</li></ul></li>
</ul>

<ul id="sortable_specialformat">
  <li id="x1item">item 1</li>
  <li id="x2item">item 1<ul><li id="xxxxx88888item">!!!</li></ul></li>
</ul>

<!-- Tests follow -->
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[

  new Test.Unit.Runner({
    
    setup: function() { with(this) {
      Sortable.create('sortable1',{only:['a','b']});
      Sortable.create('sortable2',{tag:'div'});
      Sortable.create('sortable3');
      Sortable.create('sortable_specialcreate',{ format:/(\d+)/ });
      Sortable.create('sortable_specialformat');
      Sortable.create('sortable_complex');
    }},
    
    teardown: function() { with(this) {
      Sortable.destroy('sortable1');
      Sortable.destroy('sortable2');
      Sortable.destroy('sortable3');
      Sortable.destroy('sortable_specialformat');
      Sortable.destroy('sortable_specialcreate');
      Sortable.destroy('sortable_complex');
    }},
    
    testSortableSerializeSequenceBasics: function() { with(this) {
      assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3', Sortable.serialize('sortable1'));      
      
      // test empty sortable
      assertEqual('', Sortable.serialize('sortable3'));
      assertEnumEqual([], Sortable.sequence('sortable3'));
      
      Element.remove('item_4');
      assertEqual('sortable2[]=5', Sortable.serialize('sortable2'));
      assertEnumEqual(['5'], Sortable.sequence('sortable2'));
    }},
    
    testSortableSerializeFormat: function() { with(this) {
      // should correctly serialize from option given to Sortable.create()
      assertEqual('sortable_specialcreate[]=1&sortable_specialcreate[]=2', 
        Sortable.serialize('sortable_specialcreate'));
        
      benchmark(function(){
        Sortable.serialize('sortable_specialcreate')
      },1,'Sortable.serialize');
      
      // test special format given explicitly
      assertEqual('sortable_specialformat[]=1&sortable_specialformat[]=2', 
        Sortable.serialize('sortable_specialformat', {format:/(\d+)/}));
 
      // return full id
      assertEqual('sortable_specialformat[]=x1item&sortable_specialformat[]=x2item', 
        Sortable.serialize('sortable_specialformat', {format:/(.*)/}));
      
      // test default format given explicitly
      assertEqual('sortable1[]=1&sortable1[]=2&sortable1[]=3', 
        Sortable.serialize('sortable1',{format:/^[^_]*_(.*)$/}));
        
      // Ensure default options.format handles longer, more complex list
      // item IDs
      assertEqual('sortable_complex[]=1&sortable_complex[]=2&sortable_complex[]=33&sortable_complex[]=',
        Sortable.serialize('sortable_complex'));
    }},
    
    testSortableSerializeRule: function() { with(this) {
      var ids = ['x','x-y','test_test','x_y_z','x_y-x_z'];
      ids.each(function(id){
        assertEqual('1',
          (id+'_1').match(Sortable.SERIALIZE_RULE)[1]);
      });
      
      assertNull('x'.match(Sortable.SERIALIZE_RULE));
    }},

    testSortableSerializeName: function() { with(this) {
      assertEqual('dumdidu[]=1&dumdidu[]=2',
      Sortable.serialize('sortable_specialcreate',{name:'dumdidu'}));
    }},
    
    testSortableSequenceFormat: function() { with(this) {
      // shauld correctly serialize from option given to Sortable.create()
      assertEnumEqual(['1','2'], 
        Sortable.sequence('sortable_specialcreate'));
      
      // test special format given explicitly
      assertEnumEqual(['1','2'], 
        Sortable.sequence('sortable_specialformat', {format:/(\d+)/}));
 
      // return full id
      assertEnumEqual(['x1item','x2item'], 
        Sortable.sequence('sortable_specialformat', {format:/(.*)/}));
      
      // test default format given explicitly
      assertEnumEqual(['1','2','3'], 
        Sortable.sequence('sortable1',{format:/^[^_]*_(.*)$/}));
    }},
    
    testSortableFindElements: function() { with(this) {
      assertEqual(3, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']}).length);
      benchmark(function(){
        Sortable.findElements($('sortable1'),{tag:'li',only:['a','b']})
      },1,'Sortable.findElements/1');
      
      assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:['x']}).length);
      assertEqual(1, Sortable.findElements($('sortable1'),{tag:'li',only:'a'}).length);
      assertEqual(2, Sortable.findElements($('sortable1'),{tag:'li',only:'b'}).length);
      assertEqual(4, Sortable.findElements($('sortable1'),{tag:'li',only:['a','b','x']}).length);
    }},

    testSortableSetSequence: function() { with(this) {
      // make sure assigning current sequence is a no-op 
      var o = Sortable.sequence('sortable1');
      Sortable.setSequence('sortable1', ['1','2','3']);
      assertEnumEqual(o, Sortable.sequence('sortable1'));
      
      // check new sequence   
      Sortable.setSequence('sortable1', ['3','2','1']);
      assertEnumEqual(['3','2','1'], Sortable.sequence('sortable1'));
      
      // non-default format
      Sortable.setSequence('sortable_specialformat', ['2','1'], { format:/(\d+)/ });
      assertEnumEqual(['2','1'], Sortable.sequence('sortable_specialformat'));
      
      // invalid sequence ids should be ignored
      Sortable.setSequence('sortable1', ['x', '1', 'y', '2', '3', 'z']);
      assertEnumEqual(['1', '2', '3'], Sortable.sequence('sortable1'));
      
      // elements omitted in new sequence should be cropped
      Sortable.setSequence('sortable1', ['1', '3']);
      assertEnumEqual(['1', '3'], Sortable.sequence('sortable1'));
    }}

  }, "testlog");
// ]]>
</script>
</body>
</html>
