################################################################################
# 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.
#*/

#/**
#* 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/Prolog.jpg">GUI</a>
#* @see <a href="../../images/Prolog_block.jpg">Excel</a>
#*/

public function FRM_Prolog ( in table, in test, in idx, in mode )
{
	auto rc;
	auto force;
	static wins[] = {
		 "MessageBox"  
		,"SecurityAlert"
	};

	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 ("Home") != E_OK )
	{
		AUT_invoke();
		rc+=set_window( "Login", 10 );
		rc+=FRM_edit_set( table, test, "username" );
		rc+=FRM_edit_set( table, test, "password" );
		rc+=web_image_click( "login", 3, 3 );
		rc+=web_sync( 5 );
	}
	rc+=set_window( "Home", 10 );
	return rc;
}

#/**
#* @see <a href="../../images/Login.jpg">GUI</a>
#* @see <a href="../../images/Login_block.jpg">Excel</a>
#*/

public function FRM_Login ( in table, in test, in idx, inout mode )
{
	auto rc;
	rc = FRM_init_block( table, test, idx, mode );
	if ( rc != E_OK )
		return rc;
	rc+=set_window( "Login", 5 );
	rc+=FRM_edit_set( table, test, "username" );
	rc+=FRM_edit_set( table, test, "password" );
	rc+=FRM_web_image_click( table, test, "login" );
	return rc;
}

#/**
#* Provides the possibility to click on up to two images.
#* You may want to add clicks to links as well (makes sense for many AUTs).
#*
#* @see <a href="../../images/Main.jpg">GUI</a>
#* @see <a href="../../images/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 ("Home", 60);
	rc+=web_sync(5);
	rc+=FRM_web_image_click( table, test );
	rc+=web_sync(5);
	rc+=close_security_alerts();
	rc+=FRM_web_image_click( table, test );
	rc+=web_sync(5);
	rc+=close_security_alerts();
	return rc;
}

#/**
#* Provides the possibility to click on three images/buttons on left-side
#* navigation bar.
#* Note how we have solved the problem of having to click on same images
#* within different frames. We have replaced the inital set_window()
#* with web_sync() and included phisical properties instead of the logical names.
#*
#* @see <a href="../../images/Navbar.jpg">GUI</a>
#* @see <a href="../../images/Navbar_block.jpg">Excel</a>
#*/

public function FRM_Navbar ( in table, in test, in idx, inout mode )
{
	auto rc;
	rc = FRM_init_block( table, test, idx, mode );
	if ( rc != E_OK )
		return rc;
	rc+=web_sync( 5 );
	rc+=FRM_web_image_click( table, test, "{class:object, MSW_class: html_rect,html_name:\"Search Flights Button\"}" );
	rc+=FRM_web_image_click( table, test, "{class:object, MSW_class: html_rect,html_name:\"Home Button\"}" );
	rc+=FRM_web_image_click( table, test, "{class:object, MSW_class: html_rect,html_name:\"SignOff Button\"}" );
	rc+=web_sync( 5 );
	rc+=close_security_alerts();
	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, "MessageBox", 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;
}

