|
1 | 1 | import asyncio |
2 | 2 | import random |
3 | | -from contextlib import contextmanager |
| 3 | +from contextlib import contextmanager, AsyncExitStack |
4 | 4 | from time import monotonic |
5 | 5 | from types import FunctionType |
6 | 6 |
|
@@ -1632,4 +1632,106 @@ async def test_shell(api): |
1632 | 1632 | await api.print('Test finished successfully') |
1633 | 1633 |
|
1634 | 1634 |
|
| 1635 | +async def test_window(api): |
| 1636 | + async with api.window.create( |
| 1637 | + api.term.get_current_target(), |
| 1638 | + 15, 5, 5, 5, False, |
| 1639 | + ) as win: |
| 1640 | + assert await win.getPosition() == (15, 5) |
| 1641 | + assert await win.getSize() == (5, 5) |
| 1642 | + |
| 1643 | + await win.setBackgroundColor(api.colors.red) |
| 1644 | + await win.clear() |
| 1645 | + await win.setVisible(True) |
| 1646 | + |
| 1647 | + await asyncio.sleep(1) |
| 1648 | + |
| 1649 | + await win.setVisible(False) |
| 1650 | + await win.setCursorPos(1, 1) |
| 1651 | + await win.setTextColor(api.colors.yellow) |
| 1652 | + await win.write('*********') |
| 1653 | + await win.setVisible(True) |
| 1654 | + |
| 1655 | + await asyncio.sleep(1) |
| 1656 | + |
| 1657 | + await api.term.clear() |
| 1658 | + |
| 1659 | + await asyncio.sleep(1) |
| 1660 | + |
| 1661 | + await win.redraw() |
| 1662 | + assert await win.getLine(1) == ('*****', '44444', 'eeeee') |
| 1663 | + |
| 1664 | + # draws immediately |
| 1665 | + await win.reposition(21, 5) |
| 1666 | + await win.reposition(27, 5) |
| 1667 | + |
| 1668 | + await api.print('Test finished successfully') |
| 1669 | + |
| 1670 | + |
| 1671 | +async def test_redirect_to_window(api): |
| 1672 | + w, h = await api.term.getSize() |
| 1673 | + async with AsyncExitStack() as stack: |
| 1674 | + left = await stack.enter_async_context(api.window.create( |
| 1675 | + api.term.get_current_target(), |
| 1676 | + 1, 1, w // 2, h, True, |
| 1677 | + )) |
| 1678 | + right = await stack.enter_async_context(api.window.create( |
| 1679 | + api.term.get_current_target(), |
| 1680 | + w // 2 + 1, 1, w // 2, h, True, |
| 1681 | + )) |
| 1682 | + async with api.term.redirect(left.get_term_target()): |
| 1683 | + await api.term.setBackgroundColor(api.colors.green) |
| 1684 | + await api.term.setTextColor(api.colors.white) |
| 1685 | + await api.term.clear() |
| 1686 | + await api.term.setCursorPos(1, h // 2) |
| 1687 | + await api.print('Left part') |
| 1688 | + async with api.term.redirect(right.get_term_target()): |
| 1689 | + await api.term.setBackgroundColor(api.colors.red) |
| 1690 | + await api.term.setTextColor(api.colors.yellow) |
| 1691 | + await api.term.clear() |
| 1692 | + await api.term.setCursorPos(1, h // 2) |
| 1693 | + await api.print('Right part') |
| 1694 | + await api.print('Default terminal restored') |
| 1695 | + |
| 1696 | + await api.print('Test finished successfully') |
| 1697 | + |
| 1698 | + |
| 1699 | +async def test_redirect_to_local_monitor(api): |
| 1700 | + side = 'left' |
| 1701 | + await step(api, f'Attach 3x3 color monitor to {side} side of computer') |
| 1702 | + |
| 1703 | + async with api.term.redirect(api.peripheral.get_term_target(side)): |
| 1704 | + await api.term.setBackgroundColor(api.colors.green) |
| 1705 | + await api.term.setTextColor(api.colors.white) |
| 1706 | + await api.term.clear() |
| 1707 | + await api.term.setCursorPos(1, 1) |
| 1708 | + await api.print('Redirected to monitor') |
| 1709 | + |
| 1710 | + await api.print('Test finished successfully') |
| 1711 | + |
| 1712 | + |
| 1713 | +async def test_redirect_to_remote_monitor(api): |
| 1714 | + side = 'back' |
| 1715 | + await step(api, f'Attach wired modem to {side} side of computer') |
| 1716 | + |
| 1717 | + mod = await api.peripheral.wrap(side) |
| 1718 | + |
| 1719 | + await step(api, 'Connect remote monitor using wires, activate its modem') |
| 1720 | + |
| 1721 | + for name in await mod.getNamesRemote(): |
| 1722 | + if await mod.getTypeRemote(name) == 'monitor': |
| 1723 | + break |
| 1724 | + else: |
| 1725 | + assert False |
| 1726 | + |
| 1727 | + async with api.term.redirect(api.peripheral.get_term_target(name)): |
| 1728 | + await api.term.setBackgroundColor(api.colors.blue) |
| 1729 | + await api.term.setTextColor(api.colors.white) |
| 1730 | + await api.term.clear() |
| 1731 | + await api.term.setCursorPos(1, 1) |
| 1732 | + await api.print('Redirected to monitor') |
| 1733 | + |
| 1734 | + await api.print('Test finished successfully') |
| 1735 | + |
| 1736 | + |
1635 | 1737 | # vector won't be implemented, use python equivalent |
0 commit comments