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

TP.dom.WebComponent.Type.describe('TP.dom.WebComponent: anonymous webcomponent - no shadow DOM',
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_button.xhtml');
            await driver.setLocation(loadURI);

            this.startTrackingSignals();
        });

    //  ---

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

            await 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 button,
            focusedElem;

        button = TP.byId('button1', windowContext);

        //  Change the focus via 'direct' method

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

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

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

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

    //  ---

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

        var button;

        //  Change the content via 'user' interaction

        button = TP.byId('button1', windowContext);

        //  Individual mousedown/mouseup

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


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

        test.getSuite().resetSignalTracking();

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

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

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

        test.getSuite().resetSignalTracking();

        //  click

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


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

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

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

    //  ---

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

        var button;

        //  Change the content via 'user' interaction

        button = TP.byId('button1', windowContext);

        //  Individual keydown/keyup

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

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

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

        test.getSuite().resetSignalTracking();

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

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

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

    //  ---

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

        var button;

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

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

        //  --- Focus

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

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

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

        test.getSuite().resetSignalTracking();

        //  --- Individual mousedown/mouseup

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

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

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

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

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

        test.getSuite().resetSignalTracking();

        //  --- click

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

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

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

        test.getSuite().resetSignalTracking();

        //  --- Individual keydown/keyup

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

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

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

        test.getSuite().resetSignalTracking();

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

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

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

TP.dom.WebComponent.Type.describe('TP.dom.WebComponent: anonymous webcomponent - no shadow DOM',
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/tibet/webcomponents/test_templateexpander.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('Rendering', function(test, options) {

        var resultSpans;

        //  Change the content via 'user' interaction

        resultSpans = TP.byCSSPath('span.result', windowContext, true);
        test.assert.isSizeOf(resultSpans, 2);
    });

}).skip();

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

TP.dom.WebComponent.Type.describe('TP.dom.WebComponent: customized built-in webcomponent - no shadow DOM',
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/tibet/webcomponents/test_expandinglist.xhtml');
            await driver.setLocation(loadURI);

            this.startTrackingSignals();
        });

    //  ---

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

            await 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('Activation - mouse', async function(test, options) {

        var ukList,
            ukTrigger,
            usList,
            usTrigger;

        //  Change the content via 'user' interaction

        ukTrigger = TP.byCSSPath('#UKTrigger > span', windowContext, true);
        ukList = TP.byId('UKList', windowContext);

        usTrigger = TP.byCSSPath('#USTrigger > span', windowContext, true);
        usList = TP.byId('USList', windowContext);

        test.refute.isVisible(ukList);
        test.refute.isVisible(usList);

        //  Individual click

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

        test.assert.isVisible(ukList);
        test.refute.isVisible(usList);

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

        test.assert.isVisible(ukList);
        test.assert.isVisible(usList);

        test.getSuite().resetSignalTracking();
    }).skip();

});

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

TP.dom.WebComponent.Type.describe('TP.dom.WebComponent: lifecycle',
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/tibet/webcomponents/test_customsquare.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('Activation - mouse', async function(test, options) {

        var addButton,
            updateButton,
            removeButton,

            customSquare;

        //  Change the content via 'user' interaction

        addButton = TP.byCSSPath('button.add', windowContext, true);
        updateButton = TP.byCSSPath('button.update', windowContext, true);
        removeButton = TP.byCSSPath('button.remove', windowContext, true);

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

        customSquare = TP.byCSSPath('custom-square', windowContext, true);
        test.assert.isVisible(customSquare);

        test.assert.isEqualTo(
            customSquare.getAttribute('l'),
            '100');
        test.assert.isEqualTo(
            customSquare.getAttribute('c'),
            'red');

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

        test.refute.isEqualTo(
            customSquare.getAttribute('l'),
            '100');
        test.refute.isEqualTo(
            customSquare.getAttribute('c'),
            'red');

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

        customSquare = TP.byCSSPath('custom-square', windowContext, true);
        test.assert.isEmpty(customSquare);
    });

});

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