diff --git a/.github/workflows/gnome_unit_test.yml b/.github/workflows/gnome_unit_test.yml index 6934e552..107e6e45 100644 --- a/.github/workflows/gnome_unit_test.yml +++ b/.github/workflows/gnome_unit_test.yml @@ -13,7 +13,7 @@ jobs: unit-tests: name: Build and run unit tests runs-on: ubuntu-latest - + steps: - name: Configure cache if: ${{ !env.ACT }} @@ -32,7 +32,7 @@ jobs: - name: Install packages run: | sudo apt update - sudo apt-get install -y pkg-config libglib2.0-dev libnm-dev libcurl4-openssl-dev ninja-build + sudo apt-get install -y pkg-config libglib2.0-dev libnm-dev libcurl4-openssl-dev lcov ninja-build - name: Configure Python uses: actions/setup-python@v4 @@ -90,7 +90,7 @@ jobs: uses: actions/checkout@v3 with: path: networkmanager - + - name: Build networkmanager with Gnome Proxy run: > cmake @@ -99,7 +99,32 @@ jobs: -DCMAKE_TOOLCHAIN_FILE="${{ env.TOOLCHAIN_FILE }}" -DCMAKE_INSTALL_PREFIX="${{github.workspace}}/install/usr" -DCMAKE_MODULE_PATH="${{github.workspace}}/install/tools/cmake" + -DCMAKE_CXX_FLAGS=" -fprofile-arcs -ftest-coverage " -DENABLE_GNOME_NETWORKMANAGER=ON + -DENABLE_UNIT_TESTING=ON && cmake --build build/GnomeNetworkmanager --target install -j8 + + - name: Run unit tests without valgrind + run: > + PATH=${{github.workspace}}/install/usr/bin:${PATH} + LD_LIBRARY_PATH=${{github.workspace}}/install/usr/lib:${{github.workspace}}/install/usr/lib/wpeframework/plugins:${LD_LIBRARY_PATH} + tests + + - name: Generate coverage + run: | + lcov -c -o coverage.info -d build/GnomeNetworkmanager + lcov -r coverage.info '/usr/include/*' '*/build/GnomeNetworkmanager/*' \ + '*/install/usr/include/*' '*/Tests/*' 'googlemock/*' 'googletest/*' \ + -o filtered_coverage.info + - name: Generate the html report + run: | + genhtml filtered_coverage.info --output-directory /tmp/coverage_report + + - name: Upload the coverage report to Pull request using actions + uses: actions/upload-artifact@v4 + with: + name: coverage-report + path: /tmp/coverage_report + diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bfac119..48846adb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,8 @@ set(PROJECT_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) include(CmakeHelperFunctions) option(USE_RDK_LOGGER "Enable RDK Logger for logging" OFF ) +option(ENABLE_UNIT_TESTING "Enable unit tests" OFF) + string(TOLOWER ${NAMESPACE} STORAGE_DIRECTORY) get_directory_property(SEVICES_DEFINES COMPILE_DEFINITIONS) @@ -167,3 +169,7 @@ write_config() write_config(PLUGINS LegacyPlugin_NetworkAPIs CLASSNAME Network LOCATOR lib${PLUGIN_LEGACY_DEPRECATED_NETWORK}.so) write_config(PLUGINS LegacyPlugin_WiFiManagerAPIs CLASSNAME WiFiManager LOCATOR lib${PLUGIN_LEGACY_DEPRECATED_WIFI}.so) +if(ENABLE_UNIT_TESTING) + include(Tests/unit_test/tests.cmake) +endif(ENABLE_UNIT_TESTING) + diff --git a/Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp b/Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp new file mode 100644 index 00000000..a2e1d3f9 --- /dev/null +++ b/Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp @@ -0,0 +1,58 @@ +#include "WiFiSignalStrengthMonitor.h" +#include "NetworkManagerImplementation.h" +#include "NetworkManagerLogger.h" +#include "INetworkManager.h" +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace WPEFramework; +using namespace WPEFramework::Plugin; +namespace WPEFramework +{ + namespace Plugin + { + NetworkManagerImplementation* _instance; + void NetworkManagerImplementation::ReportWiFiSignalStrengthChange(const string ssid, const string strength, const WiFiSignalQuality quality) + { + return; + } + void NetworkManagerImplementation::ReportInternetStatusChange(const InternetStatus prevState, const InternetStatus currState) + { + return; + } + } +} + +class WiFiSignalStrengthMonitorTest : public ::testing::Test { + protected: + WPEFramework::Plugin::WiFiSignalStrengthMonitor monitor; + void SetUp() override { + } + void TearDown() override { + } +}; +#define BUFFER_SIZE 1024 + +TEST_F(WiFiSignalStrengthMonitorTest, GetSignalData_Connected) { + std::string ssid = "TestSSID"; + Exchange::INetworkManager::WiFiSignalQuality quality; + std::string strengthOut= "-55"; + monitor.getSignalData(ssid, quality, strengthOut); +} + +TEST_F(WiFiSignalStrengthMonitorTest, StartWiFiSignalStrengthMonitor) { + monitor.startWiFiSignalStrengthMonitor(1); + +} + +int main(int argc, char **argv) { + ::testing::InitGoogleMock(&argc, argv); + return RUN_ALL_TESTS(); +} + diff --git a/Tests/unit_test/tests.cmake b/Tests/unit_test/tests.cmake new file mode 100644 index 00000000..177c0294 --- /dev/null +++ b/Tests/unit_test/tests.cmake @@ -0,0 +1,43 @@ +cmake_minimum_required(VERSION 3.10) +set(UNIT_TEST "tests") + +find_package(PkgConfig REQUIRED) +pkg_check_modules(GLIB REQUIRED glib-2.0) +pkg_check_modules(GIO REQUIRED gio-2.0) +pkg_check_modules(LIBNM REQUIRED libnm) + +include(FetchContent) +FetchContent_Declare( + googletest + URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip +) + +FetchContent_MakeAvailable(googletest) +add_executable(${UNIT_TEST} + Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp + WiFiSignalStrengthMonitor.cpp + NetworkManagerLogger.cpp + NetworkManagerConnectivity.cpp +) +set_target_properties(${UNIT_TEST} PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES +) +target_compile_options(${UNIT_TEST} PRIVATE -Wall -include ${CMAKE_SOURCE_DIR}/INetworkManager.h) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage") + +target_include_directories(${UNIT_TEST} PRIVATE + ${GLIB_INCLUDE_DIRS} + ${LIBNM_INCLUDE_DIRS} + ${GIO_INCLUDE_DIRS} + ${PROJECT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR} + Tests + ${gtest_SOURCE_DIR}/include + ${gtest_SOURCE_DIR}/../googlemock/include +) + +target_link_libraries(${UNIT_TEST} PRIVATE gmock_main ${NAMESPACE}Core::${NAMESPACE}Core ${GLIB_LIBRARIES} ${GIO_LIBRARIES} ${LIBNM_LIBRARIES} ${CURL_LIBRARIES}) +target_include_directories(${UNIT_TEST} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) +install(TARGETS ${UNIT_TEST} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) + diff --git a/WiFiSignalStrengthMonitor.cpp b/WiFiSignalStrengthMonitor.cpp index 839de661..09b33cc2 100644 --- a/WiFiSignalStrengthMonitor.cpp +++ b/WiFiSignalStrengthMonitor.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "NetworkManagerLogger.h" #include "NetworkManagerImplementation.h" #include "WiFiSignalStrengthMonitor.h"