Skip to content

Commit

Permalink
fix(settings): better API test and clean persistence reset (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrogTheFrog authored Jan 26, 2025
1 parent f434ff2 commit 53a0ea5
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace display_device {
*
* In case the settings cannot be reverted, because the display is turned or some other reason,
* this allows to "accept" the current state and start from scratch, but only if the persistence was
* cleared successfuly.
* cleared successfully.
* @examples
* SettingsManagerInterface* iface = getIface(...);
* auto result = iface->applySettings(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace display_device {
* @brief Set new display modes for the devices.
* @param modes A map of modes to set.
* @returns True if modes were set, false otherwise.
* @warning if any of the specified devices are duplicated, modes modes be provided
* @warning if any of the specified devices are duplicated, modes be provided
* for duplicates too!
* @examples
* WinDisplayDeviceInterface* iface = getIface(...);
Expand Down
5 changes: 2 additions & 3 deletions src/windows/settings_manager_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ namespace display_device {
}

bool SettingsManager::resetPersistence() {
// Trying to revert one more time in case we succeed.
if (revertSettings() == RevertResult::Ok) {
DD_LOG(info) << "Trying to reset persistent display device settings.";
if (const auto &cached_state {m_persistence_state->getState()}; !cached_state) {
return true;
}

DD_LOG(info) << "Trying to reset persistent display device settings.";
if (!m_persistence_state->persistState(std::nullopt)) {
DD_LOG(error) << "Failed to clear persistence!";
return false;
Expand Down
11 changes: 2 additions & 9 deletions src/windows/win_display_device_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@ namespace display_device {
}

bool WinDisplayDevice::isApiAccessAvailable() const {
const auto display_data {m_w_api->queryDisplayConfig(QueryType::All)};
if (!display_data) {
DD_LOG(debug) << "WinDisplayDevice::isApiAccessAvailable failed while querying display data.";
return false;
}

// Here we are supplying the retrieved display data back to SetDisplayConfig (with VALIDATE flag only, so that we make no actual changes).
// Unless something is really broken on Windows, this call should never fail under normal circumstances - the configuration is 100% correct, since it was
// provided by Windows.
const UINT32 flags {SDC_VALIDATE | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_VIRTUAL_MODE_AWARE};
const LONG result {m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags)};
const UINT32 flags {SDC_VALIDATE | SDC_USE_DATABASE_CURRENT};
const LONG result {m_w_api->setDisplayConfig({}, {}, flags)};

DD_LOG(debug) << "WinDisplayDevice::isApiAccessAvailable result: " << m_w_api->getErrorString(result);
return result == ERROR_SUCCESS;
Expand Down
9 changes: 0 additions & 9 deletions tests/unit/windows/test_settings_manager_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ TEST_F_S_MOCKED(ResetPersistence, FailedToReset) {
EXPECT_CALL(*m_settings_persistence_api, load())
.Times(1)
.WillOnce(Return(serializeState(ut_consts::SDCS_FULL)));
EXPECT_CALL(*m_dd_api, isApiAccessAvailable())
.Times(1)
.WillOnce(Return(false));
EXPECT_CALL(*m_settings_persistence_api, clear())
.Times(1)
.WillOnce(Return(false));
Expand All @@ -120,9 +117,6 @@ TEST_F_S_MOCKED(ResetPersistence, PersistenceReset, NoCapturedDevice) {
EXPECT_CALL(*m_settings_persistence_api, load())
.Times(1)
.WillOnce(Return(serializeState(ut_consts::SDCS_FULL)));
EXPECT_CALL(*m_dd_api, isApiAccessAvailable())
.Times(1)
.WillOnce(Return(false));
EXPECT_CALL(*m_settings_persistence_api, clear())
.Times(1)
.WillOnce(Return(true));
Expand All @@ -138,9 +132,6 @@ TEST_F_S_MOCKED(ResetPersistence, PersistenceReset, WithCapturedDevice) {
EXPECT_CALL(*m_settings_persistence_api, load())
.Times(1)
.WillOnce(Return(serializeState(ut_consts::SDCS_FULL)));
EXPECT_CALL(*m_dd_api, isApiAccessAvailable())
.Times(1)
.WillOnce(Return(false));
EXPECT_CALL(*m_settings_persistence_api, clear())
.Times(1)
.WillOnce(Return(true));
Expand Down
20 changes: 3 additions & 17 deletions tests/unit/windows/test_win_display_device_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace {
#define TEST_F_S_MOCKED(...) DD_MAKE_TEST(TEST_F, WinDisplayDeviceGeneralMocked, __VA_ARGS__)

// Additional convenience global const(s)
const UINT32 FLAGS {SDC_VALIDATE | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_VIRTUAL_MODE_AWARE};
const UINT32 FLAGS {SDC_VALIDATE | SDC_USE_DATABASE_CURRENT};
} // namespace

TEST_F_S(NullptrLayerProvided) {
Expand All @@ -50,10 +50,7 @@ TEST_F_S(IsApiAccessAvailable) {
}

TEST_F_S_MOCKED(IsApiAccessAvailable) {
EXPECT_CALL(*m_layer, queryDisplayConfig(display_device::QueryType::All))
.Times(1)
.WillOnce(Return(ut_consts::PAM_3_ACTIVE));
EXPECT_CALL(*m_layer, setDisplayConfig(ut_consts::PAM_3_ACTIVE->m_paths, ut_consts::PAM_3_ACTIVE->m_modes, FLAGS))
EXPECT_CALL(*m_layer, setDisplayConfig(std::vector<DISPLAYCONFIG_PATH_INFO> {}, std::vector<DISPLAYCONFIG_MODE_INFO> {}, FLAGS))
.Times(1)
.WillOnce(Return(ERROR_SUCCESS));
EXPECT_CALL(*m_layer, getErrorString(ERROR_SUCCESS))
Expand All @@ -63,19 +60,8 @@ TEST_F_S_MOCKED(IsApiAccessAvailable) {
EXPECT_TRUE(m_win_dd.isApiAccessAvailable());
}

TEST_F_S_MOCKED(IsApiAccessAvailable, FailedToGetDisplayData) {
EXPECT_CALL(*m_layer, queryDisplayConfig(display_device::QueryType::All))
.Times(1)
.WillOnce(Return(ut_consts::PAM_NULL));

EXPECT_FALSE(m_win_dd.isApiAccessAvailable());
}

TEST_F_S_MOCKED(IsApiAccessAvailable, FailedToSetDisplayConfig) {
EXPECT_CALL(*m_layer, queryDisplayConfig(display_device::QueryType::All))
.Times(1)
.WillOnce(Return(ut_consts::PAM_3_ACTIVE));
EXPECT_CALL(*m_layer, setDisplayConfig(ut_consts::PAM_3_ACTIVE->m_paths, ut_consts::PAM_3_ACTIVE->m_modes, FLAGS))
EXPECT_CALL(*m_layer, setDisplayConfig(std::vector<DISPLAYCONFIG_PATH_INFO> {}, std::vector<DISPLAYCONFIG_MODE_INFO> {}, FLAGS))
.Times(1)
.WillOnce(Return(ERROR_ACCESS_DENIED));
EXPECT_CALL(*m_layer, getErrorString(ERROR_ACCESS_DENIED))
Expand Down

0 comments on commit 53a0ea5

Please sign in to comment.