def test_interpolation_alpha(self, colorspace): """Test interpolation of colorspace's alpha.""" start = testutils.Color(0, 0, 0, 30) stop = testutils.Color(0, 0, 0, 100) color = qtutils.interpolate_color(start, stop, 50, colorspace) expected = testutils.Color(0, 0, 0, 65) assert testutils.Color(color) == expected
def test_dark_mode_mathml(quteproc_new, request, qtbot): if not request.config.webengine: pytest.skip("Skipped with QtWebKit") args = _base_args(request.config) + [ '--temp-basedir', '-s', 'colors.webpage.darkmode.enabled', 'true', '-s', 'colors.webpage.darkmode.algorithm', 'brightness-rgb', ] quteproc_new.start(args) quteproc_new.open_path('data/darkmode/mathml.html') quteproc_new.wait_for_js('Image loaded') # First make sure loading finished by looking outside of the image expected = testutils.Color(0, 0, 206) if IS_ARM else testutils.Color( 0, 0, 204) quteproc_new.get_screenshot( probe_pos=QPoint(105, 0), probe_color=expected, ) # Then get the actual formula color, probing again in case it's not displayed yet... quteproc_new.get_screenshot( probe_pos=QPoint(4, 4), probe_color=testutils.Color(255, 255, 255), )
def test_0_100(self, colors, colorspace): """Test 0% and 100% in different colorspaces.""" white = qtutils.interpolate_color(colors.white, colors.black, 0, colorspace) black = qtutils.interpolate_color(colors.white, colors.black, 100, colorspace) assert testutils.Color(white) == colors.white assert testutils.Color(black) == colors.black
def test_interpolation_hsl(self): """Test an interpolation in the HSL colorspace.""" start = testutils.Color() stop = testutils.Color() start.setHsl(0, 40, 100) stop.setHsl(0, 20, 200) color = qtutils.interpolate_color(start, stop, 50, QColor.Hsl) expected = testutils.Color() expected.setHsl(0, 30, 150) assert testutils.Color(color) == expected
def test_preferred_colorscheme_with_dark_mode(request, quteproc_new, webengine_versions): """Test interaction between preferred-color-scheme and dark mode.""" if not request.config.webengine: pytest.skip("Skipped with QtWebKit") args = _base_args(request.config) + [ '--temp-basedir', '-s', 'colors.webpage.preferred_color_scheme', 'dark', '-s', 'colors.webpage.darkmode.enabled', 'true', '-s', 'colors.webpage.darkmode.algorithm', 'brightness-rgb', ] quteproc_new.start(args) quteproc_new.open_path('data/darkmode/prefers-color-scheme.html') content = quteproc_new.get_content() qtwe_version = webengine_versions.webengine xfail = None if utils.VersionNumber(5, 15, 3) <= qtwe_version <= utils.VersionNumber(6): # https://bugs.chromium.org/p/chromium/issues/detail?id=1177973 # No workaround known. expected_text = 'Light preference detected.' # light website color, inverted by darkmode expected_color = testutils.Color(127, 127, 127) xfail = "Chromium bug 1177973" elif qtwe_version == utils.VersionNumber(5, 15, 2): # Our workaround breaks when dark mode is enabled... # Also, for some reason, dark mode doesn't work on that page either! expected_text = 'No preference detected.' expected_color = testutils.Color(0, 170, 0) # green xfail = "QTBUG-89753" else: # Qt 5.14 and 5.15.0/.1 work correctly. # Hopefully, so does Qt 6.x in the future? expected_text = 'Dark preference detected.' expected_color = testutils.Color(34, 34, 34) # dark website color xfail = False pos = QPoint(0, 0) img = quteproc_new.get_screenshot(probe_pos=pos, probe_color=expected_color) color = testutils.Color(img.pixelColor(pos)) assert content == expected_text assert color == expected_color if xfail: # We still do some checks, but we want to mark the test outcome as xfail. pytest.xfail(xfail)
def test_dark_mode(webengine_versions, quteproc_new, request, filename, algorithm, colors): if not request.config.webengine: pytest.skip("Skipped with QtWebKit") args = _base_args(request.config) + [ '--temp-basedir', '-s', 'colors.webpage.darkmode.enabled', 'true', '-s', 'colors.webpage.darkmode.algorithm', algorithm, ] quteproc_new.start(args) ver = webengine_versions.webengine minor_version = f'{ver.majorVersion()}.{ver.minorVersion()}' expected = colors.get(minor_version, colors[None]) quteproc_new.open_path(f'data/darkmode/{filename}.html') # Position chosen by fair dice roll. # https://xkcd.com/221/ pos = QPoint(4, 4) img = quteproc_new.get_screenshot(probe_pos=pos, probe_color=expected) color = testutils.Color(img.pixelColor(pos)) # For pytest debug output assert color == expected
def get_screenshot( self, *, probe_pos: QPoint = None, probe_color: QColor = testutils.Color(0, 0, 0), ) -> QImage: """Get a screenshot of the current page. Arguments: probe: If given, only continue if the pixel at the given position isn't black (or whatever is specified by probe_color). """ for _ in range(5): tmp_path = self.request.getfixturevalue('tmp_path') counter = self._screenshot_counters[self.request.node.nodeid] path = tmp_path / f'screenshot-{next(counter)}.png' self.send_cmd(f':screenshot {path}') screenshot_msg = f'Screenshot saved to {path}' self.wait_for(message=screenshot_msg) print(screenshot_msg) img = QImage(str(path)) assert not img.isNull() if probe_pos is None: return img probed_color = testutils.Color(img.pixelColor(probe_pos)) if probed_color == probe_color: return img # Rendering might not be completed yet... time.sleep(0.5) # Using assert again for pytest introspection assert probed_color == probe_color, "Color probing failed, values on last try:" raise utils.Unreachable()
quteproc_new.start(args) # Check cookies quteproc_new.open_path('cookies') content = quteproc_new.get_content() data = json.loads(content) expected_cookies = {'cookie': 'value'} if store else {} assert data == {'cookies': expected_cookies} quteproc_new.send_cmd(':quit') quteproc_new.wait_for_quit() @pytest.mark.parametrize('filename, algorithm, colors', [ ('blank', 'lightness-cielab', { '5.15': testutils.Color(18, 18, 18), '5.14': testutils.Color(27, 27, 27), None: testutils.Color(0, 0, 0), }), ('blank', 'lightness-hsl', { None: testutils.Color(0, 0, 0) }), ('blank', 'brightness-rgb', { None: testutils.Color(0, 0, 0) }), ('yellow', 'lightness-cielab', { '5.15': testutils.Color(35, 34, 0), '5.14': testutils.Color(35, 34, 0), None: testutils.Color(204, 204, 0), }), ('yellow', 'lightness-hsl', {
def test_interpolation_rgb(self): """Test an interpolation in the RGB colorspace.""" color = qtutils.interpolate_color(testutils.Color(0, 40, 100), testutils.Color(0, 20, 200), 50, QColor.Rgb) assert testutils.Color(color) == testutils.Color(0, 30, 150)
def test_invalid_end(self, colors): """Test an invalid end color.""" with pytest.raises(qtutils.QtValueError): qtutils.interpolate_color(colors.white, testutils.Color(), 0)
def test_invalid_start(self, colors): """Test an invalid start color.""" with pytest.raises(qtutils.QtValueError): qtutils.interpolate_color(testutils.Color(), colors.white, 0)
def colors(self): """Example colors to be used.""" return self.Colors(testutils.Color('white'), testutils.Color('black'))
quteproc_new.open_path('cookies') content = quteproc_new.get_content() data = json.loads(content) expected_cookies = {'cookie': 'value'} if store else {} assert data == {'cookies': expected_cookies} quteproc_new.send_cmd(':quit') quteproc_new.wait_for_quit() # The 'colors' dictionaries in the parametrize decorator below have (QtWebEngine # version, CPU architecture) as keys. Either of those (or both) can be None to # say "on all other Qt versions" or "on all other CPU architectures". @pytest.mark.parametrize('filename, algorithm, colors', [ ('blank', 'lightness-cielab', { ('5.15', None): testutils.Color(18, 18, 18), ('5.15', 'aarch64'): testutils.Color(16, 16, 16), ('5.14', None): testutils.Color(27, 27, 27), ('5.14', 'aarch64'): testutils.Color(24, 24, 24), (None, None): testutils.Color(0, 0, 0), }), ('blank', 'lightness-hsl', { (None, None): testutils.Color(0, 0, 0) }), ('blank', 'brightness-rgb', { (None, None): testutils.Color(0, 0, 0) }), ('yellow', 'lightness-cielab', { ('5.15', None): testutils.Color(35, 34, 0), ('5.15', 'aarch64'): testutils.Color(33, 32, 0), ('5.14', None): testutils.Color(35, 34, 0),
expected_cookies = {'cookie': 'value'} if store else {} assert data == {'cookies': expected_cookies} quteproc_new.send_cmd(':quit') quteproc_new.wait_for_quit() # The 'colors' dictionaries in the parametrize decorator below have (QtWebEngine # version, CPU architecture) as keys. Either of those (or both) can be None to # say "on all other Qt versions" or "on all other CPU architectures". @pytest.mark.parametrize('filename, algorithm, colors', [ ( 'blank', 'lightness-cielab', { ('5.15', None): testutils.Color(18, 18, 18), ('5.15', 'aarch64'): testutils.Color(16, 16, 16), ('5.14', None): testutils.Color(27, 27, 27), ('5.14', 'aarch64'): testutils.Color(24, 24, 24), (None, None): testutils.Color(0, 0, 0), } ), ('blank', 'lightness-hsl', {(None, None): testutils.Color(0, 0, 0)}), ('blank', 'brightness-rgb', {(None, None): testutils.Color(0, 0, 0)}), ( 'yellow', 'lightness-cielab', { ('5.15', None): testutils.Color(35, 34, 0), ('5.15', 'aarch64'): testutils.Color(33, 32, 0),
def test_interpolation_none(self, percentage, expected): """Test an interpolation with a gradient turned off.""" color = qtutils.interpolate_color( testutils.Color(0, 0, 0), testutils.Color(255, 255, 255), percentage, None) assert isinstance(color, QColor) assert testutils.Color(color) == testutils.Color(*expected)