diff --git a/testbed/tests/widgets/test_multilinetextinput.py b/testbed/tests/widgets/test_multilinetextinput.py index f89de18426..7fb201f4a3 100644 --- a/testbed/tests/widgets/test_multilinetextinput.py +++ b/testbed/tests/widgets/test_multilinetextinput.py @@ -64,7 +64,7 @@ async def test_scroll_position(widget, probe): original_width, original_height = probe.width, probe.height # The scroll position is at the origin. - assert probe.vertical_scroll_position == pytest.approx(0.0) + assert probe.vertical_scroll_position == pytest.approx(0, abs=1) # Add a lot of content widget.value = "All work and no play makes Jack a dull boy... " * 1000 @@ -79,13 +79,13 @@ async def test_scroll_position(widget, probe): assert probe.height * 2 < probe.document_height # The scroll position is at the origin. - assert probe.vertical_scroll_position == pytest.approx(0.0) + assert probe.vertical_scroll_position == pytest.approx(0, abs=1) widget.scroll_to_top() await probe.redraw("The document has been explicitly scrolled to the top") # The scroll position is still the origin. - assert probe.vertical_scroll_position == pytest.approx(0.0) + assert probe.vertical_scroll_position == pytest.approx(0, abs=1) widget.scroll_to_bottom() await probe.wait_for_scroll_completion() diff --git a/winforms/tests_backend/widgets/base.py b/winforms/tests_backend/widgets/base.py index 1a04887c0b..ea94ae93f5 100644 --- a/winforms/tests_backend/widgets/base.py +++ b/winforms/tests_backend/widgets/base.py @@ -1,5 +1,6 @@ import asyncio +from pytest import approx from System import EventArgs, Object from System.Drawing import FontFamily, SystemColors, SystemFonts @@ -106,11 +107,14 @@ def assert_layout(self, size, position): assert self.native.Parent is not None # size and position is as expected. - assert (self.native.Width, self.native.Height) == size + assert (self.width, self.height) == approx(size, abs=1) assert ( - self.native.Left, - self.native.Top - self.widget._impl.container.vertical_shift, - ) == position + self.native.Left / self.scale_factor, + ( + (self.native.Top - self.widget._impl.container.vertical_shift) + / self.scale_factor + ), + ) == approx(position, abs=1) async def press(self): self.native.OnClick(EventArgs.Empty) diff --git a/winforms/tests_backend/widgets/multilinetextinput.py b/winforms/tests_backend/widgets/multilinetextinput.py index 7d29fe3c21..b2f14a9579 100644 --- a/winforms/tests_backend/widgets/multilinetextinput.py +++ b/winforms/tests_backend/widgets/multilinetextinput.py @@ -27,6 +27,30 @@ def placeholder_hides_on_focus(self): def readonly(self): return self.native.ReadOnly + def _char_pos(self, index): + return self.native.GetPositionFromCharIndex(index) + + @property + def document_height(self): + text_len = len(self.native.Text) + height = self._char_pos(text_len - 1).Y - self._char_pos(0).Y + + line_count = self.native.GetLineFromCharIndex(text_len - 1) + 1 + if line_count > 1: + # Scale up to include the final line. + assert height > 0 + height *= line_count / (line_count - 1) + + return height / self.scale_factor + + @property + def document_width(self): + return self.width + + @property + def vertical_scroll_position(self): + return -(self._char_pos(0).Y) / self.scale_factor + async def wait_for_scroll_completion(self): pass