xref: /trunk/main/xmerge/source/palmtests/qa-wrapper/bin/qa_test_driver.pl (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1#!/usr/bin/perl
2#########################################################################
3
4 #*************************************************************************
5 #
6# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7#
8# Copyright 2000, 2010 Oracle and/or its affiliates.
9#
10# OpenOffice.org - a multi-platform office productivity suite
11#
12# This file is part of OpenOffice.org.
13#
14# OpenOffice.org is free software: you can redistribute it and/or modify
15# it under the terms of the GNU Lesser General Public License version 3
16# only, as published by the Free Software Foundation.
17#
18# OpenOffice.org is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU Lesser General Public License version 3 for more details
22# (a copy is included in the LICENSE file that accompanied this code).
23#
24# You should have received a copy of the GNU Lesser General Public License
25# version 3 along with OpenOffice.org.  If not, see
26# <http://www.openoffice.org/license.html>
27# for a copy of the LGPLv3 License.
28#
29 #*************************************************************************
30
31####################################################################
32# File Name: test_driver.pl
33# Version  : 1.0
34# Project  : Xmerge
35# Author   : Brian Cameron
36# Date     : 5th Sept. 2001
37#
38#
39# This script does the following:
40#
41# Processes the input file, and runs the tests specified in that
42# file.  This will do the following for each test:
43#
44# 1. Convert a file from XML to PDB format
45# 2. Starts up the Palm OS emulator with the appropriate program
46#    running and the converted file loaded the program.
47# 3. Makes automated changes as specified in the inputfile to
48#    this script..
49# 4. Returns to the main applications window.
50#
51# Parameter
52#   Filename to convert and change
53#
54##########################################################################
55
56# Turn on auto-flushing
57#
58$|=1;
59
60use EmRPC;
61
62# Directory where converterlib is located...
63#
64#use lib "/export/home/test/qadir/qa/lib";
65use lib $ENV{'QA_LIB_HOME'};
66use converterlib;
67
68#-------------------- Start of main script ------------------------------------
69
70# Environmental Settings
71
72$pose_exe = "";
73$pose_prc = "";
74$test_list = "";
75$infile = "";
76$merge_opt = 0;
77# if testcase hasn't completed in 10 mins, then kill it off
78$testcase_timeout=600;
79
80# You may need to change this from the default if your pose emulator
81# starts faster or slower than mine.
82#
83if ($ENV{'POSE_TIMEOUT'})
84{
85  $pose_timeout  = "$ENV{'POSE_TIMEOUT'}";
86}
87else
88{
89  $pose_timeout = 15;
90}
91
92$cmdline_len = @ARGV;
93if ($cmdline_len <= 0)
94{
95    print_usage();
96    exit (0);
97}
98
99&process_cmdline(@ARGV);
100&print_env();
101&verify_env_options();
102
103# Make the output directories with timestamps included in the
104# directory names.
105#
106mkdir $pdb_orig, 0777 || die "can not create directory <$pdb_orig>.";
107`chmod 777 $pdb_orig`;
108mkdir $pdb_new,  0777 || die "can not create directory <$pdb_new>.";
109`chmod 777 $pdb_new`;
110mkdir $xml_new,  0777 || die "can not create directory <$xml_new>.";
111`chmod 777 $xml_new`;
112
113&verify_prcs_exist("DBExporter.prc");
114
115if ($test_list ne "")
116{
117    open (TESTLIST, $test_list) || die "Couldn't open testcase list file $test_list";
118
119    while (<TESTLIST>)
120    {
121        &process_testcase($_);
122    }
123}
124elsif ($infile ne "")
125{
126    if (!defined($child_pid = fork()))
127    {
128        # there was an error forking a process
129        print "ERROR: Unable to fork process\n";
130        die "ERROR: Unable to fork process\n";
131    }
132    elsif ($child_pid)
133    {
134        # this is the parent process
135        # run the actual test here
136print "********\tPARENT (pid = $$): process_testcase...\n";
137        &process_testcase($infile);
138print "********\tPARENT (pid = $$): ...process_testcase finished normally\n";
139
140        # test finished normally, so kill the monitor
141        # that is running in the child process
142print "********\tPARENT (pid = $$): kill child process ($child_pid)\n";
143print "********\tPARENT Before:\n";
144system( "/usr/bin/ptree" );
145        kill( $child_pid );
146        kill( 9, $child_pid );
147print "********\tPARENT After:\n";
148system( "/usr/bin/ptree" );
149    }
150    else
151    {
152print "********\tCHILD (pid = $$): sleep for $testcase_timeout seconds\n";
153        # this is the child process
154        # wait on the test running in the parent, and
155        # kill it if it hasn't finished on time
156        sleep( $testcase_timeout );
157
158        # if the parent hasn't killed this process before it
159        # gets here, then it's probably hung, so we need to
160        # kill it.
161        print "********\tCHILD (pid = $$): TEST HUNG? still "
162            ."running after [$testcase_timeout] seconds - "
163            ."need to kill test process\n";
164        $parent = getppid;
165
166        if ( $parent != 1 ) {
167            print "********\nCHILD (pid = $$): Killing process ($parent)\n";
168            kill( $parent );
169            kill( 9, $parent );
170        } else {
171            # If we cannot get the ppid, then the parent might
172            # have died abnormally, before it got a chance to
173            # kill this (child) process.
174            print "********\nCHILD (pid = $$): cannot determine ppid - "
175                ."terminating\n";
176system( "/usr/bin/ptree" );
177            exit(2);
178        }
179
180        exit(1);
181    }
182}
183else
184{
185    die ("You didn't supply any test cases to process");
186}
187
188print "Finished.\n";
189exit(0);
190
191#-------------------- End of main script ----------------------------------------
192
193#--------------------------------------------------------------------------------
194# Various sub routines
195#--------------------------------------------------------------------------------
196
197# process_testcase
198# infile - test case file name
199#
200# This is the main driver function
201# Opens the infile, reads it in parses it, runs the appropriate conversion
202# starts pose and load the file into the emulator. It launches the
203# appropriate editor and then runs the commands specified in the test case.
204# It then exports the file and saves it locally. Finally it is converted
205# back to the original office format.
206#
207sub process_testcase
208{
209    my $infile = $_[0];
210    my $convert_file = "";
211    my $rc;
212
213    # Process the inputfile
214    #
215    open (INFILE, $infile) || die "Failed to open test case <$infile>";
216
217    $running_testtype = "";
218
219    # Process the input file.
220    #
221    while ($c_inline = <INFILE>)
222    {
223       chomp $c_inline;
224       @entry = split('\|', $c_inline);
225
226       # Process TEST
227       #
228       if ($c_inline =~ /^ *#/ || $c_inline =~ /^[ \t]*$/)
229       {
230          # skip comments and blank lines.
231          #
232          next;
233       }
234       elsif ("$entry[0]" eq "TEST")
235       {
236          # Close the test if one is running.
237          #
238          &close_program($convert_file);
239          $running_testtype = "";
240
241          $valid_test = 0;
242
243          if ($#entry != 3)
244          {
245             print "\nERROR, $entry[0] invalid number of arguments\n\n";
246          }
247          else
248          {
249             # Start the test.
250             #
251             print "\nStarting test: $entry[1]\n";
252             $convert_file = $entry[3];
253
254             if ("$entry[2]"  =~ /[Qq][Uu][Ii][Cc][Kk][Ww][Oo][Rr][Dd]/)
255             {
256                $xml_extension = "sxw";
257                $convert_to = "application/x-aportisdoc";
258
259                # Convert XML file to pdb format.
260                #
261                $rc = &convert_to_pdb("$xml_orig", $convert_file, $xml_extension ,
262                   $convert_to,"$pdb_orig");
263                if ($rc != 0)
264                {
265                   print "\nERROR, problem converting file $convert_file\n\n";
266                }
267                else
268                {
269                   # Start pose
270                   #
271                   $rc = &start_pose("$pose_exe",
272                     "$pose_prc/Quickword.PRC,$pose_prc/DBExporter.prc,$pdb_orig/$convert_file.pdb",
273                     "Quickword", $pose_timeout);
274
275                   if ($rc == 0)
276                   {
277                      &start_quickword();
278                      $valid_test = 1;
279                      $running_testtype = "QUICKWORD";
280                      print "\npose launched, begin automated test sequence for QuickWord\n";
281                   }
282                   else
283                   {
284                      &kill_pose();
285                      $running_testtype = "";
286                   }
287                }
288             }
289             elsif ("$entry[2]"  =~ /[Mm][Ii][Nn][Ii][Cc][Aa][Ll][Cc]/)
290             {
291                $xml_extension = "sxc";
292                $convert_to = "application/x-minicalc";
293
294                # Convert XML file to pdb format.
295                #
296                $rc = &convert_to_pdb("$xml_orig", $convert_file,
297                  $xml_extension, $convert_to,"$pdb_orig");
298                if ($rc != 0)
299                {
300                   print "\nERROR, problem converting file $convert_file\n\n";
301                }
302                else
303                {
304                   # Get minicalc PDB file names, since an SXC file can
305                   # be converted to more than one.
306                   #
307                   $pdb_files="";
308                   $i = 1;
309                   while (-f "$pdb_orig/$convert_file-Sheet$i.pdb")
310                   {
311                     if ($i > 1)
312                     {
313                       $pdb_files .= ",";
314                     }
315                     $pdb_files .= "$pdb_orig/$convert_file-Sheet$i.pdb";
316                     $i++;
317                   }
318                   $number = $i-1;
319
320                   # Start pose
321                   #
322                   $rc = &start_pose("$pose_exe",
323                      "$pose_prc/MiniCalc.prc,$pose_prc/DBExporter.prc,$pdb_files",
324                      "MiniCalc", $pose_timeout);
325
326                   if ($rc == 0)
327                   {
328                      &start_minicalc();
329                      $valid_test = 1;
330                      $running_testtype = "MINICALC";
331                      print "pose launched, begin automated test sequence for MiniCalc\n";
332                   }
333                   else
334                   {
335                      &kill_pose();
336                      $running_testtype = "";
337                   }
338                }
339             }
340             else
341             {
342                print "\nERROR, invalid extension <$entry[2]>\n\n";
343             }
344          }
345       }
346
347       # Process DB_EXPORT
348       #
349       elsif ("$entry[0]" eq "DB_EXPORT")
350       {
351          if ($#entry != 1)
352          {
353            print "\nERROR, $entry[0] invalid number of arguments\n\n";
354          }
355          else
356          {
357             &db_export($entry[1]);
358          }
359       }
360
361       # Process TAP_APPLICATIONS
362       #
363       elsif ("$entry[0]" eq "TAP_APPLICATIONS")
364       {
365          if ($#entry != 0)
366          {
367            print "\nERROR, $entry[0] invalid number of arguments\n\n";
368          }
369          else
370          {
371             &tap_applications(0);
372          }
373       }
374
375       # Process ENTER_STRING_AT_LOCATION
376       #
377       elsif ("$entry[0]" eq "ENTER_STRING_AT_LOCATION")
378       {
379          if ($#entry != 3)
380          {
381             print "\nERROR, $entry[0] invalid number of arguments\n\n";
382          }
383          elsif ($valid_test == 0)
384          {
385             print "\nERROR, can not process $entry[0] for invalid test\n\n";
386          }
387          else
388          {
389             &enter_string_at_location($entry[1], $entry[2],
390               $entry[3], $running_testtype);
391          }
392       }
393
394       # Process TAP_PEN
395       #
396       elsif ("$entry[0]" eq "TAP_PEN")
397       {
398          if ($#entry != 2)
399          {
400             print "\nERROR, $entry[0] invalid number of arguments\n\n";
401          }
402          elsif ($valid_test == 0)
403          {
404             print "\nERROR, can not process $entry[0] for invalid test\n\n";
405          }
406          else
407          {
408             &pose_tap_pen($entry[1], $entry[2], 0);
409          }
410       }
411
412       # Process TAP_BUTTON
413       #
414       elsif ("$entry[0]" eq "TAP_BUTTON")
415       {
416          if ($#entry != 1)
417          {
418             print "\nERROR, $entry[0] invalid number of arguments\n\n";
419          }
420          elsif ($valid_test == 0)
421          {
422             print "\nERROR, can not process $entry[0] for invalid test\n\n";
423          }
424          else
425          {
426             &pose_tap_button($entry[1], 0);
427          }
428       }
429
430           # Process TAP_PEN_HARD
431           #
432           elsif ("$entry[0]" eq "TAP_PEN_HARD")
433           {
434              if ($#entry != 2)
435              {
436                 print "\nERROR, $entry[0] invalid number of arguments\n\n";
437              }
438              elsif ($valid_test == 0)
439              {
440                 print "\nERROR, can not process $entry[0] for invalid test\n\n";
441              }
442              else
443              {
444                 &pose_tap_pen_hard($entry[1],$entry[2], 0);
445              }
446           }
447
448
449       # Process SLEEP
450       #
451       elsif ("$entry[0]" eq "SLEEP")
452       {
453          if ($#entry != 1)
454          {
455            print "\nERROR, $entry[0] invalid number of arguments\n\n";
456          }
457          else
458          {
459             &pose_sleep($entry[1]);
460          }
461       }
462
463       # Process MINICALC_ENTER_CELL
464       #
465       elsif ("$entry[0]" eq "MINICALC_ENTER_CELL")
466       {
467          if ($#entry != 3)
468          {
469             print "\nERROR, $entry[0] invalid number of arguments\n\n";
470          }
471          elsif ($valid_test == 0)
472          {
473             print "\nERROR, can not process $entry[0] for invalid test\n\n";
474          }
475          else
476          {
477             &minicalc_enter_cell($entry[1], $entry[2], $entry[3]);
478          }
479       }
480
481       # Process QUICKWORD_FIND_REPLACE
482       #
483       elsif ("$entry[0]" eq "QUICKWORD_FIND_REPLACE")
484       {
485          if ($#entry != 2)
486          {
487             print "\nERROR, $entry[0] invalid number of arguments\n\n";
488          }
489          elsif ($valid_test == 0)
490          {
491             print "\nERROR, can not process $entry[0] for invalid test\n\n";
492          }
493          else
494          {
495             &quickword_find_replace($entry[1], $entry[2]);
496          }
497       }
498       else
499       {
500          print "\nERROR, invalid line <$c_inline>\n";
501       }
502    }
503
504    &close_program($convert_file);
505}
506
507# close_program
508# convert_file - file to export
509#
510# closes the program running in pose and kills pose
511#
512sub close_program
513{
514    my $convert_file = $_[0];
515
516    if ($running_testtype eq "QUICKWORD")
517    {
518        print "QuickWord test completed.\n";
519        &close_program_quickword($convert_file);
520    }
521    elsif ($running_testtype eq "MINICALC")
522    {
523        print "MiniCalc test completed.\n";
524        &close_program_minicalc($convert_file, $number);
525    }
526}
527
528# close_program_quickword
529# convert_file - file to export
530#
531# Closes quickword and kills pose
532#
533sub close_program_quickword
534{
535      my $convert_file = $_[0];
536      my $error_file = "./error.txt";
537      my $rc;
538
539      &close_quickword();
540
541      &db_export($convert_file);
542      print "Moving /tmp/$convert_file.pdb to $pdb_new\n";
543      `mv /tmp/$convert_file.pdb $pdb_new`;
544      `chmod 666 $pdb_new/$convert_file.pdb`;
545
546      &close_connection(1);
547      &kill_pose();
548      print "\nFinishing test...\n";
549
550      # The path of where to put the error file should be specified
551      # in the properties file.  Not sure if it is really necessary
552      # to put this out to a separate file.  STDOUT should be fine.
553      #
554      $rc = &convert_to_xml($xml_new, $xml_orig,
555        "$pdb_new/$convert_file.pdb", "application/x-aportisdoc" ,
556          "sxw", $convert_file, $merge_opt);
557      if ($rc != 0)
558      {
559        print "\nERROR, problem converting file $pdb_new/$convert_file.pdb\n\n";
560      }
561}
562
563# close_program_minicalc
564# convert_file - file to export
565#
566# Closes minicalc and kills pose
567#
568sub close_program_minicalc
569{
570      my $convert_file = $_[0];
571      my $num_files   = $_[1];
572      my $list="";
573      my $rc;
574
575      &close_minicalc();
576
577      for ($a=1; $a <= $num_files; $a++)
578      {
579        &db_export("$convert_file-Sheet$a");
580        print "Moving /tmp/$convert_file-Sheet$a.pdb to $pdb_new/\n";
581        `mv /tmp/$convert_file-Sheet$a.pdb $pdb_new/`;
582        `chmod 666 $pdb_new/$convert_file-Sheet$a.pdb`;
583      }
584
585      &close_connection(1);
586      &kill_pose();
587      print "\nFinishing test...\n";
588
589      for ($a=1; $a <= $num_files; $a++)
590      {
591        $list .="$pdb_new/$convert_file-Sheet$a.pdb "
592      }
593
594      $rc = &convert_to_xml($xml_new, $xml_orig, "$list",
595          "application/x-minicalc", "sxc", $convert_file, $merge_opt);
596      if ($rc != 0)
597      {
598        print "\nERROR, problem converting file(s) $list\n\n";
599      }
600
601      &pose_sleep(5);
602}
603
604# print_usage
605#
606# prints the usage for this program.
607#
608sub print_usage
609{
610    print "Usage : test_driver.pl\n";
611    print "\t-test=<file> \t\t: individual test case file to run\n";
612    print "\t-list=<file> \t\t: list of test case files\n";
613    print "\t-env=<file> \t\t: Properites like file defining env\n";
614    print "\t-pose-exe=<fullpath> \t: path to pose executable\n";
615    print "\t-pose-prc=<path> \t: path to directory holding prc files\n";
616    print "\t-pdb-orig=<path> \t: directory to hold original pdb files\n";
617    print "\t-pdb-new=<path> \t: directory to hold new pdb files\n";
618    print "\t-xml-orig=<path> \t: directory to hold original office documents\n";
619    print "\t-xml-new=<path> \t: directory to hold new office documents\n";
620    print "\t-merge          \t: Invokes the merge option when converting\n";
621    print "\t                \t  from PDB back to XML.\n";
622}
623
624# print_env
625#
626# Prints the current environment.
627#
628sub print_env
629{
630    print "\nUsing the following environment:\n";
631    print "\tPOSE_EXE  = $pose_exe\n";
632    print "\tPOSE_PRC  = $pose_prc\n";
633    print "\tPDB_ORIG  = $pdb_orig\n";
634    print "\tPDB_NEW   = $pdb_new\n";
635    print "\tXML_ORIG  = $xml_orig\n";
636    print "\tXML_NEW   = $xml_new\n";
637}
638
639# process_cmdline
640#
641# command line options come in as key/value pairs.
642# read them and set the appropriate global variable
643#
644# Sets these globals: pose_exe, pose_prc, xml_orig, xml_new_dir,
645# xml_new, pdb_orig_dir, pdb_orig, pdb_new_dir, pdb_new.
646#
647sub process_cmdline
648{
649    foreach $i (@_)
650    {
651        my @arg= split('=', $i);
652        @arg[0] =~ tr/A-Z/a-z/;
653
654        if (@arg[0] eq "-pose-exe")
655        {
656            $pose_exe=$arg[1];
657        }
658        elsif (@arg[0] eq "-pose-prc")
659        {
660            $pose_prc=$arg[1];
661        }
662        elsif (@arg[0] eq "-pdb-orig")
663        {
664            $pdb_orig_dir=$arg[1];
665            $pdb_orig=$arg[1];
666        }
667        elsif (@arg[0] eq "-pdb-new")
668        {
669            $pdb_new_dir=$arg[1];
670            $pdb_new=$arg[1];
671        }
672        elsif (@arg[0] eq "-xml-orig")
673        {
674            $xml_orig=$arg[1];
675        }
676        elsif (@arg[0] eq "-xml-new")
677        {
678            $xml_new_dir=$arg[1];
679            $xml_new=$arg[1];
680        }
681        elsif (@arg[0] eq "-env")
682        {
683            &set_env_from_props($arg[1]);
684        }
685        elsif (@arg[0] eq "-list")
686        {
687            $test_list = $arg[1];
688        }
689        elsif (@arg[0] eq "-test")
690        {
691            $infile = $arg[1];
692        }
693        elsif (@arg[0] eq "-merge")
694        {
695             $merge_opt = 1;
696        }
697        else
698        {
699            print_usage();
700            die "Incorrect command line";
701        }
702    }
703}
704
705# set_env_from_props
706# infile - property file
707#
708# Read the properties file, of the form key=value
709# Valid key values are :
710#   POSE_EXE
711#   POSE_PRC
712#   PDB_ORIG
713#   PDB_NEW
714#   XML_ORIG
715#   XML_NEW
716# If a value is found the appropriate global variable is set.
717#
718# Sets these globals: pose_exe, pose_prc, xml_orig, xml_new_dir,
719# xml_new, pdb_orig_dir, pdb_orig, pdb_new_dir, pdb_new.
720#
721sub set_env_from_props
722{
723    my $infile = $_[0];
724
725    open(PROPSFILE, $infile) || die "Could not open properties file <$infile>";
726
727    while (<PROPSFILE>)
728    {
729        chomp $_;
730        my @arg = split('=', $_);
731        @arg[0] =~ tr/a-z/A-Z/;
732        my $len = @arg;
733        if ($len != 2)
734        {
735            die "Malformed property in $arg[0]";
736        }
737        if (@arg[0] eq "POSE_EXE")
738        {
739            $pose_exe=$arg[1];
740        }
741        elsif (@arg[0] eq "POSE_PRC")
742        {
743            $pose_prc=$arg[1];
744        }
745        elsif (@arg[0] eq "PDB_ORIG")
746        {
747            $pdb_orig_dir=$arg[1];
748            $pdb_orig=$arg[1];
749        }
750        elsif (@arg[0] eq "PDB_NEW")
751        {
752            $pdb_new_dir=$arg[1];
753            $pdb_new=$arg[1];
754        }
755        elsif (@arg[0] eq "XML_ORIG")
756        {
757            $xml_orig=$arg[1];
758        }
759        elsif (@arg[0] eq "XML_NEW")
760        {
761            $xml_new_dir=$arg[1];
762            $xml_new=$arg[1];
763        }
764
765    }
766    close PROPSFILE;
767}
768
769# verify_env_options
770#
771# Verify that input options are correctly set.
772# Assumes pose_exe, pose_prc, xml_orig, xml_new_dir,
773# pdb_orig_dir, and pdb_new_dir are already set.
774#
775sub verify_env_options
776{
777    if (!-e "$pose_exe")
778    {
779        die "The pose executable cannot be found at $pose_exe.";
780    }
781    if (!-x $pose_exe)
782    {
783        die "$pose_exe exists but is not executable.";
784    }
785
786    if (!-e "$pose_prc")
787    {
788        die "The PRC directory specified as $pose_prc does not exist.";
789    }
790    if (!-d "$pose_prc")
791    {
792        die "The PRC location specified as $pose_prc exists, but is not a directory.";
793    }
794
795    if (!-e "$pdb_orig_dir")
796    {
797        die "The original PDB directory specified as $pdb_orig_dir does not exist.";
798    }
799    if (!-d "$pdb_orig_dir")
800    {
801        die "The original PDB directory specified as $pdb_orig_dir exists but is not a directory.";
802    }
803
804    if (!-e "$pdb_new_dir")
805    {
806        die "The new PDB directory specified as $pdb_new_dir does not exist.";
807    }
808    if (!-d "$pdb_new_dir")
809    {
810        die "The new PDB directory specified as $pdb_new_dir exists but is not a directory.";
811    }
812
813    if (!-e "$xml_orig")
814    {
815        die "The original Office document directory specified as $xml_orig does not exist.";
816    }
817    if (!-d "$xml_orig")
818    {
819        die "The original Office document location specified as $xml_orig exists but is not a directory.";
820    }
821
822    if (!-e "$xml_new_dir")
823    {
824        die "The new Office document directory specified as $xml_new_dir does not exist.";
825    }
826    if (!-d "$xml_new_dir")
827    {
828        die "The new Office document location specified as $xml_new_dir exists but is not a directory.";
829    }
830}
831
832# verify_prcs_exist
833# prcfile - the PRC file to check
834#
835# Verifies that the specified PRC file exists.
836#
837sub verify_prcs_exist
838{
839    my $prcfile = $_[0];
840
841    if (!-e "$pose_prc/$prcfile")
842    {
843        die "The pose PRC directory ($pose_prc) is correct, but I can't find $prcfile there.";
844    }
845}
846
847