Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/plugin-qt/shortcut/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@

find_package(Qt6 REQUIRED COMPONENTS Test)

add_executable(tst-brightnesspolicy
tst_brightnesspolicy.cpp
)

target_include_directories(tst-brightnesspolicy PRIVATE
../tools/dde-shortcut-tool
)

target_link_libraries(tst-brightnesspolicy PRIVATE
Qt6::Core
Qt6::Test
)

add_test(NAME shortcut-brightnesspolicy COMMAND tst-brightnesspolicy)

add_executable(tst-physicalkeyalias
tst_physicalkeyalias.cpp
../src/core/physicalkeyalias.cpp
Expand Down
38 changes: 38 additions & 0 deletions src/plugin-qt/shortcut/tests/tst_brightnesspolicy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <QtTest>

Check warning on line 5 in src/plugin-qt/shortcut/tests/tst_brightnesspolicy.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QtTest> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "constant.h"

Check warning on line 7 in src/plugin-qt/shortcut/tests/tst_brightnesspolicy.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "constant.h" not found.

class BrightnessPolicyTest : public QObject
{
Q_OBJECT

private Q_SLOTS:

Check warning on line 13 in src/plugin-qt/shortcut/tests/tst_brightnesspolicy.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.
void clampsAtLowerBound();
void clampsAtUpperBound();
void keepsValueInsideRange();
};

void BrightnessPolicyTest::clampsAtLowerBound()
{
QCOMPARE(Brightness::adjustedValue(10.0, false), 10.0);
QCOMPARE(Brightness::adjustedValue(12.0, false), 10.0);
}

void BrightnessPolicyTest::clampsAtUpperBound()
{
QCOMPARE(Brightness::adjustedValue(100.0, true), 100.0);
}

void BrightnessPolicyTest::keepsValueInsideRange()
{
QCOMPARE(Brightness::adjustedValue(50.0, false), 45.0);
QCOMPARE(Brightness::adjustedValue(50.0, true), 55.0);
}

QTEST_APPLESS_MAIN(BrightnessPolicyTest)

#include "tst_brightnesspolicy.moc"

Check warning on line 38 in src/plugin-qt/shortcut/tests/tst_brightnesspolicy.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "tst_brightnesspolicy.moc" not found.
20 changes: 20 additions & 0 deletions src/plugin-qt/shortcut/tools/dde-shortcut-tool/constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ enum PowerAction {
PowerActionShowUI = 4
};

namespace Brightness {

constexpr double Step = 5.0;
constexpr double Minimum = 10.0;
constexpr double Maximum = 100.0;

inline double adjustedValue(double current, bool raised)
{
const double value = current + (raised ? Step : -Step);
if (value < Minimum) {
return Minimum;
}
if (value > Maximum) {
return Maximum;
}
return value;
}

} // namespace Brightness

namespace Config {

// DConfig App ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "treelandbrightnesscontroller.h"
#include "constant.h"

#include <QDebug>

Check warning on line 8 in src/plugin-qt/shortcut/tools/dde-shortcut-tool/treelandbrightnesscontroller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDir>

Check warning on line 9 in src/plugin-qt/shortcut/tools/dde-shortcut-tool/treelandbrightnesscontroller.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QEventLoop>
#include <QLockFile>
#include <QStandardPaths>
Expand All @@ -17,7 +18,6 @@

namespace {

constexpr double BrightnessStep = 5.0;
constexpr int BrightnessCommitTimeoutMs = 1000;
constexpr int BrightnessLockTimeoutMs = 1500;

Expand Down Expand Up @@ -49,8 +49,7 @@

double TreelandColorControl::changeBrightness(bool raised)
{
const double delta = raised ? BrightnessStep : -BrightnessStep;
const double target = std::clamp(m_brightness + delta, 0.0, 100.0);
const double target = Brightness::adjustedValue(m_brightness, raised);

set_brightness(wl_fixed_from_double(target));
commit();
Expand Down
Loading