/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package org.richfaces.testng;

import org.ajax4jsf.template.Template;
import org.richfaces.RichSeleniumTest;
import org.richfaces.SeleniumTestBase;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.SeleniumException;

public class TogglePanelTest extends SeleniumTestBase implements RichSeleniumTest {

    public TogglePanelTest() {
        super("http", "localhost", serverPort);
    }

    /**
     * This method are invoking before selenium tests started
     */
    @BeforeMethod
    @Parameters({"browser", "filterPrefix"})
    public void startSelenium(String browser, String filterPrefix) {
    	super.startSelenium(browser, filterPrefix);
    }
    
    
    @BeforeMethod
    @Parameters({"loadStyleStrategy", "loadScriptStrategy"})
    protected void loadConfiguration(String loadStyleStrategy, String loadScriptStrategy) throws Exception {
	super.loadConfiguration(loadStyleStrategy, loadScriptStrategy);
    }

    /**
     * This method are invoking after selenium tests completed
     */
    @AfterMethod(alwaysRun=true)
    public void stopSelenium() {
        super.stopSelenium();
    }

    @Test
    public void testTogglePanelComponent() throws Exception {
        _testTogglePanelComponent(Template.SIMPLE);
        _testTogglePanelComponent(Template.DATA_TABLE);
        _testTogglePanelComponent(Template.MODAL_PANEL);
    }

    private void _testTogglePanelComponent(Template template) {
        renderPage(template);
        String parentId = getParentId() + "_form:";

        String inputId = parentId + "_value";
        String outputId = parentId + "_value2";

        String controlNext = parentId + "ajax_next";
        String controlOne = parentId + "ajax_one";
        String controlTwo = parentId + "ajax_two";

        String oneFacet = parentId + "ajax_state_one";
        String twoFacet = parentId + "ajax_state_two";

        String cleanButton = parentId + "clean";

        writeStatus("Click on ajax controlNext");
        clickById(controlNext);
        waitForAjaxCompletion();
        // pause(6000, "panel_ajax");
        AssertValueEquals(inputId, "ajax_next");
        AssertTextEquals(outputId, "1");
        Assert.assertTrue(isVisibleById(twoFacet));
        try {
            getTextById(oneFacet);
            Assert.fail("Both of 'one' and 'two' states were rendered for ajax toggle panel.");
        } catch (SeleniumException se) {

        }

        writeStatus("Click on ajax controlOne");
        clickById(controlOne);
        waitForAjaxCompletion(5000);
        AssertValueEquals(inputId, "ajax_one");
        AssertTextEquals(outputId, "2");
        Assert.assertTrue(isVisibleById(oneFacet));
        try {
            getTextById(twoFacet);
            Assert.fail("Both of 'one' and 'two' states were rendered for ajax toggle panel.");
        } catch (SeleniumException se) {

        }

        writeStatus("Click on ajax controlTwo");
        clickById(controlTwo);
        waitForAjaxCompletion(5000);
        AssertValueEquals(inputId, "ajax_two");
        AssertTextEquals(outputId, "1");
        Assert.assertTrue(isVisibleById(twoFacet));
        try {
            getTextById(oneFacet);
            Assert.fail("Both of 'one' and 'two' states were rendered for ajax toggle panel.");
        } catch (SeleniumException se) {

        }

        controlNext = parentId + "server_next";
        controlOne = parentId + "server_one";
        controlTwo = parentId + "server_two";
        oneFacet = parentId + "server_state_one";
        twoFacet = parentId + "server_state_two";

        writeStatus("Click on controlNext");
        clickCommandAndWait(controlNext);
        AssertValueEquals(inputId, "server_next");
        AssertTextEquals(outputId, "2");
        Assert.assertTrue(isVisibleById(twoFacet));
        try {
            getTextById(oneFacet);
            Assert.fail("Both of 'one' and 'two' states were rendered for server toggle panel.");
        } catch (SeleniumException se) {

        }

        writeStatus("Click on server controlOne");
        clickCommandAndWait(controlOne);
        AssertValueEquals(inputId, "server_one");
        AssertTextEquals(outputId, "3");
        Assert.assertTrue(isVisibleById(oneFacet));
        try {
            getTextById(twoFacet);
            Assert.fail("Both of 'one' and 'two' states were rendered for server toggle panel.");
        } catch (SeleniumException se) {

        }

        writeStatus("Click on server controlTwo");
        clickCommandAndWait(controlTwo);
        AssertValueEquals(inputId, "server_two");
        AssertTextEquals(outputId, "4");
        Assert.assertTrue(isVisibleById(twoFacet));
        try {
            getTextById(oneFacet);
            Assert.fail("Both of 'one' and 'two' states were rendered for server toggle panel.");
        } catch (SeleniumException se) {

        }

        controlNext = parentId + "client_next";
        controlOne = parentId + "client_one";
        controlTwo = parentId + "client_two";
        oneFacet = parentId + "client_state_one";
        twoFacet = parentId + "client_state_two";

        writeStatus("Click on client controlNext");
        clickById(controlNext);
        Assert.assertTrue(isVisibleById(twoFacet));
        Assert.assertFalse(isVisibleById(oneFacet));

        writeStatus("Click on client controlOne");
        clickById(controlOne);
        Assert.assertTrue(isVisibleById(oneFacet));
        Assert.assertFalse(isVisibleById(twoFacet));

        writeStatus("Click on client controlTwo");
        clickById(controlTwo);
        Assert.assertTrue(isVisibleById(twoFacet));
        Assert.assertFalse(isVisibleById(oneFacet));

        writeStatus("Clean bean values");
        clickById(cleanButton);
        waitForAjaxCompletion();
    }

    public String getTestUrl() {
        return "pages/togglePanel/togglePanelTest.xhtml";
    }

}
