diff --git a/tests/features/test_gamma.py b/tests/features/test_gamma.py index e46701d..875f28c 100644 --- a/tests/features/test_gamma.py +++ b/tests/features/test_gamma.py @@ -122,6 +122,28 @@ def test_identify_getramp_fails(): assert identify_display(r"\\.\DISPLAY1") is False +def test_identify_flash_fails(): + from winrandr.features.gamma import identify_display + + with patch("winrandr.features.gamma._CreateDCW", return_value=0x1234): + with patch("winrandr.features.gamma._GetDeviceGammaRamp", return_value=1): + with patch("winrandr.features.gamma._SetDeviceGammaRamp", side_effect=[0, 1]): + with patch("winrandr.features.gamma._DeleteDC"): + with patch("winrandr.features.gamma.time.sleep"): + assert identify_display(r"\\.\DISPLAY1") is False + + +def test_identify_blank_fails(): + from winrandr.features.gamma import identify_display + + with patch("winrandr.features.gamma._CreateDCW", return_value=0x1234): + with patch("winrandr.features.gamma._GetDeviceGammaRamp", return_value=1): + with patch("winrandr.features.gamma._SetDeviceGammaRamp", side_effect=[1, 0, 1]): + with patch("winrandr.features.gamma._DeleteDC"): + with patch("winrandr.features.gamma.time.sleep"): + assert identify_display(r"\\.\DISPLAY1") is False + + def test_identify_exception(): from winrandr.features.gamma import identify_display diff --git a/winrandr/features/gamma.py b/winrandr/features/gamma.py index 2c9eefa..d285020 100644 --- a/winrandr/features/gamma.py +++ b/winrandr/features/gamma.py @@ -87,6 +87,7 @@ def identify_display(device_name: str, duration: float = 2.0) -> bool: return False saved = (c_uint16 * (3 * 256))() + all_ok = True try: ramp = (c_uint16 * (3 * 256))() if not _GetDeviceGammaRamp(dc, ramp): @@ -102,11 +103,17 @@ def identify_display(device_name: str, duration: float = 2.0) -> bool: interval = duration / 6 for _ in range(3): - _SetDeviceGammaRamp(dc, flash) + if not _SetDeviceGammaRamp(dc, flash): + logger.warning("设置 %s 闪烁白屏失败(伽马写入被拒绝)", device_name) + all_ok = False + break time.sleep(interval) - _SetDeviceGammaRamp(dc, blank) + if not _SetDeviceGammaRamp(dc, blank): + logger.warning("设置 %s 闪烁黑屏失败(伽马写入被拒绝)", device_name) + all_ok = False + break time.sleep(interval) finally: _SetDeviceGammaRamp(dc, saved) _DeleteDC(dc) - return True + return all_ok