Commit c7432cda authored by Joao Oliveira's avatar Joao Oliveira

Implemented support for AFSpecial_Format and AFSpecial_Keystroke

parent 052662d7
Pipeline #6514 passed with stage
in 17 minutes and 16 seconds
......@@ -22,6 +22,7 @@ if(Poppler_Qt5_FOUND)
TEST_NAME "kjsfunctionstest"
LINK_LIBRARIES Qt5::Widgets Qt5::Test okularcore
)
ecm_add_test(formattest.cpp
TEST_NAME "formattest"
LINK_LIBRARIES Qt5::Widgets Qt5::Test okularcore
......
......@@ -28,6 +28,7 @@ private slots:
void initTestCase();
void cleanupTestCase();
void testTimeFormat();
void testSpecialFormat();
private:
Okular::Document *m_document;
......@@ -135,6 +136,79 @@ void FormatTest::testTimeFormat()
QCOMPARE( QStringLiteral( "1:20:00 pm" ), m_formattedText );
}
void FormatTest::testSpecialFormat()
{
Okular::FormFieldText *fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ QStringLiteral( "CEP" ) ] );
// This field will just become itself, so we test only if keystroke worked.
fft->setText( QStringLiteral( "12345" ) );
bool ok = false;
m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok );
QCOMPARE( true, ok );
fft->setText( QStringLiteral( "123456" ) );
ok = false;
m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok );
m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft );
QCOMPARE( false, ok );
fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ QStringLiteral( "8Digits" ) ] );
fft->setText( QStringLiteral( "123456789" ) );
ok = false;
m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok );
m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft );
QCOMPARE( true, ok );
QCOMPARE( m_formattedText, QStringLiteral( "12345-6789" ) );
fft->setText( QStringLiteral( "1234567890" ) );
ok = false;
m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok );
m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft );
QCOMPARE( false, ok );
QCOMPARE( m_formattedText, QStringLiteral( "12345-6789" ) );
fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ QStringLiteral( "telefone" ) ] );
fft->setText( QStringLiteral( "1234567890" ) );
ok = false;
m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok );
m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft );
QCOMPARE( true, ok );
QCOMPARE( m_formattedText, QStringLiteral("(123) 456-7890" ) );
fft->setText( QStringLiteral( "12345678900" ) );
ok = false;
m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok );
m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft );
QCOMPARE( false, ok );
QCOMPARE( m_formattedText, QStringLiteral("(123) 456-7890" ) );
fft = reinterpret_cast< Okular::FormFieldText * >( m_fields[ QStringLiteral( "CPF" ) ] );
fft->setText( QStringLiteral( "123456789" ) );
ok = false;
m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok );
m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft );
QCOMPARE( true, ok );
QCOMPARE( m_formattedText, QStringLiteral( "123-45-6789" ) );
fft->setText( QStringLiteral( "1234567890" ) );
ok = false;
m_document->processKeystrokeAction( fft->additionalAction( Okular::FormField::FieldModified ), fft, ok );
m_document->processFormatAction( fft->additionalAction( Okular::FormField::FormatField ), fft );
QCOMPARE( false, ok );
QCOMPARE( m_formattedText, QStringLiteral( "123-45-6789" ) );
}
void FormatTest::cleanupTestCase()
{
m_document->closeDocument();
......
......@@ -156,3 +156,92 @@ function AFTime_Keystroke( ptf )
{
return;
}
/** AFSpecial_Format
* psf is the type of formatting to use:
* 0 = zip code
* 1 = zip + 4
* 2 = phone
* 3 = SSN
*
* These are all in the US format.
*/
function AFSpecial_Format( psf )
{
if( !event.value || psf == 0 )
{
return;
}
var ret = event.value;
if( psf === 1 )
ret = ret.substr( 0, 5 ) + '-' + ret.substr( 5, 4 );
else if( psf === 2 )
ret = '(' + ret.substr( 0, 3 ) + ') ' + ret.substr( 3, 3 ) + '-' + ret.substr( 6, 4 );
else if( psf === 3 )
ret = ret.substr( 0, 3 ) + '-' + ret.substr( 3, 2 ) + '-' + ret.substr( 5, 4 );
event.value = ret;
}
/** AFSpecial_Keystroke
*
* Checks if the String in event.value is valid.
*
* Parameter description based on Acrobat Help:
*
* psf is the type of formatting to use:
* 0 = zip code
* 1 = zip + 4
* 2 = phone
* 3 = SSN
*
* These are all in the US format. We check to see if only numbers are inserted and the length of the string.
*/
function AFSpecial_Keystroke( psf )
{
if ( !event.value )
{
return;
}
var str = event.value;
if( psf === 0 )
{
if( str.length > 5 )
{
event.rc = false;
return;
}
}
else if( psf === 1 || psf === 3 )
{
if( str.length > 9 )
{
event.rc = false;
return;
}
}
else if( psf === 2 )
{
if( str.length > 10 )
{
event.rc = false;
return;
}
}
for( i = 0 ; i < str.length ; ++i )
{
if( !( str[i] <= '9' && str[i] >= '0' ) )
{
event.rc = false;
return;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment