/**
 * 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;

public class PanelBarTest extends SeleniumTestBase implements RichSeleniumTest {

    public PanelBarTest() {
        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 testPanelBarComponent() throws Exception {
        _testPanelBarComponent(Template.SIMPLE);
        _testPanelBarComponent(Template.DATA_TABLE);
        _testPanelBarComponent(Template.MODAL_PANEL);
    }

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

        String label2 = parentId + "label2";
        String label3 = parentId + "label3";

        String output1 = parentId + "output1";
        String output2 = parentId + "output2";
        String output3 = parentId + "output3";

        String ajaxButton = parentId + "button_ajax";
        String simpleButton = parentId + "button_simple";

        writeStatus("Click on label two. It should be opened.");
        clickById(label2);
        Assert.assertFalse(isVisibleById(output1));
        Assert.assertTrue(isVisibleById(output2));
        Assert.assertFalse(isVisibleById(output3));

        writeStatus("Click on simple commandButton. Opened states should not change after page reloaded.");
        clickCommandAndWait(simpleButton);
        Assert.assertFalse(isVisibleById(output1));
        Assert.assertTrue(isVisibleById(output2));
        Assert.assertFalse(isVisibleById(output3));

        writeStatus("Click on label three. It should be opened.");
        clickById(label3);
        Assert.assertFalse(isVisibleById(output1));
        Assert.assertFalse(isVisibleById(output2));
        Assert.assertTrue(isVisibleById(output3));

        writeStatus("Click on ajax commandButton. Opened states should not change.");
        clickById(ajaxButton);
        waitForAjaxCompletion();
        Assert.assertFalse(isVisibleById(output1));
        Assert.assertFalse(isVisibleById(output2));
        Assert.assertTrue(isVisibleById(output3));
    }

    public String getTestUrl() {
        return "pages/panelBar/panelBarTest.xhtml";
    }

}
