view htdocs/svg-edit/test/contextmenu_test.html @ 632:8c00e2be925e

inital veraion 2.7 r2891 of svg-edit
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Thu, 05 Feb 2015 16:37:37 +0100
parents
children
line wrap: on
line source
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Unit Tests for contextmenu.js</title>
  <link rel='stylesheet' href='qunit/qunit.css' type='text/css'/>
  <script src='../editor/jquery.js'></script>
  <script>
  // Mock for browser.js
  window.svgEditor = {ready:function(){}};
  </script>
  <script src='../editor/contextmenu.js'></script>
  <script src='qunit/qunit.js'></script>
  <script>
  $(function() {
    // log function
    QUnit.log = function(result, message) {
      if (window.console && window.console.log) {
        window.console.log(result +' :: '+ message);
      }
    };

    function tearDown(){
      svgedit.contextmenu.resetCustomMenus();
    }

    module('svgedit.contextmenu');

    test('Test svgedit.contextmenu package', function() {
      expect(4);

      ok(svgedit.contextmenu,"contextmenu registered correctly");
      ok(svgedit.contextmenu.add,"contextmenu.add registered correctly");
      ok(svgedit.contextmenu.hasCustomHandler,"contextmenu hasCustomHandler registered correctly");
      ok(svgedit.contextmenu.getCustomHandler,"contextmenu getCustomHandler registered correctly");
    });

    test('Test svgedit.contextmenu does not add invalid menu item', function() {
      expect(3);

      svgedit.contextmenu.add({id:"justanid"});
      ok(!svgedit.contextmenu.hasCustomHandler("justanid"),"menu item with just an id is invalid");

      svgedit.contextmenu.add({id:"idandlabel",label:"anicelabel"});
      ok(!svgedit.contextmenu.hasCustomHandler("idandlabel"),"menu item with just an id and label is invalid");

      svgedit.contextmenu.add({id:"idandlabel",label:"anicelabel",action:'notafunction'});
      ok(!svgedit.contextmenu.hasCustomHandler("idandlabel"),"menu item with action that is not a function is invalid");
    });

    test('Test svgedit.contextmenu adds valid menu item', function() {
      expect(2);

      var validItem = {id:"valid",label:"anicelabel",action:function(){alert('testing')}};
      svgedit.contextmenu.add(validItem);

      ok(svgedit.contextmenu.hasCustomHandler("valid"),"Valid menu item is added.");
      equals(svgedit.contextmenu.getCustomHandler("valid"),validItem.action,"Valid menu action is added.");
      tearDown();
    });

    test('Test svgedit.contextmenu rejects valid duplicate menu item id', function() {
      expect(1);

      var validItem1 = {id:"valid",label:"anicelabel",action:function(){alert('testing')}};
      var validItem2 = {id:"valid",label:"anicelabel",action:function(){alert('testingtwice')}};
      svgedit.contextmenu.add(validItem1);
      svgedit.contextmenu.add(validItem2);

      equals(svgedit.contextmenu.getCustomHandler("valid"),validItem1.action,"duplicate menu item is rejected.");
      tearDown();
    });
  });
  </script>
</head>
<body>
  <h1 id='qunit-header'>Unit Tests for contextmenu.js</h1>
  <h2 id='qunit-banner'></h2>
  <h2 id='qunit-userAgent'></h2>
  <ol id='qunit-tests'></ol>
  <div id='svgroot' style='visibility:hidden'></div>
</body>
</html>