/**
 * 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 TabPanelTest extends SeleniumTestBase implements RichSeleniumTest {

    public TabPanelTest() {
        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 testTabPanelComponent() throws Exception {
        _testRichTabPanelComponent(Template.SIMPLE);
        _testRichTabPanelComponent(Template.DATA_TABLE);
        _testRichTabPanelComponent(Template.MODAL_PANEL);
    }

    private void _testRichTabPanelComponent(Template template) {
        renderPage(template);
        String parentId = getParentId() + "_form:";
        String linkId = parentId + "tab2_lbl";
        String tabId1 = parentId + "tab1";
        String tabId2 = parentId + "tab2";
        String tabId4 = parentId + "tab4";
        String inputId = parentId + "_value";
        String outputId = parentId + "_value2";

        writeStatus("Click on tab1");
        clickById(linkId);
        waitForAjaxCompletion();
        AssertValueEquals(inputId, "tab2");
        AssertTextEquals(outputId, "2");
        Assert.assertTrue(isVisibleById(tabId2));
        Assert.assertFalse(isVisibleById(tabId4));
        try {
            getTextById(tabId1);
            Assert.fail("Both of 'tab1' and 'tab2' tabs were rendered for tab panel.");
        } catch (SeleniumException se) {

        }

        writeStatus("Click on tab2");
        linkId = parentId + "tab1_lbl";
        clickCommandAndWait(linkId);
        AssertValueEquals(inputId, "tab1");
        AssertTextEquals(outputId, "1");
        Assert.assertTrue(isVisibleById(tabId1));
        Assert.assertFalse(isVisibleById(tabId4));
        try {
            getTextById(tabId2);
            Assert.fail("Both of 'tab1' and 'tab2' tabs were rendered for tab panel.");
        } catch (SeleniumException se) {

        }

        writeStatus("Click on tab3");
        linkId = parentId + "tab3_lbl";
        clickById(linkId);
        AssertValueEquals(inputId, "tab1");
        Assert.assertTrue(isVisibleById(tabId1));

        writeStatus("Click on tab4");
        linkId = parentId + "tab4_lbl";
        clickById(linkId);
        Assert.assertTrue(isVisibleById(tabId4));
        Assert.assertFalse(isVisibleById(tabId1));
        try {
            getTextById(tabId2);
            Assert.fail("Both of 'tab1' and 'tab2' tabs were rendered for tab panel.");
        } catch (SeleniumException se) {

        }
    }

    public String getTestUrl() {
        return "pages/tabPanel/tabPanelTest.xhtml";
    }

}
