From af3261b16f7a551d59b00036fef60a44e3a1b7c4 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 8 Sep 2020 17:04:36 +0200 Subject: [PATCH 1/5] [keyboard] Refactor findLegacyKeymap into something testable --- src/modules/keyboard/SetKeyboardLayoutJob.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/modules/keyboard/SetKeyboardLayoutJob.cpp b/src/modules/keyboard/SetKeyboardLayoutJob.cpp index d0ad8fcbf..a36604926 100644 --- a/src/modules/keyboard/SetKeyboardLayoutJob.cpp +++ b/src/modules/keyboard/SetKeyboardLayoutJob.cpp @@ -79,8 +79,8 @@ SetKeyboardLayoutJob::findConvertedKeymap( const QString& convertedKeymapPath ) } -QString -SetKeyboardLayoutJob::findLegacyKeymap() const +STATICTEST QString +findLegacyKeymap( const QString& layout, const QString& model, const QString& variant ) { cDebug() << "Looking for legacy keymap in QRC"; @@ -109,20 +109,20 @@ SetKeyboardLayoutJob::findLegacyKeymap() const // Determine how well matching this entry is // We assume here that we have one X11 layout. If the UI changes to // allow more than one layout, this should change too. - if ( m_layout == mapping[ 1 ] ) + if ( layout == mapping[ 1 ] ) // If we got an exact match, this is best { matching = 10; } // Look for an entry whose first layout matches ours - else if ( mapping[ 1 ].startsWith( m_layout + ',' ) ) + else if ( mapping[ 1 ].startsWith( layout + ',' ) ) { matching = 5; } if ( matching > 0 ) { - if ( m_model.isEmpty() || m_model == mapping[ 2 ] ) + if ( model.isEmpty() || model == mapping[ 2 ] ) { matching++; } @@ -137,7 +137,7 @@ SetKeyboardLayoutJob::findLegacyKeymap() const mappingVariant.remove( 1, 0 ); } - if ( m_variant == mappingVariant ) + if ( variant == mappingVariant ) { matching++; } @@ -162,6 +162,12 @@ SetKeyboardLayoutJob::findLegacyKeymap() const return name; } +QString +SetKeyboardLayoutJob::findLegacyKeymap() const +{ + return ::findLegacyKeymap( m_layout, m_model, m_variant ); +} + bool SetKeyboardLayoutJob::writeVConsoleData( const QString& vconsoleConfPath, const QString& convertedKeymapPath ) const From 0d8e0d9b96f7bc0f668d535615615775caadf106 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 8 Sep 2020 17:13:44 +0200 Subject: [PATCH 2/5] [keyboard] Add a stub for unit tests --- src/modules/keyboard/CMakeLists.txt | 7 ++++ src/modules/keyboard/Tests.cpp | 64 +++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/modules/keyboard/Tests.cpp diff --git a/src/modules/keyboard/CMakeLists.txt b/src/modules/keyboard/CMakeLists.txt index 4ee83ec14..40e8a85d7 100644 --- a/src/modules/keyboard/CMakeLists.txt +++ b/src/modules/keyboard/CMakeLists.txt @@ -21,3 +21,10 @@ calamares_add_plugin( keyboard calamaresui SHARED_LIB ) + +calamares_add_test( + keyboardtest + SOURCES + Tests.cpp + SetKeyboardLayoutJob.cpp +) diff --git a/src/modules/keyboard/Tests.cpp b/src/modules/keyboard/Tests.cpp new file mode 100644 index 000000000..95c317b79 --- /dev/null +++ b/src/modules/keyboard/Tests.cpp @@ -0,0 +1,64 @@ +/* === This file is part of Calamares - === + * + * SPDX-FileCopyrightText: 2020 Adriaan de Groot + * SPDX-License-Identifier: GPL-3.0-or-later + * + * Calamares is Free Software: see the License-Identifier above. + * + */ +#include "utils/Logger.h" + +#include + +// Internals of SetKeyboardLayoutJob.cpp +extern QString findLegacyKeymap( const QString& layout, const QString& model, const QString& variant ); + +class KeyboardLayoutTests : public QObject +{ + Q_OBJECT +public: + KeyboardLayoutTests() {} + virtual ~KeyboardLayoutTests() {} + +private Q_SLOTS: + void initTestCase(); + + void testSimpleLayoutLookup_data(); + void testSimpleLayoutLookup(); +}; + +void +KeyboardLayoutTests::initTestCase() +{ + Logger::setupLogLevel( Logger::LOGDEBUG ); +} + +void +KeyboardLayoutTests::testSimpleLayoutLookup_data() +{ + QTest::addColumn< QString >( "layout" ); + QTest::addColumn< QString >( "model" ); + QTest::addColumn< QString >( "variant" ); + QTest::addColumn< QString >( "vconsole" ); + + QTest::newRow( "us" ) << QString( "us" ) << QString() << QString() << QString( "us" ); +} + + +void +KeyboardLayoutTests::testSimpleLayoutLookup() +{ + QFETCH( QString, layout ); + QFETCH( QString, model ); + QFETCH( QString, variant ); + QFETCH( QString, vconsole ); + + QCOMPARE( findLegacyKeymap( layout, model, variant ), vconsole ); +} + + +QTEST_GUILESS_MAIN( KeyboardLayoutTests ) + +#include "utils/moc-warnings.h" + +#include "Tests.moc" From 2aece7ff1b372382b7e7f48052d7455c9a5a0cb4 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 9 Sep 2020 11:47:50 +0200 Subject: [PATCH 3/5] [keyboard] Warn if QRC is not available --- src/modules/keyboard/SetKeyboardLayoutJob.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/modules/keyboard/SetKeyboardLayoutJob.cpp b/src/modules/keyboard/SetKeyboardLayoutJob.cpp index a36604926..cabe0b5c0 100644 --- a/src/modules/keyboard/SetKeyboardLayoutJob.cpp +++ b/src/modules/keyboard/SetKeyboardLayoutJob.cpp @@ -82,13 +82,18 @@ SetKeyboardLayoutJob::findConvertedKeymap( const QString& convertedKeymapPath ) STATICTEST QString findLegacyKeymap( const QString& layout, const QString& model, const QString& variant ) { - cDebug() << "Looking for legacy keymap in QRC"; + cDebug() << "Looking for legacy keymap" << layout << model << variant << "in QRC"; int bestMatching = 0; QString name; QFile file( ":/kbd-model-map" ); - file.open( QIODevice::ReadOnly | QIODevice::Text ); + if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) + { + cDebug() << Logger::SubEntry << "Could not read QRC"; + return QString(); + } + QTextStream stream( &file ); while ( !stream.atEnd() ) { From aeffbac9cdde56d1be976dee6ac355ca8dae1129 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 9 Sep 2020 11:58:56 +0200 Subject: [PATCH 4/5] CMake: add resources to tests Some tests -- notably the keyboard module -- need to have the QRC for the module loaded as well (e.g. because of data in the QRC). Add a RESOURCES parameter to calamares_add_test() like calamares_add_plugin() already has, to build the resources into the test. Keyboard test now passes, since it was missing the data for lookups before. --- CMakeModules/CalamaresAddTest.cmake | 9 +++++++-- src/modules/keyboard/CMakeLists.txt | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CMakeModules/CalamaresAddTest.cmake b/CMakeModules/CalamaresAddTest.cmake index 5bedf81b5..56a45d7dc 100644 --- a/CMakeModules/CalamaresAddTest.cmake +++ b/CMakeModules/CalamaresAddTest.cmake @@ -14,6 +14,7 @@ # calamares_add_test( # # [GUI] +# [RESOURCES FILE] # SOURCES # ) @@ -24,13 +25,14 @@ function( calamares_add_test ) # parse arguments (name needs to be saved before passing ARGN into the macro) set( NAME ${ARGV0} ) set( options GUI ) + set( oneValueArgs NAME RESOURCES ) set( multiValueArgs SOURCES LIBRARIES DEFINITIONS ) - cmake_parse_arguments( TEST "${options}" "" "${multiValueArgs}" ${ARGN} ) + cmake_parse_arguments( TEST "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) set( TEST_NAME ${NAME} ) if( ECM_FOUND AND BUILD_TESTING ) ecm_add_test( - ${TEST_SOURCES} + ${TEST_SOURCES} ${TEST_RESOURCES} TEST_NAME ${TEST_NAME} LINK_LIBRARIES @@ -44,5 +46,8 @@ function( calamares_add_test ) if( TEST_GUI ) target_link_libraries( ${TEST_NAME} calamaresui Qt5::Gui ) endif() + if( TEST_RESOURCES ) + calamares_autorcc( ${TEST_NAME} ${TEST_RESOURCES} ) + endif() endif() endfunction() diff --git a/src/modules/keyboard/CMakeLists.txt b/src/modules/keyboard/CMakeLists.txt index 40e8a85d7..e9037bc03 100644 --- a/src/modules/keyboard/CMakeLists.txt +++ b/src/modules/keyboard/CMakeLists.txt @@ -27,4 +27,6 @@ calamares_add_test( SOURCES Tests.cpp SetKeyboardLayoutJob.cpp + RESOURCES + keyboard.qrc ) From 633186778b322234b705ba7dd40cebcf0efc789f Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 9 Sep 2020 12:11:50 +0200 Subject: [PATCH 5/5] [keyboard] Add test for Turkish F variant - test keyboard lookup for "tr" - "f" variations - add data mapping "tr" - "f" to legacy keymap "trf" FIXES #1397 --- src/modules/keyboard/Tests.cpp | 3 +++ src/modules/keyboard/kbd-model-map | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/modules/keyboard/Tests.cpp b/src/modules/keyboard/Tests.cpp index 95c317b79..16983685a 100644 --- a/src/modules/keyboard/Tests.cpp +++ b/src/modules/keyboard/Tests.cpp @@ -42,6 +42,9 @@ KeyboardLayoutTests::testSimpleLayoutLookup_data() QTest::addColumn< QString >( "vconsole" ); QTest::newRow( "us" ) << QString( "us" ) << QString() << QString() << QString( "us" ); + QTest::newRow( "turkish default" ) << QString( "tr" ) << QString() << QString() << QString( "trq" ); + QTest::newRow( "turkish alt-q" ) << QString( "tr" ) << QString() << QString( "alt" ) << QString( "trq" ); + QTest::newRow( "turkish f" ) << QString( "tr" ) << QString() << QString( "f" ) << QString( "trf" ); } diff --git a/src/modules/keyboard/kbd-model-map b/src/modules/keyboard/kbd-model-map index e113c92ba..6ec00c81b 100644 --- a/src/modules/keyboard/kbd-model-map +++ b/src/modules/keyboard/kbd-model-map @@ -12,6 +12,13 @@ # # Updates: # - 2018-09-26 Added "Austrian" keyboard (de at). Issue #1035 +# - 2020-09-09 Added "Turkish F" keyboard. Issue #1397 +# +# Note that keyboard variants should be listed from least to most-specific +# within a layout. Keyboard lookups only consider a subsequent +# line if it has a strictly better match than previous ones: +# listing specific variants early can mean a poor match with them +# is not overridden by a poor match with a later generic variant. # # Generated from system-config-keyboard's model list # consolelayout xlayout xmodel xvariant xoptions @@ -19,6 +26,7 @@ sg ch pc105 de_nodeadkeys terminate:ctrl_alt_bksp nl nl pc105 - terminate:ctrl_alt_bksp mk-utf mk,us pc105 - terminate:ctrl_alt_bksp,grp:shifts_toggle,grp_led:scroll trq tr pc105 - terminate:ctrl_alt_bksp +trf tr pc105 f terminate:ctrl_alt_bksp uk gb pc105 - terminate:ctrl_alt_bksp is-latin1 is pc105 - terminate:ctrl_alt_bksp de de pc105 - terminate:ctrl_alt_bksp