################################################################################
# LIBRARY:	FRM_lib
################################################################################
# $Revision: 1.3 $
# $Author: drajovic $
# $Date: 2002/12/16 18:34:11 $
# $Source: d:/Archive/CVS_QS/EMOS/EMOS_FRM_EXAMPLES/FlightDemo/Scripts/LIB/FRM_lib/script,v $
# $NoKeywords: $
################################################################################

#/***
#* This library contains generic Framework-related functions. 
#* "Generic" means that they are likely to be used in all contexts.
#*/

#/**
#* Ensures the initial state of the application. This function is the backbone of the
#* robustness startegy for the whole test suite. It will close all unwanted windows if necesary. 
#* It will (re)invoke the AUT if necessary and perform the login. By the exit of this function
#* we can assume that the main application window is displayed. This is the starting point
#* for all tests.
#*
#* @see <a href="../../images/AUT/FRM_Prolog.jpg">GUI</a>
#* @see <a href="../../images/AUT/FRM_Prolog_block.jpg">Excel</a>
#*/

public function FRM_Prolog ( in table, in test, in idx, in mode )
{
	auto rc;
	auto force;
	rc = FRM_init_block( table, test, idx, mode );
	if ( rc != E_OK ) 
		return rc;
	rc = FRM_get_next( table, test, force );  # get "Force login?" flag from table
	if ( rc == E_FRM_SKIP )
		force = "no";			# "Force login?" not defined, assume "no"
	else if ( rc != E_OK )
		return rc;
	if ( yes( force ) )
	{
		AUT_close();
	}
	else
	{	
		if ( AUT_reset() == E_OK )
		{
			return E_OK;
		}
		AUT_close();
	}
	if ( win_exists ("Login") != E_OK )
	{
		AUT_invoke();
	}
	rc =set_window( "Login", 10 );
	rc+=FRM_edit_set( table, test, "Agent Name:" );
	rc+=FRM_edit_set( table, test, "Password:" );
	rc+=button_press( "OK" );
	return rc;
}

#/**
#* This is the short version of Prolog() with the difference that it does not attempt 
#* to log in. It ensures that the Login window is open instead of the main AUT window. 
#* This was done so that we can test the login dialog on its own.
#*
#* @see <a href="../../images/AUT/FRM_Prolog2_block.jpg">Excel</a>
#*/

public function FRM_Prolog2 ( in table, in test, in idx, in mode )
{
	auto rc;
	auto force;
	static wins[] = {
		 "PopupMessage"  
		,"FlightsTable"
		,"Open Order"
		,"Search Results"
		,"Flight Reservation"
	};

	rc = FRM_init_block( table, test, idx, mode );
	if ( rc != E_OK ) 
		return rc;
	rc = FRM_get_next( table, test, force );  # get "Force login?" flag from table
	if ( rc == E_FRM_SKIP )
		force = "no";			# "Force login?" not defined, assume "no"
	else if ( rc != E_OK )
		return rc;
	if ( yes( force ) )
	{
		AUT_close();
	}
	EMOS_win_close_all( wins );
	if ( win_exists ("Login") != E_OK )
	{
		AUT_invoke();
	}
	rc =set_window( "Login", 10 );
	return rc;
}

#/**
#* @see <a href="../../images/AUT/FRM_Login.jpg">GUI</a>
#* @see <a href="../../images/AUT/FRM_Login_block.jpg">Excel</a>
#*/

public function FRM_Login ( in table, in test, in idx, in mode )
{
	auto rc;
	rc = FRM_init_block( table, test, idx, mode );
	if ( rc != E_OK ) 
		return rc;
	rc =set_window( "Login", 10 );
	rc+=FRM_edit_set( table, test, "Agent Name:" );
	rc+=FRM_edit_set( table, test, "Password:" );
	rc+=FRM_button_press( table, test, "OK" );
	rc+=FRM_button_press( table, test, "Cancel" );
	return rc;
}

#/**
#* Provides the possibility to chose an item from the main menu and toolbar. 
#*
#* @see <a href="../../images/AUT/FRM_Main.jpg">GUI</a>
#* @see <a href="../../images/AUT/FRM_Main_block.jpg">Excel</a>
#*/

public function FRM_Main ( in table, in test, in idx, in mode )
{
	auto rc;
	rc = FRM_init_block( table, test, idx, mode );
	if ( rc != E_OK ) 
		return rc;
	rc+=set_window ("Flight Reservation", 60);
	rc+=FRM_menu_select_item1( table, test );	# main menu
	rc+=FRM_button_press( table, test );			# toolbar
	return rc;
}

#/**
#* Handles the dynamic popup message dialog. The tester can define three deisions: whether
#* the dialog was expected or not, what message should be desplayed and what button to
#* use to close the dialog. Different types of message dialogs are handled with the same
#* function, e.g. OK, Yes/No, OK/Cancel. You have to make sure that the GUI-map is 
#* propeprly configured (not a trivial task).
#*/

public function FRM_PopUp_Message ( in table, in test, in idx, in mode )
{
	auto rc, usr, pwd, force;
	rc = FRM_init_block( table, test, idx, mode );
	if ( rc != E_OK ) 
		return rc;
	rc = FRM_CheckPopUpMessage( table, test, "PopupMessage", 1 );
	return rc;
}

###################
# Private Functions
###################

#/**
#* Handles the pop-up message dialog. It reads up to three lines fom the data
#* table and checks whether the dialog popped up and whether the expected
#* message matches (i.e. regular expresion) the given string. It also handles
#* situations when message window is not expected.
#* @param table (in)	index of the data table
#* @param test (in)		test
#* @param window (in)	logical name of the popup window
#* @param timeout (in)	timeout in seconds to wait for popup window
#*/

static function FRM_CheckPopUpMessage ( in table, in test, in window, in timeout )
{
	auto rc;
	auto is_win_expected;
	
	# do nothing in CHK/GEN/ATR mode
	#if ( FRM_get_mode( table ) != FRM_SET_MODE )
	#	return E_OK;

	rc=FRM_get_next( table, test, is_win_expected );	

	switch ( rc )
	{
	case E_OK:			# first value provided
	
		if( yes( is_win_expected ) )	# we expect message window to show up
			rc = process_expected_popup_win( table, test, window, timeout );
		else
			rc = process_unexpected_popup_win( table, test, window, timeout );
		return rc;
		
	case E_FRM_SKIP:	# first value not provided

		rc = ignore_popup_win( table, test, window, timeout );
		return rc;

	default:			# error retrieving first value
	
		return rc;
	}
}

static function process_expected_popup_win( in table, in test, in window, in timeout )
{
	auto rc;
	auto txt_expected;
	auto txt_actual;
	
	set_timeout( timeout );
	rc = win_exists( window, 1 );
	restore_timeout();
	if( rc != E_OK )
	{
		tl_step( "Message", FAIL, "No message window shown" );
		return E_NOT_FOUND;
	}
	tl_step( "Message", PASS, "Expected message window shown" );
	set_window( window );
	rc = FRM_get_next( table, test, txt_expected );	
	if ( rc == E_OK )		# check message if text provided
	{
		rc = static_get_text( "message", txt_actual );
		if ( rc == E_OK )
		{
			report_msg( "Message: '" & txt_actual & "'" );
			if ( match( txt_actual, txt_expected ) > 0 )
			{
				tl_step( "Message", PASS, "matches '" & txt_expected & "'" );
				rc = E_OK;
			}
			else 
			{
				tl_step( "Message", FAIL, "does not match '" & txt_expected & "'" );
				rc = E_MISMATCH;
			}
		}
		else
		{
			tl_step( "Message", FAIL, "error retrieving message text [rc=" & rc & "]" );
		}
	}
	rc += FRM_button_press( table, test );
	return rc;
}

static function process_unexpected_popup_win( in table, in test, in window, in timeout )
{
	auto rc;
	auto txt_actual;

	set_timeout( timeout );
	rc = win_exists( window, 1 );
	restore_timeout();
	if( rc != E_OK )
	{
		tl_step( "Message", PASS, "Message window not shown" );
		return E_OK;
	}
	rc = E_UNEXP_WIN;
	tl_step( "Message", FAIL, "Unexpected message window shown" );
	set_window( window );
	static_get_text( "message", txt_actual );
	tl_step( "Message", FAIL, "displayed '" & txt_actual & "'" );
	FRM_skip( table, test );  # skip the cell with expected text (irelevant in this case)
	rc += FRM_button_press( table, test );	# then press the button to close the window
	return rc;
}

static function ignore_popup_win( in table, in test, in window, in timeout )
{
	auto rc;
	auto txt_actual;

	set_timeout( timeout );
	rc = win_exists( window, 1 );
	restore_timeout();
	if( rc != E_OK )
	{
		return E_OK;
	}
	report_msg( "Popup window ignored" );
	set_window( window );
	static_get_text( "message", txt_actual );
	report_msg( "Message displayed '" & txt_actual & "'" );
	FRM_skip( table, test );  # skip the cell with expected text (irelevant in this case)
	rc += FRM_button_press( table, test );	# then press the button to close the window
	return rc;
}

