package org.richfaces.testng;

import org.ajax4jsf.template.Template;
import org.richfaces.SeleniumTestBase;
import org.testng.Assert;
import org.testng.annotations.Test;

public class InplaceSelectTest extends SeleniumTestBase {

    @Test
    public void testInplaceSelectComponent(Template template) {
        renderPage(template);

        String parentId = getParentId() + "_form:";
        String inplaceSelectId = parentId + "inplaceSelect";
        String okButton = parentId + "inplaceSelect" + "ok";
        //String inplaceTmpValueElemId = parentId + "inplaceSelect" + "inplaceTmpValue";
        String cancelButton = parentId + "inplaceSelect" + "cancel";
        String popupId = "list" + inplaceSelectId;

        writeStatus("click the component");

        clickById(inplaceSelectId);
        Assert.assertFalse(isVisibleById(popupId), "Component pop-up should not show up on solitary click");

        writeStatus("double click the component");

        selenium.doubleClick(inplaceSelectId);
        Assert.assertTrue(isVisibleById(popupId), "Component pop-up should show up on double click");

        writeStatus("Check that controls buttons are present");

        Assert.assertTrue(isPresentById(okButton));
        Assert.assertTrue(isPresentById(cancelButton));

        writeStatus("Select second element"); //Birch

        selenium.mouseMove("xpath=//div[@id='list" + inplaceSelectId + "']/span[2]");
        selenium.mouseDown(okButton);

        writeStatus("Check that a new element is selected");

        AssertValueEquals(inplaceSelectId + "inplaceValue", "Birch");

        writeStatus("Select another element"); // Aspen

        selenium.doubleClick(inplaceSelectId);

        selenium.mouseMove("xpath=//div[@id='list" + inplaceSelectId + "']/span[3]");
        selenium.mouseDown(cancelButton);

        writeStatus("Cancel selected value.");
        AssertValueEquals(inplaceSelectId + "inplaceValue", "Birch", "A value has not to be changed");

        writeStatus("Verify javascript event triggering");

        runScript("theLatestEvent='undefined'");

        writeStatus("Double click the component");
        selenium.doubleClick(inplaceSelectId);

        Assert.assertEquals(runScript("theLatestEvent"), "editactivated", "editactivated event has not been triggered");

        writeStatus("Stop editing with ok");
        selenium.mouseDown(okButton);
        Assert.assertEquals(runScript("theLatestEvent"), "viewactivated", "viewactivated event has not been triggered");

        writeStatus("Stop editing with cancel");
        selenium.mouseDown(cancelButton);
        Assert.assertEquals(runScript("theLatestEvent"), "viewactivated", "viewactivated event has not been triggered");
    }

    public String getTestUrl() {
        return "pages/inplaceSelect/inplaceSelectTest.xhtml";
    }

}
