from kivy.base import EventLoop
from kivy.tests.common import GraphicUnitTest
from kivy.logger import Logger
import networkx as nx

from LiSE.character import Facade
from ELiDE.app import ELiDEApp
from ELiDE.grid.board import GridBoard, GridBoardView
from ELiDE.tests.util import MockTouch, all_spots_placed, all_pawns_placed, all_arrows_placed, idle_until, window_with_widget


class GridBoardTest(GraphicUnitTest):
    @staticmethod
    def test_layout_grid():
        spots_wide = 3
        spots_tall = 3
        spot_width = 32
        spot_height = 32
        graph = nx.grid_2d_graph(spots_wide, spots_tall)
        char = Facade(graph)
        something = char.place[1, 1].new_thing('something')
        otherthing = char.place[2, 2].new_thing('otherthing')
        assert len(char.thing) == 2
        board = GridBoard(
            character=char
        )
        win = window_with_widget(GridBoardView(board=board))
        while not (all_spots_placed(board, char) and all_pawns_placed(board, char)):
            EventLoop.idle()
        otherthing['location'] = board.pawn['otherthing'].loc_name = (0, 0)
        zero = board.spot[0, 0]
        that = board.pawn['otherthing']
        idle_until(lambda: that in zero.children, 1000, "pawn 'otherthing' did not relocate within 1000 ticks")
        assert that.parent == zero
        for x in range(spots_wide):
            for y in range(spots_tall):
                spot = board.spot[x, y]
                assert spot.x == x * spot_width
                assert spot.y == y * spot_height
        assert board.pawn['something'].parent == board.spot[1, 1]
        assert board.pawn['something'].pos == board.spot[1, 1].pos
        assert board.pawn['otherthing'].parent == board.spot[0, 0]
        assert board.pawn['otherthing'].pos == board.spot[0, 0].pos
@@ -1,16 +1,112 @@
+import shutil%0Aimport sys%0Afrom tempfile import mkdtemp%0Afrom time import monotonic%0A%0Aimport pytest%0A
 from kivy.base i
@@ -221,16 +221,40 @@
  as nx%0A%0A
+from LiSE import Engine%0A
 from LiS
@@ -396,19 +396,8 @@
 port
- MockTouch,
  all
@@ -432,27 +432,8 @@
 ced,
- all_arrows_placed,
  idl
@@ -1824,32 +1824,32 @@
 oard.spot%5B0, 0%5D%0A
-
         assert b
@@ -1895,12 +1895,1510 @@
 ot%5B0, 0%5D.pos
+%0A%0A%0Aclass SwitchGridTest(GraphicUnitTest):%0A    def setUp(self):%0A        super(SwitchGridTest, self).setUp()%0A        self.prefix = mkdtemp()%0A        self.old_argv = sys.argv.copy()%0A        sys.argv = %5B'python', '-m', 'ELiDE', self.prefix%5D%0A%0A    def tearDown(self, fake=False):%0A        super().tearDown(fake=fake)%0A        shutil.rmtree(self.prefix)%0A        sys.argv = self.old_argv%0A%0A    def test_character_switch_grid(self):%0A        with Engine(self.prefix) as eng:%0A            eng.add_character('physical', nx.grid_2d_graph(10, 1))%0A            eng.add_character('tall', nx.grid_2d_graph(1, 10))%0A        app = ELiDEApp()%0A        app._run_prepare()%0A        idle_until(lambda: hasattr(app, 'mainscreen') and app.mainscreen.mainview and app.mainscreen.statpanel and hasattr(app.mainscreen, 'gridview'))%0A        app.mainscreen.statpanel.toggle_gridview()%0A        idle_until(lambda: app.mainscreen.gridview in app.mainscreen.mainview.children)%0A        idle_until(lambda: app.mainscreen.gridview.board.children)%0A        assert all(child.y == 0 for child in app.mainscreen.gridview.board.children)%0A        assert not all(child.x == 0 for child in app.mainscreen.gridview.board.children)%0A        app.character_name = 'tall'%0A        idle_until(lambda: all(child.x == 0 for child in app.mainscreen.gridview.board.children), 1000, %22Never got the new board%22)%0A        idle_until(lambda: not all(child.y == 0 for child in app.mainscreen.gridview.board.children), 1000, %22New board arranged weird%22)%0A        app.stop()%0A

Add SwitchGridTest