From 2e96378f801f2086c8d236f29e040e10828e11be Mon Sep 17 00:00:00 2001 From: nirenjan Date: Sat, 12 Aug 2017 08:43:13 -0700 Subject: [PATCH] Add test cases for MFD clock display --- tests/Makefile.am | 3 +- tests/common_infra.sh | 6 ++ tests/libx52/test_clock.sh | 119 +++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 tests/libx52/test_clock.sh diff --git a/tests/Makefile.am b/tests/Makefile.am index b9c49ee..9857a7e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,7 +6,8 @@ AM_SH_LOG_FLAGS = TESTS = \ libx52/test_leds.sh \ libx52/test_brightness.sh \ - libx52/test_indicator.sh + libx52/test_indicator.sh \ + libx52/test_clock.sh EXTRA_DIST = common_infra.sh $(TESTS) diff --git a/tests/common_infra.sh b/tests/common_infra.sh index 7943b71..90a6576 100644 --- a/tests/common_infra.sh +++ b/tests/common_infra.sh @@ -58,6 +58,12 @@ X52_SHIFT_INDICATOR_INDEX='00fd' X52_INDICATOR_STATE_ON='0051' X52_INDICATOR_STATE_OFF='0050' +X52_CLOCK_1_INDEX='00c0' +X52_CLOCK_2_INDEX='00c1' +X52_CLOCK_3_INDEX='00c2' +X52_CLOCK_DATE_INDEX='00c4' +X52_CLOCK_YEAR_INDEX='00c8' + find_programs() { # Tests and distcheck do not work on OSX, skip the tests diff --git a/tests/libx52/test_clock.sh b/tests/libx52/test_clock.sh new file mode 100644 index 0000000..3f37a5c --- /dev/null +++ b/tests/libx52/test_clock.sh @@ -0,0 +1,119 @@ +#!/bin/bash + +source $(dirname $0)/../common_infra.sh + +TEST_SUITE_ID="libx52 clock tests" + +clock_test_utc() +{ + local timezone=$1 + local time_format=$2 + local date_format=$3 + TEST_ID="Test setting clock to $timezone time, $time_format, $date_format" + + local dd=$(date +%_d) + local mm=$(date +%_m) + local yy=$(date +%_y) + local hr=$(date +%_H) + local mn=$(date +%_M) + + local clock_value=$(($hr * 256 + $mn)) + if [[ $time_format == 24hr ]] + then + clock_value=$(($clock_value + 32768)) + fi + + case $date_format in + ddmmyy) + date_value=$(($mm * 256 + $dd)) + year_value=$yy + ;; + mmddyy) + date_value=$(($dd * 256 + $mm)) + year_value=$yy + ;; + yymmdd) + date_value=$(($mm * 256 + $yy)) + year_value=$dd + ;; + *) + ;; + esac + + expect_pattern \ + $X52_CLOCK_DATE_INDEX $(printf '%04x' $date_value) \ + $X52_CLOCK_YEAR_INDEX $(printf '%04x' $year_value) \ + $X52_CLOCK_1_INDEX $(printf '%04x' $clock_value) \ + + $X52CLI clock $timezone $time_format $date_format + + verify_output +} + +offset_test() +{ + local clock_id=$1 + local offset=$2 + local time_format=$3 + TEST_ID="Test setting clock $clock_id offset to $offset, $time_format" + + local index="\$X52_CLOCK_${clock_id}_INDEX" + local value=0 + + # Handle negative case + if [[ $offset -lt 0 ]] + then + value=$((-$offset)) + value=$(($value & 0x3ff)) + value=$(($value | 1024)) + else + value=$offset + fi + + if [[ $time_format == 24hr ]] + then + value=$(($value + 32768)) + fi + + expect_pattern $(eval echo $index) $(printf '%04x' $value) + + $X52CLI offset $clock_id $offset $time_format + + verify_output + +} + +# For all test cases, we want to assume that the local timezone +# is the same as UTC, as otherwise, it can cause havoc, depending +# on when and where the tests are run. +export TZ=UTC + +for timezone in 'local' gmt +do + for time_format in 12hr 24hr + do + for date_format in ddmmyy mmddyy yymmdd + do + clock_test_utc $timezone $time_format $date_format + done + done +done + +# For the offset test cases, x52cli reinitializes the internal data +# structure every time it is called, therefore, the timezone for clock +# 1 is automatically reset to UTC. +unset TZ + +for clock in 2 3 +do + for time_format in 12hr 24hr + do + # Run offset tests for every 30 minute offset from UTC + for offset in $(seq -1020 30 1020) + do + offset_test $clock $offset $time_format + done + done +done +verify_test_suite +