//  ========================================================================
//  xctrls:checkitem
//  ========================================================================

TP.xctrls.checkitem.Type.describe('TP.xctrls.checkitem: manipulation',
function() {

    var driver,
        windowContext,

        unloadURI,
        loadURI;

    unloadURI = TP.uc(TP.sys.cfg('path.blank_page'));

    //  ---

    this.before(
        async function(suite, options) {

            driver = this.getDriver();
            windowContext = driver.get('windowContext');

            loadURI = TP.uc('~lib_test/src/xctrls/xctrls_checkitem.xhtml');
            await driver.setLocation(loadURI);

            this.startTrackingSignals();
        });

    //  ---

    this.after(
        async function(suite, options) {

            this.stopTrackingSignals();

            //  Unload the current page by setting it to the blank
            await driver.setLocation(unloadURI);

            //  Unregister the URI to avoid a memory leak
            loadURI.unregister();
        });

    //  ---

    this.afterEach(
        function(test, options) {
            this.getSuite().resetSignalTracking();
        });

    //  ---

    this.it('Focusing', async function(test, options) {

        var checkitem,
            focusedElem;

        checkitem = TP.byId('checkitem1', windowContext);

        //  Change the focus via 'direct' method

        await driver.constructSequence().
                        sendEvent(TP.hc('type', 'focus'), checkitem).
                        run();

        test.assert.hasAttribute(checkitem, 'pclass:focus');

        await test.assert.didSignal(checkitem, 'TP.sig.UIFocus');
        await test.assert.didSignal(checkitem, 'TP.sig.UIDidFocus');

        focusedElem = driver.getFocusedElement();
        test.assert.isIdenticalTo(focusedElem, checkitem);
    });

    //  ---

    this.it('Activation - mouse', async function(test, options) {

        var checkitem;

        //  Change the content via 'user' interaction

        checkitem = TP.byId('checkitem1', windowContext);

        //  Individual mousedown/mouseup

        await driver.constructSequence().
                        mouseDown(TP.LEFT, checkitem).
                        run();

        test.assert.hasAttribute(checkitem, 'pclass:active');

        await test.assert.didSignal(checkitem, 'TP.sig.UIActivate');
        await test.assert.didSignal(checkitem, 'TP.sig.UIDidActivate');

        test.getSuite().resetSignalTracking();

        await driver.constructSequence().
                        mouseUp(TP.LEFT, checkitem).
                        run();

        test.refute.hasAttribute(checkitem, 'pclass:active');

        await test.assert.didSignal(checkitem, 'TP.sig.UIDeactivate');
        await test.assert.didSignal(checkitem, 'TP.sig.UIDidDeactivate');

        test.getSuite().resetSignalTracking();

        //  click

        await driver.constructSequence().
                        click(TP.LEFT, checkitem).
                        run();

        //  Don't test the attribute here - it will already have been
        //  removed.

        await test.assert.didSignal(checkitem, 'TP.sig.UIActivate');
        await test.assert.didSignal(checkitem, 'TP.sig.UIDidActivate');

        await test.assert.didSignal(checkitem, 'TP.sig.UIDeactivate');
        await test.assert.didSignal(checkitem, 'TP.sig.UIDidDeactivate');
    });

    //  ---

    this.it('Activation - keyboard', async function(test, options) {

        var checkitem;

        //  Change the content via 'user' interaction

        checkitem = TP.byId('checkitem1', windowContext);

        //  Individual keydown/keyup

        await driver.constructSequence().
                        keyDown('[Enter]', checkitem).
                        run();

        test.assert.hasAttribute(checkitem, 'pclass:active');

        await test.assert.didSignal(checkitem, 'TP.sig.UIActivate');
        await test.assert.didSignal(checkitem, 'TP.sig.UIDidActivate');

        test.getSuite().resetSignalTracking();

        await driver.constructSequence().
                        keyUp('[Enter]', checkitem).
                        run();

        test.refute.hasAttribute(checkitem, 'pclass:active');

        await test.assert.didSignal(checkitem, 'TP.sig.UIDeactivate');
        await test.assert.didSignal(checkitem, 'TP.sig.UIDidDeactivate');
    });

    //  ---

    this.it('Disabled behavior', async function(test, options) {

        var checkitem;

        //  All of these mechanisms to 'activate' the control should fail - it's
        //  disabled.

        //  Disable it
        checkitem = TP.byId('checkitem1', windowContext);
        await checkitem.setAttrDisabled(true);

        //  --- Focus

        await driver.constructSequence().
                        sendEvent(TP.hc('type', 'focus'), checkitem).
                        run();

        test.refute.hasAttribute(checkitem, 'pclass:focus');

        await test.refute.didSignal(checkitem, 'TP.sig.UIFocus');
        await test.refute.didSignal(checkitem, 'TP.sig.UIDidFocus');

        test.getSuite().resetSignalTracking();

        //  --- Individual mousedown/mouseup

        await driver.constructSequence().
                        mouseDown(TP.LEFT, checkitem).
                        run();

        test.refute.hasAttribute(checkitem, 'pclass:active');

        await test.refute.didSignal(checkitem, 'TP.sig.UIActivate');
        await test.refute.didSignal(checkitem, 'TP.sig.UIDidActivate');

        await driver.constructSequence().
                        mouseUp(TP.LEFT, checkitem).
                        run();

        await test.refute.didSignal(checkitem, 'TP.sig.UIDeactivate');
        await test.refute.didSignal(checkitem, 'TP.sig.UIDidDeactivate');

        test.getSuite().resetSignalTracking();

        //  --- click

        await driver.constructSequence().
                        click(TP.LEFT, checkitem).
                        run();

        await test.refute.didSignal(checkitem, 'TP.sig.UIActivate');
        await test.refute.didSignal(checkitem, 'TP.sig.UIDidActivate');

        await test.refute.didSignal(checkitem, 'TP.sig.UIDeactivate');
        await test.refute.didSignal(checkitem, 'TP.sig.UIDidDeactivate');

        test.getSuite().resetSignalTracking();

        //  --- Individual keydown/keyup

        await driver.constructSequence().
                        keyDown('[Enter]', checkitem).
                        run();

        test.refute.hasAttribute(checkitem, 'pclass:active');

        await test.refute.didSignal(checkitem, 'TP.sig.UIActivate');
        await test.refute.didSignal(checkitem, 'TP.sig.UIDidActivate');

        test.getSuite().resetSignalTracking();

        await driver.constructSequence().
                        keyUp('[Enter]', checkitem).
                        run();

        await test.refute.didSignal(checkitem, 'TP.sig.UIDeactivate');
        await test.refute.didSignal(checkitem, 'TP.sig.UIDidDeactivate');
    });
});

//  ------------------------------------------------------------------------

TP.xctrls.checkitem.Type.describe('TP.xctrls.checkitem: get/set value - no multiple',
function() {

    var driver,
        windowContext,

        unloadURI,
        loadURI,

        testData;

    unloadURI = TP.uc(TP.sys.cfg('path.blank_page'));

    //  ---

    this.before(
        async function(suite, options) {

            driver = this.getDriver();
            windowContext = driver.get('windowContext');

            TP.$$setupCommonObjectValues();
            testData = TP.$$commonObjectValues;

            loadURI = TP.uc('~lib_test/src/xctrls/xctrls_checkitem.xhtml');
            await driver.setLocation(loadURI);
        });

    //  ---

    this.after(
        async function(suite, options) {

            //  Unload the current page by setting it to the blank
            await driver.setLocation(unloadURI);

            //  Unregister the URI to avoid a memory leak
            loadURI.unregister();
        });

    //  ---

    this.it('xctrls:checkitem - setting value to scalar values', async function(test, options) {

        var tpElem,
            value;

        tpElem = TP.byId('testGroup1', windowContext);

        //  undefined
        await tpElem.set('value', testData.at(TP.UNDEF));
        value = tpElem.get('value');
        test.assert.isNull(value);

        //  null
        await tpElem.set('value', testData.at(TP.NULL));
        value = tpElem.get('value');
        test.assert.isNull(value);

        //  String
        await tpElem.set('value', testData.at('String'));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.str(testData.at('String')));

        //  Number
        await tpElem.set('value', testData.at('Number'));
        value = tpElem.get('value');
        test.assert.isNull(value);

        //  Boolean
        await tpElem.set('value', testData.at('Boolean'));
        value = tpElem.get('value');
        test.assert.isNull(value);
    });

    //  ---

    this.it('xctrls:checkitem - setting value to complex object values', async function(test, options) {

        var tpElem,
            value;

        tpElem = TP.byId('testGroup1', windowContext);

        //  RegExp
        await tpElem.set('value', testData.at('RegExp'));
        value = tpElem.get('value');
        test.assert.isNull(value);

        //  Date
        await tpElem.set('value', testData.at('Date'));
        value = tpElem.get('value');
        test.assert.isNull(value);

        //  Array
        await tpElem.set('value', TP.ac('foo', 'bar', 'baz'));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'foo');

        //  Object
        await tpElem.set('value',
            {
                foo: 'baz'
            });
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'baz');

        //  TP.core.Hash
        await tpElem.set('value', TP.hc('foo', 'bar'));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'bar');
    });

    //  ---

    this.it('xctrls:checkitem - setting value to markup', async function(test, options) {

        var tpElem,
            value;

        tpElem = TP.byId('testGroup1', windowContext);

        //  XMLDocument
        await tpElem.set('value', TP.nodeCloneNode(testData.at('XMLDocument')));
        value = tpElem.get('value');
        test.assert.isNull(value);

        //  XMLElement
        await tpElem.set('value', TP.nodeCloneNode(testData.at('XMLElement')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'bar');

        //  AttributeNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('AttributeNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'bar');

        //  TextNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('TextNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'foo');

        //  CDATASectionNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('CDATASectionNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'foo');

        //  PINode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('PINode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'bar');

        //  CommentNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('CommentNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'foo');

        //  DocumentFragmentNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('DocumentFragmentNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'foo');

        //  NodeList
        await tpElem.set('value', testData.at('NodeList'));
        value = tpElem.get('value');
        test.assert.isNull(value);

        //  NamedNodeMap
        await tpElem.set('value', testData.at('NamedNodeMap'));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, 'baz');
    });
});

//  ------------------------------------------------------------------------

TP.xctrls.checkitem.Type.describe('TP.xctrls.checkitem: get/set value - multiple',
function() {

    var driver,
        windowContext,

        unloadURI,
        loadURI,

        testData;

    unloadURI = TP.uc(TP.sys.cfg('path.blank_page'));

    //  ---

    this.before(
        async function(suite, options) {

            driver = this.getDriver();
            windowContext = driver.get('windowContext');

            TP.$$setupCommonObjectValues();
            testData = TP.$$commonObjectValues;

            loadURI = TP.uc('~lib_test/src/xctrls/xctrls_checkitem.xhtml');
            await driver.setLocation(loadURI);
        });

    //  ---

    this.beforeEach(
        async function(test, options) {

            var tpElem;

            //  Make sure that each test starts with a freshly reset item
            tpElem = TP.byId('testGroup2', windowContext);
            await tpElem.deselectAll();
        });

    //  ---

    this.after(
        async function(suite, options) {

            //  Unload the current page by setting it to the blank
            await driver.setLocation(unloadURI);

            //  Unregister the URI to avoid a memory leak
            loadURI.unregister();
        });

    //  ---

    this.it('xctrls:checkitem - setting value to scalar values', async function(test, options) {

        var tpElem,
            value;

        tpElem = TP.byId('testGroup2', windowContext);

        //  undefined
        await tpElem.set('value', testData.at(TP.UNDEF));
        value = tpElem.get('value');
        test.assert.isEmpty(value);

        //  null
        await tpElem.set('value', testData.at(TP.NULL));
        value = tpElem.get('value');
        test.assert.isEmpty(value);

        //  String
        await tpElem.set('value', testData.at('String'));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('bar'));

        //  reset
        await tpElem.deselectAll();

        //  String (multiple)
        await tpElem.set('value', 'foo;baz');
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('foo', 'baz'));

        //  reset
        await tpElem.deselectAll();

        //  Number
        await tpElem.set('value', testData.at('Number'));
        value = tpElem.get('value');
        test.assert.isEmpty(value);

        //  Boolean
        await tpElem.set('value', testData.at('Boolean'));
        value = tpElem.get('value');
        test.assert.isEmpty(value);
    });

    //  ---

    this.it('xctrls:checkitem - setting value to complex object values', async function(test, options) {

        var tpElem,
            value;

        tpElem = TP.byId('testGroup2', windowContext);

        //  RegExp
        await tpElem.set('value', testData.at('RegExp'));
        value = tpElem.get('value');
        test.assert.isEmpty(value);

        //  Date
        await tpElem.set('value', testData.at('Date'));
        value = tpElem.get('value');
        test.assert.isEmpty(value);

        //  Array
        await tpElem.set('value', TP.ac('foo', 'bar', 'baz'));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('foo', 'bar', 'baz'));

        //  reset
        await tpElem.deselectAll();

        //  Object
        await tpElem.set('value',
            {
                foo: 'baz'
            });
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('baz'));

        //  reset
        await tpElem.deselectAll();

        //  TP.core.Hash
        await tpElem.set('value', TP.hc('foo', 'bar'));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('bar'));
    });

    //  ---

    this.it('xctrls:checkitem - setting value to markup', async function(test, options) {

        var tpElem,
            value;

        tpElem = TP.byId('testGroup2', windowContext);

        //  XMLDocument
        await tpElem.set('value', TP.nodeCloneNode(testData.at('XMLDocument')));
        value = tpElem.get('value');
        test.assert.isEmpty(value);

        //  XMLElement
        await tpElem.set('value', TP.nodeCloneNode(testData.at('XMLElement')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('bar'));

        //  reset
        await tpElem.deselectAll();

        //  AttributeNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('AttributeNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('bar'));

        //  reset
        await tpElem.deselectAll();

        //  TextNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('TextNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('foo'));

        //  reset
        await tpElem.deselectAll();

        //  CDATASectionNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('CDATASectionNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('foo'));

        //  reset
        await tpElem.deselectAll();

        //  PINode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('PINode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('bar'));

        //  reset
        await tpElem.deselectAll();

        //  CommentNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('CommentNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('foo'));

        //  reset
        await tpElem.deselectAll();

        //  DocumentFragmentNode
        await tpElem.set('value', TP.nodeCloneNode(testData.at('DocumentFragmentNode')));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('foo', 'bar'));

        //  NodeList
        await tpElem.set('value', testData.at('NodeList'));
        value = tpElem.get('value');
        test.assert.isEmpty(value);

        //  NamedNodeMap
        await tpElem.set('value', testData.at('NamedNodeMap'));
        value = tpElem.get('value');
        test.assert.isEqualTo(value, TP.ac('baz'));
    });
});

//  ------------------------------------------------------------------------

TP.xctrls.checkitem.Type.describe('TP.xctrls.checkitem: selection management - no multiple',
function() {

    var driver,
        windowContext,

        unloadURI,
        loadURI;

    unloadURI = TP.uc(TP.sys.cfg('path.blank_page'));

    //  ---

    this.before(
        async function(suite, options) {

            driver = this.getDriver();
            windowContext = driver.get('windowContext');

            TP.$$setupCommonObjectValues();

            loadURI = TP.uc('~lib_test/src/xctrls/xctrls_checkitem.xhtml');
            await driver.setLocation(loadURI);
        });

    //  ---

    this.after(
        async function(suite, options) {

            //  Unload the current page by setting it to the blank
            await driver.setLocation(unloadURI);

            //  Unregister the URI to avoid a memory leak
            loadURI.unregister();
        });

    //  ---

    this.it('xctrls:checkitem - addSelection', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup1', windowContext);

        //  ---

        //  allowsMultiples

        //  radio elements do *not* allow multiples
        test.assert.isFalse(tpElem.allowsMultiples());

        //  ---

        //  (property defaults to 'value')
        await tpElem.addSelection('baz');
        test.assert.isFalse(TP.byId('datacheckitem1', windowContext).isSelected());
        test.assert.isFalse(TP.byId('datacheckitem2', windowContext).isSelected());
        test.assert.isTrue(TP.byId('datacheckitem3', windowContext).isSelected());

        //  'value' property
        await tpElem.addSelection('bar', 'value');
        test.assert.isFalse(TP.byId('datacheckitem1', windowContext).isSelected());
        test.assert.isTrue(TP.byId('datacheckitem2', windowContext).isSelected());
        test.assert.isFalse(TP.byId('datacheckitem3', windowContext).isSelected());
    });

    //  ---

    this.it('xctrls:datacheckitem - removeSelection', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup1', windowContext);

        await tpElem.addSelection('bar');

        //  (property defaults to 'value')
        await tpElem.removeSelection('baz');
        test.assert.isFalse(TP.byId('datacheckitem1', windowContext).isSelected());
        test.assert.isTrue(TP.byId('datacheckitem2', windowContext).isSelected());
        test.assert.isFalse(TP.byId('datacheckitem3', windowContext).isSelected());

        await tpElem.removeSelection('baz');
        test.assert.isFalse(TP.byId('datacheckitem3', windowContext).isSelected());

        //  'value' property
        await tpElem.removeSelection('bar', 'value');
        test.assert.isFalse(TP.byId('datacheckitem1', windowContext).isSelected());
        test.assert.isFalse(TP.byId('datacheckitem2', windowContext).isSelected());

        //  NB: This is different from XHTML in that we can have a checkitem
        //  with 'no selection'
        test.assert.isFalse(TP.byId('datacheckitem3', windowContext).isSelected());

        await tpElem.removeSelection('bar', 'value');
        test.assert.isFalse(TP.byId('datacheckitem2', windowContext).isSelected());
    });

    //  ---

    this.it('xctrls:datacheckitem - select', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup1', windowContext);

        //  (property defaults to 'value')
        await tpElem.select('bar');
        test.assert.isFalse(TP.byId('datacheckitem1', windowContext).isSelected());
        test.assert.isTrue(TP.byId('datacheckitem2', windowContext).isSelected());
        test.assert.isFalse(TP.byId('datacheckitem3', windowContext).isSelected());

        await tpElem.select('baz');
        test.assert.isFalse(TP.byId('datacheckitem1', windowContext).isSelected());
        test.assert.isFalse(TP.byId('datacheckitem2', windowContext).isSelected());
        test.assert.isTrue(TP.byId('datacheckitem3', windowContext).isSelected());
    });

});

//  ------------------------------------------------------------------------

TP.xctrls.checkitem.Type.describe('TP.xctrls.checkitem: selection management - multiple',
function() {

    var driver,
        windowContext,

        unloadURI,
        loadURI,

        getSelectedIndices;

    unloadURI = TP.uc(TP.sys.cfg('path.blank_page'));

    getSelectedIndices = function() {

        var groupElem,
            checkboxIndices;

        groupElem = TP.byId('testGroup2', windowContext);

        checkboxIndices = groupElem.get('xctrls|checkitem').collect(
                            function(valueTPElem, anIndex) {

                                if (valueTPElem.hasAttribute(
                                                    'pclass:checked')) {
                                    return anIndex;
                                }
                            });

        //  Removes nulls and undefineds
        return checkboxIndices.compact();
    };

    //  ---

    this.before(
        async function(suite, options) {

            driver = this.getDriver();
            windowContext = driver.get('windowContext');

            TP.$$setupCommonObjectValues();

            loadURI = TP.uc('~lib_test/src/xctrls/xctrls_checkitem.xhtml');
            await driver.setLocation(loadURI);
        });

    //  ---

    this.beforeEach(
        async function(test, options) {

            var tpElem;

            //  Make sure that each test starts with a freshly reset item
            tpElem = TP.byId('testGroup2', windowContext);
            await tpElem.deselectAll();
        });

    //  ---

    this.after(
        async function(suite, options) {

            //  Unload the current page by setting it to the blank
            await driver.setLocation(unloadURI);

            //  Unregister the URI to avoid a memory leak
            loadURI.unregister();
        });

    //  ---

    this.it('xctrls:checkitem - addSelection', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup2', windowContext);

        //  ---

        //  allowsMultiples

        //  checkbox elements allow multiples
        test.assert.isTrue(tpElem.allowsMultiples());

        //  ---

        //  (property defaults to 'value')
        await tpElem.deselectAll();
        await tpElem.addSelection(TP.ac('bar', 'baz'));
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(1, 2));

        //  'value' property
        await tpElem.deselectAll();
        await tpElem.addSelection(TP.ac('foo', 'bar'), 'value');
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(0, 1));
    });

    //  ---

    this.it('xctrls:checkitem - removeSelection', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup2', windowContext);

        //  (property defaults to 'value')
        await tpElem.deselectAll();
        await tpElem.addSelection(TP.ac('foo', 'bar'));
        await tpElem.removeSelection('baz');
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(0, 1));

        await tpElem.deselectAll();
        await tpElem.addSelection(TP.ac('bar', 'baz'));
        await tpElem.removeSelection('baz');
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(1));

        //  'value' property
        await tpElem.deselectAll();
        await tpElem.addSelection(TP.ac('foo', 'baz'));
        await tpElem.removeSelection('bar', 'value');
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(0, 2));

        await tpElem.deselectAll();
        await tpElem.addSelection(TP.ac('bar', 'baz'));
        await tpElem.removeSelection('bar', 'value');
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(2));
    });

    //  ---

    this.it('xctrls:checkitem - selectAll', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup2', windowContext);

        await tpElem.selectAll();
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(0, 1, 2));
    });

    //  ---

    this.it('xctrls:checkitem - select', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup2', windowContext);

        await tpElem.deselectAll();
        await tpElem.select('bar');
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(1));
        await tpElem.select('baz');
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(1, 2));

        await tpElem.deselectAll();
        await tpElem.select(TP.ac('foo', 'baz'));
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(0, 2));
    });

    //  ---

    this.it('xctrls:checkitem - select with RegExp', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup2', windowContext);

        await tpElem.deselectAll();
        await tpElem.select(/ba/);
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(1, 2));
    });

    //  ---

    this.it('xctrls:checkitem - deselect', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup2', windowContext);

        await tpElem.selectAll();
        await tpElem.deselect('bar');
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(0, 2));
        await tpElem.deselect('baz');
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(0));

        await tpElem.selectAll();
        await tpElem.deselect(TP.ac('foo', 'baz'));
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(1));
    });

    //  ---

    this.it('xctrls:checkitem - deselect with RegExp', async function(test, options) {

        var tpElem;

        tpElem = TP.byId('testGroup2', windowContext);

        await tpElem.selectAll();
        await tpElem.deselect(/ba/);
        test.assert.isEqualTo(getSelectedIndices(tpElem), TP.ac(0));
    });

});

//  ------------------------------------------------------------------------

TP.xctrls.checkitem.Type.describe('TP.xctrls.checkitem: data binding - no multiple',
function() {

    var driver,
        windowContext,

        unloadURI,
        loadURI;

    unloadURI = TP.uc(TP.sys.cfg('path.blank_page'));

    //  ---

    this.before(
        async function(suite, options) {

            driver = this.getDriver();
            windowContext = driver.get('windowContext');

            loadURI = TP.uc('~lib_test/src/xctrls/xctrls_checkitem.xhtml');
            await driver.setLocation(loadURI);
        });

    //  ---

    this.after(
        async function(suite, options) {

            //  Unload the current page by setting it to the blank
            await driver.setLocation(unloadURI);

            //  Unregister the URI to avoid a memory leak
            loadURI.unregister();
        });

    //  ---

    this.it('xctrls:checkitem - initial setup', function(test, options) {

        var tpElem,

            modelObj;

        tpElem = TP.byId('testGroup3', windowContext);

        modelObj = TP.uc('urn:tibet:test_data').getResource().get('result');

        test.assert.isEqualTo(
            tpElem.get('value'),
            'bar');

        test.assert.isEqualTo(
            TP.val(modelObj.get('selection_set_1')),
            'bar');
    });

    //  ---

    this.it('xctrls:checkitem - change value via user interaction', async function(test, options) {

        var tpElem,

            modelObj,
            datacheckitem7;

        tpElem = TP.byId('testGroup3', windowContext);

        modelObj = TP.uc('urn:tibet:test_data').getResource().get('result');

        //  Change the content via 'user' interaction

        datacheckitem7 = TP.byId('datacheckitem7', windowContext);

        await driver.constructSequence().
                        click(TP.LEFT, datacheckitem7).
                        run();

        test.assert.isEqualTo(
            tpElem.get('value'),
            'foo');

        test.assert.isEqualTo(
            TP.val(modelObj.get('selection_set_1')),
            'foo');
    });

});

//  ------------------------------------------------------------------------

TP.xctrls.checkitem.Type.describe('TP.xctrls.checkitem: data binding - multiple',
function() {

    var driver,
        windowContext,

        unloadURI,
        loadURI;

    unloadURI = TP.uc(TP.sys.cfg('path.blank_page'));

    //  ---

    this.before(
        async function(suite, options) {

            driver = this.getDriver();
            windowContext = driver.get('windowContext');

            loadURI = TP.uc('~lib_test/src/xctrls/xctrls_checkitem.xhtml');
            await driver.setLocation(loadURI);
        });

    //  ---

    this.after(
        async function(suite, options) {

            //  Unload the current page by setting it to the blank
            await driver.setLocation(unloadURI);

            //  Unregister the URI to avoid a memory leak
            loadURI.unregister();
        });

    //  ---

    this.it('xctrls:checkitem - initial setup', function(test, options) {

        var tpElem,

            modelObj;

        tpElem = TP.byId('testGroup4', windowContext);

        modelObj = TP.uc('urn:tibet:test_data').getResource().get('result');

        test.assert.isEqualTo(
            tpElem.get('value'),
            TP.ac('foo', 'baz'));

        test.assert.isEqualTo(
            TP.val(modelObj.get('selection_set_2')),
            TP.ac('foo', 'baz'));
    });

    //  ---

    this.it('xctrls:checkitem - change value via user interaction', async function(test, options) {

        var tpElem,

            modelObj,
            datacheckitem11,
            datacheckitem12;

        tpElem = TP.byId('testGroup4', windowContext);

        modelObj = TP.uc('urn:tibet:test_data').getResource().get('result');

        //  Change the content via 'user' interaction

        datacheckitem11 = TP.byId('datacheckitem11', windowContext);

        await driver.constructSequence().
                        click(TP.LEFT, datacheckitem11).
                        run();

        test.assert.isEqualTo(
            tpElem.get('value'),
            TP.ac('foo', 'bar', 'baz'));

        test.assert.isEqualTo(
            TP.val(modelObj.get('selection_set_2')),
            TP.ac('foo', 'bar', 'baz'));

        datacheckitem12 = TP.byId('datacheckitem12', windowContext);

        await driver.constructSequence().
                        click(TP.LEFT, datacheckitem12).
                        run();

        test.assert.isEqualTo(
            tpElem.get('value'),
            TP.ac('foo', 'bar'));

        test.assert.isEqualTo(
            TP.val(modelObj.get('selection_set_2')),
            TP.ac('foo', 'bar'));
    });

});

//  ------------------------------------------------------------------------
//  end
//  ========================================================================
