import pytest
from pytestqt.qt_compat import qt_api

from random import random
from app import MainWindow

# TODO: Add this when project saves stack too
# def test_stack(app, qtbot, delay=25):
#     test_poly_tool(app, qtbot, delay=10)
#     for _ in range(0, 100):
#         app.interface.scene.stack.setIndex(int(random()*app.interface.scene.stack.count()-1))
#         qtbot.wait(delay)
#     assert app.interface.scene.stack.count() == 789
# 
#     qtbot.wait(1000)
# 
#     test_bbox_tool(app, qtbot, delay=25)
#     for _ in range(0, 100):
#         app.interface.scene.stack.setIndex(int(random()*app.interface.scene.stack.count()-1))
#         qtbot.wait(delay)
#     assert app.interface.scene.stack.count() == 16
# 
#     qtbot.wait(1000)

def test_bbox_tool(app, qtbot, delay=25, n_drag_points=10):
    app.file_widget.file_view.setCurrentIndex(
            app.file_widget.file_model.index("tests/resources/road.jpg"))

    app.tool_bar.bbox_action.trigger()
    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("car")

    def drag_between(tl, br):
        p1 = app.graphics_view.mapFromScene(qt_api.QtCore.QPointF(tl[0], tl[1]))
        p2 = app.graphics_view.mapFromScene(qt_api.QtCore.QPointF(br[0], br[1]))

        x_values = [p1.x()+ n*(p2.x()-p1.x())/(n_drag_points-1) for n in range(n_drag_points)]
        y_values = [p1.y()+ n*(p2.y()-p1.y())/(n_drag_points-1) for n in range(n_drag_points)]

        # Add noise
        for n in range(n_drag_points-1):
            x_values[n] += random()*20
            y_values[n] += random()*20

        qtbot.mousePress(app.graphics_view.viewport(), qt_api.QtCore.Qt.LeftButton, pos=p1)
        for n in range(n_drag_points):
            event = qt_api.QtGui.QMouseEvent(
                qt_api.QtCore.QEvent.MouseMove,
                qt_api.QtCore.QPointF(x_values[n], y_values[n]),
                qt_api.QtCore.Qt.NoButton,
                qt_api.QtCore.Qt.LeftButton,
                qt_api.QtCore.Qt.NoModifier,
            )
            qt_api.QtCore.QCoreApplication.sendEvent(app.graphics_view.viewport(), event)
            qtbot.wait(delay)
        qtbot.mouseRelease(app.graphics_view.viewport(), qt_api.QtCore.Qt.LeftButton)

    drag_between((233, 648), (517, 893))
    drag_between((682, 210), (899, 375))
    drag_between((808, 335), (990, 529))
    drag_between((1155, 569), (1428, 808))
    drag_between((1314, 1058), (1815, 1399))
    drag_between((1781, 1029), (2230, 1308))
    drag_between((1786, 1507), (2486, 1968))
    drag_between((2532, 1428), (3220, 1860))
    drag_between((3841, 1519), (4444, 1923))

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("post")

    drag_between((591, 0), (643, 227))
    drag_between((745, 0), (802, 159))
    drag_between((1445, 0), (1536, 2230))
    drag_between((2168, 0), (2213, 512))
    drag_between((2355, 0), (2418, 261))
    drag_between((2959, 0), (3015, 961))
    drag_between((1866, 517), (2037, 961))

    app.tool_bar.lasso_action.trigger()

    assert app.interface.scene.stack.count() == 16
    qtbot.wait(1000)

def test_poly_tool(app, qtbot, delay=10):
    app.file_widget.file_view.setCurrentIndex(
            app.file_widget.file_model.index("tests/resources/zoo.jpg"))

    qtbot.wait(1000)

    app.tool_bar.poly_action.trigger()
    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("giraffe")

    giraffe_1 = [
            (135,  90), (145, 104), (165, 111), (151, 121), (187, 171), (223, 212), 
            (261, 256), (274, 284), (267, 321), (270, 343), (269, 372), (272, 413),
            (258, 410), (258, 386), (260, 349), (254, 317), (249, 306), (246, 326), 
            (251, 340), (251, 363), (249, 389), (256, 416), (234, 414), (239, 394), 
            (229, 361), (225, 341), (220, 317), (217, 302), (205, 297), (201, 299), 
            (199, 340), (194, 398), (193, 416), (175, 416), (187, 373), (187, 339), 
            (184, 309), (176, 289), (171, 258), (174, 246), (175, 225), (168, 203), 
            (152, 173), (138, 134), ( 97, 130), (114, 114), (125,  90), (130,  90)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in giraffe_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    giraffe_2 = [ 
            (471, 253), (464, 244), (464, 230), (454, 225), (442, 228), (430, 238),
            (398, 251), (370, 257), (347, 272), (331, 286), (321, 303), (321, 330),
            (325, 336), (326, 352), (327, 399), (326, 417), (339, 416), (339, 396),
            (339, 379), (345, 359), (361, 334), (363, 354), (359, 373), (359, 393),
            (358, 413), (370, 409), (370, 389), (373, 368), (373, 348), (380, 332),
            (380, 353), (380, 375), (381, 393), (390, 386), (390, 371), (394, 343),
            (404, 318), (407, 298), (403, 285), (428, 271), (444, 253), (454, 258),
            (464, 258), (471, 253)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in giraffe_2]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("zebra")

    zebra_1 = [
            (361, 430), (384, 399), (412, 373), (442, 363), (486, 352), (510, 358),
            (519, 381), (518, 400), (514, 416), (518, 437), (519, 451), (513, 451),
            (505, 435), (503, 413), (495, 410), (491, 425), (496, 439), (496, 458),
            (483, 458), (483, 436), (473, 425), (454, 428), (448, 445), (449, 458),
            (435, 464), (435, 437), (431, 427), (417, 425), (399, 440), (394, 454),
            (395, 472), (379, 491), (368, 491), (361, 486), (358, 468), (363, 442),
            (354, 436), (361, 430)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in zebra_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    zebra_2 = [
            (794, 399), (789, 375), (795, 350), (812, 339), (843, 334), (864, 334),
            (891, 347), (926, 377), (934, 390), (940, 414), (927, 417), (917, 425),
            (912, 432), (908, 414), (893, 400), (882, 398), (872, 419), (872, 450),
            (876, 458), (864, 460), (862, 451), (862, 434), (868, 400), (859, 403),
            (850, 421), (850, 437), (845, 463), (838, 463), (838, 444), (841, 422),
            (845, 405), (838, 405), (829, 431), (838, 474), (826, 477), (822, 458),
            (820, 440), (820, 423), (820, 408), (818, 473), (809, 473), (809, 450),
            (808, 431), (794, 399)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in zebra_2]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    zebra_3 = [
            (514, 551), (524, 535), (532, 512), (536, 490), (560, 460), (605, 437),
            (656, 439), (705, 437), (715, 459), (728, 480), (744, 499), (757, 514),
            (744, 527), (733, 581), (724, 582), (716, 541), (699, 586), (688, 587),
            (689, 579), (693, 561), (701, 520), (657, 522), (623, 513), (611, 532),
            (609, 560), (610, 588), (632, 615), (613, 615), (601, 588), (590, 561),
            (583, 584), (586, 604), (595, 620), (578, 623), (570, 607), (568, 563),
            (565, 552), (575, 528), (561, 495), (561, 476), (549, 488), (546, 517),
            (540, 535), (514, 551), (748, 448), (777, 459), (794, 472), (808, 488),
            (818, 509), (795, 500), (774, 483), (748, 448)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in zebra_3]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("rock")

    rocks_1 = [
            (42, 551), (22, 541), (49, 537), (69, 532), (72, 523), (95, 524),
            (127, 500), (124, 490), (156, 494), (147, 481), (141, 465), (124, 460),
            (110, 458), (107, 467), (92, 460), (66, 465), (52, 474), (46, 485),
            (27, 482), (5, 491), (3, 556), (42, 551), (86, 618), (138, 650),
            (147, 664), (37, 665), (43, 641), (86, 618), (134, 583), (156, 570),
            (208, 597), (211, 605), (252, 598), (283, 616), (313, 666), (174, 666),
            (170, 655), (173, 639), (137, 632), (128, 620), (130, 600), (134, 583),
            (165, 471), (197, 454), (217, 457), (223, 474), (252, 494), (238, 503),
            (210, 503), (189, 503), (183, 495), (164, 491), (165, 471), (288, 485),
            (308, 464), (335, 459), (353, 472), (353, 487), (293, 497), (288, 485),
            (280, 455), (283, 431), (308, 419), (327, 425), (340, 439), (339, 446),
            (318, 459), (280, 455), (367, 540), (375, 509), (393, 499), (404, 499),
            (416, 490), (421, 552), (417, 632), (412, 652), (389, 641), (380, 633),
            (357, 633), (357, 611), (384, 601), (382, 595), (379, 590), (371, 559),
            (375, 549), (367, 540)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in rocks_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)


    rocks_2 = [
            (343, 441), (354, 432), (359, 442), (358, 454), (344, 455), (343, 441),
            (395, 453), (400, 439), (418, 426), (426, 454), (413, 464), (398, 467),
            (395, 453), (458, 428), (481, 430), (485, 445), (455, 451), (449, 451),
            (458, 428), (487, 458), (503, 458), (523, 454), (528, 462), (559, 453),
            (558, 462), (541, 478), (526, 501), (482, 505), (471, 491), (471, 478),
            (487, 458), (894, 468), (898, 446), (912, 428), (934, 418), (953, 421),
            (939, 412), (939, 403), (965, 404), (990, 404), (999, 407), (999, 425),
            (967, 439), (965, 455), (949, 446), (936, 448), (925, 469), (894, 468),
            (904, 427), (894, 418), (880, 419), (871, 439), (877, 451), (890, 451),
            (904, 427), (859, 423), (849, 426), (844, 450), (858, 448), (859, 423),
            (973, 510), (990, 533), (999, 665), (756, 665), (770, 645), (771, 618),
            (832, 573), (871, 550), (918, 533), (954, 506), (973, 510), (554, 665),
            (533, 651), (514, 646), (495, 653), (488, 665), (554, 665)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in rocks_2]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("tree")

    trees_1 = [
            (451, 465), (465, 478), (460, 501), (474, 563), (463, 634), (512, 625),
            (546, 616), (554, 627), (601, 628), (614, 632), (676, 596), (746, 578),
            (780, 538), (739, 492), (714, 454), (670, 381), (637, 355), (620, 325),
            (652, 345), (676, 358), (688, 353), (706, 359), (710, 379), (740, 440),
            (761, 468), (785, 494), (809, 512), (847, 505), (904, 499), (939, 446),
            (955, 454), (985, 471), (995, 483), (999, 501), (999, 527), (986, 523),
            (969, 506), (950, 510), (902, 538), (858, 559), (820, 578), (761, 636),
            (738, 666), (633, 666), (593, 657), (540, 651), (510, 645), (483, 659),
            (465, 659), (460, 665), (329, 665), (318, 659), (318, 643), (366, 643),
            (391, 645), (409, 653), (421, 586), (419, 533), (410, 513), (416, 487),
            (418, 468), (451, 465), (3, 414), (4, 1), (1000, 0), (999, 394),
            (946, 396), (934, 385), (885, 335), (861, 331), (829, 335), (802, 340),
            (790, 357), (785, 376), (795, 404), (731, 409), (710, 364), (694, 350),
            (669, 352), (629, 321), (619, 329), (628, 350), (660, 379), (682, 409),
            (526, 413), (522, 376), (504, 350), (480, 349), (396, 385), (394, 353),
            (405, 320), (407, 293), (425, 274), (441, 256), (462, 265), (477, 253),
            (465, 238), (463, 225), (444, 223), (430, 234), (403, 248), (375, 254),
            (361, 266), (334, 283), (318, 307), (325, 344), (327, 368), (326, 413),
            (275, 417), (271, 375), (271, 330), (275, 274), (247, 228), (214, 203),
            (188, 168), (156, 124), (165, 110), (143, 105), (141, 91), (130, 86),
            (118, 104), (104, 121), (102, 130), (129, 133), (174, 223), (170, 247),
            (170, 270), (184, 325), (184, 362), (180, 402), (175, 416), (3, 414),
            (194, 414), (199, 299), (214, 303), (224, 341), (231, 363), (240, 395),
            (237, 414), (194, 414), (249, 387), (253, 350), (247, 308), (260, 336),
            (260, 399), (249, 387), (340, 414), (340, 379), (361, 335), (357, 410),
            (340, 414), (371, 408), (377, 336), (381, 340), (384, 393), (371, 408)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in trees_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("grass")

    grass_1 = [
            (3, 417), (367, 417), (350, 437), (341, 437), (320, 423), (295, 422),
            (279, 444), (280, 459), (316, 458), (338, 448), (359, 457), (361, 487),
            (377, 494), (398, 469), (425, 451), (422, 468), (416, 488), (375, 505),
            (368, 538), (373, 583), (382, 602), (355, 607), (352, 633), (320, 641),
            (320, 662), (317, 662), (279, 607), (240, 598), (212, 601), (187, 583),
            (164, 568), (133, 572), (128, 597), (128, 627), (173, 643), (173, 661),
            (146, 661), (134, 646), (95, 619), (81, 616), (61, 627), (49, 636),
            (36, 659), (3, 668), (5, 565), (50, 559), (27, 541), (87, 529),
            (127, 506), (124, 495), (146, 495), (180, 491), (188, 501), (217, 509),
            (249, 500), (294, 496), (343, 492), (352, 482), (350, 465), (331, 458),
            (295, 467), (283, 482), (281, 492), (249, 495), (238, 476), (226, 472),
            (223, 454), (201, 450), (176, 458), (161, 468), (161, 483), (151, 483),
            (143, 464), (121, 455), (111, 457), (106, 464), (96, 459), (61, 463),
            (43, 482), (4, 490), (3, 417), (519, 414), (691, 412), (701, 437),
            (606, 435), (578, 444), (558, 453), (527, 455), (519, 414), (501, 417),
            (512, 453), (495, 450), (497, 434), (491, 419), (501, 417), (454, 455),
            (486, 451), (473, 472), (454, 455), (464, 495), (468, 556), (474, 579),
            (465, 632), (514, 621), (550, 623), (581, 629), (575, 607), (564, 555),
            (575, 532), (556, 497), (547, 499), (536, 541), (513, 556), (517, 533),
            (523, 513), (464, 495), (621, 512), (650, 523), (698, 519), (697, 555),
            (687, 586), (634, 613), (607, 565), (621, 512), (590, 569), (618, 620),
            (613, 632), (597, 625), (584, 595), (590, 569), (730, 409), (798, 404),
            (804, 434), (809, 473), (802, 477), (780, 457), (760, 449), (749, 449),
            (730, 409), (958, 398), (999, 398), (999, 408), (968, 405), (958, 398),
            (965, 439), (997, 432), (999, 495), (990, 472), (968, 462), (965, 439),
            (922, 472), (900, 499), (841, 518), (821, 506), (812, 483), (838, 477),
            (847, 460), (864, 462), (880, 451), (895, 450), (889, 468), (922, 472),
            (905, 412), (885, 398), (876, 414), (890, 413), (907, 427), (905, 412),
            (857, 405), (857, 405), (863, 425), (852, 419), (861, 402), (863, 425),
            (836, 404), (840, 421), (838, 450), (830, 455), (831, 428), (836, 404),
            (757, 519), (775, 537), (743, 579), (733, 579), (740, 547), (757, 519),
            (543, 652), (583, 653), (632, 664), (609, 666), (554, 665), (543, 652),
            (642, 632), (670, 616), (685, 618), (669, 632), (642, 632)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in grass_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.lasso_action.trigger()

    qtbot.wait(1000)
    assert app.interface.scene.stack.count() == 789

import pytest
from pytestqt.qt_compat import qt_api

from random import random
from app import MainWindow

# TODO: Add this when project saves stack too
# def test_stack(app, qtbot, delay=25):
#     test_poly_tool(app, qtbot, delay=10)
#     for _ in range(0, 100):
#         app.interface.scene.stack.setIndex(int(random()*app.interface.scene.stack.count()-1))
#         qtbot.wait(delay)
#     assert app.interface.scene.stack.count() == 789
# 
#     qtbot.wait(1000)
# 
#     test_bbox_tool(app, qtbot, delay=25)
#     for _ in range(0, 100):
#         app.interface.scene.stack.setIndex(int(random()*app.interface.scene.stack.count()-1))
#         qtbot.wait(delay)
#     assert app.interface.scene.stack.count() == 16
# 
#     qtbot.wait(1000)

def test_bbox_tool(app, qtbot, delay=25, n_drag_points=10):
    app.file_widget.file_view.setCurrentIndex(
            app.file_widget.file_model.index("tests/resources/road.jpg"))

    app.tool_bar.bbox_action.trigger()
    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("car")

    def drag_between(tl, br):
        p1 = app.graphics_view.mapFromScene(qt_api.QtCore.QPointF(tl[0], tl[1]))
        p2 = app.graphics_view.mapFromScene(qt_api.QtCore.QPointF(br[0], br[1]))

        x_values = [p1.x()+ n*(p2.x()-p1.x())/(n_drag_points-1) for n in range(n_drag_points)]
        y_values = [p1.y()+ n*(p2.y()-p1.y())/(n_drag_points-1) for n in range(n_drag_points)]

        # Add noise
        for n in range(n_drag_points-1):
            x_values[n] += random()*20
            y_values[n] += random()*20

        qtbot.mousePress(app.graphics_view.viewport(), qt_api.QtCore.Qt.LeftButton, pos=p1)
        for n in range(n_drag_points):
            event = qt_api.QtGui.QMouseEvent(
                qt_api.QtCore.QEvent.MouseMove,
                qt_api.QtCore.QPointF(x_values[n], y_values[n]),
                qt_api.QtCore.Qt.NoButton,
                qt_api.QtCore.Qt.LeftButton,
                qt_api.QtCore.Qt.NoModifier,
            )
            qt_api.QtCore.QCoreApplication.sendEvent(app.graphics_view.viewport(), event)
            qtbot.wait(delay)
        qtbot.mouseRelease(app.graphics_view.viewport(), qt_api.QtCore.Qt.LeftButton)

    drag_between((233, 648), (517, 893))
    drag_between((682, 210), (899, 375))
    drag_between((808, 335), (990, 529))
    drag_between((1155, 569), (1428, 808))
    drag_between((1314, 1058), (1815, 1399))
    drag_between((1781, 1029), (2230, 1308))
    drag_between((1786, 1507), (2486, 1968))
    drag_between((2532, 1428), (3220, 1860))
    drag_between((3841, 1519), (4444, 1923))

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("post")

    drag_between((591, 0), (643, 227))
    drag_between((745, 0), (802, 159))
    drag_between((1445, 0), (1536, 2230))
    drag_between((2168, 0), (2213, 512))
    drag_between((2355, 0), (2418, 261))
    drag_between((2959, 0), (3015, 961))
    drag_between((1866, 517), (2037, 961))

    app.tool_bar.lasso_action.trigger()

    assert app.interface.scene.stack.count() == 16
    qtbot.wait(1000)

def test_poly_tool(app, qtbot, delay=10):
    app.file_widget.file_view.setCurrentIndex(
            app.file_widget.file_model.index("tests/resources/zoo.jpg"))

    qtbot.wait(1000)

    app.tool_bar.poly_action.trigger()
    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("giraffe")

    giraffe_1 = [
            (135,  90), (145, 104), (165, 111), (151, 121), (187, 171), (223, 212), 
            (261, 256), (274, 284), (267, 321), (270, 343), (269, 372), (272, 413),
            (258, 410), (258, 386), (260, 349), (254, 317), (249, 306), (246, 326), 
            (251, 340), (251, 363), (249, 389), (256, 416), (234, 414), (239, 394), 
            (229, 361), (225, 341), (220, 317), (217, 302), (205, 297), (201, 299), 
            (199, 340), (194, 398), (193, 416), (175, 416), (187, 373), (187, 339), 
            (184, 309), (176, 289), (171, 258), (174, 246), (175, 225), (168, 203), 
            (152, 173), (138, 134), ( 97, 130), (114, 114), (125,  90), (130,  90)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in giraffe_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    giraffe_2 = [ 
            (471, 253), (464, 244), (464, 230), (454, 225), (442, 228), (430, 238),
            (398, 251), (370, 257), (347, 272), (331, 286), (321, 303), (321, 330),
            (325, 336), (326, 352), (327, 399), (326, 417), (339, 416), (339, 396),
            (339, 379), (345, 359), (361, 334), (363, 354), (359, 373), (359, 393),
            (358, 413), (370, 409), (370, 389), (373, 368), (373, 348), (380, 332),
            (380, 353), (380, 375), (381, 393), (390, 386), (390, 371), (394, 343),
            (404, 318), (407, 298), (403, 285), (428, 271), (444, 253), (454, 258),
            (464, 258), (471, 253)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in giraffe_2]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("zebra")

    zebra_1 = [
            (361, 430), (384, 399), (412, 373), (442, 363), (486, 352), (510, 358),
            (519, 381), (518, 400), (514, 416), (518, 437), (519, 451), (513, 451),
            (505, 435), (503, 413), (495, 410), (491, 425), (496, 439), (496, 458),
            (483, 458), (483, 436), (473, 425), (454, 428), (448, 445), (449, 458),
            (435, 464), (435, 437), (431, 427), (417, 425), (399, 440), (394, 454),
            (395, 472), (379, 491), (368, 491), (361, 486), (358, 468), (363, 442),
            (354, 436), (361, 430)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in zebra_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    zebra_2 = [
            (794, 399), (789, 375), (795, 350), (812, 339), (843, 334), (864, 334),
            (891, 347), (926, 377), (934, 390), (940, 414), (927, 417), (917, 425),
            (912, 432), (908, 414), (893, 400), (882, 398), (872, 419), (872, 450),
            (876, 458), (864, 460), (862, 451), (862, 434), (868, 400), (859, 403),
            (850, 421), (850, 437), (845, 463), (838, 463), (838, 444), (841, 422),
            (845, 405), (838, 405), (829, 431), (838, 474), (826, 477), (822, 458),
            (820, 440), (820, 423), (820, 408), (818, 473), (809, 473), (809, 450),
            (808, 431), (794, 399)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in zebra_2]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    zebra_3 = [
            (514, 551), (524, 535), (532, 512), (536, 490), (560, 460), (605, 437),
            (656, 439), (705, 437), (715, 459), (728, 480), (744, 499), (757, 514),
            (744, 527), (733, 581), (724, 582), (716, 541), (699, 586), (688, 587),
            (689, 579), (693, 561), (701, 520), (657, 522), (623, 513), (611, 532),
            (609, 560), (610, 588), (632, 615), (613, 615), (601, 588), (590, 561),
            (583, 584), (586, 604), (595, 620), (578, 623), (570, 607), (568, 563),
            (565, 552), (575, 528), (561, 495), (561, 476), (549, 488), (546, 517),
            (540, 535), (514, 551), (748, 448), (777, 459), (794, 472), (808, 488),
            (818, 509), (795, 500), (774, 483), (748, 448)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in zebra_3]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("rock")

    rocks_1 = [
            (42, 551), (22, 541), (49, 537), (69, 532), (72, 523), (95, 524),
            (127, 500), (124, 490), (156, 494), (147, 481), (141, 465), (124, 460),
            (110, 458), (107, 467), (92, 460), (66, 465), (52, 474), (46, 485),
            (27, 482), (5, 491), (3, 556), (42, 551), (86, 618), (138, 650),
            (147, 664), (37, 665), (43, 641), (86, 618), (134, 583), (156, 570),
            (208, 597), (211, 605), (252, 598), (283, 616), (313, 666), (174, 666),
            (170, 655), (173, 639), (137, 632), (128, 620), (130, 600), (134, 583),
            (165, 471), (197, 454), (217, 457), (223, 474), (252, 494), (238, 503),
            (210, 503), (189, 503), (183, 495), (164, 491), (165, 471), (288, 485),
            (308, 464), (335, 459), (353, 472), (353, 487), (293, 497), (288, 485),
            (280, 455), (283, 431), (308, 419), (327, 425), (340, 439), (339, 446),
            (318, 459), (280, 455), (367, 540), (375, 509), (393, 499), (404, 499),
            (416, 490), (421, 552), (417, 632), (412, 652), (389, 641), (380, 633),
            (357, 633), (357, 611), (384, 601), (382, 595), (379, 590), (371, 559),
            (375, 549), (367, 540)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in rocks_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)


    rocks_2 = [
            (343, 441), (354, 432), (359, 442), (358, 454), (344, 455), (343, 441),
            (395, 453), (400, 439), (418, 426), (426, 454), (413, 464), (398, 467),
            (395, 453), (458, 428), (481, 430), (485, 445), (455, 451), (449, 451),
            (458, 428), (487, 458), (503, 458), (523, 454), (528, 462), (559, 453),
            (558, 462), (541, 478), (526, 501), (482, 505), (471, 491), (471, 478),
            (487, 458), (894, 468), (898, 446), (912, 428), (934, 418), (953, 421),
            (939, 412), (939, 403), (965, 404), (990, 404), (999, 407), (999, 425),
            (967, 439), (965, 455), (949, 446), (936, 448), (925, 469), (894, 468),
            (904, 427), (894, 418), (880, 419), (871, 439), (877, 451), (890, 451),
            (904, 427), (859, 423), (849, 426), (844, 450), (858, 448), (859, 423),
            (973, 510), (990, 533), (999, 665), (756, 665), (770, 645), (771, 618),
            (832, 573), (871, 550), (918, 533), (954, 506), (973, 510), (554, 665),
            (533, 651), (514, 646), (495, 653), (488, 665), (554, 665)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in rocks_2]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("tree")

    trees_1 = [
            (451, 465), (465, 478), (460, 501), (474, 563), (463, 634), (512, 625),
            (546, 616), (554, 627), (601, 628), (614, 632), (676, 596), (746, 578),
            (780, 538), (739, 492), (714, 454), (670, 381), (637, 355), (620, 325),
            (652, 345), (676, 358), (688, 353), (706, 359), (710, 379), (740, 440),
            (761, 468), (785, 494), (809, 512), (847, 505), (904, 499), (939, 446),
            (955, 454), (985, 471), (995, 483), (999, 501), (999, 527), (986, 523),
            (969, 506), (950, 510), (902, 538), (858, 559), (820, 578), (761, 636),
            (738, 666), (633, 666), (593, 657), (540, 651), (510, 645), (483, 659),
            (465, 659), (460, 665), (329, 665), (318, 659), (318, 643), (366, 643),
            (391, 645), (409, 653), (421, 586), (419, 533), (410, 513), (416, 487),
            (418, 468), (451, 465), (3, 414), (4, 1), (1000, 0), (999, 394),
            (946, 396), (934, 385), (885, 335), (861, 331), (829, 335), (802, 340),
            (790, 357), (785, 376), (795, 404), (731, 409), (710, 364), (694, 350),
            (669, 352), (629, 321), (619, 329), (628, 350), (660, 379), (682, 409),
            (526, 413), (522, 376), (504, 350), (480, 349), (396, 385), (394, 353),
            (405, 320), (407, 293), (425, 274), (441, 256), (462, 265), (477, 253),
            (465, 238), (463, 225), (444, 223), (430, 234), (403, 248), (375, 254),
            (361, 266), (334, 283), (318, 307), (325, 344), (327, 368), (326, 413),
            (275, 417), (271, 375), (271, 330), (275, 274), (247, 228), (214, 203),
            (188, 168), (156, 124), (165, 110), (143, 105), (141, 91), (130, 86),
            (118, 104), (104, 121), (102, 130), (129, 133), (174, 223), (170, 247),
            (170, 270), (184, 325), (184, 362), (180, 402), (175, 416), (3, 414),
            (194, 414), (199, 299), (214, 303), (224, 341), (231, 363), (240, 395),
            (237, 414), (194, 414), (249, 387), (253, 350), (247, 308), (260, 336),
            (260, 399), (249, 387), (340, 414), (340, 379), (361, 335), (357, 410),
            (340, 414), (371, 408), (377, 336), (381, 340), (384, 393), (371, 408)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in trees_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.add_action.trigger()
    app.tool_bar.label_select.setCurrentText("grass")

    grass_1 = [
            (3, 417), (367, 417), (350, 437), (341, 437), (320, 423), (295, 422),
            (279, 444), (280, 459), (316, 458), (338, 448), (359, 457), (361, 487),
            (377, 494), (398, 469), (425, 451), (422, 468), (416, 488), (375, 505),
            (368, 538), (373, 583), (382, 602), (355, 607), (352, 633), (320, 641),
            (320, 662), (317, 662), (279, 607), (240, 598), (212, 601), (187, 583),
            (164, 568), (133, 572), (128, 597), (128, 627), (173, 643), (173, 661),
            (146, 661), (134, 646), (95, 619), (81, 616), (61, 627), (49, 636),
            (36, 659), (3, 668), (5, 565), (50, 559), (27, 541), (87, 529),
            (127, 506), (124, 495), (146, 495), (180, 491), (188, 501), (217, 509),
            (249, 500), (294, 496), (343, 492), (352, 482), (350, 465), (331, 458),
            (295, 467), (283, 482), (281, 492), (249, 495), (238, 476), (226, 472),
            (223, 454), (201, 450), (176, 458), (161, 468), (161, 483), (151, 483),
            (143, 464), (121, 455), (111, 457), (106, 464), (96, 459), (61, 463),
            (43, 482), (4, 490), (3, 417), (519, 414), (691, 412), (701, 437),
            (606, 435), (578, 444), (558, 453), (527, 455), (519, 414), (501, 417),
            (512, 453), (495, 450), (497, 434), (491, 419), (501, 417), (454, 455),
            (486, 451), (473, 472), (454, 455), (464, 495), (468, 556), (474, 579),
            (465, 632), (514, 621), (550, 623), (581, 629), (575, 607), (564, 555),
            (575, 532), (556, 497), (547, 499), (536, 541), (513, 556), (517, 533),
            (523, 513), (464, 495), (621, 512), (650, 523), (698, 519), (697, 555),
            (687, 586), (634, 613), (607, 565), (621, 512), (590, 569), (618, 620),
            (613, 632), (597, 625), (584, 595), (590, 569), (730, 409), (798, 404),
            (804, 434), (809, 473), (802, 477), (780, 457), (760, 449), (749, 449),
            (730, 409), (958, 398), (999, 398), (999, 408), (968, 405), (958, 398),
            (965, 439), (997, 432), (999, 495), (990, 472), (968, 462), (965, 439),
            (922, 472), (900, 499), (841, 518), (821, 506), (812, 483), (838, 477),
            (847, 460), (864, 462), (880, 451), (895, 450), (889, 468), (922, 472),
            (905, 412), (885, 398), (876, 414), (890, 413), (907, 427), (905, 412),
            (857, 405), (857, 405), (863, 425), (852, 419), (861, 402), (863, 425),
            (836, 404), (840, 421), (838, 450), (830, 455), (831, 428), (836, 404),
            (757, 519), (775, 537), (743, 579), (733, 579), (740, 547), (757, 519),
            (543, 652), (583, 653), (632, 664), (609, 666), (554, 665), (543, 652),
            (642, 632), (670, 616), (685, 618), (669, 632), (642, 632)]
    for p in [app.graphics_view.mapFromScene(qt_api.QtCore.QPoint(*p)) for p in grass_1]:
        qtbot.mouseClick(app.graphics_view.viewport(), qt_api.Qt.LeftButton, pos=p, delay=delay)

    app.tool_bar.lasso_action.trigger()

    qtbot.wait(1000)
    assert app.interface.scene.stack.count() == 789

# TODO: Add this when project saves stack too # def test_stack(app, qtbot, delay=25): #     test_poly_tool(app, qtbot, delay=10) #     for _ in range(0, 100): #         app.interface.scene.stack.setIndex(int(random()*app.interface.scene.stack.count()-1)) #         qtbot.wait(delay) #     assert app.interface.scene.stack.count() == 789 # #     qtbot.wait(1000) # #     test_bbox_tool(app, qtbot, delay=25) #     for _ in range(0, 100): #         app.interface.scene.stack.setIndex(int(random()*app.interface.scene.stack.count()-1)) #         qtbot.wait(delay) #     assert app.interface.scene.stack.count() == 16 # #     qtbot.wait(1000) # Add noise