From 6c166cfa6976c314452e5c0c75c833a1613c7b3d Mon Sep 17 00:00:00 2001 From: ColinDuquesnoy Date: Sun, 17 Sep 2017 12:34:11 +0200 Subject: [PATCH] #115 Add platform class --- .../PlantUml/FilterPluginsByPlatform.puml | 71 ++++++++++ .../Infrastructure/CMakeLists.txt | 8 ++ .../Infrastructure/Platform/Platform.cpp | 59 +++++++++ .../Infrastructure/Platform/Platform.hpp | 46 +++++++ .../Infrastructure/Platform/PlatformTests.cpp | 125 ++++++++++++++++++ 5 files changed, 309 insertions(+) create mode 100644 docs/developers/PlantUml/FilterPluginsByPlatform.puml create mode 100644 lib/MellowPlayer/Infrastructure/Platform/Platform.cpp create mode 100644 lib/MellowPlayer/Infrastructure/Platform/Platform.hpp create mode 100644 tests/UnitTests/Infrastructure/Platform/PlatformTests.cpp diff --git a/docs/developers/PlantUml/FilterPluginsByPlatform.puml b/docs/developers/PlantUml/FilterPluginsByPlatform.puml new file mode 100644 index 00000000..91be6633 --- /dev/null +++ b/docs/developers/PlantUml/FilterPluginsByPlatform.puml @@ -0,0 +1,71 @@ +@startuml + +package MellowPlayer::Application { + interface IStreamingServiceLoader { + + load() + } +} + +package MellowPlayer::Infrastructure { + class StreamingServiceLoader { + + load() + - _filtersFactory: IPlatformFiltersFactory + - _platform: IPlatform + } + + IStreamingServiceLoader <|-- StreamingServiceLoader + + StreamingServiceLoader -left-> IPlatformFiltersFactory: _filtersFactory + StreamingServiceLoader -right-> IPlatform: _platform + + interface IPlatform { + + name(): QString + -- + + isLinux() : bool + + isAppImage() : bool + + isWindows() : bool + + isOsx() : bool + -- + + match(QList): bool + } + + class Platform + IPlatform <|-- Platform + + Platform .left.> IPlatformFilter: <> + + interface IPlatformFiltersFactory { + + create(QString) : QList + } + + class PlatformFiltersFactory { + + create(QString) : QList + } + + IPlatformFiltersFactory <|-- PlatformFiltersFactory + + interface IPlatformFilter { + match(IPlatform): bool + } + IPlatform <.. IPlatformFilter: <> + + class AllFilter + IPlatformFilter <|-- AllFilter + note left + Always return true + end note + + class LinuxFilter + IPlatformFilter <|-- LinuxFilter + + class AppImageFilter + IPlatformFilter <|-- AppImageFilter + + class OsxFilter + IPlatformFilter <|-- OsxFilter + + class WindowsFilter + IPlatformFilter <|-- WindowsFilter +} + +@enduml \ No newline at end of file diff --git a/lib/MellowPlayer/Infrastructure/CMakeLists.txt b/lib/MellowPlayer/Infrastructure/CMakeLists.txt index 67f4606b..8ffcda53 100644 --- a/lib/MellowPlayer/Infrastructure/CMakeLists.txt +++ b/lib/MellowPlayer/Infrastructure/CMakeLists.txt @@ -5,7 +5,15 @@ set(LIB_NAME "${PROJECT_NAME}.Infrastructure") glob_recurse_excl(SOURCE_FILES *.cpp "Platform") glob_recurse_excl(HEADER_FILES *.hpp "Platform") +file(GLOB_RECURSE PLATFORM_FILTERS_SOURCES Platform/Filters/*.cpp) +file(GLOB_RECURSE PLATFORM_FILTERS_HEADERS Platform/Filters/*.cpp) +set(SOURCE_FILES ${SOURCE_FILES} ${PLATFORM_FILTERS_SOURCES}) +set(HEADER_FILES ${HEADER_FILES} ${PLATFORM_FILTERS_HEADERS}) + set(SOURCE_FILES ${SOURCE_FILES} ${CMAKE_SOURCE_DIR}/3rdparty/libqxt/src/widgets/qxtglobalshortcut.cpp) +set(SOURCE_FILES ${SOURCE_FILES} Platform/Platform.cpp) +set(HEADER_FILES ${HEADER_FILES} Platform/Platform.hpp) + if (WIN32) set(SOURCE_FILES ${SOURCE_FILES} ${CMAKE_SOURCE_DIR}/3rdparty/libqxt/src/widgets/win/qxtglobalshortcut_win.cpp) file(GLOB_RECURSE PLATFORM_SOURCE_FILES Platform/Windows/*.cpp) diff --git a/lib/MellowPlayer/Infrastructure/Platform/Platform.cpp b/lib/MellowPlayer/Infrastructure/Platform/Platform.cpp new file mode 100644 index 00000000..0f3634af --- /dev/null +++ b/lib/MellowPlayer/Infrastructure/Platform/Platform.cpp @@ -0,0 +1,59 @@ +#include "Platform.hpp" + +using namespace MellowPlayer::Infrastructure; + + +QString Platform::Unsupported = "Unsupported platform"; +QString Platform::Linux = "GNU/Linux"; +QString Platform::AppImage = "AppImage"; +QString Platform::Windows = "Windows"; +QString Platform::OSX = "Mac OSX"; + +QString Platform::name() const +{ + QString plaformName = Unsupported; + +#ifdef Q_OS_LINUX + QString appImagePath = qgetenv("APPIMAGE"); + if (appImagePath.isEmpty()) + plaformName = Linux; + else + plaformName = AppImage; +#endif + +#ifdef Q_OS_OSX + plaformName = OSX; +#endif + +#ifdef Q_OS_WIN + plaformName = Windows; +#endif + + return plaformName; +} + +bool Platform::match(QList&) const +{ + return false; +} + +bool Platform::isLinux() const +{ + return name() == Linux; +} + +bool Platform::isAppImage() const +{ + return name() == AppImage; +} + +bool Platform::isWindows() const +{ + return name() == Windows; +} + +bool Platform::isOsx() const +{ + return name() == OSX; +} + diff --git a/lib/MellowPlayer/Infrastructure/Platform/Platform.hpp b/lib/MellowPlayer/Infrastructure/Platform/Platform.hpp new file mode 100644 index 00000000..ab0f94f2 --- /dev/null +++ b/lib/MellowPlayer/Infrastructure/Platform/Platform.hpp @@ -0,0 +1,46 @@ +#pragma once + +#include + +namespace MellowPlayer::Infrastructure +{ + class IPlatformFilter; + + class IPlatform + { + public: + virtual ~IPlatform() = default; + + virtual QString name() const = 0; + + virtual bool isLinux() const = 0; + virtual bool isAppImage() const = 0; + virtual bool isWindows() const = 0; + virtual bool isOsx() const = 0; + + virtual bool match(QList& filters) const = 0; + }; + + class Platform: public IPlatform + { + public: + QString name() const override; + + bool isLinux() const override; + + bool isAppImage() const override; + + bool isWindows() const override; + + bool isOsx() const override; + + bool match(QList& filters) const override; + + private: + static QString Unsupported; + static QString Linux; + static QString AppImage; + static QString Windows; + static QString OSX; + }; +} diff --git a/tests/UnitTests/Infrastructure/Platform/PlatformTests.cpp b/tests/UnitTests/Infrastructure/Platform/PlatformTests.cpp new file mode 100644 index 00000000..7f4047fd --- /dev/null +++ b/tests/UnitTests/Infrastructure/Platform/PlatformTests.cpp @@ -0,0 +1,125 @@ +#include +#include + +using namespace MellowPlayer::Infrastructure; + +SCENARIO("PlatformTests") +{ + GIVEN("A platform instance") + { + Platform platform; + + WHEN("Call name()") + { + auto name = platform.name(); + + THEN("name is not empty") + { + REQUIRE(!name.isEmpty()); + } + } + +#ifdef Q_OS_LINUX + WHEN("on GNU/Linux with no APPIMAGE environment variable defined") + { + THEN("isLinux returns true") + { + REQUIRE(platform.isLinux()); + } + + THEN("isAppImage returns false") + { + REQUIRE(!platform.isAppImage()); + } + + THEN("isOsx returns false") + { + REQUIRE(!platform.isOsx()); + } + + THEN("isWindows returns false") + { + REQUIRE(!platform.isWindows()); + } + } + + WHEN("on GNU/Linux with APPIMAGE environment variable defined") + { + qputenv("APPIMAGE", "/path/to/appimage"); + + THEN("isAppImage returns true") + { + REQUIRE(platform.isAppImage()); + } + + THEN("isLinux returns false") + { + REQUIRE(!platform.isLinux()); + } + + THEN("isOsx returns false") + { + REQUIRE(!platform.isOsx()); + } + + THEN("isWindows returns false") + { + REQUIRE(!platform.isWindows()); + } + + qputenv("APPIMAGE", ""); + } +#endif + +#ifdef Q_OS_OSX + WHEN("on Mac OSX") + { + THEN("isOsx returns true") + { + REQUIRE(platform.isOsx()); + } + + THEN("isLinux returns false") + { + REQUIRE(!platform.isLinux()); + } + + THEN("isAppImage returns false") + { + REQUIRE(!platform.isAppImage()); + } + + THEN("isWindows returns false") + { + REQUIRE(!platform.isWindows()); + } + } +#endif + +#ifdef Q_OS_WIN + WHEN("on Windows") + { + THEN("isWindows returns true") + { + REQUIRE(platform.isWindows()); + } + + THEN("isOsx returns false") + { + REQUIRE(!platform.isOsx()); + } + + THEN("isLinux returns false") + { + REQUIRE(!platform.isLinux()); + } + + THEN("isAppImage returns false") + { + REQUIRE(!platform.isAppImage()); + } + } +#endif + + } +} \ No newline at end of file