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

public class ContextMenuTest extends SeleniumTestBase {

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

        String parentId = getParentId() + "_form:";
        String inputId = parentId + "input";
        String input2Id = parentId + "input2";
        String outputId = parentId + "_value2";
        String menuId = parentId + "menu_input_menu";
        String menu2Id = parentId + "menu_input2_menu";

        String menu1_item_ajax = parentId + "menu1_item1";
        String menu1_item_server = parentId + "menu1_item2";
        String menu2_item_ajax = parentId + "menu2_item1";
        String menu2_item_server = parentId + "menu2_item2";

        writeStatus("Click on first input");
        clickById(inputId);
        antifast(menuId);
        Assert.assertTrue(isVisibleById(menuId));

        writeStatus("Click on second input");
        clickById(input2Id);
        antifast(menu2Id);
        Assert.assertFalse(isVisibleById(menuId));
        Assert.assertTrue(isVisibleById(menu2Id));

        writeStatus("Click on first input again");
        clickById(inputId);
        antifast(menuId);
        Assert.assertTrue(isVisibleById(menuId));
        Assert.assertFalse(isVisibleById(menu2Id));

        writeStatus("Click on ajax item in first menu");
        clickAjaxCommandAndWait(menu1_item_ajax);
        AssertValueEquals(inputId, "menu1_item1");
        AssertTextEquals(outputId, "1");

        writeStatus("Click on server item in first menu");
        clickCommandAndWait(menu1_item_server);
        AssertValueEquals(inputId, "menu1_item2");
        AssertTextEquals(outputId, "2");

        writeStatus("Click on second input again");
        clickById(input2Id);
        antifast(menu2Id);
        Assert.assertTrue(isVisibleById(menu2Id));

        writeStatus("Click on ajax item in first menu");
        clickAjaxCommandAndWait(menu2_item_ajax);
        AssertValueEquals(inputId, "menu2_item1");
        AssertTextEquals(outputId, "3");

        writeStatus("Click on server item in second menu");
        clickCommandAndWait(menu2_item_server);
        AssertValueEquals(inputId, "menu2_item2");
        AssertTextEquals(outputId, "4");
    }

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

    private void antifast(String id) {
        waiteForCondition("document.getElementById('" + id + "') != undefined", 500);
    }

}
