# HG changeset patch # User Pierre-Yves David # Date 1365501387 -7200 # Node ID 1cad6662e199974c5da337c0d3e9161b84ee3423 # Parent daada86d3d17c1ae8a02a4d7b058c73e8dcbfa71# Parent 982a49239420d54692cc5cb73b23773dc803cebf Merge testfunc testfunc resurrection Closes #2535377 diff -r daada86d3d17 -r 1cad6662e199 devtools/cwwindmill.py --- a/devtools/cwwindmill.py Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,160 +0,0 @@ -# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. -# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr -# -# This file is part of CubicWeb. -# -# CubicWeb is free software: you can redistribute it and/or modify it under the -# terms of the GNU Lesser General Public License as published by the Free -# Software Foundation, either version 2.1 of the License, or (at your option) -# any later version. -# -# CubicWeb is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more -# details. -# -# You should have received a copy of the GNU Lesser General Public License along -# with CubicWeb. If not, see . -"""this module contains base classes for windmill integration - -:todo: - - * import CubicWeb session object into windmill scope to be able to run RQL - * manage command line option from pytest to run specific use tests only -""" - - -import os, os.path as osp -from logging import getLogger, ERROR -import sys - -# imported by default to simplify further import statements -from logilab.common.testlib import TestCase, unittest_main, Tags - -try: - import windmill - from windmill.dep import functest - from windmill.bin.admin_lib import configure_global_settings, setup, teardown -except ImportError: - windmill = None - -from cubicweb.devtools.httptest import CubicWebServerTC, CubicWebServerConfig - -if windmill is None: - class CubicWebWindmillUseCase(CubicWebServerTC): - tags = CubicWebServerTC.tags & Tags(('windmill',)) - - def testWindmill(self): - self.skipTest("can't import windmill") -else: - # Excerpt from :ref:`windmill.authoring.unit` - class UnitTestReporter(functest.reports.FunctestReportInterface): - def summary(self, test_list, totals_dict, stdout_capture): - self.test_list = test_list - - unittestreporter = UnitTestReporter() - functest.reports.register_reporter(unittestreporter) - - class CubicWebWindmillUseCase(CubicWebServerTC): - """basic class for Windmill use case tests - - If you want to change cubicweb test server parameters, define a new - :class:`CubicWebServerConfig` and override the :var:`configcls` - attribute: - - configcls = CubicWebServerConfig - - From Windmill configuration: - - .. attribute:: browser - identification string (firefox|ie|safari|chrome) (firefox by default) - .. attribute :: edit_test - load and edit test for debugging (False by default) - .. attribute:: test_dir (optional) - testing file path or directory (windmill directory under your unit case - file by default) - - Examples: - - browser = 'firefox' - test_dir = osp.join(__file__, 'windmill') - edit_test = False - - If you prefer, you can put here the use cases recorded by windmill GUI - (services transformer) instead of the windmill sub-directory - You can change `test_dir` as following: - - test_dir = __file__ - - Instead of toggle `edit_test` value, try `python -f` - """ - browser = 'firefox' - - edit_test = "-i" in sys.argv # detection for pytest invocation - # Windmill use case are written with no anonymous user - anonymous_allowed = False - - tags = CubicWebServerTC.tags & Tags(('windmill',)) - - def _test_dir(self): - """access to class attribute if possible or make assumption - of expected directory""" - try: - return getattr(self, 'test_dir') - except AttributeError: - if os.path.basename(sys.argv[0]) == "pytest": - test_dir = os.getcwd() - else: - import inspect - test_dir = os.path.dirname(inspect.stack()[-1][1]) - return osp.join(test_dir, 'windmill') - - def setUp(self): - # Start CubicWeb session before running the server to populate self.vreg - CubicWebServerTC.setUp(self) - # XXX reduce log output (should be done in a cleaner way) - # windmill fu** up our logging configuration - for logkey in ('windmill', 'logilab', 'cubicweb'): - getLogger(logkey).setLevel(ERROR) - self.test_dir = self._test_dir() - msg = "provide a valid 'test_dir' as the given test file/dir (current: %s)" - assert os.path.exists(self.test_dir), (msg % self.test_dir) - # windmill setup - windmill.stdout, windmill.stdin = sys.stdout, sys.stdin - configure_global_settings() - windmill.settings['TEST_URL'] = self.config['base-url'] - if hasattr(self,"windmill_settings"): - for (setting,value) in self.windmill_settings.iteritems(): - windmill.settings[setting] = value - self.windmill_shell_objects = setup() - - def tearDown(self): - teardown(self.windmill_shell_objects) - CubicWebServerTC.tearDown(self) - - def testWindmill(self): - if self.edit_test: - # see windmill.bin.admin_options.Firebug - windmill.settings['INSTALL_FIREBUG'] = 'firebug' - windmill.settings.setdefault('MOZILLA_PLUGINS', []).extend( - ['/usr/share/mozilla-extensions/', - '/usr/share/xul-ext/']) - controller = self.windmill_shell_objects['start_' + self.browser]() - self.windmill_shell_objects['do_test'](self.test_dir, - load=self.edit_test, - threaded=False) - # set a breakpoint to be able to debug windmill test - if self.edit_test: - import pdb; pdb.set_trace() - return - - # reporter - for test in unittestreporter.test_list: - msg = "" - self._testMethodDoc = getattr(test, "__doc__", None) - self._testMethodName = test.__name__ - # try to display a better message in case of failure - if hasattr(test, "tb"): - msg = '\n'.join(test.tb) - self.assertEqual(test.result, True, msg=msg) - diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/ajax_url0.html --- a/testfunc/test/jstests/ajax_url0.html Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,3 +0,0 @@ -
-

Hello

-
diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/ajax_url1.html --- a/testfunc/test/jstests/ajax_url1.html Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -
-
- -
-

Hello

-
diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/ajax_url2.html --- a/testfunc/test/jstests/ajax_url2.html Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -
-
- - -
-

Hello

-
diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/ajaxresult.json --- a/testfunc/test/jstests/ajaxresult.json Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -['foo', 'bar'] diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/test_ajax.html --- a/testfunc/test/jstests/test_ajax.html Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - -
-

cubicweb.ajax.js functions tests

-

-
    - - diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/test_ajax.js --- a/testfunc/test/jstests/test_ajax.js Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,244 +0,0 @@ -$(document).ready(function() { - - module("ajax", { - setup: function() { - this.scriptsLength = $('head script[src]').length-1; - this.cssLength = $('head link[rel=stylesheet]').length-1; - // re-initialize cw loaded cache so that each tests run in a - // clean environment, have a lookt at _loadAjaxHtmlHead implementation - // in cubicweb.ajax.js for more information. - cw.loaded_src = []; - cw.loaded_href = []; - }, - teardown: function() { - $('head script[src]:gt(' + this.scriptsLength + ')').remove(); - $('head link[rel=stylesheet]:gt(' + this.cssLength + ')').remove(); - } - }); - - function jsSources() { - return $.map($('head script[src]'), function(script) { - return script.getAttribute('src'); - }); - } - - test('test simple h1 inclusion (ajax_url0.html)', function() { - expect(3); - equals(jQuery('#main').children().length, 0); - stop(); - jQuery('#main').loadxhtml('/../ajax_url0.html', { - callback: function() { - equals(jQuery('#main').children().length, 1); - equals(jQuery('#main h1').html(), 'Hello'); - start(); - } - }); - }); - - test('test simple html head inclusion (ajax_url1.html)', function() { - expect(6); - var scriptsIncluded = jsSources(); - equals(jQuery.inArray('http://foo.js', scriptsIncluded), - 1); - stop(); - jQuery('#main').loadxhtml('/../ajax_url1.html', { - callback: function() { - var origLength = scriptsIncluded.length; - scriptsIncluded = jsSources(); - // check that foo.js has been *appended* to - equals(scriptsIncluded.length, origLength + 1); - equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0); - // check that
    has been removed - equals(jQuery('#main').children().length, 1); - equals(jQuery('div.ajaxHtmlHead').length, 0); - equals(jQuery('#main h1').html(), 'Hello'); - start(); - } - }); - }); - - test('test addCallback', function() { - expect(3); - equals(jQuery('#main').children().length, 0); - stop(); - var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); - d.addCallback(function() { - equals(jQuery('#main').children().length, 1); - equals(jQuery('#main h1').html(), 'Hello'); - start(); - }); - }); - - test('test callback after synchronous request', function() { - expect(1); - var deferred = new Deferred(); - var result = jQuery.ajax({ - url: './ajax_url0.html', - async: false, - beforeSend: function(xhr) { - deferred._req = xhr; - }, - success: function(data, status) { - deferred.success(data); - } - }); - stop(); - deferred.addCallback(function() { - // add an assertion to ensure the callback is executed - ok(true, "callback is executed"); - start(); - }); - }); - - test('test addCallback with parameters', function() { - expect(3); - equals(jQuery('#main').children().length, 0); - stop(); - var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); - d.addCallback(function(data, req, arg1, arg2) { - equals(arg1, 'Hello'); - equals(arg2, 'world'); - start(); - }, - 'Hello', 'world'); - }); - - test('test callback after synchronous request with parameters', function() { - var deferred = new Deferred(); - var result = jQuery.ajax({ - url: '/../ajax_url0.html', - async: false, - beforeSend: function(xhr) { - deferred._req = xhr; - }, - success: function(data, status) { - deferred.success(data); - } - }); - deferred.addCallback(function(data, req, arg1, arg2) { - // add an assertion to ensure the callback is executed - ok(true, "callback is executed"); - equals(arg1, 'Hello'); - equals(arg2, 'world'); - }, - 'Hello', 'world'); - }); - - test('test addErrback', function() { - expect(1); - stop(); - var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); - d.addCallback(function() { - // throw an exception to start errback chain - throw new Error(); - }); - d.addErrback(function() { - ok(true, "errback is executed"); - start(); - }); - }); - - test('test callback / errback execution order', function() { - expect(4); - var counter = 0; - stop(); - var d = jQuery('#main').loadxhtml('/../ajax_url0.html', { - callback: function() { - equals(++counter, 1); // should be executed first - start(); - } - }); - d.addCallback(function() { - equals(++counter, 2); // should be executed and break callback chain - throw new Error(); - }); - d.addCallback(function() { - // should not be executed since second callback raised an error - ok(false, "callback is executed"); - }); - d.addErrback(function() { - // should be executed after the second callback - equals(++counter, 3); - }); - d.addErrback(function() { - // should be executed after the first errback - equals(++counter, 4); - }); - }); - - test('test already included resources are ignored (ajax_url1.html)', function() { - expect(10); - var scriptsIncluded = jsSources(); - // NOTE: - equals(jQuery.inArray('http://foo.js', scriptsIncluded), -1); - equals(jQuery('head link').length, 1); - /* use endswith because in pytest context we have an absolute path */ - ok(jQuery('head link').attr('href').endswith('/qunit.css')); - stop(); - jQuery('#main').loadxhtml('/../ajax_url1.html', { - callback: function() { - var origLength = scriptsIncluded.length; - scriptsIncluded = jsSources(); - try { - // check that foo.js has been inserted in - equals(scriptsIncluded.length, origLength + 1); - equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0); - // check that
    has been removed - equals(jQuery('#main').children().length, 1); - equals(jQuery('div.ajaxHtmlHead').length, 0); - equals(jQuery('#main h1').html(), 'Hello'); - // qunit.css is not added twice - equals(jQuery('head link').length, 1); - /* use endswith because in pytest context we have an absolute path */ - ok(jQuery('head link').attr('href').endswith('/qunit.css')); - } finally { - start(); - } - } - }); - }); - - test('test synchronous request loadRemote', function() { - var res = loadRemote('/../ajaxresult.json', {}, - 'GET', true); - same(res, ['foo', 'bar']); - }); - - test('test event on CubicWeb', function() { - expect(1); - stop(); - var events = null; - jQuery(CubicWeb).bind('server-response', function() { - // check that server-response event on CubicWeb is triggered - events = 'CubicWeb'; - }); - jQuery('#main').loadxhtml('/../ajax_url0.html', { - callback: function() { - equals(events, 'CubicWeb'); - start(); - } - }); - }); - - test('test event on node', function() { - expect(3); - stop(); - var nodes = []; - jQuery('#main').bind('server-response', function() { - nodes.push('node'); - }); - jQuery(CubicWeb).bind('server-response', function() { - nodes.push('CubicWeb'); - }); - jQuery('#main').loadxhtml('/../ajax_url0.html', { - callback: function() { - equals(nodes.length, 2); - // check that server-response event on CubicWeb is triggered - // only once and event server-response on node is triggered - equals(nodes[0], 'CubicWeb'); - equals(nodes[1], 'node'); - start(); - } - }); - }); -}); - diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/test_htmlhelpers.html --- a/testfunc/test/jstests/test_htmlhelpers.html Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,21 +0,0 @@ - - - - - - - - - - - - - -
    -
    -

    cubicweb.htmlhelpers.js functions tests

    -

    -

    -
      - - diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/test_htmlhelpers.js --- a/testfunc/test/jstests/test_htmlhelpers.js Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -$(document).ready(function() { - - module("module2", { - setup: function() { - $('#main').append(''); - } - }); - - test("test first selected", function() { - $('#theselect').append('' + - '' + - '' + - ''); - var selected = firstSelected(document.getElementById("theselect")); - equals(selected.value, 'bar'); - }); - - test("test first selected 2", function() { - $('#theselect').append('' + - '' + - '' + - ''); - var selected = firstSelected(document.getElementById("theselect")); - equals(selected, null); - }); - - module("visibilty"); - test('toggleVisibility', function() { - $('#main').append('
      '); - toggleVisibility('foo'); - ok($('#foo').hasClass('hidden'), 'check hidden class is set'); - }); - -}); - diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/test_utils.html --- a/testfunc/test/jstests/test_utils.html Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - -
      -
      -

      cw.utils functions tests

      -

      -

      -
        - - diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/test_utils.js --- a/testfunc/test/jstests/test_utils.js Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -$(document).ready(function() { - - module("datetime"); - - test("test full datetime", function() { - equals(cw.utils.toISOTimestamp(new Date(1986, 3, 18, 10, 30, 0, 0)), - '1986-04-18 10:30:00'); - }); - - test("test only date", function() { - equals(cw.utils.toISOTimestamp(new Date(1986, 3, 18)), '1986-04-18 00:00:00'); - }); - - test("test null", function() { - equals(cw.utils.toISOTimestamp(null), null); - }); - - module("parsing"); - test("test basic number parsing", function() { - var d = strptime('2008/08/08', '%Y/%m/%d'); - same(datetuple(d), [2008, 8, 8, 0, 0]); - d = strptime('2008/8/8', '%Y/%m/%d'); - same(datetuple(d), [2008, 8, 8, 0, 0]); - d = strptime('8/8/8', '%Y/%m/%d'); - same(datetuple(d), [8, 8, 8, 0, 0]); - d = strptime('0/8/8', '%Y/%m/%d'); - same(datetuple(d), [0, 8, 8, 0, 0]); - d = strptime('-10/8/8', '%Y/%m/%d'); - same(datetuple(d), [-10, 8, 8, 0, 0]); - d = strptime('-35000', '%Y'); - same(datetuple(d), [-35000, 1, 1, 0, 0]); - }); - - test("test custom format parsing", function() { - var d = strptime('2008-08-08', '%Y-%m-%d'); - same(datetuple(d), [2008, 8, 8, 0, 0]); - d = strptime('2008 - ! 08: 08', '%Y - ! %m: %d'); - same(datetuple(d), [2008, 8, 8, 0, 0]); - d = strptime('2008-08-08 12:14', '%Y-%m-%d %H:%M'); - same(datetuple(d), [2008, 8, 8, 12, 14]); - d = strptime('2008-08-08 1:14', '%Y-%m-%d %H:%M'); - same(datetuple(d), [2008, 8, 8, 1, 14]); - d = strptime('2008-08-08 01:14', '%Y-%m-%d %H:%M'); - same(datetuple(d), [2008, 8, 8, 1, 14]); - }); - - module("sliceList"); - test("test slicelist", function() { - var list = ['a', 'b', 'c', 'd', 'e', 'f']; - same(sliceList(list, 2), ['c', 'd', 'e', 'f']); - same(sliceList(list, 2, -2), ['c', 'd']); - same(sliceList(list, -3), ['d', 'e', 'f']); - same(sliceList(list, 0, -2), ['a', 'b', 'c', 'd']); - same(sliceList(list), list); - }); - - module("formContents", { - setup: function() { - $('#main').append('
        '); - } - }); - // XXX test fckeditor - test("test formContents", function() { - $('#test-form').append(''); - $('#test-form').append(' '); - $('#test-form').append(''); - $('#test-form').append(''); - $('#test-form').append(''); - $('#test-form').append(''); - $('#test-form').append(''); - $('#theselect').append('' + - ''); - //Append an unchecked radio input : should not be in formContents list - $('#test-form').append(''); - $('#test-form').append(''); - same(formContents($('#test-form')[0]), [ - ['input-text', 'mytextarea', 'choice', 'check', 'theselect'], - ['toto', 'Hello World!', 'no', 'no', 'foo'] - ]); - }); -}); - diff -r daada86d3d17 -r 1cad6662e199 testfunc/test/jstests/utils.js --- a/testfunc/test/jstests/utils.js Tue Apr 09 09:03:12 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -function datetuple(d) { - return [d.getFullYear(), d.getMonth()+1, d.getDate(), - d.getHours(), d.getMinutes()]; -} - -function pprint(obj) { - print('{'); - for(k in obj) { - print(' ' + k + ' = ' + obj[k]); - } - print('}'); -} - -function arrayrepr(array) { - return '[' + array.join(', ') + ']'; -} - -function assertArrayEquals(array1, array2) { - if (array1.length != array2.length) { - throw new crosscheck.AssertionFailure(array1.join(', ') + ' != ' + array2.join(', ')); - } - for (var i=0; i +

        Hello

        +
    diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/ajax_url1.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/ajax_url1.html Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,6 @@ +
    +
    + +
    +

    Hello

    +
    diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/ajax_url2.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/ajax_url2.html Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,7 @@ +
    +
    + + +
    +

    Hello

    +
    diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/ajaxresult.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/ajaxresult.json Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,1 @@ +['foo', 'bar'] diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/test_ajax.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/test_ajax.html Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + +
    +

    cubicweb.ajax.js functions tests

    +

    +
      + + diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/test_ajax.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/test_ajax.js Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,289 @@ +$(document).ready(function() { + + module("ajax", { + setup: function() { + this.scriptsLength = $('head script[src]').length-1; + this.cssLength = $('head link[rel=stylesheet]').length-1; + // re-initialize cw loaded cache so that each tests run in a + // clean environment, have a lookt at _loadAjaxHtmlHead implementation + // in cubicweb.ajax.js for more information. + cw.loaded_src = []; + cw.loaded_href = []; + }, + teardown: function() { + $('head script[src]:gt(' + this.scriptsLength + ')').remove(); + $('head link[rel=stylesheet]:gt(' + this.cssLength + ')').remove(); + } + }); + + function jsSources() { + return $.map($('head script[src]'), function(script) { + return script.getAttribute('src'); + }); + } + + test('test simple h1 inclusion (ajax_url0.html)', function() { + expect(3); + equals(jQuery('#main').children().length, 0); + stop(); + jQuery('#main').loadxhtml('/../ajax_url0.html', { + callback: function() { + try { + equals(jQuery('#main').children().length, 1); + equals(jQuery('#main h1').html(), 'Hello'); + } finally { + start(); + }; + } + }); + }); + + test('test simple html head inclusion (ajax_url1.html)', function() { + expect(6); + var scriptsIncluded = jsSources(); + equals(jQuery.inArray('http://foo.js', scriptsIncluded), - 1); + stop(); + jQuery('#main').loadxhtml('/../ajax_url1.html', { + callback: function() { + try { + var origLength = scriptsIncluded.length; + scriptsIncluded = jsSources(); + // check that foo.js has been *appended* to + equals(scriptsIncluded.length, origLength + 1); + equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0); + // check that
      has been removed + equals(jQuery('#main').children().length, 1); + equals(jQuery('div.ajaxHtmlHead').length, 0); + equals(jQuery('#main h1').html(), 'Hello'); + } finally { + start(); + }; + } + }); + }); + + test('test addCallback', function() { + expect(3); + equals(jQuery('#main').children().length, 0); + stop(); + var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); + d.addCallback(function() { + try { + equals(jQuery('#main').children().length, 1); + equals(jQuery('#main h1').html(), 'Hello'); + } finally { + start(); + }; + }); + }); + + test('test callback after synchronous request', function() { + expect(1); + var deferred = new Deferred(); + var result = jQuery.ajax({ + url: './ajax_url0.html', + async: false, + beforeSend: function(xhr) { + deferred._req = xhr; + }, + success: function(data, status) { + deferred.success(data); + } + }); + stop(); + deferred.addCallback(function() { + try { + // add an assertion to ensure the callback is executed + ok(true, "callback is executed"); + } finally { + start(); + }; + }); + }); + + test('test addCallback with parameters', function() { + expect(3); + equals(jQuery('#main').children().length, 0); + stop(); + var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); + d.addCallback(function(data, req, arg1, arg2) { + try { + equals(arg1, 'Hello'); + equals(arg2, 'world'); + } finally { + start(); + }; + }, + 'Hello', 'world'); + }); + + test('test callback after synchronous request with parameters', function() { + expect(2); + var deferred = new Deferred(); + deferred.addCallback(function(data, req, arg1, arg2) { + // add an assertion to ensure the callback is executed + try { + ok(true, "callback is executed"); + equals(arg1, 'Hello'); + equals(arg2, 'world'); + } finally { + start(); + }; + }, + 'Hello', 'world'); + deferred.addErrback(function() { + // throw an exception to start errback chain + try { + throw this._error; + } finally { + start(); + }; + }); + stop(); + var result = jQuery.ajax({ + url: '/../ajax_url0.html', + async: false, + beforeSend: function(xhr) { + deferred._req = xhr; + }, + success: function(data, status) { + deferred.success(data); + } + }); + }); + + test('test addErrback', function() { + expect(1); + stop(); + var d = jQuery('#main').loadxhtml('/../ajax_url0.html'); + d.addCallback(function() { + // throw an exception to start errback chain + try { + throw new Error(); + } finally { + start(); + }; + }); + d.addErrback(function() { + try { + ok(true, "errback is executed"); + } finally { + start(); + }; + }); + }); + + test('test callback / errback execution order', function() { + expect(4); + var counter = 0; + stop(); + var d = jQuery('#main').loadxhtml('/../ajax_url0.html', { + callback: function() { + try { + equals(++counter, 1); // should be executed first + } finally { + start(); + }; + } + }); + d.addCallback(function() { + equals(++counter, 2); // should be executed and break callback chain + throw new Error(); + }); + d.addCallback(function() { + // should not be executed since second callback raised an error + ok(false, "callback is executed"); + }); + d.addErrback(function() { + // should be executed after the second callback + equals(++counter, 3); + }); + d.addErrback(function() { + // should be executed after the first errback + equals(++counter, 4); + }); + }); + + test('test already included resources are ignored (ajax_url1.html)', function() { + expect(10); + var scriptsIncluded = jsSources(); + // NOTE: + equals(jQuery.inArray('http://foo.js', scriptsIncluded), -1); + equals(jQuery('head link').length, 1); + /* use endswith because in pytest context we have an absolute path */ + ok(jQuery('head link').attr('href').endswith('/qunit.css')); + stop(); + jQuery('#main').loadxhtml('/../ajax_url1.html', { + callback: function() { + var origLength = scriptsIncluded.length; + scriptsIncluded = jsSources(); + try { + // check that foo.js has been inserted in + equals(scriptsIncluded.length, origLength + 1); + equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0); + // check that
      has been removed + equals(jQuery('#main').children().length, 1); + equals(jQuery('div.ajaxHtmlHead').length, 0); + equals(jQuery('#main h1').html(), 'Hello'); + // qunit.css is not added twice + equals(jQuery('head link').length, 1); + /* use endswith because in pytest context we have an absolute path */ + ok(jQuery('head link').attr('href').endswith('/qunit.css')); + } finally { + start(); + } + } + }); + }); + + test('test synchronous request loadRemote', function() { + var res = loadRemote('/../ajaxresult.json', {}, + 'GET', true); + same(res, ['foo', 'bar']); + }); + + test('test event on CubicWeb', function() { + expect(1); + stop(); + var events = null; + jQuery(CubicWeb).bind('server-response', function() { + // check that server-response event on CubicWeb is triggered + events = 'CubicWeb'; + }); + jQuery('#main').loadxhtml('/../ajax_url0.html', { + callback: function() { + try { + equals(events, 'CubicWeb'); + } finally { + start(); + }; + } + }); + }); + + test('test event on node', function() { + expect(3); + stop(); + var nodes = []; + jQuery('#main').bind('server-response', function() { + nodes.push('node'); + }); + jQuery(CubicWeb).bind('server-response', function() { + nodes.push('CubicWeb'); + }); + jQuery('#main').loadxhtml('/../ajax_url0.html', { + callback: function() { + try { + equals(nodes.length, 2); + // check that server-response event on CubicWeb is triggered + // only once and event server-response on node is triggered + equals(nodes[0], 'CubicWeb'); + equals(nodes[1], 'node'); + } finally { + start(); + }; + } + }); + }); +}); + diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/test_htmlhelpers.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/test_htmlhelpers.html Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + +
      +

      cubicweb.htmlhelpers.js functions tests

      +

      +
        + + diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/test_htmlhelpers.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/test_htmlhelpers.js Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,36 @@ +$(document).ready(function() { + + module("module2", { + setup: function() { + $('#main').append(''); + } + }); + + test("test first selected", function() { + $('#theselect').append('' + + '' + + '' + + ''); + var selected = firstSelected(document.getElementById("theselect")); + equals(selected.value, 'bar'); + }); + + test("test first selected 2", function() { + $('#theselect').append('' + + '' + + '' + + ''); + var selected = firstSelected(document.getElementById("theselect")); + equals(selected, null); + }); + + module("visibilty"); + test('toggleVisibility', function() { + $('#main').append('
        '); + toggleVisibility('foo'); + ok($('#foo').hasClass('hidden'), 'check hidden class is set'); + }); + +}); + diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/test_utils.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/test_utils.html Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + +
        +

        cw.utils functions tests

        +

        +
          + + diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/test_utils.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/test_utils.js Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,92 @@ +$(document).ready(function() { + + module("datetime"); + + test("test full datetime", function() { + equals(cw.utils.toISOTimestamp(new Date(1986, 3, 18, 10, 30, 0, 0)), + '1986-04-18 10:30:00'); + }); + + test("test only date", function() { + equals(cw.utils.toISOTimestamp(new Date(1986, 3, 18)), '1986-04-18 00:00:00'); + }); + + test("test null", function() { + equals(cw.utils.toISOTimestamp(null), null); + }); + + module("parsing"); + test("test basic number parsing", function() { + var d = strptime('2008/08/08', '%Y/%m/%d'); + same(datetuple(d), [2008, 8, 8, 0, 0]); + d = strptime('2008/8/8', '%Y/%m/%d'); + same(datetuple(d), [2008, 8, 8, 0, 0]); + d = strptime('8/8/8', '%Y/%m/%d'); + same(datetuple(d), [8, 8, 8, 0, 0]); + d = strptime('0/8/8', '%Y/%m/%d'); + same(datetuple(d), [0, 8, 8, 0, 0]); + d = strptime('-10/8/8', '%Y/%m/%d'); + same(datetuple(d), [-10, 8, 8, 0, 0]); + d = strptime('-35000', '%Y'); + same(datetuple(d), [-35000, 1, 1, 0, 0]); + }); + + test("test custom format parsing", function() { + var d = strptime('2008-08-08', '%Y-%m-%d'); + same(datetuple(d), [2008, 8, 8, 0, 0]); + d = strptime('2008 - ! 08: 08', '%Y - ! %m: %d'); + same(datetuple(d), [2008, 8, 8, 0, 0]); + d = strptime('2008-08-08 12:14', '%Y-%m-%d %H:%M'); + same(datetuple(d), [2008, 8, 8, 12, 14]); + d = strptime('2008-08-08 1:14', '%Y-%m-%d %H:%M'); + same(datetuple(d), [2008, 8, 8, 1, 14]); + d = strptime('2008-08-08 01:14', '%Y-%m-%d %H:%M'); + same(datetuple(d), [2008, 8, 8, 1, 14]); + }); + + module("sliceList"); + test("test slicelist", function() { + var list = ['a', 'b', 'c', 'd', 'e', 'f']; + same(sliceList(list, 2), ['c', 'd', 'e', 'f']); + same(sliceList(list, 2, -2), ['c', 'd']); + same(sliceList(list, -3), ['d', 'e', 'f']); + same(sliceList(list, 0, -2), ['a', 'b', 'c', 'd']); + same(sliceList(list), list); + }); + + module("formContents", { + setup: function() { + $('#main').append('
          '); + } + }); + // XXX test fckeditor + test("test formContents", function() { + $('#test-form').append(''); + $('#test-form').append(' '); + $('#test-form').append(''); + $('#test-form').append(''); + $('#test-form').append(''); + $('#test-form').append(''); + $('#test-form').append(''); + $('#theselect').append('' + + ''); + //Append an unchecked radio input : should not be in formContents list + $('#test-form').append(''); + $('#test-form').append(''); + same(formContents($('#test-form')[0]), [ + ['input-text', 'mytextarea', 'choice', 'check', 'theselect'], + ['toto', 'Hello World!', 'no', 'no', 'foo'] + ]); + }); +}); + diff -r daada86d3d17 -r 1cad6662e199 web/test/jstests/utils.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/test/jstests/utils.js Tue Apr 09 11:56:27 2013 +0200 @@ -0,0 +1,29 @@ +function datetuple(d) { + return [d.getFullYear(), d.getMonth()+1, d.getDate(), + d.getHours(), d.getMinutes()]; +} + +function pprint(obj) { + print('{'); + for(k in obj) { + print(' ' + k + ' = ' + obj[k]); + } + print('}'); +} + +function arrayrepr(array) { + return '[' + array.join(', ') + ']'; +} + +function assertArrayEquals(array1, array2) { + if (array1.length != array2.length) { + throw new crosscheck.AssertionFailure(array1.join(', ') + ' != ' + array2.join(', ')); + } + for (var i=0; i