Skip to content

Commit

Permalink
tests/libgestures/condition: add
Browse files Browse the repository at this point in the history
  • Loading branch information
taj-ny committed Oct 16, 2024
1 parent 0a57c39 commit d7bd88f
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 1 deletion.
14 changes: 13 additions & 1 deletion tests/libgestures/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
add_subdirectory(gestures)
add_subdirectory(gestures)

set(testCondition_SRCS
test_condition.cpp
mockwindowdataprovider.cpp
)
qt_add_executable(testCondition ${testCondition_SRCS})
add_test(NAME "condition" COMMAND testCondition)
target_link_libraries(testCondition PRIVATE
libgestures
Qt::Core
Qt::Test
)
13 changes: 13 additions & 0 deletions tests/libgestures/mockwindowdataprovider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "mockwindowdataprovider.h"

#include <utility>

MockWindowDataProvider::MockWindowDataProvider(std::optional<const WindowData> data)
: m_data(std::move(data))
{
}

std::optional<const WindowData> MockWindowDataProvider::getDataForActiveWindow() const
{
return m_data;
}
11 changes: 11 additions & 0 deletions tests/libgestures/mockwindowdataprovider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "windowdataprovider.h"

class MockWindowDataProvider : public WindowDataProvider
{
public:
MockWindowDataProvider(std::optional<const WindowData> data);

std::optional<const WindowData> getDataForActiveWindow() const override;
private:
const std::optional<const WindowData> m_data;
};
163 changes: 163 additions & 0 deletions tests/libgestures/test_condition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#include "condition.h"
#include "test_condition.h"

TestCondition::TestCondition()
: m_noActiveWindow(std::make_shared<MockWindowDataProvider>(std::nullopt)),
m_normalWindow(std::make_shared<MockWindowDataProvider>(std::make_optional<WindowData>("Firefox", "firefox", "firefox", WindowState::Unimportant))),
m_maximizedWindow(std::make_shared<MockWindowDataProvider>(std::make_optional<WindowData>("Firefox", "firefox", "firefox", WindowState::Maximized))),
m_fullscreenWindow(std::make_shared<MockWindowDataProvider>(std::make_optional<WindowData>("Firefox", "firefox", "firefox", WindowState::Maximized)))
{
}

void TestCondition::isSatisfied_noActiveWindow_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_noActiveWindow, false, std::nullopt, std::nullopt);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndNoActiveWindow_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_noActiveWindow, true, std::nullopt, std::nullopt);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_noSubConditions_returnsTrue()
{
const auto condition = std::make_shared<Condition>(m_normalWindow, false, std::nullopt, std::nullopt);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndNoSubConditions_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_normalWindow, true, std::nullopt, std::nullopt);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_windowClassRegexMatchesResourceClass_returnsTrue()
{
const auto window = std::make_shared<MockWindowDataProvider>(std::make_optional<WindowData>("Firefox", "firefox", "", WindowState::Unimportant));
const auto condition = std::make_shared<Condition>(window, false, QRegularExpression("firefox"), std::nullopt);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_windowClassRegexMatchesResourceName_returnsTrue()
{
const auto window = std::make_shared<MockWindowDataProvider>(std::make_optional<WindowData>("Firefox", "", "firefox", WindowState::Unimportant));
const auto condition = std::make_shared<Condition>(window, false, QRegularExpression("firefox"), std::nullopt);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_windowClassRegexDoesntMatch_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_normalWindow, false, QRegularExpression("firefoxx"), std::nullopt);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndWindowClassRegexMatches_returnsFalse()
{
const auto window = std::make_shared<MockWindowDataProvider>(std::make_optional<WindowData>("Firefox", "firefox", "", WindowState::Unimportant));
const auto condition = std::make_shared<Condition>(window, true, QRegularExpression("firefox"), std::nullopt);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndWindowClassRegexDoesntMatch_returnsTrue()
{
const auto condition = std::make_shared<Condition>(m_normalWindow, true, QRegularExpression("firefoxx"), std::nullopt);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_windowStateDoesntMatch_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_normalWindow, false, std::nullopt, WindowState::Maximized);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_windowStateMatches_returnsTrue()
{
const auto condition = std::make_shared<Condition>(m_maximizedWindow, false, std::nullopt, WindowState::Maximized);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_windowStateMatchesEither_returnsTrue()
{
const auto condition = std::make_shared<Condition>(m_maximizedWindow, false, std::nullopt, WindowState::Fullscreen | WindowState::Maximized);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndWindowStateDoesntMatch_returnsTrue()
{
const auto condition = std::make_shared<Condition>(m_normalWindow, true, std::nullopt, WindowState::Maximized);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndWindowStateMatches_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_maximizedWindow, true, std::nullopt, WindowState::Maximized);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndWindowStateMatchesEither_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_maximizedWindow, true, std::nullopt, WindowState::Fullscreen | WindowState::Maximized);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_windowClassMatchesAndStateDoesnt_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_normalWindow, false, QRegularExpression("firefox"), WindowState::Maximized);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_windowClassDoesntMatchAndStateDoes_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_maximizedWindow, false, QRegularExpression("firefoxx"), WindowState::Maximized);

QVERIFY(!condition->isSatisfied());
}

void TestCondition::isSatisfied_windowClassMatchesAndStateMatches_returnsTrue()
{
const auto condition = std::make_shared<Condition>(m_maximizedWindow, false, QRegularExpression("firefox"), WindowState::Maximized);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndWindowClassMatchesAndStateDoesnt_returnsTrue()
{
const auto condition = std::make_shared<Condition>(m_normalWindow, true, QRegularExpression("firefox"), WindowState::Maximized);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndWindowClassDoesntMatchAndStateDoes_returnsTrue()
{
const auto condition = std::make_shared<Condition>(m_maximizedWindow, true, QRegularExpression("firefoxx"), WindowState::Maximized);

QVERIFY(condition->isSatisfied());
}

void TestCondition::isSatisfied_negatedAndWindowClassMatchesAndStateMatches_returnsFalse()
{
const auto condition = std::make_shared<Condition>(m_maximizedWindow, true, QRegularExpression("firefox"), WindowState::Maximized);

QVERIFY(!condition->isSatisfied());
}

QTEST_MAIN(TestCondition)
#include "test_condition.moc"
39 changes: 39 additions & 0 deletions tests/libgestures/test_condition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "mockwindowdataprovider.h"
#include <QTest>

class TestCondition : public QObject
{
Q_OBJECT
public:
TestCondition();
private slots:
void isSatisfied_noActiveWindow_returnsFalse();
void isSatisfied_negatedAndNoActiveWindow_returnsFalse();
void isSatisfied_noSubConditions_returnsTrue();
void isSatisfied_negatedAndNoSubConditions_returnsFalse();
void isSatisfied_windowClassRegexMatchesResourceClass_returnsTrue();
void isSatisfied_windowClassRegexMatchesResourceName_returnsTrue();
void isSatisfied_windowClassRegexDoesntMatch_returnsFalse();
void isSatisfied_negatedAndWindowClassRegexMatches_returnsFalse();
void isSatisfied_negatedAndWindowClassRegexDoesntMatch_returnsTrue();
void isSatisfied_windowStateDoesntMatch_returnsFalse();
void isSatisfied_windowStateMatches_returnsTrue();
void isSatisfied_windowStateMatchesEither_returnsTrue();
void isSatisfied_negatedAndWindowStateDoesntMatch_returnsTrue();
void isSatisfied_negatedAndWindowStateMatches_returnsFalse();
void isSatisfied_negatedAndWindowStateMatchesEither_returnsFalse();
void isSatisfied_windowClassMatchesAndStateDoesnt_returnsFalse();
void isSatisfied_windowClassDoesntMatchAndStateDoes_returnsFalse();
void isSatisfied_windowClassMatchesAndStateMatches_returnsTrue();
void isSatisfied_negatedAndWindowClassMatchesAndStateDoesnt_returnsTrue();
void isSatisfied_negatedAndWindowClassDoesntMatchAndStateDoes_returnsTrue();
void isSatisfied_negatedAndWindowClassMatchesAndStateMatches_returnsFalse();
private:
const std::shared_ptr<MockWindowDataProvider> m_noActiveWindow;
const std::shared_ptr<MockWindowDataProvider> m_normalWindow;
const std::shared_ptr<MockWindowDataProvider> m_maximizedWindow;
const std::shared_ptr<MockWindowDataProvider> m_fullscreenWindow;
};

0 comments on commit d7bd88f

Please sign in to comment.