Skip to content

Commit

Permalink
Add tests for ProgressBarCounter angle and spans calculations.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Dec 23, 2024
1 parent 10f6bef commit 87f1df0
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 32 deletions.
5 changes: 0 additions & 5 deletions include/wble/ProgressBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ class WBLE_EXPORT ProgressBar : public QWidget

void initPainter(QPainter* painter) const override;

static constexpr int FULL_DEGREE{16};
static constexpr double HUNDREDTH_OF_FULL_CIRCLE{3.6};
static constexpr int QUARTER_CIRCLE_ANGLE{90};
static constexpr int HALF_CIRCLE_ANGLE{2 * QUARTER_CIRCLE_ANGLE};

/**
* Paint graphically progress bar arc.
* @param painter Painter which should be used.
Expand Down
6 changes: 3 additions & 3 deletions src/ProgressBarCounter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <wble/ProgressBarCounter.h>
#include "Utilities.h"

ProgressBarCounter::ProgressBarCounter(QString title, int maxValue,
QWidget* parent)
Expand Down Expand Up @@ -27,9 +28,8 @@ void ProgressBarCounter::updateProgress(int newValue)

void ProgressBarCounter::paintProgressBar(QPainter& painter)
{
constexpr int startAngle{QUARTER_CIRCLE_ANGLE * FULL_DEGREE};
const int spanAngle{static_cast<int>(std::lround(
-currentPercent_ * HUNDREDTH_OF_FULL_CIRCLE * FULL_DEGREE))};
const int startAngle{utilities::getStartingAngleCounter()};
const int spanAngle{utilities::getSpanAngleCounter(currentPercent_)};
painter.drawArc(getArcRectangle(), startAngle, spanAngle);
painter.drawText(getArcRectangle(), Qt::AlignCenter,
QString::number(currentPercent_, 'f', 0) + "%");
Expand Down
6 changes: 3 additions & 3 deletions src/ProgressBarInfinite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ void ProgressBarInfinite::reset()

void ProgressBarInfinite::paintProgressBar(QPainter& painter)
{
const auto [firstAngle,
secondAngle]{utilities::getStartingAngles(progressCounter_)};
const int spanAngle{utilities::getSpanAngle()};
const auto [firstAngle, secondAngle]{
utilities::getStartingAnglesInfinite(progressCounter_)};
const int spanAngle{utilities::getSpanAngleInfinite()};

painter.drawArc(getArcRectangle(), firstAngle, spanAngle);
painter.drawArc(getArcRectangle(), secondAngle, spanAngle);
Expand Down
17 changes: 15 additions & 2 deletions src/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bool doublesAreEqual(double left, double right)
std::max({1.0, std::abs(left), std::abs(right)}));
}

std::pair<int, int> getStartingAngles(int progressCounter)
std::pair<int, int> getStartingAnglesInfinite(int progressCounter)
{
int firstAngle{static_cast<int>(
std::lround(progressCounter * HUNDREDTH_OF_FULL_CIRCLE * FULL_DEGREE))};
Expand All @@ -31,10 +31,23 @@ std::pair<int, int> getStartingAngles(int progressCounter)
return {firstAngle, secondAngle};
}

int getSpanAngle()
int getStartingAngleCounter()
{
constexpr int startAngle{QUARTER_CIRCLE_ANGLE * FULL_DEGREE};
return startAngle;
}

int getSpanAngleInfinite()
{
constexpr int step{45};
constexpr const int spanAngle{-step * FULL_DEGREE};
return spanAngle;
}

int getSpanAngleCounter(int progressCounter)
{
const int spanAngle{static_cast<int>(std::lround(
-progressCounter * HUNDREDTH_OF_FULL_CIRCLE * FULL_DEGREE))};
return spanAngle;
}
} // namespace utilities
9 changes: 7 additions & 2 deletions src/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ namespace utilities
{
bool doublesAreEqual(double left, double right);

std::pair<int, int> getStartingAngles(int progressCounter);
std::pair<int, int> getStartingAnglesInfinite(int progressCounter);

int getStartingAngleCounter();

int getSpanAngleInfinite();

int getSpanAngleCounter(int progressCounter);

int getSpanAngle();
} // namespace utilities
55 changes: 42 additions & 13 deletions tests/UtilitiesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,58 @@

#include "Utilities.h"

void UtilitiesTest::testAnglesZeroProgress()
namespace
{
auto [firstAngle, secondAngle]{utilities::getStartingAngles(0)};
constexpr int FULL_CIRCLE_ANGLE{5760};
} // namespace

void UtilitiesTest::testAnglesInfiniteZeroProgress()
{
auto [firstAngle, secondAngle]{utilities::getStartingAnglesInfinite(0)};
QCOMPARE(firstAngle, 0);
QCOMPARE(secondAngle, 2880);
QCOMPARE(secondAngle, FULL_CIRCLE_ANGLE / 2);
}

void UtilitiesTest::testAnglesHalfProgress()
void UtilitiesTest::testAnglesInfiniteHalfProgress()
{
auto [firstAngle, secondAngle]{utilities::getStartingAngles(50)};
QCOMPARE(firstAngle, 2880);
QCOMPARE(secondAngle, 5760);
auto [firstAngle, secondAngle]{utilities::getStartingAnglesInfinite(50)};
QCOMPARE(firstAngle, FULL_CIRCLE_ANGLE / 2);
QCOMPARE(secondAngle, FULL_CIRCLE_ANGLE);
}

void UtilitiesTest::testAnglesFullProgress()
void UtilitiesTest::testAnglesInfiniteFullProgress()
{
auto [firstAngle, secondAngle]{utilities::getStartingAngles(100)};
QCOMPARE(firstAngle, 5760);
QCOMPARE(secondAngle, 8640);
auto [firstAngle, secondAngle]{utilities::getStartingAnglesInfinite(100)};
QCOMPARE(firstAngle, FULL_CIRCLE_ANGLE);
QCOMPARE(secondAngle, FULL_CIRCLE_ANGLE + FULL_CIRCLE_ANGLE / 2);
}

void UtilitiesTest::testGetSpanAngle()
void UtilitiesTest::testGetSpanAngleInfinite()
{
const int spanAngle{utilities::getSpanAngle()};
const int spanAngle{utilities::getSpanAngleInfinite()};
QCOMPARE(spanAngle, -720);
}

void UtilitiesTest::testStartingAngleCounter()
{
const int angle{utilities::getStartingAngleCounter()};
QCOMPARE(angle, 1440);
}

void UtilitiesTest::testGetSpanAngleCounterZero()
{
const int spanAngle{utilities::getSpanAngleCounter(0)};
QCOMPARE(spanAngle, 0);
}

void UtilitiesTest::testGetSpanAngleCounterHalf()
{
const int spanAngle{utilities::getSpanAngleCounter(50)};
QCOMPARE(spanAngle, -FULL_CIRCLE_ANGLE / 2);
}

void UtilitiesTest::testGetSpanAngleCounterFull()
{
const int spanAngle{utilities::getSpanAngleCounter(100)};
QCOMPARE(spanAngle, -FULL_CIRCLE_ANGLE);
}
14 changes: 10 additions & 4 deletions tests/UtilitiesTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ class UtilitiesTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
static void testAnglesZeroProgress();
static void testAnglesInfiniteZeroProgress();

static void testAnglesHalfProgress();
static void testAnglesInfiniteHalfProgress();

static void testAnglesFullProgress();
static void testAnglesInfiniteFullProgress();

static void testGetSpanAngle();
static void testGetSpanAngleInfinite();

static void testStartingAngleCounter();

static void testGetSpanAngleCounterZero();
static void testGetSpanAngleCounterHalf();
static void testGetSpanAngleCounterFull();
};

0 comments on commit 87f1df0

Please sign in to comment.