/**
 * 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 java.text.ParseException;
import java.util.Date;

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

public class CalendarTest extends SeleniumTestBase {

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

	String containerId = getParentId() + "_form:";
	String calendarOpenedId = containerId + "calendar";
	String calendarCollapsedId = calendarOpenedId + "Popup";
	String calendarInputDate = calendarOpenedId + "InputDate";
	String calendarPopupButton = calendarCollapsedId + "Button";
	String outputPanel = containerId + "outputPanel";

	Assert.assertFalse(isVisibleById(calendarOpenedId), "Calendar window should NOT be visible on the component!");

	writeStatus("Mouse click on calendar InputDate field");
	clickById(calendarInputDate);
	
	Assert.assertTrue(isVisibleById(calendarOpenedId), "Calendar window should be visible on the component!");

	writeStatus("Mouse click outside calendar");
	clickById(outputPanel);

	Assert.assertFalse(isVisibleById(calendarOpenedId), "Calendar window should NOT be visible on the component!");

	writeStatus("Mouse click on calendar popup button");
	clickById(calendarPopupButton);	

	Assert.assertTrue(isVisibleById(calendarOpenedId), "Calendar window should be visible on the component!");
    }

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

	Date newSelectedDate = CalendarTestBean.getDayInMay(15);

	String containerId = getParentId() + "_form:";
	String calendarOpenedId = containerId + "calendar";
	String calendarCollapsedId = calendarOpenedId + "Popup";
	String calendarInputDate = calendarOpenedId + "InputDate";
	String calendarPopupButton = calendarCollapsedId + "Button";
	String outputPanel = containerId + "outputPanel";

	Assert.assertFalse(isVisibleById(calendarOpenedId), "Calendar window should NOT be visible on the component!");
	writeStatus("Mouse click on calendar popup button");
	clickById(calendarPopupButton);	
	Assert.assertTrue(isVisibleById(calendarOpenedId), "Calendar window should be visible on the component!");

	String inputDateString = getValueById(calendarInputDate);
	Date readDate = null;
	try {
	    readDate = CalendarTestBean.DATE_FORMAT.parse(inputDateString);
	} catch(ParseException parseException) {
	    // skip exception
	}
	Assert.assertEquals(readDate, CalendarTestBean.DEFAULT_DATE, "Default date representation is wrong!");

	// click on 15th of May
	String newSelectedDateId = calendarOpenedId + "DayCell18";
	clickById(newSelectedDateId);

	writeStatus("Mouse click outside calendar");
	clickById(outputPanel);

	inputDateString = getValueById(calendarInputDate);
	try {
	    readDate = CalendarTestBean.DATE_FORMAT.parse(inputDateString);
	} catch(ParseException parseException) {
	    // skip exception
	}
	Assert.assertEquals(readDate, newSelectedDate, "Date representation after selecting 15.May.2008 is wrong!");

	Assert.assertFalse(isVisibleById(calendarOpenedId), "Calendar window should NOT be visible on the component!");	
    }

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