// This test checks the position of the `i` for the notable traits.
include: "utils.goml"
go-to: "file://" + |DOC_PATH| + "/test_docs/struct.NotableStructWithLongName.html"
show-text: true

define-function: (
    "check-notable-tooltip-position",
    [x, i_x],
    block {
        // Checking they have the same y position.
        compare-elements-position: (
            "//*[@id='method.create_an_iterator_from_read']//a[normalize-space()='NotableStructWithLongName']",
            "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']",
            ["y"],
        )
        // Checking they don't have the same x position.
        compare-elements-position-false: (
            "//*[@id='method.create_an_iterator_from_read']//a[normalize-space()='NotableStructWithLongName']",
            "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']",
            ["x"],
        )
        // The `i` should be *after* the type.
        assert-position: (
            "//*[@id='method.create_an_iterator_from_read']//a[normalize-space()='NotableStructWithLongName']",
            {"x": |x|},
        )
        assert-position: (
            "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']",
            {"x": |i_x|},
        )
    },
)

define-function: (
    "check-notable-tooltip-position-complete",
    [x, i_x, popover_x],
    block {
        call-function: ("check-notable-tooltip-position", {"x": |x|, "i_x": |i_x|})
        assert-count: ("//*[@class='tooltip popover']", 0)
        click: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
        assert-count: ("//*[@class='tooltip popover']", 1)
        compare-elements-position-near: (
            "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']",
            "//*[@class='tooltip popover']",
            {"y": 30}
        )
        compare-elements-position-false: (
            "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']",
            "//*[@class='tooltip popover']",
            ["x"]
        )
        assert-position: (
            "//*[@class='tooltip popover']",
            {"x": |popover_x|}
        )
        click: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
        move-cursor-to: "//h1"
        assert-count: ("//*[@class='tooltip popover']", 0)
    },
)

// We start with a wide screen.
set-window-size: (1100, 600)
call-function: ("check-notable-tooltip-position-complete", {
    "x": 677,
    "i_x": 955,
    "popover_x": 463,
})

// Now only the `i` should be on the next line.
set-window-size: (1055, 600)
compare-elements-position-false: (
    "//*[@id='method.create_an_iterator_from_read']//a[normalize-space()='NotableStructWithLongName']",
    "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']",
    ["y", "x"],
)

// Now both the `i` and the struct name should be on the next line.
set-window-size: (980, 600)
call-function: ("check-notable-tooltip-position", {
    "x": 245,
    "i_x": 523,
})

// Checking on mobile now.
set-window-size: (650, 600)
call-function: ("check-notable-tooltip-position-complete", {
    "x": 15,
    "i_x": 293,
    "popover_x": 0,
})

// Now check the colors.
define-function: (
    "check-colors",
    [theme, header_color, content_color, type_color, trait_color, link_color],
    block {
        go-to: "file://" + |DOC_PATH| + "/test_docs/struct.NotableStructWithLongName.html"
        // This is needed to ensure that the text color is computed.
        show-text: true

        call-function: ("switch-theme", {"theme": |theme|})

        assert-css: (
             "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']",
             {"color": |content_color|},
             ALL,
        )

        move-cursor-to: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
        wait-for-count: (".tooltip.popover", 1)

        assert-css: (
             "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']",
             {"color": |link_color|},
             ALL,
        )

        assert-css: (
             ".tooltip.popover h3",
             {"color": |header_color|},
             ALL,
        )
        assert-css: (
             ".tooltip.popover pre",
             {"color": |content_color|},
             ALL,
        )
        assert-css: (
             ".tooltip.popover pre a.struct",
             {"color": |type_color|},
             ALL,
        )
        assert-css: (
             ".tooltip.popover pre a.trait",
             {"color": |trait_color|},
             ALL,
        )
    },
)

call-function: (
    "check-colors",
    {
        "theme": "ayu",
        "link_color": "#39afd7",
        "content_color": "#e6e1cf",
        "header_color": "#fff",
        "type_color": "#ffa0a5",
        "trait_color": "#39afd7",
    },
)

call-function: (
    "check-colors",
    {
        "theme": "dark",
        "link_color": "#d2991d",
        "content_color": "#ddd",
        "header_color": "#ddd",
        "type_color": "#2dbfb8",
        "trait_color": "#b78cf2",
    },
)

call-function: (
    "check-colors",
    {
        "theme": "light",
        "link_color": "#3873ad",
        "content_color": "black",
        "header_color": "black",
        "type_color": "#ad378a",
        "trait_color": "#6e4fc9",
    },
)

reload:

// Check that pressing escape works
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
move-cursor-to: "//*[@class='tooltip popover']"
assert-count: ("//*[@class='tooltip popover']", 1)
press-key: "Escape"
assert-count: ("//*[@class='tooltip popover']", 0)
assert: "#method\.create_an_iterator_from_read .tooltip:focus"

// Check that clicking outside works.
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
assert-count: ("//*[@class='tooltip popover']", 1)
click: ".search-input"
assert-count: ("//*[@class='tooltip popover']", 0)
assert-false: "#method\.create_an_iterator_from_read .tooltip:focus"

// Check that pressing tab over and over works.
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
move-cursor-to: "//*[@class='tooltip popover']"
assert-count: ("//*[@class='tooltip popover']", 1)
press-key: "Tab"
press-key: "Tab"
press-key: "Tab"
press-key: "Tab"
press-key: "Tab"
press-key: "Tab"
press-key: "Tab"
assert-count: ("//*[@class='tooltip popover']", 0)
assert: "#method\.create_an_iterator_from_read .tooltip:focus"

define-function: (
    "setup-popup",
    [],
    block {
        store-window-property: {"scrollY": scroll}
        click: "#method\.create_an_iterator_from_read .fn"
        // We ensure that the scroll position changed.
        assert-window-property-false: {"scrollY": |scroll|}
        // Store the new position.
        store-window-property: {"scrollY": scroll}
        click: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
        wait-for: "//*[@class='tooltip popover']"
        click: "#settings-menu a"
    }
)

// Now we check that the focus isn't given back to the wrong item when opening
// another popover.
call-function: ("setup-popup", {})
click: ".search-input"
// We ensure we didn't come back to the previous focused item.
assert-window-property-false: {"scrollY": |scroll|}

// Same but with Escape handling.
call-function: ("setup-popup", {})
press-key: "Escape"
// We ensure we didn't come back to the previous focused item.
assert-window-property-false: {"scrollY": |scroll|}

// Opening the mobile sidebar should close the popover.
set-window-size: (650, 600)
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
assert-count: ("//*[@class='tooltip popover']", 1)
click: ".sidebar-menu-toggle"
assert: "//*[@class='sidebar shown']"
assert-count: ("//*[@class='tooltip popover']", 0)
assert-false: "#method\.create_an_iterator_from_read .tooltip:focus"
// Clicking a notable trait tooltip popover should close the sidebar.
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
assert-count: ("//*[@class='tooltip popover']", 1)
assert-false: "//*[@class='sidebar shown']"

// Also check the focus handling for the help button.
set-window-size: (1100, 600)
reload:
assert-count: ("//*[@class='tooltip popover']", 0)
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='tooltip']"
assert-count: ("//*[@class='tooltip popover']", 1)
click: "#help-button a"
assert-count: ("//*[@class='tooltip popover']", 0)
assert-false: "#method\.create_an_iterator_from_read .tooltip:focus"