[M76 Dev][API] Add the ewk_view_page_close API
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / utc_gtest_run.sh
1 #!/bin/bash
2
3 ##  @file   utc_gtest_run.sh
4 ##  @author Jongsoo Yoon <join.yoon@samsung.com>, Mikolaj Czyzewski <m.czyzewski@samsung.com>
5 ##  @date   2014-02-20
6 ##  @mod    2015-01-16
7 ##  @brief  This shell-script file is made to test all unit test cases automatically for Chromium EFL API
8
9 # Tests can be run in a four different ways:
10 # -single test
11 #   ./utc_gtest_run.sh -s utc_blink_ewk_view_visibility_set.POS_TEST1
12 # -set of tests
13 #   ./utc_gtest_run.sh -s utc_blink_ewk_view_user_agent*
14 # -tests listed in the file (each test_name/API_function in separate line)
15 #   ./utc_gtest_run.sh -l file
16 # -all tests
17 #   ./utc_gtest_run.sh -a
18 #
19 # To generate list of all tests:
20 #   ./utc_gtest_run.sh -g     (to stdout)
21 #   ./utc_gtest_run.sh -g > all_tests.txt
22 #   ./utc_gtest_run.sh -g | grep ewk_view > ewk_view_tests.txt
23 #
24 # The test results are generated in /opt/usr/utc_results/unittest-result-{DATE}-{TIME}
25 # To parse the results:
26 #   ./utc_gtest_run.sh -p /opt/usr/utc_results/unittest-result-{DATE}-{TIME}
27 # This creates file unittest-result-{DATE}-{TIME}.txt in current directory.
28
29 function usage {
30   cat << EOF
31 Usage: utc_gtest_run.sh [OPTION]
32   -a       run all tests
33   -g       generate list of all tests
34   -h       show this help
35   -l file  run all tests from a text file (every test pattern in separate line)
36   -p dir   search directory for xml files, parse them and print results
37   -s name  run single test
38 EOF
39 }
40
41 function getHostArch() {
42   echo $(uname -m | sed -e \
43         's/i.86/ia32/;s/x86_64/x64/;s/amd64/x64/;s/arm.*/arm/;s/i86pc/ia32/')
44 }
45
46 #  Set target environment depending on the host architecture
47 function init {
48   case "$(getHostArch)" in
49   x64)
50     UTC_EXEC="ewk_unittests"
51     tmp_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
52     CHROMIUM_DIR=$(dirname $tmp_dir)
53     echo $CHROMIUM_DIR
54     binaries=($(find "$CHROMIUM_DIR/out.x64" -type f -name "$UTC_EXEC"))
55     num_of_bin=${#binaries[@]}
56
57     case $num_of_bin in
58     0)
59       echo "=== No $UTC_EXEC binary found. Exiting ==="
60       exit 2
61       ;;
62     1)
63       echo "=== Only one utc binary found: ==="
64       echo "=== ${binaries[@]} ==="
65       EXEC=$binaries
66       ;;
67     *)
68       echo "=== Please choose utc binary to run: ==="
69       i=1
70       for bin in ${binaries[@]}; do
71         echo "$i) $bin"
72         ((i++))
73       done
74       while true; do
75         read number
76         if [[ $number =~ ^-?[0-9]+$ ]] && [ $number -ge 1 ] && [ $number -le $num_of_bin ]; then
77           break
78         else
79           echo "=== Please enter an integer from 1 to $num_of_bin ==="
80         fi
81       done
82       EXEC="${binaries[$number-1]}"
83       echo "=== Running $EXEC ==="
84       ;;
85     esac
86
87     UTC_RESULTS_ROOT_DIR=/tmp/utc_results
88
89     CHROMIUM_EFL_LIBDIR="$(dirname $EXEC)/lib"
90     CHROMIUM_EFL_DEPENDENCIES_LIBDIR="$CHROMIUM_DIR/out.x64/Dependencies/Root/lib64"
91     export LD_LIBRARY_PATH="$CHROMIUM_EFL_DEPENDENCIES_LIBDIR:$CHROMIUM_EFL_LIBDIR:${LD_LIBRARY_PATH}"
92     export UTC_RESOURCE_PATH="$CHROMIUM_DIR/ewk/unittest/resources"
93     EXEC="$EXEC --disable-gpu-driver-bug-workarounds"
94     ;;
95
96   arm)
97     UTC_EXEC=ewk_unittests
98     EXEC="/opt/usr/utc_exec/$UTC_EXEC"
99     chmod +x $EXEC
100     UTC_RESULTS_ROOT_DIR=/opt/usr/utc_results
101     killall -s QUIT lockscreen
102     ;;
103
104   *)
105     echo "=== Unsupported architecture ==="
106     ;;
107   esac # getHostArch
108
109   FILE_DATE=$(date +%y%m%d_%H%M%S)
110   UTC_RESULT_DIR="$UTC_RESULTS_ROOT_DIR/unittest-result-$FILE_DATE"
111
112   OPTIND=1
113   shift $((OPTIND-1))
114   [ "$1" = "--" ] && shift
115
116   echo "=== EXEC = $EXEC ==="
117   echo "=== UTC_RESULT_DIR = $UTC_RESULT_DIR ==="
118
119 }
120
121 function deinit {
122   echo -e "=== Test results were saved in \033[0;34m$UTC_RESULT_DIR\033[0m ==="
123 }
124
125 # Parse single xml file and print result
126 function parse_xml () {
127   xml_file=$1
128   if [ ! -e "$xml_file" ]; then
129     echo "=== File $xml_file doesn't exist ==="
130     return
131   fi
132
133   testsuite_count=$(xmllint --xpath "count(//testsuites/testsuite)" $xml_file)
134   for (( i=1; i<=$testsuite_count; i++ )); do
135     testcase_count=$(xmllint --xpath "count(//testsuites/testsuite[$i]/testcase)" $xml_file)
136     for (( j=1; j<=$testcase_count; j++ )); do
137       classname=$(xmllint --xpath "string(//testsuites/testsuite[$i]/testcase[$j]/@classname)" $xml_file)
138       name=$(xmllint --xpath "string(//testsuites/testsuite[$i]/testcase[$j]/@name)" $xml_file)
139       message=$(xmllint --xpath "string(//testsuites/testsuite[$i]/testcase[$j]/failure/@message)" $xml_file)
140       message=$(echo $message | tr -d "\n\r")
141       if [ -n "$message" ]; then
142         echo "FAILURE $classname.$name $message"
143       else
144         echo "SUCCESS $classname.$name"
145       fi
146     done
147   done
148 }
149
150 # Search directory for xml files, parse them and print results
151 function parse_dir () {
152   xml_dir=$1
153  pushd $xml_dir &>/dev/null
154   echo "=== Parsing files in: `pwd` ==="
155
156   for file in *.xml; do
157     echo "$(parse_xml $file)"
158   done
159
160   popd &>/dev/null
161 }
162
163 # Generate list of all tests and print it to stdout
164 function generate_tests_list () {
165   if [ -n "$1" ]; then
166     filter="*$1*"
167   else
168     filter="*"
169   fi
170   list=$($EXEC --gtest_list_tests --gtest_filter="$filter" | egrep "^[a-zA-Z ]")
171   utc_line=""
172   result=""
173
174   while read line; do
175     if [[ $line == utc_* ]]; then
176       utc_line=$line
177     else
178       echo "$utc_line$line"
179     fi
180   done <<< "$list"
181 }
182
183 # Run single test
184 function run_test () {
185   test_name=$1
186   if [ -n "$test_name" ]; then
187     echo "=== Running test $test_name ==="
188     file="$UTC_RESULT_DIR/$test_name.xml"
189
190     # Generate fake xml file that tells about possible segfault
191     if [ ! -f $file ]; then
192       IFS='.' read -ra name <<< "$test_name"
193       echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" >> $file
194       echo "<testsuites tests=\"1\" failures=\"0\" disabled=\"0\" errors=\"1\" time=\"0.1\" name=\"AllTests\">" >> $file
195       echo "  <testsuite name=\"${name[0]}\" tests=\"1\" failures=\"0\" disabled=\"0\" errors=\"1\" time=\"0.1\">" >> $file
196       echo "    <testcase name=\"${name[1]}\" status=\"run\" time=\"0.1\" classname=\"${name[0]}\">" >> $file
197       echo "      <failure message=\"SIGSEGV\" type=\"\"></failure>" >> $file
198       echo "    </testcase>" >> $file
199       echo "  </testsuite>" >> $file
200       echo "</testsuites>" >> $file
201     fi
202
203     $EXEC --gtest_output="xml:$UTC_RESULT_DIR/$test_name.xml" --gtest_filter="$test_name"
204     echo "=== Finished test $test_name ==="
205     return 0
206   else
207     echo "=== No test name specified ==="
208     return 1
209   fi
210 }
211
212 # Run tests listed in file
213 function run_list () {
214   test_file=$1
215   if [ -f "$test_file" ]; then
216     echo "=== Running tests from file: $test_file ==="
217   else
218     echo "=== Test file not specified ==="
219     return 1
220   fi
221
222   ## Assume that in this file are only patterns (i.e. ewk_view_application_name) instead of
223   ## full test names (i.e. utc_blink_ewk_view_application_name_for_user_agent_set.POS_TEST).
224   ## So for each pattern we find all matching tests and run them one by one.
225   while read test_pattern; do
226     find_tests=$(generate_tests_list "$test_pattern")
227     while read single_test; do
228       run_test "$single_test"
229     done <<< "$find_tests"
230   done < "$test_file"
231
232   echo "=== Finished running tests from file: $test_file ==="
233 }
234
235 function run_all {
236   echo "=== Running all test ==="
237
238   while read test; do
239     run_test "$test"
240   done <<< "$(generate_tests_list)"
241
242   echo "=== Finished running all test ==="
243 }
244
245 while getopts "aghHl:p:s:" opt; do
246   case "$opt" in
247   # all tests
248   a)
249     init
250     mkdir -p $UTC_RESULT_DIR
251     run_all
252     deinit
253     exit 0
254     ;;
255   # generate list of all tests
256   g)
257     init
258     generate_tests_list
259     exit 0
260     ;;
261   # help
262   h|H)
263     usage
264     exit 1
265     ;;
266   # run from list
267   l)
268     init
269     mkdir -p $UTC_RESULT_DIR
270     run_list $OPTARG
271     deinit
272     exit 0
273     ;;
274   # parse dir
275   p)
276     init
277     xml_dir="$OPTARG"
278     if [ ! -e "$xml_dir" ]; then
279       echo "=== Directory '$xml_dir' doesn't exist ==="
280       exit 4
281     fi
282
283     result=$(parse_dir $xml_dir)
284     file_name="$(basename "$xml_dir").txt"
285     file="$UTC_RESULTS_ROOT_DIR/$file_name"
286
287     echo "$result" > $file
288     success=$(echo "$result" | grep "^SUCCESS" | wc -l)
289     failure=$(echo "$result" | grep "^FAILURE" | wc -l)
290     sigsegv=$(echo "$result" | grep "SIGSEGV"  | wc -l)
291     echo "" | tee -a $file
292     echo "SUCCESS  : $success" | tee -a $file
293     echo "FAILURE  : $failure ($sigsegv)" | tee -a $file
294     echo "TOTAL    : $(expr $success + $failure)" | tee -a $file
295     echo "" | tee -a $file
296     echo "=== Results saved to $UTC_RESULTS_ROOT_DIR/$file_name ==="
297     exit 0
298     ;;
299   # single test
300   s)
301     init
302     mkdir -p $UTC_RESULT_DIR
303     run_test $OPTARG
304     deinit
305     exit 0
306     ;;
307   esac
308 done
309
310 usage
311 exit 1