/**
 * 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.SeleniumTestBase;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.SeleniumException;

public class TabPanelTest extends SeleniumTestBase {

    @Test
    public 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) {

        }
    }

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

}
