*** Settings ***
Documentation     Tests delete of Projects, Datasets, Images

Resource          ../../resources/config.txt
Resource          ../../resources/web/login.txt
Resource          ../../resources/web/tree.txt

Suite Setup         User "${USERNAME}" logs in with password "${PASSWORD}"

*** Variables ***

${thumbnailsXpath}      //ul[@id='dataIcons']//div[contains(@class, 'image')]

*** Keywords ***

Clear Activity
    [Documentation]         Clear any activities from earlier tests etc.
    Click Element           id=launch_activities
    Click Element           id=clear_activities

Check Fileset Delete Warning
    Wait Until Element Is Visible           id=delete-dialog-form
    Wait Until Element Is Visible           xpath=//div[@class='split_filesets_info']
    # For some reason, fails to find full text: 'Multi-image filesets cannot be partially deleted'
    Wait Until Page Contains                partially deleted
    # Should be 3 thumbnails shown for this fileset
    Page Should Contain Element             //div[@class='split_fileset']//img[@class='fileset_image']        limit=3
    # Yes button shouldn't be visible
    ${xpath}=                               Get Dialog Button Xpath     Yes
    Element Should Not Be Visible           xpath=${xpath}

*** Test Cases ***

Test Delete Project
    [Documentation]     Create and Delete a Project

    Clear Activity
    Select Experimenter
    ${pid}=                                 Create project          robot test delete
    Click Element                           refreshButton
    ${nodeId}=                              Wait For Project Node   ${pid}
    Click Element                           id=deleteButton
    Wait Until Element Is Visible           id=delete-dialog-form
    Click Dialog Button                     Yes
    # Wait for activities to show job done, then refresh tree...
    Wait Until Keyword Succeeds     ${TIMEOUT}   ${INTERVAL}   Page Should Contain Element     xpath=//span[@id='jobstatus'][contains(text(),'1')]
    Page Should Not Contain Element         id=${nodeId}
    Click Element                           refreshButton
    # On POST success, Experimenter should be selected and project removed from tree
    User Should Be Selected
    Project Should Not Exist in Tree        ${pid}

Test Delete Project Dataset
    [Documentation]     Create and Delete a Project containing a Dataset

    Clear Activity
    Select Experimenter
    ${pid}=                                 Create project      robot test delete
    ${did}=                                 Create Dataset      robot test deleteChildren
    Click Element                           refreshButton
    ${projectNodeId}=                       Select Project By Id                    ${pid}
    Click Element                           id=deleteButton
    Wait Until Element Is Visible           id=delete-dialog-form
    Click Dialog Button                     Yes
    # Wait for activities to show job done, then refresh tree...
    Wait Until Keyword Succeeds             ${TIMEOUT}   ${INTERVAL}   Page Should Contain Element     xpath=//span[@id='jobstatus'][contains(text(),'1')]
    Click Element                           refreshButton
    User Should Be Selected
    Project Should Not Exist in Tree        ${pid}
    # Dataset should be Deleted too
    Dataset Should Not Exist In Tree        ${did}

Test Delete Images in Dataset
    [Documentation]     Deletes images pre-imported into a dataset named "Delete"

    Clear Activity
    Select Experimenter
    # Click on Dataset named "Delete", wait for thumbnails and count them
    ${nodeId}=                              Wait For Dataset Node Text              Delete
    Click Node                              ${nodeId}
    Wait Until Page Contains Element        id=dataIcons
    ${thumbCount}=                          Get Element Count     ${thumbnailsXpath}
    # Click first image in Tree
    Select First Image
    Click Element                           id=deleteButton
    Wait Until Element Is Visible           id=delete-dialog-form
    Click Dialog Button                     Yes
    # Should see almost instant removal of 1 thumbnail...
    ${delThumbCount}=                       Evaluate   ${thumbCount} - 1
    Wait Until Keyword Succeeds             1   0.1   Page Should Contain Element   ${thumbnailsXpath}   limit=${delThumbCount}
    # ...Need to check that centre panel doesn't reload and show image during delete: #12866
    Sleep                                   5
    Page Should Contain Element               ${thumbnailsXpath}   limit=${delThumbCount}

Test Delete MIF Images
    [Documentation]     Checks warnings when trying to delete Multi-Image-Fileset images

    Clear Activity
    Select Experimenter
    # Click on Dataset named "MIF Images"
    Select First Dataset With Name          MIF Images
    ${nodeId}=                              Select First Image With Name            test&series=3.fake [test]
    Wait Until Element Is Visible           xpath=//ul[@id='dataIcons']//li[contains(@class, 'fs-selected')]
    Page Should Contain Element              //ul[@id='dataIcons']//li[contains(@class, 'fs-selected')]       limit=3
    # Check warning is generated via toolbar button...
    Click Element                           id=deleteButton
    Check Fileset Delete Warning
    Click Dialog Button                     Cancel
    # ...and from right-click menu
    Open Context Menu                       xpath=//li[@id='${nodeId}']/a
    Wait Until Element Is Visible           xpath=//ul[contains(@class, 'jstree-contextmenu')]//a[contains(text(), 'Delete')]
    Click Element                           xpath=//ul[contains(@class, 'jstree-contextmenu')]//a[contains(text(), 'Delete')]
    Check Fileset Delete Warning
    Click Dialog Button                     Cancel

Test Delete Images in Share
    [Documentation]     Deletes pre-imported images after putting them in Share

    Clear Activity
    Select Experimenter
    # Add image in 'Delete' Dataset to Share
    ${nodeId}=                                  Wait For Dataset Node Text              Delete
    Click Node                                  ${nodeId}
    Wait Until Page Contains Element            id=dataIcons
    # Click first image in Tree
    ${imageId}=                                 Select First Image

    Click Element                               createshareButton
    Wait Until Page Contains Element            id_message
    Input Text                                  id_message      Robot Test Delete Images in Share
    Click Element                               xpath=//div[@id='id_members_chosen']/ul[@class='chosen-choices']
    Page Should Contain Element                 xpath=//div[@id='id_members_chosen']/div[@class='chosen-drop']/ul[@class='chosen-results']
    Click Element                               xpath=//div[@id='id_members_chosen']/div[@class='chosen-drop']/ul[@class='chosen-results']/li[@data-option-array-index='1']

    Submit Form                                 create_share_form
    Wait Until Element Is Visible               id=share_dialog_form
    ${shareId}=                                 Get Text                    id=shareCreatedId
    Click Dialog Button                         OK

    # Delete Image in share
    Go To                                       ${WELCOME URL}?show=image-${imageId}
    Wait Until Page Contains Element            id=dataIcons
    Click Element                               id=deleteButton
    Wait Until Element Is Visible               id=delete-dialog-form
    Click Dialog Button                         Yes
    Wait Until Keyword Succeeds     10   ${INTERVAL}   Page Should Contain Element     xpath=//span[@id='jobstatus'][contains(text(),'1')]

    # Check share has 'Object Deleted' for deleted image
    Go To                                       ${WELCOME URL}public/
    Select First Share
    # Should be image with id='image-123', with 'Object Deleted' in Tree and icons
    Wait Until Page Contains Element            xpath=//div[@id='dataTree']//li[@data-id='${imageId}']/a[contains(text(), 'Object deleted')]
    Page Should Contain Element                  ${thumbnailsXpath}   limit=0

[Teardown]    Close Browser
