Skip to content

Commit

Permalink
Winforms at 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed May 23, 2023
1 parent bb71950 commit 3ad5352
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
6 changes: 3 additions & 3 deletions testbed/tests/widgets/test_multilinetextinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
12 changes: 8 additions & 4 deletions winforms/tests_backend/widgets/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio

from pytest import approx
from System import EventArgs, Object
from System.Drawing import FontFamily, SystemColors, SystemFonts

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions winforms/tests_backend/widgets/multilinetextinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3ad5352

Please sign in to comment.