diff --git a/tests/Makefile.am b/tests/Makefile.am index 28aa9c0..25719a7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -13,7 +13,12 @@ TESTS = \ libx52/test_brightness.sh \ libx52/test_indicator.sh \ libx52/test_mfd.sh \ - libx52/test_clock.sh + libx52/test_clock.sh \ + libx52/test_timezone.sh + +# NOTE: Timezone tests currently fail when local time is daylight time. Once +# this is fixed, remove the line below. +XFAIL_TESTS = libx52/test_timezone.sh EXTRA_DIST = common_infra.sh $(TESTS) diff --git a/tests/common_infra.sh b/tests/common_infra.sh index be1ceb4..6e3f24f 100644 --- a/tests/common_infra.sh +++ b/tests/common_infra.sh @@ -106,6 +106,25 @@ find_programs() fi } +# This is for additional programs that are not needed by every test case +require_programs() +{ + local skip=false + for prog in "$@" + do + if ! command -v "$prog" + then + echo "Required program '$prog' not found, skipping test suite" + skip=true + fi + done + + if $skip + then + exit $EXIT_SKIP + fi +} + setup_preload() { # Find the libusb stub library diff --git a/tests/libx52/test_timezone.sh b/tests/libx52/test_timezone.sh new file mode 100644 index 0000000..083e350 --- /dev/null +++ b/tests/libx52/test_timezone.sh @@ -0,0 +1,68 @@ +#!/bin/bash +# Timezone tests +# +# Copyright (C) 2020 Nirenjan Krishnan (nirenjan@nirenjan.org) +# +# SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0 + +source $(dirname $0)/../common_infra.sh +require_programs faketime + +TEST_SUITE_ID="libx52 timezone tests" + +# Use the America/Los_Angeles timezone +# Standard time is UTC-08:00, Daylight time is UTC-07:00 +export TZ=America/Los_Angeles + +# All timezone tests use DD-MM-YY and 12hr format +timezone_test() +{ + local datetime="$1" + + TEST_ID="Test setting clock to '$(date --date="$datetime")'" + + local dd=$(date --date="$datetime" +%_d) + local mm=$(date --date="$datetime" +%_m) + local yy=$(date --date="$datetime" +%_y) + local hr=$(date --date="$datetime" +%_H) + local mn=$(date --date="$datetime" +%_M) + + local off + if [[ "$(date --date="$datetime" +%Z)" == 'PDT' ]] + then + # Pacific Daylight Time + # Default offset for clocks 2 & 3 should be +420 minutes + off=420 + else + # Pacific Standard time + # Default offset for clocks 2 & 3 should be +480 minutes + off=480 + fi + + local clock_value=$(($hr * 256 + $mn)) + local date_value=$(($mm * 256 + $dd)) + local year_value=$yy + + 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) \ + $X52_CLOCK_2_INDEX $(printf '%04x' $off) \ + $X52_CLOCK_3_INDEX $(printf '%04x' $off) \ + + faketime "$datetime" $X52CLI clock local 12hr ddmmyy + verify_output +} + +# Check timezone test for 14:15:16 on the 13th day of every month from +# January 2017 to December 2019 + +for year in $(seq 2017 2019) +do + for month in $(seq 1 12) + do + timezone_test "$(printf '%d-%02d-13 14:15:16' $year $month)" + done +done + +verify_test_suite